From 66cfe9af2bb016f31a7450a88f847c5ee68bc50d Mon Sep 17 00:00:00 2001 From: "Reece H. Dunn" Date: Wed, 7 Oct 2015 21:12:05 +0100 Subject: [PATCH] Implement SpeechSynthesis.getSampleRate in Java. --- android/jni/jni/eSpeakService.cpp | 18 ++++-------------- .../com/reecedunn/espeak/SpeechSynthesis.java | 14 ++++++-------- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/android/jni/jni/eSpeakService.cpp b/android/jni/jni/eSpeakService.cpp index 8525faee..2fdaf77a 100644 --- a/android/jni/jni/eSpeakService.cpp +++ b/android/jni/jni/eSpeakService.cpp @@ -112,12 +112,10 @@ enum synthesis_result { struct native_data_t { JNIEnv *env; jobject object; - int sampleRate; native_data_t() { env = NULL; object = NULL; - sampleRate = 0; } }; @@ -172,7 +170,7 @@ JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeClassInit( return JNI_TRUE; } -JNIEXPORT jboolean +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); @@ -180,7 +178,7 @@ JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeCreate( if (nat == NULL) { LOGE("%s: out of memory!", __FUNCTION__); - return JNI_FALSE; + return 0; } env->SetIntField(object, FIELD_mNativeData, (jint) nat); @@ -189,11 +187,11 @@ JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeCreate( nat->object = env->NewWeakGlobalRef(object); if (DEBUG) LOGV("Initializing with path %s", c_path); - nat->sampleRate = espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, bufferSizeInMillis, c_path, 0); + int sampleRate = espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, bufferSizeInMillis, c_path, 0); if (c_path) env->ReleaseStringUTFChars(path, c_path); - return (nat->sampleRate > 0) ? JNI_TRUE : JNI_FALSE; + return sampleRate; } JNIEXPORT jboolean @@ -217,14 +215,6 @@ JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeGetVersion( return env->NewStringUTF(espeak_Info(NULL)); } -JNIEXPORT jint -JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeGetSampleRate( - JNIEnv *env, jobject object) { - if (DEBUG) LOGV("%s", __FUNCTION__); - const native_data_t *nat = getNativeData(env, object); - return (jint)(nat ? nat->sampleRate : 0); -} - JNIEXPORT jobjectArray JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeGetAvailableVoices( JNIEnv *env, jobject object) { diff --git a/android/src/com/reecedunn/espeak/SpeechSynthesis.java b/android/src/com/reecedunn/espeak/SpeechSynthesis.java index 0d461b84..9760f386 100644 --- a/android/src/com/reecedunn/espeak/SpeechSynthesis.java +++ b/android/src/com/reecedunn/espeak/SpeechSynthesis.java @@ -65,6 +65,7 @@ public class SpeechSynthesis { private boolean mInitialized = false; private static int mVoiceCount = 0; + private int mSampleRate = 0; public SpeechSynthesis(Context context, SynthReadyCallback callback) { // First, ensure the data directory exists, otherwise init will crash. @@ -96,7 +97,7 @@ public class SpeechSynthesis { } public int getSampleRate() { - return nativeGetSampleRate(); + return mSampleRate; } public int getChannelCount() { @@ -108,9 +109,7 @@ public class SpeechSynthesis { } public int getBufferSizeInBytes() { - final int bufferSizeInMillis = BUFFER_SIZE_IN_MILLIS; - final int sampleRate = nativeGetSampleRate(); - return (bufferSizeInMillis * sampleRate) / 1000; + return (BUFFER_SIZE_IN_MILLIS * mSampleRate) / 1000; } public List getAvailableVoices() { @@ -311,7 +310,8 @@ public class SpeechSynthesis { return; } - if (!nativeCreate(mDatapath, BUFFER_SIZE_IN_MILLIS)) { + mSampleRate = nativeCreate(mDatapath, BUFFER_SIZE_IN_MILLIS); + if (mSampleRate == 0) { Log.e(TAG, "Failed to initialize speech synthesis library"); return; } @@ -337,14 +337,12 @@ public class SpeechSynthesis { private static native final boolean nativeClassInit(); - private native final boolean nativeCreate(String path, int bufferSizeInMillis); + private native final int nativeCreate(String path, int bufferSizeInMillis); private native final boolean nativeDestroy(); private native final static String nativeGetVersion(); - private native final int nativeGetSampleRate(); - private native final String[] nativeGetAvailableVoices(); private native final boolean nativeSetVoiceByName(String name);