Browse Source

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

master
Reece H. Dunn 12 years ago
parent
commit
54ccfc39b4

+ 91
- 7
android/eSpeakTests/src/com/reecedunn/espeak/test/VoiceSettingsTest.java View File



import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.test.AndroidTestCase;


import com.reecedunn.espeak.SpeechSynthesis; import com.reecedunn.espeak.SpeechSynthesis;
import com.reecedunn.espeak.VoiceSettings; import com.reecedunn.espeak.VoiceSettings;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*; 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) // No Settings (New Install)


public void testNoPreferences() public void testNoPreferences()
editor.clear(); editor.clear();
editor.commit(); 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.getVoiceVariant().toString(), is("male"));
assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
} }


// Old Settings // Old Settings
editor.putString("default_gender", Integer.toString(SpeechSynthesis.GENDER_MALE)); editor.putString("default_gender", Integer.toString(SpeechSynthesis.GENDER_MALE));
editor.commit(); 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.getVoiceVariant().toString(), is("male"));
assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
} }


public void testDefaultGenderFemale() public void testDefaultGenderFemale()
editor.putString("default_gender", Integer.toString(SpeechSynthesis.GENDER_FEMALE)); editor.putString("default_gender", Integer.toString(SpeechSynthesis.GENDER_FEMALE));
editor.commit(); 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.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 // New Settings
editor.putString("espeak_variant", "klatt2-old"); editor.putString("espeak_variant", "klatt2-old");
editor.commit(); 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.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 // Mixed (Old and New) Settings
editor.putString("espeak_variant", "klatt4"); editor.putString("espeak_variant", "klatt4");
editor.commit(); 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.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));
} }
} }

+ 19
- 1
android/src/com/reecedunn/espeak/VoiceSettings.java View File



public class VoiceSettings { public class VoiceSettings {
private final SharedPreferences mPreferences; private final SharedPreferences mPreferences;
private final SpeechSynthesis mEngine;


public static final String PREF_DEFAULT_GENDER = "default_gender"; public static final String PREF_DEFAULT_GENDER = "default_gender";
public static final String PREF_VARIANT = "espeak_variant"; 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; mPreferences = preferences;
mEngine = engine;
} }


public VoiceVariant getVoiceVariant() { public VoiceVariant getVoiceVariant() {
return VoiceVariant.parseVoiceVariant(variant); 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) { private int getPreferenceValue(String preference, int defaultValue) {
String prefString = mPreferences.getString(preference, null); String prefString = mPreferences.getString(preference, null);
if (prefString == null) { if (prefString == null) {

Loading…
Cancel
Save