diff --git a/android/eSpeakTests/src/com/reecedunn/espeak/test/VoiceSettingsTest.java b/android/eSpeakTests/src/com/reecedunn/espeak/test/VoiceSettingsTest.java index 1f8de60e..866bff06 100644 --- a/android/eSpeakTests/src/com/reecedunn/espeak/test/VoiceSettingsTest.java +++ b/android/eSpeakTests/src/com/reecedunn/espeak/test/VoiceSettingsTest.java @@ -53,6 +53,7 @@ public class VoiceSettingsTest extends TextToSpeechTestCase VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("male")); assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); + assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); } // Old Settings @@ -69,6 +70,7 @@ public class VoiceSettingsTest extends TextToSpeechTestCase VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("male")); assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); + assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); } public void testDefaultGenderFemale() @@ -83,6 +85,7 @@ public class VoiceSettingsTest extends TextToSpeechTestCase VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("female")); assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); + assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); } public void defaultRateTest(int prefValue, int settingValue, SpeechSynthesis synth) @@ -96,6 +99,7 @@ public class VoiceSettingsTest extends TextToSpeechTestCase VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("male")); assertThat(settings.getRate(), is(settingValue)); + assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); } public void testDefaultRate() @@ -108,6 +112,31 @@ public class VoiceSettingsTest extends TextToSpeechTestCase defaultRateTest( 25, 80, synth); // clamped to minimum value } + public void defaultPitchTest(int prefValue, int settingValue, SpeechSynthesis synth) + { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + SharedPreferences.Editor editor = prefs.edit(); + editor.clear(); + editor.putString("default_pitch", Integer.toString(prefValue)); + editor.commit(); + + VoiceSettings settings = new VoiceSettings(prefs, synth); + assertThat(settings.getVoiceVariant().toString(), is("male")); + assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); + assertThat(settings.getPitch(), is(settingValue)); + } + + public void testDefaultPitch() + { + SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback); + defaultPitchTest(250, 100, synth); // clamped to maximum value + defaultPitchTest(200, 100, synth); + defaultPitchTest(100, 50, synth); // default value + defaultPitchTest( 50, 25, synth); + defaultPitchTest( 0, 0, synth); + defaultPitchTest( -5, 0, synth); // clamped to minimum value + } + // New Settings public void testEspeakVariant() @@ -122,6 +151,7 @@ public class VoiceSettingsTest extends TextToSpeechTestCase VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("klatt2-old")); assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); + assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); } public void espeakRateTest(int prefValue, int settingValue, SpeechSynthesis synth) @@ -135,6 +165,7 @@ public class VoiceSettingsTest extends TextToSpeechTestCase VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("male")); assertThat(settings.getRate(), is(settingValue)); + assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); } public void testEspeakRate() @@ -148,6 +179,30 @@ public class VoiceSettingsTest extends TextToSpeechTestCase espeakRateTest( 70, 80, synth); // clamped to minimum value } + public void espeakPitchTest(int prefValue, int settingValue, SpeechSynthesis synth) + { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + SharedPreferences.Editor editor = prefs.edit(); + editor.clear(); + editor.putString("espeak_pitch", Integer.toString(prefValue)); + editor.commit(); + + VoiceSettings settings = new VoiceSettings(prefs, synth); + assertThat(settings.getVoiceVariant().toString(), is("male")); + assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); + assertThat(settings.getPitch(), is(settingValue)); + } + + public void testEspeakPitch() + { + SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback); + espeakPitchTest(110, 100, synth); // clamped to maximum value + espeakPitchTest(100, 100, synth); + espeakPitchTest( 50, 50, synth); // default value + espeakPitchTest( 10, 10, synth); + espeakPitchTest( -5, 0, synth); // clamped to minimum value + } + // Mixed (Old and New) Settings public void testEspeakVariantWithDefaultGenderFemale() @@ -163,6 +218,7 @@ public class VoiceSettingsTest extends TextToSpeechTestCase VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("klatt4")); assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); + assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); } public void testEspeakRateWithDefaultRate() @@ -178,5 +234,6 @@ public class VoiceSettingsTest extends TextToSpeechTestCase VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("male")); assertThat(settings.getRate(), is(200)); + assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); } } diff --git a/android/src/com/reecedunn/espeak/VoiceSettings.java b/android/src/com/reecedunn/espeak/VoiceSettings.java index 2676f17e..3d3602e8 100644 --- a/android/src/com/reecedunn/espeak/VoiceSettings.java +++ b/android/src/com/reecedunn/espeak/VoiceSettings.java @@ -26,6 +26,8 @@ public class VoiceSettings { public static final String PREF_VARIANT = "espeak_variant"; public static final String PREF_DEFAULT_RATE = "default_rate"; public static final String PREF_RATE = "espeak_rate"; + public static final String PREF_DEFAULT_PITCH = "default_pitch"; + public static final String PREF_PITCH = "espeak_pitch"; public VoiceSettings(SharedPreferences preferences, SpeechSynthesis engine) { mPreferences = preferences; @@ -58,6 +60,20 @@ public class VoiceSettings { return rate; } + public int getPitch() { + int min = mEngine.Pitch.getMinValue(); + int max = mEngine.Pitch.getMaxValue(); + + int pitch = getPreferenceValue(PREF_PITCH, Integer.MIN_VALUE); + if (pitch == Integer.MIN_VALUE) { + pitch = getPreferenceValue(PREF_DEFAULT_PITCH, 100) / 2; + } + + if (pitch > max) pitch = max; + if (pitch < min) pitch = min; + return pitch; + } + private int getPreferenceValue(String preference, int defaultValue) { String prefString = mPreferences.getString(preference, null); if (prefString == null) {