From e6f6e976805e00d28a4f9208e3ebf9a101bfcbec Mon Sep 17 00:00:00 2001 From: "Reece H. Dunn" Date: Wed, 7 Oct 2015 21:23:58 +0100 Subject: [PATCH] Remove the use of the native_data_t object. --- android/jni/jni/eSpeakService.cpp | 53 ++++--------------- .../com/reecedunn/espeak/SpeechSynthesis.java | 7 --- 2 files changed, 9 insertions(+), 51 deletions(-) diff --git a/android/jni/jni/eSpeakService.cpp b/android/jni/jni/eSpeakService.cpp index 2fdaf77a..4eb20f75 100644 --- a/android/jni/jni/eSpeakService.cpp +++ b/android/jni/jni/eSpeakService.cpp @@ -109,29 +109,20 @@ enum synthesis_result { SYNTH_ABORT = 1 }; -struct native_data_t { - JNIEnv *env; - jobject object; - - native_data_t() { - env = NULL; - object = NULL; - } -}; - +static JavaVM *jvm = NULL; jmethodID METHOD_nativeSynthCallback; -jfieldID FIELD_mNativeData; -static inline native_data_t *getNativeData(JNIEnv *env, jobject object) { - return (native_data_t *) (env->GetIntField(object, FIELD_mNativeData)); +static JNIEnv *getJniEnv() { + JNIEnv *env = NULL; + jvm->AttachCurrentThread(&env, NULL); + return env; } /* Callback from espeak. Should call back to the TTS API */ static int SynthCallback(short *audioData, int numSamples, espeak_EVENT *events) { - native_data_t *nat = (native_data_t *) events->user_data; - JNIEnv *env = nat->env; - jobject object = nat->object; + JNIEnv *env = getJniEnv(); + jobject object = (jobject)events->user_data; if (numSamples < 1) { env->CallVoidMethod(object, METHOD_nativeSynthCallback, NULL); @@ -150,6 +141,7 @@ extern "C" { JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { + jvm = vm; JNIEnv *env; if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) { @@ -165,7 +157,6 @@ JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeClassInit( JNIEnv* env, jclass clazz) { if (DEBUG) LOGV("%s", __FUNCTION__); METHOD_nativeSynthCallback = env->GetMethodID(clazz, "nativeSynthCallback", "([B)V"); - FIELD_mNativeData = env->GetFieldID(clazz, "mNativeData", "I"); return JNI_TRUE; } @@ -174,18 +165,9 @@ JNIEXPORT jint JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeCreate( JNIEnv *env, jobject object, jstring path, jint bufferSizeInMillis) { if (DEBUG) LOGV("%s [env=%p, object=%p]", __FUNCTION__, env, object); - native_data_t *nat = new native_data_t; - - if (nat == NULL) { - LOGE("%s: out of memory!", __FUNCTION__); - return 0; - } - - env->SetIntField(object, FIELD_mNativeData, (jint) nat); const char *c_path = path ? env->GetStringUTFChars(path, NULL) : NULL; - nat->object = env->NewWeakGlobalRef(object); if (DEBUG) LOGV("Initializing with path %s", c_path); int sampleRate = espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, bufferSizeInMillis, c_path, 0); @@ -194,20 +176,6 @@ JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeCreate( return sampleRate; } -JNIEXPORT jboolean -JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeDestroy( - JNIEnv *env, jobject object) { - if (DEBUG) LOGV("%s [env=%p, object=%p]", __FUNCTION__, env, object); - - native_data_t *nat = getNativeData(env, object); - if (nat) { - env->DeleteWeakGlobalRef(nat->object); - delete nat; - } - - return JNI_TRUE; -} - JNIEXPORT jobject JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeGetVersion( JNIEnv *env, jclass clazz) { @@ -347,18 +315,15 @@ JNIEXPORT jboolean JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeSynthesize( JNIEnv *env, jobject object, jstring text, jboolean isSsml) { if (DEBUG) LOGV("%s", __FUNCTION__); - native_data_t *nat = getNativeData(env, object); const char *c_text = text ? env->GetStringUTFChars(text, NULL) : NULL; unsigned int unique_identifier; - nat->env = env; - espeak_SetSynthCallback(SynthCallback); const espeak_ERROR result = espeak_Synth(c_text, strlen(c_text), 0, // position POS_CHARACTER, 0, // end position (0 means no end position) isSsml ? espeakCHARS_UTF8 | espeakSSML // UTF-8 encoded SSML : espeakCHARS_UTF8, // UTF-8 encoded text - &unique_identifier, nat); + &unique_identifier, object); espeak_Synchronize(); if (c_text) env->ReleaseStringUTFChars(text, c_text); diff --git a/android/src/com/reecedunn/espeak/SpeechSynthesis.java b/android/src/com/reecedunn/espeak/SpeechSynthesis.java index 9760f386..5131aef4 100644 --- a/android/src/com/reecedunn/espeak/SpeechSynthesis.java +++ b/android/src/com/reecedunn/espeak/SpeechSynthesis.java @@ -83,11 +83,6 @@ public class SpeechSynthesis { attemptInit(); } - @Override - protected void finalize() { - nativeDestroy(); - } - public static String getVersion() { return nativeGetVersion(); } @@ -339,8 +334,6 @@ public class SpeechSynthesis { private native final int nativeCreate(String path, int bufferSizeInMillis); - private native final boolean nativeDestroy(); - private native final static String nativeGetVersion(); private native final String[] nativeGetAvailableVoices();