Skip to content

Commit 93edc8f

Browse files
committed
add android source code (#552)
1 parent f515f93 commit 93edc8f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1920
-7
lines changed

CMakeLists.txt

+20-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,23 @@
33

44
cmake_minimum_required(VERSION 3.13.0)
55
project(cavacore)
6-
add_library(cavacore STATIC cavacore.c)
6+
if (ANDROID)
7+
add_library(cavacore SHARED cavacore.c)
8+
if (NOT DEFINED FFTW_DIR)
9+
message(FATAL_ERROR "FFTW_DIR not set, required by android, see cavandroid/README.md")
10+
endif()
11+
if (NOT EXISTS ${FFTW_DIR})
12+
message(FATAL_ERROR "given FFTW_DIR: ${FFTW_DIR} does not exist")
13+
endif()
14+
if (NOT EXISTS "${FFTW_DIR}/jni/fftw3/api/")
15+
message(FATAL_ERROR "given fftw include dir: ${FFTW_DIR}/jni/fftw3/api/ does not exist")
16+
endif()
17+
if (NOT EXISTS "${FFTW_DIR}/obj/local/${CMAKE_ANDROID_ARCH_ABI}/")
18+
message(FATAL_ERROR "given lib dir: ${FFTW_DIR}/obj/local/${CMAKE_ANDROID_ARCH_ABI}/ does not exist, did you build it? See cavandroid/README.md")
19+
endif()
20+
target_include_directories(cavacore PRIVATE "${FFTW_DIR}/jni/fftw3/api/")
21+
target_link_directories(cavacore PRIVATE "${FFTW_DIR}/obj/local/${CMAKE_ANDROID_ARCH_ABI}/")
22+
target_link_libraries(cavacore fftw3)
23+
else()
24+
add_library(cavacore STATIC cavacore.c)
25+
endif()

cavacore.c

+76-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
#include <math.h>
77
#include <stdlib.h>
88
#include <string.h>
9+
#ifdef __ANDROID__
10+
#include <jni.h>
11+
struct cava_plan *plan;
12+
double *cava_in;
13+
double *cava_out;
14+
#endif
915

1016
struct cava_plan *cava_init(int number_of_bars, unsigned int rate, int channels, int autosens,
1117
double noise_reduction, int low_cut_off, int high_cut_off) {
@@ -88,6 +94,11 @@ struct cava_plan *cava_init(int number_of_bars, unsigned int rate, int channels,
8894
p->frame_skip = 1;
8995
p->noise_reduction = noise_reduction;
9096

97+
int fftw_flag = FFTW_MEASURE;
98+
#ifdef __ANDROID__
99+
fftw_flag = FFTW_ESTIMATE;
100+
#endif
101+
91102
p->FFTbassbufferSize = treble_buffer_size * 8;
92103
p->FFTmidbufferSize = treble_buffer_size * 4;
93104
p->FFTtreblebufferSize = treble_buffer_size;
@@ -125,20 +136,20 @@ struct cava_plan *cava_init(int number_of_bars, unsigned int rate, int channels,
125136
p->in_bass_l_raw = fftw_alloc_real(p->FFTbassbufferSize);
126137
p->out_bass_l = fftw_alloc_complex(p->FFTbassbufferSize / 2 + 1);
127138
p->p_bass_l =
128-
fftw_plan_dft_r2c_1d(p->FFTbassbufferSize, p->in_bass_l, p->out_bass_l, FFTW_MEASURE);
139+
fftw_plan_dft_r2c_1d(p->FFTbassbufferSize, p->in_bass_l, p->out_bass_l, fftw_flag);
129140

130141
// MID
131142
p->in_mid_l = fftw_alloc_real(p->FFTmidbufferSize);
132143
p->in_mid_l_raw = fftw_alloc_real(p->FFTmidbufferSize);
133144
p->out_mid_l = fftw_alloc_complex(p->FFTmidbufferSize / 2 + 1);
134-
p->p_mid_l = fftw_plan_dft_r2c_1d(p->FFTmidbufferSize, p->in_mid_l, p->out_mid_l, FFTW_MEASURE);
145+
p->p_mid_l = fftw_plan_dft_r2c_1d(p->FFTmidbufferSize, p->in_mid_l, p->out_mid_l, fftw_flag);
135146

136147
// TREBLE
137148
p->in_treble_l = fftw_alloc_real(p->FFTtreblebufferSize);
138149
p->in_treble_l_raw = fftw_alloc_real(p->FFTtreblebufferSize);
139150
p->out_treble_l = fftw_alloc_complex(p->FFTtreblebufferSize / 2 + 1);
140151
p->p_treble_l =
141-
fftw_plan_dft_r2c_1d(p->FFTtreblebufferSize, p->in_treble_l, p->out_treble_l, FFTW_MEASURE);
152+
fftw_plan_dft_r2c_1d(p->FFTtreblebufferSize, p->in_treble_l, p->out_treble_l, fftw_flag);
142153

143154
memset(p->in_bass_l, 0, sizeof(double) * p->FFTbassbufferSize);
144155
memset(p->in_mid_l, 0, sizeof(double) * p->FFTmidbufferSize);
@@ -155,22 +166,22 @@ struct cava_plan *cava_init(int number_of_bars, unsigned int rate, int channels,
155166
p->in_bass_r_raw = fftw_alloc_real(p->FFTbassbufferSize);
156167
p->out_bass_r = fftw_alloc_complex(p->FFTbassbufferSize / 2 + 1);
157168
p->p_bass_r =
158-
fftw_plan_dft_r2c_1d(p->FFTbassbufferSize, p->in_bass_r, p->out_bass_r, FFTW_MEASURE);
169+
fftw_plan_dft_r2c_1d(p->FFTbassbufferSize, p->in_bass_r, p->out_bass_r, fftw_flag);
159170

160171
// MID
161172
p->in_mid_r = fftw_alloc_real(p->FFTmidbufferSize);
162173
p->in_mid_r_raw = fftw_alloc_real(p->FFTmidbufferSize);
163174
p->out_mid_r = fftw_alloc_complex(p->FFTmidbufferSize / 2 + 1);
164175
p->p_mid_r =
165-
fftw_plan_dft_r2c_1d(p->FFTmidbufferSize, p->in_mid_r, p->out_mid_r, FFTW_MEASURE);
176+
fftw_plan_dft_r2c_1d(p->FFTmidbufferSize, p->in_mid_r, p->out_mid_r, fftw_flag);
166177

167178
// TREBLE
168179
p->in_treble_r = fftw_alloc_real(p->FFTtreblebufferSize);
169180
p->in_treble_r_raw = fftw_alloc_real(p->FFTtreblebufferSize);
170181
p->out_treble_r = fftw_alloc_complex(p->FFTtreblebufferSize / 2 + 1);
171182

172183
p->p_treble_r = fftw_plan_dft_r2c_1d(p->FFTtreblebufferSize, p->in_treble_r,
173-
p->out_treble_r, FFTW_MEASURE);
184+
p->out_treble_r, fftw_flag);
174185

175186
memset(p->in_bass_r, 0, sizeof(double) * p->FFTbassbufferSize);
176187
memset(p->in_mid_r, 0, sizeof(double) * p->FFTmidbufferSize);
@@ -563,3 +574,62 @@ void cava_destroy(struct cava_plan *p) {
563574
fftw_destroy_plan(p->p_treble_r);
564575
}
565576
}
577+
578+
#ifdef __ANDROID__
579+
JNIEXPORT jfloatArray JNICALL Java_com_karlstav_cava_MyGLRenderer_InitCava(
580+
JNIEnv *env, jobject thiz, jint number_of_bars_set, jint refresh_rate, jint lower_cut_off,
581+
jint higher_cut_off) {
582+
jfloatArray cuttOffFreq = (*env)->NewFloatArray(env, number_of_bars_set + 1);
583+
float noise_reduction = pow((float)refresh_rate / 130, 0.75);
584+
585+
plan =
586+
cava_init(number_of_bars_set, 44100, 1, 1, noise_reduction, lower_cut_off, higher_cut_off);
587+
cava_in = (double *)malloc(plan->FFTbassbufferSize * sizeof(double));
588+
cava_out = (double *)malloc(plan->number_of_bars * sizeof(double));
589+
(*env)->SetFloatArrayRegion(env, cuttOffFreq, 0, plan->number_of_bars + 1,
590+
plan->cut_off_frequency);
591+
return cuttOffFreq;
592+
}
593+
594+
JNIEXPORT jdoubleArray JNICALL Java_com_karlstav_cava_MyGLRenderer_ExecCava(JNIEnv *env,
595+
jobject thiz,
596+
jdoubleArray cava_input,
597+
jint new_samples) {
598+
599+
jdoubleArray cavaReturn = (*env)->NewDoubleArray(env, plan->number_of_bars);
600+
601+
cava_in = (*env)->GetDoubleArrayElements(env, cava_input, NULL);
602+
603+
cava_execute(cava_in, new_samples, cava_out, plan);
604+
(*env)->SetDoubleArrayRegion(env, cavaReturn, 0, plan->number_of_bars, cava_out);
605+
(*env)->ReleaseDoubleArrayElements(env, cava_input, cava_in, JNI_ABORT);
606+
607+
return cavaReturn;
608+
}
609+
610+
JNIEXPORT int JNICALL Java_com_karlstav_cava_CavaCoreTest_InitCava(JNIEnv *env, jobject thiz,
611+
jint number_of_bars_set) {
612+
613+
plan = cava_init(number_of_bars_set, 44100, 1, 1, 0.7, 50, 10000);
614+
return 1;
615+
}
616+
617+
JNIEXPORT jdoubleArray JNICALL Java_com_karlstav_cava_CavaCoreTest_ExecCava(JNIEnv *env,
618+
jobject thiz,
619+
jdoubleArray cava_input,
620+
jint new_samples) {
621+
622+
jdoubleArray cavaReturn = (*env)->NewDoubleArray(env, plan->number_of_bars);
623+
624+
cava_in = (*env)->GetDoubleArrayElements(env, cava_input, NULL);
625+
626+
cava_execute(cava_in, new_samples, cava_out, plan);
627+
(*env)->SetDoubleArrayRegion(env, cavaReturn, 0, plan->number_of_bars, cava_out);
628+
(*env)->ReleaseDoubleArrayElements(env, cava_input, cava_in, JNI_ABORT);
629+
630+
return cavaReturn;
631+
}
632+
JNIEXPORT void JNICALL Java_com_karlstav_cava_MyGLRenderer_DestroyCava(JNIEnv *env, jobject thiz) {
633+
cava_destroy(plan);
634+
}
635+
#endif

cavandroid/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# CAVA on android
2+
3+
## FFTW3
4+
5+
To build cava on android you need to get the fftw3 library.
6+
Luckily someone has made a repository for compiling FFTW3 on Android.
7+
Just make sure ndk-build is in your path before running the build script.
8+
9+
10+
```
11+
git clone https://github.com/Lauszus/fftw3-android
12+
cd fftw3-android
13+
./build.sh
14+
15+
```
16+
17+
By default we will look for the fftw3 lib in a folder called fftw3-android besides the cava dir:
18+
19+
```
20+
./
21+
../
22+
cava/
23+
fftw3-android/
24+
```
25+
26+
Edit the cmake argument in the app gradle file for setting it to some other place.

cavandroid/app/build.gradle.kts

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import com.android.build.api.dsl.LintOptions
2+
3+
plugins {
4+
id("com.android.application")
5+
id("org.jetbrains.kotlin.android")
6+
}
7+
8+
android {
9+
signingConfigs {
10+
getByName("debug") {
11+
storeFile = file("../keystore.jks")
12+
storePassword = "cava123"
13+
keyPassword = "cava123"
14+
keyAlias = "key0"
15+
}
16+
create("release") {
17+
storeFile = file("../keystore.jks")
18+
storePassword = "cava123"
19+
keyPassword = "cava123"
20+
keyAlias = "key0"
21+
}
22+
}
23+
namespace = "com.karlstav.cava"
24+
compileSdk = 33
25+
26+
defaultConfig {
27+
applicationId = "com.karlstav.cava"
28+
minSdk = 30
29+
targetSdk = 33
30+
versionCode = 14
31+
versionName = "@string/app_ver"
32+
33+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
34+
vectorDrawables {
35+
useSupportLibrary = true
36+
}
37+
/*
38+
ndk {
39+
abiFilters += listOf("arm64-v8a")
40+
}
41+
*/
42+
externalNativeBuild {
43+
44+
// For ndk-build, instead use the ndkBuild block.
45+
cmake {
46+
47+
// Passes optional arguments to CMake.
48+
arguments("-DFFTW_DIR=${projectDir}/../../../fftw3-android")
49+
}
50+
}
51+
}
52+
53+
buildTypes {
54+
release {
55+
isMinifyEnabled = false
56+
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
57+
isDebuggable = false
58+
signingConfig = signingConfigs.getByName("release")
59+
}
60+
}
61+
// Encapsulates your external native build configurations.
62+
externalNativeBuild {
63+
64+
// Encapsulates your CMake build configurations.
65+
cmake {
66+
// Provides a relative path to your CMake build script.
67+
path = file("../../CMakeLists.txt")
68+
}
69+
}
70+
compileOptions {
71+
sourceCompatibility = JavaVersion.VERSION_1_8
72+
targetCompatibility = JavaVersion.VERSION_1_8
73+
}
74+
kotlinOptions {
75+
jvmTarget = "1.8"
76+
}
77+
buildFeatures {
78+
compose = true
79+
}
80+
composeOptions {
81+
kotlinCompilerExtensionVersion = "1.4.3"
82+
}
83+
packaging {
84+
resources {
85+
excludes += "/META-INF/{AL2.0,LGPL2.1}"
86+
}
87+
}
88+
89+
}
90+
91+
dependencies {
92+
93+
implementation("androidx.core:core-ktx:1.9.0")
94+
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
95+
implementation("androidx.activity:activity-compose:1.7.0")
96+
implementation ("androidx.appcompat:appcompat:1.3.1")
97+
implementation("androidx.preference:preference-ktx:1.2.0")
98+
99+
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
100+
implementation("androidx.compose.ui:ui")
101+
implementation("androidx.compose.ui:ui-graphics")
102+
implementation("androidx.compose.ui:ui-tooling-preview")
103+
implementation("androidx.compose.material3:material3")
104+
testImplementation("junit:junit:4.13.2")
105+
androidTestImplementation("androidx.test.ext:junit:1.1.5")
106+
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
107+
androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
108+
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
109+
debugImplementation("androidx.compose.ui:ui-tooling")
110+
debugImplementation("androidx.compose.ui:ui-test-manifest")
111+
}

cavandroid/app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.karlstav.cava
2+
3+
import androidx.test.ext.junit.runners.AndroidJUnit4
4+
5+
import org.junit.Test
6+
import org.junit.runner.RunWith
7+
8+
import org.junit.Assert.*
9+
10+
/**
11+
* Instrumented test, which will execute on an Android device.
12+
*
13+
* See [testing documentation](http://d.android.com/tools/testing).
14+
*/
15+
@RunWith(AndroidJUnit4::class)
16+
class CavaCoreTest {
17+
external fun InitCava(pInt: Int): Int
18+
external fun ExecCava(pDoubleArray: DoubleArray, pInt: Int): DoubleArray
19+
20+
@Test
21+
fun cavaExec_null_test() {
22+
System.loadLibrary("cavacore")
23+
24+
val numberOfBars = 10
25+
val cavaData = DoubleArray(256)
26+
val barsData = FloatArray(10)
27+
28+
29+
for (i in 0 until 256) {
30+
cavaData[i] = 0.0
31+
}
32+
val rc = InitCava(numberOfBars)
33+
34+
var cavaOut = ExecCava(cavaData, 256);
35+
36+
37+
for (i in 0 until numberOfBars) {
38+
assertEquals(0.0f, barsData[0])
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)