77#include <string.h>
88#include <stdlib.h>
99
10- tsdr_lib_t tsdr ;
10+ tsdr_lib_t tsdr_instance ;
1111
1212#define THROW (x ) error(env, x, "")
1313
@@ -28,6 +28,9 @@ void error_translate (int exception_code, char * exceptionclass) {
2828 case TSDR_NOT_IMPLEMENTED :
2929 strcpy (exceptionclass , "martin/tempest/core/exceptions/TSDRFunctionNotImplemented" );
3030 return ;
31+ case TSDR_WRONG_WIDTHHEIGHT :
32+ strcpy (exceptionclass , "martin/tempest/core/exceptions/TSDRWrongWidthHeightException" );
33+ return ;
3134 default :
3235 strcpy (exceptionclass , "java/lang/Exception" );
3336 return ;
@@ -64,34 +67,31 @@ JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_init (JNIEnv * env,
6467
6568JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_nativeLoadPlugin (JNIEnv * env , jobject obj , jstring path ) {
6669 const char * npath = (* env )-> GetStringUTFChars (env , path , 0 );
67- THROW (tsdr_loadplugin (& tsdr , npath ));
70+ THROW (tsdr_loadplugin (& tsdr_instance , npath ));
6871 (* env )-> ReleaseStringUTFChars (env , path , npath );
6972}
7073
7174JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_pluginParams (JNIEnv * env , jobject obj , jstring params ) {
7275 const char * nparams = (* env )-> GetStringUTFChars (env , params , 0 );
73- THROW (tsdr_pluginparams (& tsdr , nparams ));
76+ THROW (tsdr_pluginparams (& tsdr_instance , nparams ));
7477 (* env )-> ReleaseStringUTFChars (env , params , nparams );
7578}
7679
7780JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setSampleRate (JNIEnv * env , jobject obj , jlong rate ) {
78- THROW (tsdr_setsamplerate (& tsdr , (uint32_t ) rate ));
81+ THROW (tsdr_setsamplerate (& tsdr_instance , (uint32_t ) rate ));
7982}
8083
8184JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setBaseFreq (JNIEnv * env , jobject obj , jlong freq ) {
82- THROW (tsdr_setbasefreq (& tsdr , (uint32_t ) freq ));
85+ THROW (tsdr_setbasefreq (& tsdr_instance , (uint32_t ) freq ));
8386}
8487
85- void read_async (float * buf , uint32_t len , void * ctx ) {
88+ void read_async (float * buf , int width , int height , void * ctx ) {
8689 java_context_t * context = (java_context_t * ) ctx ;
8790 JNIEnv * env ;
8891
8992 if ((* jvm )-> GetEnv (jvm , (void * * )& env , javaversion ) == JNI_EDETACHED )
9093 (* jvm )-> AttachCurrentThread (jvm , (void * * ) & env , 0 );
9194
92- const int width = 800 ;
93- const int height = 600 ;
94-
9595 jclass cls = (* env )-> GetObjectClass (env , context -> obj );
9696
9797 // fixSize(200, 200);
@@ -100,16 +100,13 @@ void read_async(float *buf, uint32_t len, void *ctx) {
100100
101101 jintArray pixels_obj = (* env )-> GetObjectField (env , context -> obj , (* env )-> GetFieldID (env , cls , "pixels" , "[I" ));
102102 jint * pixels = (* env )-> GetIntArrayElements (env ,pixels_obj ,0 );
103+ jint * data = pixels ;
103104
104105 int i ;
105106 const int size = width * height ;
106107 for (i = 0 ; i < size ; i ++ ) {
107- const int x = i % width ;
108- const int y = i / width ;
109-
110- const float rat = y / (float ) height ;
111- const int col = (int ) (buf [i ] * 255.0f );
112- pixels [i ] = col | (col << 8 ) | (col << 16 );
108+ const int col = (int ) (* (buf ++ ) * 255.0f );
109+ * (data ++ ) = col | (col << 8 ) | (col << 16 );
113110 }
114111
115112 // release elements
@@ -121,24 +118,37 @@ void read_async(float *buf, uint32_t len, void *ctx) {
121118}
122119
123120JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_start (JNIEnv * env , jobject obj ) {
121+
124122 java_context_t * context = (java_context_t * ) malloc (sizeof (java_context_t ));
125123 context -> obj = (* env )-> NewGlobalRef (env , obj );
126124 (* env )-> DeleteLocalRef (env , obj );
127125
128- tsdr_readasync (& tsdr , read_async , (void * ) context , 800 , 600 );
126+ THROW ( tsdr_readasync (& tsdr_instance , read_async , (void * ) context ) );
129127
130128 (* env )-> DeleteGlobalRef (env , context -> obj );
131129 free (context );
132130}
133131
134132JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_stop (JNIEnv * env , jobject obj ) {
135- THROW (tsdr_stop (& tsdr ));
133+ THROW (tsdr_stop (& tsdr_instance ));
136134}
137135
138136JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setGain (JNIEnv * env , jobject obj , jfloat gain ) {
139- THROW (tsdr_setgain (& tsdr , (float ) gain ));
137+ THROW (tsdr_setgain (& tsdr_instance , (float ) gain ));
140138}
141139
142140JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_unloadPlugin (JNIEnv * env , jobject obj ) {
143- THROW (tsdr_unloadplugin (& tsdr ));
141+ THROW (tsdr_unloadplugin (& tsdr_instance ));
142+ }
143+
144+ JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setResolution (JNIEnv * env , jobject obj , jint width , jint height ) {
145+ THROW (tsdr_setresolution (& tsdr_instance , (int ) width , (int ) height ));
146+ }
147+
148+ JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setVfreq (JNIEnv * env , jobject obj , jfloat freq ) {
149+ THROW (tsdr_setvfreq (& tsdr_instance , (float ) freq ));
150+ }
151+
152+ JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setHfreq (JNIEnv * env , jobject obj , jfloat freq ) {
153+ THROW (tsdr_sethfreq (& tsdr_instance , (float ) freq ));
144154}
0 commit comments