From 0086ec61c393aeaf239bb9e8e863067c28f57766 Mon Sep 17 00:00:00 2001 From: "Reece H. Dunn" Date: Wed, 4 Sep 2013 17:48:49 +0100 Subject: [PATCH] VoiceSettings.java: Support reading the espeak_volume preference. --- .../espeak/test/VoiceSettingsTest.java | 37 +++++++++++++++++++ .../com/reecedunn/espeak/VoiceSettings.java | 11 ++++++ 2 files changed, 48 insertions(+) diff --git a/android/eSpeakTests/src/com/reecedunn/espeak/test/VoiceSettingsTest.java b/android/eSpeakTests/src/com/reecedunn/espeak/test/VoiceSettingsTest.java index 1db3668c..77ecb52a 100644 --- a/android/eSpeakTests/src/com/reecedunn/espeak/test/VoiceSettingsTest.java +++ b/android/eSpeakTests/src/com/reecedunn/espeak/test/VoiceSettingsTest.java @@ -55,6 +55,7 @@ public class VoiceSettingsTest extends TextToSpeechTestCase assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue())); + assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); } // Old Settings @@ -73,6 +74,7 @@ public class VoiceSettingsTest extends TextToSpeechTestCase assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue())); + assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); } public void testDefaultGenderFemale() @@ -89,6 +91,7 @@ public class VoiceSettingsTest extends TextToSpeechTestCase assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue())); + assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); } public void defaultRateTest(int prefValue, int settingValue, SpeechSynthesis synth) @@ -104,6 +107,7 @@ public class VoiceSettingsTest extends TextToSpeechTestCase assertThat(settings.getRate(), is(settingValue)); assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue())); + assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); } public void testDefaultRate() @@ -129,6 +133,7 @@ public class VoiceSettingsTest extends TextToSpeechTestCase assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); assertThat(settings.getPitch(), is(settingValue)); assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue())); + assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); } public void testDefaultPitch() @@ -158,6 +163,7 @@ public class VoiceSettingsTest extends TextToSpeechTestCase assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue())); + assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); } public void espeakRateTest(int prefValue, int settingValue, SpeechSynthesis synth) @@ -173,6 +179,7 @@ public class VoiceSettingsTest extends TextToSpeechTestCase assertThat(settings.getRate(), is(settingValue)); assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue())); + assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); } public void testEspeakRate() @@ -199,6 +206,7 @@ public class VoiceSettingsTest extends TextToSpeechTestCase assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); assertThat(settings.getPitch(), is(settingValue)); assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue())); + assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); } public void testEspeakPitch() @@ -224,6 +232,7 @@ public class VoiceSettingsTest extends TextToSpeechTestCase assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); assertThat(settings.getPitchRange(), is(settingValue)); + assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); } public void testEspeakPitchRange() @@ -236,6 +245,32 @@ public class VoiceSettingsTest extends TextToSpeechTestCase espeakPitchRangeTest( -5, 0, synth); // clamped to minimum value } + public void espeakVolumeTest(int prefValue, int settingValue, SpeechSynthesis synth) + { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + SharedPreferences.Editor editor = prefs.edit(); + editor.clear(); + editor.putString("espeak_volume", 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(synth.Pitch.getDefaultValue())); + assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue())); + assertThat(settings.getVolume(), is(settingValue)); + } + + public void testEspeakVolume() + { + SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback); + espeakVolumeTest(210, 200, synth); // clamped to maximum value + espeakVolumeTest(150, 150, synth); + espeakVolumeTest(100, 100, synth); // default value + espeakVolumeTest( 50, 50, synth); + espeakVolumeTest( -5, 0, synth); // clamped to minimum value + } + // Mixed (Old and New) Settings public void testEspeakVariantWithDefaultGenderFemale() @@ -253,6 +288,7 @@ public class VoiceSettingsTest extends TextToSpeechTestCase assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue())); + assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); } public void testEspeakRateWithDefaultRate() @@ -270,5 +306,6 @@ public class VoiceSettingsTest extends TextToSpeechTestCase assertThat(settings.getRate(), is(200)); assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue())); + assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); } } diff --git a/android/src/com/reecedunn/espeak/VoiceSettings.java b/android/src/com/reecedunn/espeak/VoiceSettings.java index 2e265945..ffd74e25 100644 --- a/android/src/com/reecedunn/espeak/VoiceSettings.java +++ b/android/src/com/reecedunn/espeak/VoiceSettings.java @@ -29,6 +29,7 @@ public class VoiceSettings { public static final String PREF_DEFAULT_PITCH = "default_pitch"; public static final String PREF_PITCH = "espeak_pitch"; public static final String PREF_PITCH_RANGE = "espeak_pitch_range"; + public static final String PREF_VOLUME = "espeak_volume"; public VoiceSettings(SharedPreferences preferences, SpeechSynthesis engine) { mPreferences = preferences; @@ -85,6 +86,16 @@ public class VoiceSettings { return range; } + public int getVolume() { + int min = mEngine.Volume.getMinValue(); + int max = mEngine.Volume.getMaxValue(); + + int range = getPreferenceValue(PREF_VOLUME, mEngine.Volume.getDefaultValue()); + if (range > max) range = max; + if (range < min) range = min; + return range; + } + private int getPreferenceValue(String preference, int defaultValue) { String prefString = mPreferences.getString(preference, null); if (prefString == null) {