Browse Source

VoiceSettings.java: Add support for converting to a JSON object.

master
Reece H. Dunn 11 years ago
parent
commit
58ae6ea33f

+ 295
- 6
android/eSpeakTests/src/com/reecedunn/espeak/test/VoiceSettingsTest.java View File

import com.reecedunn.espeak.SpeechSynthesis; import com.reecedunn.espeak.SpeechSynthesis;
import com.reecedunn.espeak.VoiceSettings; import com.reecedunn.espeak.VoiceSettings;


import org.json.JSONException;
import org.json.JSONObject;

import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;


assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
assertThat(settings.getPunctuationCharacters(), is(nullValue())); assertThat(settings.getPunctuationCharacters(), is(nullValue()));

try {
JSONObject json = settings.toJSON();
assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
} catch (JSONException e) {
assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
}
} }


// Old Settings // Old Settings
assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
assertThat(settings.getPunctuationCharacters(), is(nullValue())); assertThat(settings.getPunctuationCharacters(), is(nullValue()));

try {
JSONObject json = settings.toJSON();
assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
} catch (JSONException e) {
assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
}
} }


public void testDefaultGenderFemale() public void testDefaultGenderFemale()
assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
assertThat(settings.getPunctuationCharacters(), is(nullValue())); assertThat(settings.getPunctuationCharacters(), is(nullValue()));

try {
JSONObject json = settings.toJSON();
assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("female"));
assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
} catch (JSONException e) {
assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
}
} }


public void defaultRateTest(int prefValue, int settingValue, SpeechSynthesis synth) public void defaultRateTest(int prefValue, int settingValue, SpeechSynthesis synth)
assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
assertThat(settings.getPunctuationCharacters(), is(nullValue())); assertThat(settings.getPunctuationCharacters(), is(nullValue()));

try {
JSONObject json = settings.toJSON();
assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(settingValue));
assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
} catch (JSONException e) {
assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
}
} }


public void testDefaultRate() public void testDefaultRate()
assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
assertThat(settings.getPunctuationCharacters(), is(nullValue())); assertThat(settings.getPunctuationCharacters(), is(nullValue()));

try {
JSONObject json = settings.toJSON();
assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(settingValue));
assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
} catch (JSONException e) {
assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
}
} }


public void testDefaultPitch() public void testDefaultPitch()
assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
assertThat(settings.getPunctuationCharacters(), is(nullValue())); assertThat(settings.getPunctuationCharacters(), is(nullValue()));

try {
JSONObject json = settings.toJSON();
assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("klatt2-old"));
assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
} catch (JSONException e) {
assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
}
} }


public void espeakRateTest(int prefValue, int settingValue, SpeechSynthesis synth) public void espeakRateTest(int prefValue, int settingValue, SpeechSynthesis synth)
assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
assertThat(settings.getPunctuationCharacters(), is(nullValue())); assertThat(settings.getPunctuationCharacters(), is(nullValue()));

try {
JSONObject json = settings.toJSON();
assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(settingValue));
assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
} catch (JSONException e) {
assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
}
} }


public void testEspeakRate() public void testEspeakRate()
assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
assertThat(settings.getPunctuationCharacters(), is(nullValue())); assertThat(settings.getPunctuationCharacters(), is(nullValue()));

try {
JSONObject json = settings.toJSON();
assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(settingValue));
assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
} catch (JSONException e) {
assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
}
} }


public void testEspeakPitch() public void testEspeakPitch()
assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
assertThat(settings.getPunctuationCharacters(), is(nullValue())); assertThat(settings.getPunctuationCharacters(), is(nullValue()));

try {
JSONObject json = settings.toJSON();
assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(settingValue));
assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
} catch (JSONException e) {
assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
}
} }


public void testEspeakPitchRange() public void testEspeakPitchRange()
assertThat(settings.getVolume(), is(settingValue)); assertThat(settings.getVolume(), is(settingValue));
assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
assertThat(settings.getPunctuationCharacters(), is(nullValue())); assertThat(settings.getPunctuationCharacters(), is(nullValue()));

try {
JSONObject json = settings.toJSON();
assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(settingValue));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
} catch (JSONException e) {
assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
}
} }


public void testEspeakVolume() public void testEspeakVolume()
espeakVolumeTest( -5, 0, synth); // clamped to minimum value espeakVolumeTest( -5, 0, synth); // clamped to minimum value
} }


public void espeakPunctuationLevelTest(int prefValue, int settingValue, SpeechSynthesis synth)
public void espeakPunctuationLevelTest(int prefValue, int settingValue, String jsonValue, SpeechSynthesis synth)
{ {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = prefs.edit(); SharedPreferences.Editor editor = prefs.edit();
assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
assertThat(settings.getPunctuationLevel(), is(settingValue)); assertThat(settings.getPunctuationLevel(), is(settingValue));
assertThat(settings.getPunctuationCharacters(), is(nullValue())); assertThat(settings.getPunctuationCharacters(), is(nullValue()));

try {
JSONObject json = settings.toJSON();
assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(jsonValue));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
} catch (JSONException e) {
assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
}
} }


