@@ -13,89 +13,106 @@ static jclass decoderClass = nullptr;
1313static jmethodID onProgressMethod = nullptr ;
1414
1515// Callback function to report progress back to Java
16- static void progressCallbackBridge (float progress, void * context) {
16+ static void progressCallbackBridge (float progress, void *context)
17+ {
1718 JNIEnv *env;
1819 // Get the JNI environment
19- jint result = javaVM->GetEnv ((void **)&env, JNI_VERSION_1_6 );
20-
21- if (result == JNI_OK ) {
20+ jint result = javaVM->GetEnv ((void **)&env, JNI_VERSION_1_6 );
21+
22+ if (result == JNI_OK )
23+ {
2224 // Call the onProgress method in Java
2325 env->CallStaticVoidMethod (decoderClass, onProgressMethod, progress);
24- } else if (result == JNI_EDETACHED ) {
26+ }
27+ else if (result == JNI_EDETACHED )
28+ {
2529 // Thread is detached, attach it
26- if (javaVM->AttachCurrentThread (&env, nullptr ) == JNI_OK ) {
30+ if (javaVM->AttachCurrentThread (&env, nullptr ) == JNI_OK )
31+ {
2732 env->CallStaticVoidMethod (decoderClass, onProgressMethod, progress);
2833 javaVM->DetachCurrentThread ();
29- } else {
34+ }
35+ else
36+ {
3037 LOGE (" Failed to attach thread for progress callback" );
3138 }
32- } else {
39+ }
40+ else
41+ {
3342 LOGE (" Failed to get JNI environment for progress callback" );
3443 }
3544}
3645
3746// The JNI implementation of the decode method
3847extern " C" JNIEXPORT jstring JNICALL
3948Java_com_tinywavpackdecoder_TinyWavPackDecoderModule_nativeDecodeWavPack (
40- JNIEnv *env,
41- jclass clazz,
42- jstring j_input_path,
43- jstring j_output_path,
44- jint j_max_samples,
45- jint j_bits_per_sample) {
46-
49+ JNIEnv *env,
50+ jclass clazz,
51+ jstring j_input_path,
52+ jstring j_output_path,
53+ jint j_max_samples,
54+ jint j_bits_per_sample)
55+ {
56+
4757 // Convert Java strings to C strings
4858 const char *inputPath = env->GetStringUTFChars (j_input_path, nullptr );
4959 const char *outputPath = env->GetStringUTFChars (j_output_path, nullptr );
50-
60+
5161 // Log the parameters
5262 LOGI (" Decoding WavPack: input=%s, output=%s, maxSamples=%d, bitsPerSample=%d" ,
5363 inputPath, outputPath, j_max_samples, j_bits_per_sample);
54-
64+
5565 // Call the decoder
5666 DecoderResult result = decode_wavpack_to_wav (
57- inputPath,
58- outputPath,
59- j_max_samples,
60- j_bits_per_sample,
61- progressCallbackBridge,
62- nullptr );
63-
67+ inputPath,
68+ outputPath,
69+ j_max_samples,
70+ j_bits_per_sample,
71+ progressCallbackBridge,
72+ nullptr );
73+
6474 // Release the C strings
6575 env->ReleaseStringUTFChars (j_input_path, inputPath);
6676 env->ReleaseStringUTFChars (j_output_path, outputPath);
67-
77+
6878 // Return the result
69- if (result.success ) {
79+ if (result.success )
80+ {
7081 return env->NewStringUTF (" Success" );
71- } else {
82+ }
83+ else
84+ {
7285 return env->NewStringUTF (result.error );
7386 }
7487}
7588
7689// Called when the library is loaded
77- JNIEXPORT jint JNI_OnLoad (JavaVM *vm, void *reserved) {
90+ JNIEXPORT jint JNI_OnLoad (JavaVM *vm, void *reserved)
91+ {
7892 javaVM = vm;
7993 JNIEnv *env;
80-
81- if (vm->GetEnv ((void **)&env, JNI_VERSION_1_6 ) != JNI_OK ) {
94+
95+ if (vm->GetEnv ((void **)&env, JNI_VERSION_1_6 ) != JNI_OK )
96+ {
8297 return JNI_ERR ;
8398 }
84-
99+
85100 // Cache the Java class and method IDs
86101 jclass localClass = env->FindClass (" com/tinywavpackdecoder/TinyWavPackDecoderModule" );
87- if (localClass == nullptr ) {
102+ if (localClass == nullptr )
103+ {
88104 LOGE (" Failed to find TinyWavPackDecoderModule class" );
89105 return JNI_ERR ;
90106 }
91-
107+
92108 decoderClass = (jclass)env->NewGlobalRef (localClass);
93109 onProgressMethod = env->GetStaticMethodID (decoderClass, " onProgress" , " (F)V" );
94-
95- if (onProgressMethod == nullptr ) {
110+
111+ if (onProgressMethod == nullptr )
112+ {
96113 LOGE (" Failed to find onProgress method" );
97114 return JNI_ERR ;
98115 }
99-
116+
100117 return JNI_VERSION_1_6 ;
101118}
0 commit comments