mirror of
https://github.com/rhasspy/espeak-ng.git
synced 2026-10-31 05:35:35 +00:00
TtsService.java: rework the logic to use the new Parameter API.
This commit is contained in:
@@ -288,44 +288,6 @@ JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeSetVoiceByProperties(
|
||||
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
|
||||
JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeSetParameter(
|
||||
JNIEnv *env, jobject object, jint parameter, jint value) {
|
||||
|
||||
@@ -183,14 +183,6 @@ public class SpeechSynthesis {
|
||||
}
|
||||
}
|
||||
|
||||
public void setRate(int rate) {
|
||||
nativeSetRate(rate);
|
||||
}
|
||||
|
||||
public void setPitch(int pitch) {
|
||||
nativeSetPitch(pitch);
|
||||
}
|
||||
|
||||
/** Don't announce any punctuation characters. */
|
||||
public static int PUNCT_NONE = 0;
|
||||
|
||||
@@ -326,10 +318,6 @@ public class SpeechSynthesis {
|
||||
|
||||
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 int nativeGetParameter(int parameter, int current);
|
||||
|
||||
@@ -196,9 +196,9 @@ public class TtsService extends TextToSpeechService {
|
||||
if (text == null)
|
||||
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();
|
||||
|
||||
if (DEBUG) {
|
||||
@@ -222,50 +222,18 @@ public class TtsService extends TextToSpeechService {
|
||||
mEngine.getChannelCount());
|
||||
|
||||
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"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales the pitch by the user-specified value.
|
||||
*
|
||||
* @param pitch A pitch value.
|
||||
* @return A scaled pitch value.
|
||||
*/
|
||||
private int scalePitch(int pitch) {
|
||||
private int getPreferenceValue(String preference, int defaultValue) {
|
||||
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() {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user