6
6
#include <math.h>
7
7
#include <stdlib.h>
8
8
#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
9
15
10
16
struct cava_plan * cava_init (int number_of_bars , unsigned int rate , int channels , int autosens ,
11
17
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,
88
94
p -> frame_skip = 1 ;
89
95
p -> noise_reduction = noise_reduction ;
90
96
97
+ int fftw_flag = FFTW_MEASURE ;
98
+ #ifdef __ANDROID__
99
+ fftw_flag = FFTW_ESTIMATE ;
100
+ #endif
101
+
91
102
p -> FFTbassbufferSize = treble_buffer_size * 8 ;
92
103
p -> FFTmidbufferSize = treble_buffer_size * 4 ;
93
104
p -> FFTtreblebufferSize = treble_buffer_size ;
@@ -125,20 +136,20 @@ struct cava_plan *cava_init(int number_of_bars, unsigned int rate, int channels,
125
136
p -> in_bass_l_raw = fftw_alloc_real (p -> FFTbassbufferSize );
126
137
p -> out_bass_l = fftw_alloc_complex (p -> FFTbassbufferSize / 2 + 1 );
127
138
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 );
129
140
130
141
// MID
131
142
p -> in_mid_l = fftw_alloc_real (p -> FFTmidbufferSize );
132
143
p -> in_mid_l_raw = fftw_alloc_real (p -> FFTmidbufferSize );
133
144
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 );
135
146
136
147
// TREBLE
137
148
p -> in_treble_l = fftw_alloc_real (p -> FFTtreblebufferSize );
138
149
p -> in_treble_l_raw = fftw_alloc_real (p -> FFTtreblebufferSize );
139
150
p -> out_treble_l = fftw_alloc_complex (p -> FFTtreblebufferSize / 2 + 1 );
140
151
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 );
142
153
143
154
memset (p -> in_bass_l , 0 , sizeof (double ) * p -> FFTbassbufferSize );
144
155
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,
155
166
p -> in_bass_r_raw = fftw_alloc_real (p -> FFTbassbufferSize );
156
167
p -> out_bass_r = fftw_alloc_complex (p -> FFTbassbufferSize / 2 + 1 );
157
168
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 );
159
170
160
171
// MID
161
172
p -> in_mid_r = fftw_alloc_real (p -> FFTmidbufferSize );
162
173
p -> in_mid_r_raw = fftw_alloc_real (p -> FFTmidbufferSize );
163
174
p -> out_mid_r = fftw_alloc_complex (p -> FFTmidbufferSize / 2 + 1 );
164
175
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 );
166
177
167
178
// TREBLE
168
179
p -> in_treble_r = fftw_alloc_real (p -> FFTtreblebufferSize );
169
180
p -> in_treble_r_raw = fftw_alloc_real (p -> FFTtreblebufferSize );
170
181
p -> out_treble_r = fftw_alloc_complex (p -> FFTtreblebufferSize / 2 + 1 );
171
182
172
183
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 );
174
185
175
186
memset (p -> in_bass_r , 0 , sizeof (double ) * p -> FFTbassbufferSize );
176
187
memset (p -> in_mid_r , 0 , sizeof (double ) * p -> FFTmidbufferSize );
@@ -563,3 +574,62 @@ void cava_destroy(struct cava_plan *p) {
563
574
fftw_destroy_plan (p -> p_treble_r );
564
575
}
565
576
}
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
0 commit comments