Browse Source

VoiceSettings.java: Support reading the default_pitch and espeak_pitch preferences.

master
Reece H. Dunn 12 years ago
parent
commit
f47729453f

+ 57
- 0
android/eSpeakTests/src/com/reecedunn/espeak/test/VoiceSettingsTest.java View File

VoiceSettings settings = new VoiceSettings(prefs, synth); 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())); assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
} }


// Old Settings // Old Settings
VoiceSettings settings = new VoiceSettings(prefs, synth); 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())); assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
} }


public void testDefaultGenderFemale() public void testDefaultGenderFemale()
VoiceSettings settings = new VoiceSettings(prefs, synth); 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())); assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
} }


public void defaultRateTest(int prefValue, int settingValue, SpeechSynthesis synth) public void defaultRateTest(int prefValue, int settingValue, SpeechSynthesis synth)
VoiceSettings settings = new VoiceSettings(prefs, synth); VoiceSettings settings = new VoiceSettings(prefs, synth);
assertThat(settings.getVoiceVariant().toString(), is("male")); assertThat(settings.getVoiceVariant().toString(), is("male"));
assertThat(settings.getRate(), is(settingValue)); assertThat(settings.getRate(), is(settingValue));
assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
} }


public void testDefaultRate() public void testDefaultRate()
defaultRateTest( 25, 80, synth); // clamped to minimum value 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 // New Settings


public void testEspeakVariant() public void testEspeakVariant()
VoiceSettings settings = new VoiceSettings(prefs, synth); 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())); assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
} }


public void espeakRateTest(int prefValue, int settingValue, SpeechSynthesis synth) public void espeakRateTest(int prefValue, int settingValue, SpeechSynthesis synth)
VoiceSettings settings = new VoiceSettings(prefs, synth); VoiceSettings settings = new VoiceSettings(prefs, synth);
assertThat(settings.getVoiceVariant().toString(), is("male")); assertThat(settings.getVoiceVariant().toString(), is("male"));
assertThat(settings.getRate(), is(settingValue)); assertThat(settings.getRate(), is(settingValue));
assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
} }


public void testEspeakRate() public void testEspeakRate()
espeakRateTest( 70, 80, synth); // clamped to minimum value 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 // Mixed (Old and New) Settings


public void testEspeakVariantWithDefaultGenderFemale() public void testEspeakVariantWithDefaultGenderFemale()
VoiceSettings settings = new VoiceSettings(prefs, synth); 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())); assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
} }


public void testEspeakRateWithDefaultRate() public void testEspeakRateWithDefaultRate()
VoiceSettings settings = new VoiceSettings(prefs, synth); VoiceSettings settings = new VoiceSettings(prefs, synth);
assertThat(settings.getVoiceVariant().toString(), is("male")); assertThat(settings.getVoiceVariant().toString(), is("male"));
assertThat(settings.getRate(), is(200)); assertThat(settings.getRate(), is(200));
assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
} }
} }

+ 16
- 0
android/src/com/reecedunn/espeak/VoiceSettings.java View File

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_DEFAULT_RATE = "default_rate";
public static final String PREF_RATE = "espeak_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) { public VoiceSettings(SharedPreferences preferences, SpeechSynthesis engine) {
mPreferences = preferences; mPreferences = preferences;
return rate; 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) { 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