public void testEspeakPunctuationLevel() public void testEspeakPunctuationLevel()
{ {
SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback); SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
espeakPunctuationLevelTest( 3, SpeechSynthesis.PUNCT_SOME, synth); // clamped to maximum value
espeakPunctuationLevelTest( 2, SpeechSynthesis.PUNCT_SOME, synth);
espeakPunctuationLevelTest( 1, SpeechSynthesis.PUNCT_ALL, synth);
espeakPunctuationLevelTest( 0, SpeechSynthesis.PUNCT_NONE, synth);
espeakPunctuationLevelTest(-1, SpeechSynthesis.PUNCT_NONE, synth); // clamped to minimum value
espeakPunctuationLevelTest( 3, SpeechSynthesis.PUNCT_SOME, "some", synth); // clamped to maximum value
espeakPunctuationLevelTest( 2, SpeechSynthesis.PUNCT_SOME, "some", synth);
espeakPunctuationLevelTest( 1, SpeechSynthesis.PUNCT_ALL, "all", synth);
espeakPunctuationLevelTest( 0, SpeechSynthesis.PUNCT_NONE, "none", synth);
espeakPunctuationLevelTest(-1, SpeechSynthesis.PUNCT_NONE, "none", synth); // clamped to minimum value
} }


public void testEspeakPunctuationCharacters() public void testEspeakPunctuationCharacters()
assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
assertThat(settings.getPunctuationCharacters(), is(".?!")); assertThat(settings.getPunctuationCharacters(), is(".?!"));

try {
JSONObject json = settings.toJSON();
assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(".?!"));
} catch (JSONException e) {
assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
}
} }


// Mixed (Old and New) Settings // Mixed (Old and New) Settings
assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
assertThat(settings.getPunctuationCharacters(), is(nullValue())); assertThat(settings.getPunctuationCharacters(), is(nullValue()));

try {
JSONObject json = settings.toJSON();
assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("klatt4"));
assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
} catch (JSONException e) {
assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
}
} }


public void testEspeakRateWithDefaultRate() public void testEspeakRateWithDefaultRate()
assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
assertThat(settings.getPunctuationCharacters(), is(nullValue())); assertThat(settings.getPunctuationCharacters(), is(nullValue()));

try {
JSONObject json = settings.toJSON();
assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(200));
assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
} catch (JSONException e) {
assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
}
} }


public void testEspeakPitchWithDefaultPitch() public void testEspeakPitchWithDefaultPitch()
assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
assertThat(settings.getPunctuationCharacters(), is(nullValue())); assertThat(settings.getPunctuationCharacters(), is(nullValue()));

try {
JSONObject json = settings.toJSON();
assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(75));
assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
} catch (JSONException e) {
assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
}
} }
} }

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



import android.content.SharedPreferences; import android.content.SharedPreferences;


import org.json.JSONException;
import org.json.JSONObject;

public class VoiceSettings { public class VoiceSettings {
private final SharedPreferences mPreferences; private final SharedPreferences mPreferences;
private final SpeechSynthesis mEngine; private final SpeechSynthesis mEngine;
public static final String PREF_PUNCTUATION_LEVEL = "espeak_punctuation_level"; public static final String PREF_PUNCTUATION_LEVEL = "espeak_punctuation_level";
public static final String PREF_PUNCTUATION_CHARACTERS = "espeak_punctuation_characters"; public static final String PREF_PUNCTUATION_CHARACTERS = "espeak_punctuation_characters";


public static final String PRESET_VARIANT = "variant";
public static final String PRESET_RATE = "rate";
public static final String PRESET_PITCH = "pitch";
public static final String PRESET_PITCH_RANGE = "pitch-range";
public static final String PRESET_VOLUME = "volume";
public static final String PRESET_PUNCTUATION_LEVEL = "punctuation-level";
public static final String PRESET_PUNCTUATION_CHARACTERS = "punctuation-characters";

public static final String PUNCTUATION_NONE = "none";
public static final String PUNCTUATION_SOME = "some";
public static final String PUNCTUATION_ALL = "all";

public VoiceSettings(SharedPreferences preferences, SpeechSynthesis engine) { public VoiceSettings(SharedPreferences preferences, SpeechSynthesis engine) {
mPreferences = preferences; mPreferences = preferences;
mEngine = engine; mEngine = engine;
} }
return Integer.parseInt(prefString); return Integer.parseInt(prefString);
} }

public JSONObject toJSON() throws JSONException {
JSONObject settings = new JSONObject();
settings.put(PRESET_VARIANT, getVoiceVariant().toString());
settings.put(PRESET_RATE, getRate());
settings.put(PRESET_PITCH, getPitch());
settings.put(PRESET_PITCH_RANGE, getPitchRange());
settings.put(PRESET_VOLUME, getVolume());
settings.put(PRESET_PUNCTUATION_CHARACTERS, getPunctuationCharacters());
switch (getPunctuationLevel()) {
case SpeechSynthesis.PUNCT_NONE:
settings.put(PRESET_PUNCTUATION_LEVEL, PUNCTUATION_NONE);
break;
case SpeechSynthesis.PUNCT_SOME:
settings.put(PRESET_PUNCTUATION_LEVEL, PUNCTUATION_SOME);
break;
case SpeechSynthesis.PUNCT_ALL:
settings.put(PRESET_PUNCTUATION_LEVEL, PUNCTUATION_ALL);
break;
}
return settings;
}
} }

Loading…
Cancel
Save