-
Notifications
You must be signed in to change notification settings - Fork 2
/
jgaborator.cc
262 lines (206 loc) · 8.84 KB
/
jgaborator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "jgaborator.h"
#include "gaborator-1.7/gaborator/gaborator.h"
#include <unordered_map>
#include <math.h>
#include <mutex>
//The size of the output array, with typical parameters around 300k
//floats are used
const int C_ARRAY_SIZE = 300000 * 2;
//A structure with the state information
struct GaboratorState {
gaborator::parameters* paramsRef;
gaborator::analyzer<float>* analyzerRef;
gaborator::coefs<float>* coefsRef;
int64_t t_in;
int min_band;
int sample_rate;
int64_t anal_support;
jfloat *cArray;
};
//A hash map with a JNIEnv * as key and a GaboratorState * as value
std::unordered_map<uintptr_t, uintptr_t> stateMap;
//A mutex to ensure that writes to the stateMap are synchronized.
std::mutex stateMutex;
//from here
long get_thread_id(JNIEnv * env){
// First, we have to find Thread class
jclass cls = env->FindClass("java/lang/Thread");
// Then, we can look for it's static method 'currentThread'
/* Remember that you can always get method signature using javap tool
> javap -s -p java.lang.Thread | grep -A 1 currentThread
public static native java.lang.Thread currentThread();
descriptor: ()Ljava/lang/Thread;
*/
jmethodID mid =
env->GetStaticMethodID( cls, "currentThread", "()Ljava/lang/Thread;");
// Once you have method, you can call it. Remember that result is
// a jobject
jobject thread = env->CallStaticObjectMethod(cls, mid);
if( thread == NULL ) {
printf("Error while calling static method: currentThread\n");
}
// Now, we have to find another method - 'getId'
/* Remember that you can always get method signature using javap tool
> javap -s -p java.lang.Thread | grep -A 1 getId
public long getId();
descriptor: ()Jjavap -s -p java.lang.Thread | grep -A 1 currentThread
*/
jmethodID mid_getid =
env->GetMethodID(cls, "getId", "()J");
if( mid_getid == NULL ) {
printf("Error while calling GetMethodID for: getId\n");
}
// This time, we are calling instance method, note the difference
// in Call... method
jlong tid = env->CallLongMethod(thread, mid_getid);
// Finally, let's call 'getName' of Thread object
/* Remember that you can always get method signature using javap tool
> javap -s -p java.lang.Thread | grep -A 1 getName
public final java.lang.String getName();
descriptor: ()Ljava/lang/String;
*/
jmethodID mid_getname =
env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
if( mid_getname == NULL ) {
printf("Error while calling GetMethodID for: getName\n");
}
// As above, we are calling instance method
jstring tname =(jstring) env->CallObjectMethod( thread, mid_getname);
// Remember to retrieve characters from String object
const char *c_str;
c_str = env->GetStringUTFChars( tname, NULL);
if(c_str == NULL) {
printf("Error getting thread info\n");
return -7 ;
}
// display message from JNI
printf("[C ] name: %s id: %ld\n", c_str, tid);
// and make sure to release allocated memory before leaving JNI
env->ReleaseStringUTFChars( tname, c_str);
return tid;
}
JNIEXPORT jint JNICALL Java_be_ugent_jgaborator_JGaborator_initialize(JNIEnv * env, jobject object, jint blocksize, jdouble fs, jint bands_per_octave, jdouble ff_min , jdouble ff_ref, jdouble ff_max, jdouble overlap, jdouble max_error){
//Makes sure only one thread writes to the stateMap
std::unique_lock<std::mutex> lck (stateMutex);
GaboratorState * state = new GaboratorState();
uintptr_t env_addresss = reinterpret_cast<uintptr_t>(env);
//get_thread_id(env);
//printf("[C ] address: %ld\n", env_addresss);
state->paramsRef = new gaborator::parameters(bands_per_octave, ff_min / fs, ff_ref / fs, overlap, max_error);
state->analyzerRef = new gaborator::analyzer<float>(*(state->paramsRef));
state->coefsRef = new gaborator::coefs<float>(*(state->analyzerRef));
//converts frequency (ff_max) in hertz to the number of bands above the min frequency
//the ceil is used to end up at a full band
int interesting_bands = ceil(bands_per_octave * log(ff_max/ff_min)/log(2.0f));
//since bands are ordered from high to low we are only interested in lower bands:
//fs/2.0 is the nyquist frequency
int total_bands = ceil(bands_per_octave * log(fs/2.0/ff_min)/log(2.0f));
state->anal_support = (int64_t) ceil(state->analyzerRef->analysis_support());
state->min_band = total_bands - interesting_bands;
state->sample_rate = (int) fs;
state->t_in = 0;
state->cArray = new jfloat[C_ARRAY_SIZE];
uintptr_t state_addresss = reinterpret_cast<uintptr_t>(state);
assert(stateMap.count(env_addresss)==0);
stateMap[env_addresss] = state_addresss;
assert(stateMap.count(env_addresss)==1);
assert(state->t_in == 0);
return (int) state->anal_support;
}
/*
* Class: be_ugent_jgaborator_JGaborator
* Method: analyse
* Signature: ([F)[F
*/
JNIEXPORT jfloatArray JNICALL Java_be_ugent_jgaborator_JGaborator_analyse(JNIEnv * env, jobject obj, jfloatArray audio_block ){
//get a ref to the state pointer
uintptr_t env_addresss = reinterpret_cast<uintptr_t>(env);
assert(stateMap.count(env_addresss)==1);
if(stateMap.count(env_addresss)==0){
return NULL;
}
GaboratorState * state = reinterpret_cast<GaboratorState *>(stateMap[env_addresss]);
//get_thread_id(env);
//printf("[C ] address: %ld\n", env_addresss);
//
// First Convert the incoming JNI jintarray to C's jfloat[]
jfloat *audio_block_c_array = env->GetFloatArrayElements(audio_block, NULL);
if (NULL == audio_block_c_array) return NULL;
jsize blocksize = env->GetArrayLength(audio_block);
std::vector<float> buf(audio_block_c_array,audio_block_c_array + blocksize);
int output_index = 0;
state->analyzerRef->analyze(buf.data(), state->t_in, state->t_in + blocksize, *(state->coefsRef));
int64_t st0 = state->t_in - state->anal_support;
int64_t st1 = state->t_in - state->anal_support + blocksize;
apply(
*state->analyzerRef,
*state->coefsRef,
[&](std::complex<float> coef, int band, int64_t audioSampleIndex ) {
//ignores everything above the max_band
if(band >= state->min_band){
//printf("%f %d %ld\n",std::abs(coef),band,audioSampleIndex);
state->cArray[output_index++] = band;
state->cArray[output_index++] = audioSampleIndex;
state->cArray[output_index++] = std::abs(coef);
//printf("output_index: %d\n", output_index++);
//output_index++;
}
},st0,
st1);
state->t_in += (int64_t) blocksize;
int64_t t_out = state->t_in - state->anal_support;
forget_before(*state->analyzerRef, *state->coefsRef, t_out - blocksize);
//Release audio block memory resources
env->ReleaseFloatArrayElements(audio_block, audio_block_c_array, 0);
//copy the relevant part of the c array to the output JNI array
jfloatArray outputArray = env->NewFloatArray(output_index);
env->SetFloatArrayRegion(outputArray, 0 , output_index, state->cArray);
return outputArray;
}
JNIEXPORT jfloatArray JNICALL Java_be_ugent_jgaborator_JGaborator_bandcenters(JNIEnv * env, jobject obj){
uintptr_t env_addresss = reinterpret_cast<uintptr_t>(env);
assert(stateMap.count(env_addresss)==1);
if(stateMap.count(env_addresss)==0){
return NULL;
}
GaboratorState * state = reinterpret_cast<GaboratorState *>(stateMap[env_addresss]);
int max_band = state->analyzerRef->bandpass_bands_end();
float band_centers[max_band+1];
for(int i = 0 ; i < max_band ; i++){
if(i<state->min_band){
band_centers[i]=-1;
}else{
band_centers[i]=state->analyzerRef->band_ff(i) * state->sample_rate;
}
}
jfloatArray outputArray = env->NewFloatArray(max_band+1);
env->SetFloatArrayRegion(outputArray, 0 , max_band+1, band_centers); // copy
return outputArray;
}
/*
* Class: be_ugent_jgaborator_JGaborator
* Method: release
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_be_ugent_jgaborator_JGaborator_release(JNIEnv *env , jobject obj){
//Makes sure only one thread writes to the stateMap
std::unique_lock<std::mutex> lck (stateMutex);
uintptr_t env_addresss = reinterpret_cast<uintptr_t>(env);
assert(stateMap.count(env_addresss)==1);
if(stateMap.count(env_addresss)==0){
return;
}
GaboratorState * state = reinterpret_cast<GaboratorState *>(stateMap[env_addresss]);
assert(stateMap.count(env_addresss)==1);
stateMap.erase(env_addresss);
assert(stateMap.count(env_addresss)==0);
//cleanup memory
delete state->analyzerRef;
delete state->coefsRef;
delete state->paramsRef;
delete [] state->cArray;
delete state;
}
int main(){
return 0;
}