| return JNI_FALSE; | return JNI_FALSE; | ||||
| } | } | ||||
| JNIEXPORT jboolean | |||||
| JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeSetRate( | |||||
| JNIEnv *env, jobject object, jint rate) { | |||||
| const int wpm = ((float)rate / 100) * espeak_GetParameter(espeakRATE, 0); | |||||
| if (DEBUG) LOGV("%s(rate=%d, wpm=%d)", __FUNCTION__, rate, wpm); | |||||
| const espeak_ERROR result = espeak_SetParameter(espeakRATE, wpm, 0); | |||||
| switch (result) { | |||||
| case EE_OK: return JNI_TRUE; | |||||
| case EE_INTERNAL_ERROR: LOGE("espeak_SetParameter: internal error."); break; | |||||
| case EE_BUFFER_FULL: LOGE("espeak_SetParameter: buffer full."); break; | |||||
| case EE_NOT_FOUND: LOGE("espeak_SetParameter: not found."); break; | |||||
| } | |||||
| return JNI_FALSE; | |||||
| } | |||||
| JNIEXPORT jboolean | |||||
| JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeSetPitch( | |||||
| JNIEnv *env, jobject object, jint pitch) { | |||||
| // The values of pitch from android range from 50 - 200, with 100 being normal. | |||||
| // The values espeak supports are from 0 - 100, with 50 being normal. | |||||
| // Therefore, halve the value to get the value that espeak supports: | |||||
| pitch = pitch / 2; | |||||
| if (DEBUG) LOGV("%s(pitch=%d)", __FUNCTION__, pitch); | |||||
| const espeak_ERROR result = espeak_SetParameter(espeakPITCH, (int) pitch, 0); | |||||
| switch (result) { | |||||
| case EE_OK: return JNI_TRUE; | |||||
| case EE_INTERNAL_ERROR: LOGE("espeak_SetParameter: internal error."); break; | |||||
| case EE_BUFFER_FULL: LOGE("espeak_SetParameter: buffer full."); break; | |||||
| case EE_NOT_FOUND: LOGE("espeak_SetParameter: not found."); break; | |||||
| } | |||||
| return JNI_FALSE; | |||||
| } | |||||
| JNIEXPORT jboolean | JNIEXPORT jboolean | ||||
| JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeSetParameter( | JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeSetParameter( | ||||
| JNIEnv *env, jobject object, jint parameter, jint value) { | JNIEnv *env, jobject object, jint parameter, jint value) { |
| } | } | ||||
| } | } | ||||
| public void setRate(int rate) { | |||||
| nativeSetRate(rate); | |||||
| } | |||||
| public void setPitch(int pitch) { | |||||
| nativeSetPitch(pitch); | |||||
| } | |||||
| /** Don't announce any punctuation characters. */ | /** Don't announce any punctuation characters. */ | ||||
| public static int PUNCT_NONE = 0; | public static int PUNCT_NONE = 0; | ||||
| private native final boolean nativeSetVoiceByProperties(String language, int gender, int age); | private native final boolean nativeSetVoiceByProperties(String language, int gender, int age); | ||||
| private native final boolean nativeSetRate(int rate); | |||||
| private native final boolean nativeSetPitch(int pitch); | |||||
| private native final boolean nativeSetParameter(int parameter, int value); | private native final boolean nativeSetParameter(int parameter, int value); | ||||
| private native final int nativeGetParameter(int parameter, int current); | private native final int nativeGetParameter(int parameter, int current); |
| if (text == null) | if (text == null) | ||||
| return; | return; | ||||
| final int gender = getDefaultGender(); | |||||
| final int rate = scaleRate(request.getSpeechRate()); | |||||
| final int pitch = scalePitch(request.getPitch()); | |||||
| final int gender = getPreferenceValue("default_gender", 0); | |||||
| final int rate = (getPreferenceValue("default_rate", 100) / 100) * mEngine.Rate.getDefaultValue(); | |||||
| final int pitch = getPreferenceValue("default_pitch", 100) / 2; | |||||
| final Bundle params = request.getParams(); | final Bundle params = request.getParams(); | ||||
| if (DEBUG) { | if (DEBUG) { | ||||
| mEngine.getChannelCount()); | mEngine.getChannelCount()); | ||||
| mEngine.setVoice(mMatchingVoice, null, gender, SpeechSynthesis.AGE_ANY); | mEngine.setVoice(mMatchingVoice, null, gender, SpeechSynthesis.AGE_ANY); | ||||
| mEngine.setRate(rate); | |||||
| mEngine.setPitch(pitch); | |||||
| mEngine.Rate.setValue(rate, request.getSpeechRate()); | |||||
| mEngine.Pitch.setValue(pitch, request.getPitch()); | |||||
| mEngine.synthesize(text, text.startsWith("<speak")); | mEngine.synthesize(text, text.startsWith("<speak")); | ||||
| } | } | ||||
| /** | |||||
| * Scales the pitch by the user-specified value. | |||||
| * | |||||
| * @param pitch A pitch value. | |||||
| * @return A scaled pitch value. | |||||
| */ | |||||
| private int scalePitch(int pitch) { | |||||
| final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); | |||||
| final String defaultPitchString = prefs.getString("default_pitch", "100"); | |||||
| final int defaultPitch = Integer.parseInt(defaultPitchString); | |||||
| return (pitch * defaultPitch / 100); | |||||
| } | |||||
| /** | |||||
| * Returns user-specified gender. | |||||
| * | |||||
| * @return A gender value. | |||||
| */ | |||||
| private int getDefaultGender() { | |||||
| private int getPreferenceValue(String preference, int defaultValue) { | |||||
| final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); | final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); | ||||
| final String defaultGenderString = prefs.getString("default_gender", "0"); | |||||
| final int defaultGender = Integer.parseInt(defaultGenderString); | |||||
| return defaultGender; | |||||
| } | |||||
| /** | |||||
| * Scales the rate by the user-specified value. | |||||
| * | |||||
| * @param rate A rate value. | |||||
| * @return A scaled rate value. | |||||
| */ | |||||
| private int scaleRate(int rate) { | |||||
| final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); | |||||
| final String defaultRateString = prefs.getString("default_rate", "100"); | |||||
| final int defaultRate = Integer.parseInt(defaultRateString); | |||||
| return (rate * defaultRate / 100); | |||||
| final String prefString = prefs.getString(preference, null); | |||||
| if (prefString == null) { | |||||
| return defaultValue; | |||||
| } | |||||
| return Integer.parseInt(prefString); | |||||
| } | } | ||||
| /** | /** |