VoiceSettings.java: Support reading the default_rate and espeak_rate preferences.

This commit is contained in:
Reece H. Dunn
2013-09-04 16:22:56 +01:00
parent 6c5631d810
commit 54ccfc39b4
2 changed files with 110 additions and 8 deletions
@@ -18,7 +18,6 @@ package com.reecedunn.espeak.test;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.test.AndroidTestCase;
import com.reecedunn.espeak.SpeechSynthesis;
import com.reecedunn.espeak.VoiceSettings;
@@ -26,8 +25,21 @@ import com.reecedunn.espeak.VoiceSettings;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class VoiceSettingsTest extends AndroidTestCase
public class VoiceSettingsTest extends TextToSpeechTestCase
{
private SpeechSynthesis.SynthReadyCallback mCallback = new SpeechSynthesis.SynthReadyCallback()
{
@Override
public void onSynthDataReady(byte[] audioData)
{
}
@Override
public void onSynthDataComplete()
{
}
};
// No Settings (New Install)
public void testNoPreferences()
@@ -37,8 +49,10 @@ public class VoiceSettingsTest extends AndroidTestCase
editor.clear();
editor.commit();
VoiceSettings settings = new VoiceSettings(prefs);
SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
VoiceSettings settings = new VoiceSettings(prefs, synth);
assertThat(settings.getVoiceVariant().toString(), is("male"));
assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
}
// Old Settings
@@ -51,8 +65,10 @@ public class VoiceSettingsTest extends AndroidTestCase
editor.putString("default_gender", Integer.toString(SpeechSynthesis.GENDER_MALE));
editor.commit();
VoiceSettings settings = new VoiceSettings(prefs);
SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
VoiceSettings settings = new VoiceSettings(prefs, synth);
assertThat(settings.getVoiceVariant().toString(), is("male"));
assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
}
public void testDefaultGenderFemale()
@@ -63,8 +79,33 @@ public class VoiceSettingsTest extends AndroidTestCase
editor.putString("default_gender", Integer.toString(SpeechSynthesis.GENDER_FEMALE));
editor.commit();
VoiceSettings settings = new VoiceSettings(prefs);
SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
VoiceSettings settings = new VoiceSettings(prefs, synth);
assertThat(settings.getVoiceVariant().toString(), is("female"));
assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
}
public void defaultRateTest(int prefValue, int settingValue, SpeechSynthesis synth)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.putString("default_rate", Integer.toString(prefValue));
editor.commit();
VoiceSettings settings = new VoiceSettings(prefs, synth);
assertThat(settings.getVoiceVariant().toString(), is("male"));
assertThat(settings.getRate(), is(settingValue));
}
public void testDefaultRate()
{
SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
defaultRateTest(300, 450, synth); // clamped to maximum value
defaultRateTest(200, 350, synth);
defaultRateTest(100, 175, synth); // default value
defaultRateTest( 50, 87, synth);
defaultRateTest( 25, 80, synth); // clamped to minimum value
}
// New Settings
@@ -77,8 +118,34 @@ public class VoiceSettingsTest extends AndroidTestCase
editor.putString("espeak_variant", "klatt2-old");
editor.commit();
VoiceSettings settings = new VoiceSettings(prefs);
SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
VoiceSettings settings = new VoiceSettings(prefs, synth);
assertThat(settings.getVoiceVariant().toString(), is("klatt2-old"));
assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
}
public void espeakRateTest(int prefValue, int settingValue, SpeechSynthesis synth)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.putString("espeak_rate", Integer.toString(prefValue));
editor.commit();
VoiceSettings settings = new VoiceSettings(prefs, synth);
assertThat(settings.getVoiceVariant().toString(), is("male"));
assertThat(settings.getRate(), is(settingValue));
}
public void testEspeakRate()
{
SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
espeakRateTest(500, 450, synth); // clamped to maximum value
espeakRateTest(400, 400, synth);
espeakRateTest(200, 200, synth);
espeakRateTest(175, 175, synth); // default value
espeakRateTest(150, 150, synth);
espeakRateTest( 70, 80, synth); // clamped to minimum value
}
// Mixed (Old and New) Settings
@@ -92,7 +159,24 @@ public class VoiceSettingsTest extends AndroidTestCase
editor.putString("espeak_variant", "klatt4");
editor.commit();
VoiceSettings settings = new VoiceSettings(prefs);
SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
VoiceSettings settings = new VoiceSettings(prefs, synth);
assertThat(settings.getVoiceVariant().toString(), is("klatt4"));
assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
}
public void testEspeakRateWithDefaultRate()
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.putString("default_rate", Integer.toString(50));
editor.putString("espeak_rate", Integer.toString(200));
editor.commit();
SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
VoiceSettings settings = new VoiceSettings(prefs, synth);
assertThat(settings.getVoiceVariant().toString(), is("male"));
assertThat(settings.getRate(), is(200));
}
}
@@ -20,12 +20,16 @@ import android.content.SharedPreferences;
public class VoiceSettings {
private final SharedPreferences mPreferences;
private final SpeechSynthesis mEngine;
public static final String PREF_DEFAULT_GENDER = "default_gender";
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 VoiceSettings(SharedPreferences preferences) {
public VoiceSettings(SharedPreferences preferences, SpeechSynthesis engine) {
mPreferences = preferences;
mEngine = engine;
}
public VoiceVariant getVoiceVariant() {
@@ -40,6 +44,20 @@ public class VoiceSettings {
return VoiceVariant.parseVoiceVariant(variant);
}
public int getRate() {
int min = mEngine.Rate.getMinValue();
int max = mEngine.Rate.getMaxValue();
int rate = getPreferenceValue(PREF_RATE, Integer.MIN_VALUE);
if (rate == Integer.MIN_VALUE) {
rate = (int)((float)getPreferenceValue(PREF_DEFAULT_RATE, 100) / 100 * (float)mEngine.Rate.getDefaultValue());
}
if (rate > max) rate = max;
if (rate < min) rate = min;
return rate;
}
private int getPreferenceValue(String preference, int defaultValue) {
String prefString = mPreferences.getString(preference, null);
if (prefString == null) {