VoiceSettings.java: Support reading the espeak_volume preference.

This commit is contained in:
Reece H. Dunn
2013-09-04 17:48:49 +01:00
parent f1588b3770
commit 0086ec61c3
2 changed files with 48 additions and 0 deletions
@@ -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()));
}
}
@@ -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) {