3838
3939Methcla::Engine* m_engine;
4040Methcla::Engine& engine () { return *m_engine; }
41- std::mutex mutex;
41+ std::mutex mutex_fft_in;
42+ std::mutex mutex_fft_out;
43+ std::mutex mutex_amp_in;
44+ std::mutex mutex_amp_out;
4245
43- static Methcla_Time kLatency = 0.1 ;
4446
45- typedef struct data_v {
46- int id;
47- std::atomic<float > amp;
48- } ServerValue, *ServerValuePtr;
47+ static Methcla_Time kLatency = 0.1 ;
4948
50- typedef struct data_a {
51- int id;
52- int fftSize=512 ;
53- std::vector<float > fft;
54- } ServerArray, *ServerArrayPtr;
49+ struct ServerValue {
50+ ServerValue () :
51+ amp (0 ),
52+ id (-1 )
53+ {
54+
55+ }
56+ float amp;
57+ int id;
58+ };
5559
60+ struct ServerArray {
61+ ServerArray () :
62+ fftSize (512 ),
63+ fft (fftSize),
64+ id (-1 )
65+ {
66+
67+ }
68+ int fftSize;
69+ std::vector<float > fft;
70+ int id;
71+ };
5672
5773// Engine
5874
@@ -1126,7 +1142,7 @@ JNIEXPORT jlong JNICALL Java_processing_sound_MethClaInterface_amplitude(JNIEnv
11261142
11271143 Methcla::Request request (engine ());
11281144
1129- ServerValue *amp_ptr = (ServerValue *) malloc ( sizeof ( ServerValue));
1145+ ServerValue * amp_ptr = new ServerValue;
11301146
11311147 ptr = (jlong)amp_ptr;
11321148
@@ -1151,8 +1167,8 @@ JNIEXPORT jlong JNICALL Java_processing_sound_MethClaInterface_amplitude(JNIEnv
11511167 auto id = engine ().addNotificationHandler ([amp_ptr](const OSCPP::Server::Message& msg) {
11521168 if (msg == " /amplitude" ) {
11531169 OSCPP::Server::ArgStream args (msg.args ());
1154- while (!args. atEnd ()) {
1155-
1170+ std::lock_guard<std::mutex> guard (mutex_amp_in);
1171+ while (!args. atEnd ()) {
11561172 amp_ptr->amp = args.float32 ();
11571173 }
11581174 return false ;
@@ -1167,30 +1183,27 @@ JNIEXPORT jlong JNICALL Java_processing_sound_MethClaInterface_amplitude(JNIEnv
11671183};
11681184
11691185JNIEXPORT jfloat JNICALL Java_processing_sound_MethClaInterface_poll_1amplitude (JNIEnv * env, jobject object, jlong ptr){
1170-
11711186 ServerValue *amp_ptr = (ServerValue*)ptr;
1172-
1187+ std::lock_guard<std::mutex> guard (mutex_amp_out);
11731188 return amp_ptr->amp ;
11741189};
11751190
11761191JNIEXPORT void JNICALL Java_processing_sound_MethClaInterface_destroy_1amplitude (JNIEnv *env, jobject object, jlong ptr){
11771192
11781193 ServerValue *amp_ptr = (ServerValue*)ptr;
11791194 engine ().removeNotificationHandler (amp_ptr->id );
1180- free (amp_ptr);
1181-
1195+ delete amp_ptr;
11821196};
11831197
11841198JNIEXPORT jlong JNICALL Java_processing_sound_MethClaInterface_fft (JNIEnv *env, jobject object, jintArray nodeId, jint fftSize){
11851199
11861200 jlong ptr;
11871201 jint* m_nodeId = env->GetIntArrayElements (nodeId, 0 );
1188- ServerArray *fft_ptr = (ServerArray *) malloc (sizeof (ServerArray) + fftSize*(sizeof (std::vector<float >)));
1189-
1190- std::cout << " 1" << std::endl;
1202+ // ServerArray *fft_ptr = (ServerArray *) malloc(sizeof(ServerArray));
11911203
1192- fft_ptr->fft .resize (fftSize);
1193- std::cout << " 2" << std::endl;
1204+ ServerArray * fft_ptr = new ServerArray;
1205+
1206+ fft_ptr->fft .resize (fftSize, 0 );
11941207
11951208 fft_ptr->fftSize =fftSize;
11961209 ptr = (jlong)fft_ptr;
@@ -1203,6 +1216,8 @@ JNIEXPORT jlong JNICALL Java_processing_sound_MethClaInterface_fft(JNIEnv *env,
12031216 Methcla::Request request (engine ());
12041217 request.openBundle (Methcla::immediately);
12051218
1219+ std::cout << fftSize << std::endl;
1220+
12061221 auto synth = request.synth (
12071222 METHCLA_PLUGINS_FFT_URI,
12081223 Methcla::NodePlacement::after (m_nodeId[0 ]),
@@ -1223,12 +1238,14 @@ JNIEXPORT jlong JNICALL Java_processing_sound_MethClaInterface_fft(JNIEnv *env,
12231238 if (msg == " /fft" ) {
12241239 OSCPP::Server::ArgStream args (msg.args ());
12251240 int i=0 ;
1226- while (!args.atEnd ()) {
1227- fft_ptr->fft [i] = args.float32 ();
1228- std::lock_guard<std::mutex> guard (mutex);
1229- i++;
1230- }
1231- return false ;
1241+ {
1242+ std::lock_guard<std::mutex> guard (mutex_fft_in);
1243+ while (!args.atEnd ()) {
1244+ fft_ptr->fft [i] = args.float32 ();
1245+ i++;
1246+ }
1247+ }
1248+ return false ;
12321249 }
12331250 return false ;
12341251 });
@@ -1243,12 +1260,10 @@ JNIEXPORT jlong JNICALL Java_processing_sound_MethClaInterface_fft(JNIEnv *env,
12431260JNIEXPORT jfloatArray JNICALL Java_processing_sound_MethClaInterface_poll_1fft (JNIEnv *env, jobject object, jlong ptr){
12441261
12451262 ServerArray *fft_ptr = (ServerArray*)ptr;
1246-
12471263 jfloatArray fft_mag = env->NewFloatArray (fft_ptr->fftSize );
1248-
1249-
12501264 jfloat *m_fft_mag = env->GetFloatArrayElements (fft_mag, NULL );
12511265
1266+ std::lock_guard<std::mutex> guard (mutex_fft_out);
12521267 for (int i = 0 ; i < fft_ptr->fftSize ; ++i)
12531268 {
12541269 m_fft_mag[i]=fft_ptr->fft [i];
@@ -1260,9 +1275,9 @@ JNIEXPORT jfloatArray JNICALL Java_processing_sound_MethClaInterface_poll_1fft(J
12601275};
12611276
12621277JNIEXPORT void JNICALL Java_processing_sound_MethClaInterface_destroy_1fft (JNIEnv *env, jobject object, jlong ptr){
1263- ServerArray *fft_ptr = (ServerArray*)ptr;
1278+ ServerArray * fft_ptr = (ServerArray*)ptr;
12641279 engine ().removeNotificationHandler (fft_ptr->id );
1265- free ( fft_ptr);
1280+ delete fft_ptr;
12661281};
12671282
12681283/* OLD VARIABLE IN OUT FUNCTION
0 commit comments