| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.reecedunn.espeak; |
| 18 |
|
| 19 |
import android.content.SharedPreferences; |
| 20 |
|
| 21 |
import org.json.JSONException; |
| 22 |
import org.json.JSONObject; |
| 23 |
|
| 24 |
public class VoiceSettings { |
| 25 |
private final SharedPreferences mPreferences; |
| 26 |
private final SpeechSynthesis mEngine; |
| 27 |
|
| 28 |
public static final String PREF_DEFAULT_GENDER = "default_gender"; |
| 29 |
public static final String PREF_VARIANT = "espeak_variant"; |
| 30 |
public static final String PREF_DEFAULT_RATE = "default_rate"; |
| 31 |
public static final String PREF_RATE = "espeak_rate"; |
| 32 |
public static final String PREF_DEFAULT_PITCH = "default_pitch"; |
| 33 |
public static final String PREF_PITCH = "espeak_pitch"; |
| 34 |
public static final String PREF_PITCH_RANGE = "espeak_pitch_range"; |
| 35 |
public static final String PREF_VOLUME = "espeak_volume"; |
| 36 |
public static final String PREF_PUNCTUATION_LEVEL = "espeak_punctuation_level"; |
| 37 |
public static final String PREF_PUNCTUATION_CHARACTERS = "espeak_punctuation_characters"; |
| 38 |
|
| 39 |
public static final String PRESET_VARIANT = "variant"; |
| 40 |
public static final String PRESET_RATE = "rate"; |
| 41 |
public static final String PRESET_PITCH = "pitch"; |
| 42 |
public static final String PRESET_PITCH_RANGE = "pitch-range"; |
| 43 |
public static final String PRESET_VOLUME = "volume"; |
| 44 |
public static final String PRESET_PUNCTUATION_LEVEL = "punctuation-level"; |
| 45 |
public static final String PRESET_PUNCTUATION_CHARACTERS = "punctuation-characters"; |
| 46 |
|
| 47 |
public static final String PUNCTUATION_NONE = "none"; |
| 48 |
public static final String PUNCTUATION_SOME = "some"; |
| 49 |
public static final String PUNCTUATION_ALL = "all"; |
| 50 |
|
| 51 |
public VoiceSettings(SharedPreferences preferences, SpeechSynthesis engine) { |
| 52 |
mPreferences = preferences; |
| 53 |
mEngine = engine; |
| 54 |
} |
| 55 |
|
| 56 |
public VoiceVariant getVoiceVariant() { |
| 57 |
String variant = mPreferences.getString(PREF_VARIANT, null); |
| 58 |
if (variant == null) { |
| 59 |
int gender = getPreferenceValue(PREF_DEFAULT_GENDER, SpeechSynthesis.GENDER_MALE); |
| 60 |
if (gender == SpeechSynthesis.GENDER_FEMALE) { |
| 61 |
return VoiceVariant.parseVoiceVariant(VoiceVariant.FEMALE); |
| 62 |
} |
| 63 |
return VoiceVariant.parseVoiceVariant(VoiceVariant.MALE); |
| 64 |
} |
| 65 |
return VoiceVariant.parseVoiceVariant(variant); |
| 66 |
} |
| 67 |
|
| 68 |
public int getRate() { |
| 69 |
int min = mEngine.Rate.getMinValue(); |
| 70 |
int max = mEngine.Rate.getMaxValue(); |
| 71 |
|
| 72 |
int rate = getPreferenceValue(PREF_RATE, Integer.MIN_VALUE); |
| 73 |
if (rate == Integer.MIN_VALUE) { |
| 74 |
rate = (int)((float)getPreferenceValue(PREF_DEFAULT_RATE, 100) / 100 * (float)mEngine.Rate.getDefaultValue()); |
| 75 |
} |
| 76 |
|
| 77 |
if (rate > max) rate = max; |
| 78 |
if (rate < min) rate = min; |
| 79 |
return rate; |
| 80 |
} |
| 81 |
|
| 82 |
public int getPitch() { |
| 83 |
int min = mEngine.Pitch.getMinValue(); |
| 84 |
int max = mEngine.Pitch.getMaxValue(); |
| 85 |
|
| 86 |
int pitch = getPreferenceValue(PREF_PITCH, Integer.MIN_VALUE); |
| 87 |
if (pitch == Integer.MIN_VALUE) { |
| 88 |
pitch = getPreferenceValue(PREF_DEFAULT_PITCH, 100) / 2; |
| 89 |
} |
| 90 |
|
| 91 |
if (pitch > max) pitch = max; |
| 92 |
if (pitch < min) pitch = min; |
| 93 |
return pitch; |
| 94 |
} |
| 95 |
|
| 96 |
public int getPitchRange() { |
| 97 |
int min = mEngine.PitchRange.getMinValue(); |
| 98 |
int max = mEngine.PitchRange.getMaxValue(); |
| 99 |
|
| 100 |
int range = getPreferenceValue(PREF_PITCH_RANGE, mEngine.PitchRange.getDefaultValue()); |
| 101 |
if (range > max) range = max; |
| 102 |
if (range < min) range = min; |
| 103 |
return range; |
| 104 |
} |
| 105 |
|
| 106 |
public int getVolume() { |
| 107 |
int min = mEngine.Volume.getMinValue(); |
| 108 |
int max = mEngine.Volume.getMaxValue(); |
| 109 |
|
| 110 |
int range = getPreferenceValue(PREF_VOLUME, mEngine.Volume.getDefaultValue()); |
| 111 |
if (range > max) range = max; |
| 112 |
if (range < min) range = min; |
| 113 |
return range; |
| 114 |
} |
| 115 |
|
| 116 |
public int getPunctuationLevel() { |
| 117 |
int min = mEngine.Punctuation.getMinValue(); |
| 118 |
int max = mEngine.Punctuation.getMaxValue(); |
| 119 |
|
| 120 |
int level = getPreferenceValue(PREF_PUNCTUATION_LEVEL, mEngine.Punctuation.getDefaultValue()); |
| 121 |
if (level > max) level = max; |
| 122 |
if (level < min) level = min; |
| 123 |
return level; |
| 124 |
} |
| 125 |
|
| 126 |
public String getPunctuationCharacters() { |
| 127 |
return mPreferences.getString(PREF_PUNCTUATION_CHARACTERS, null); |
| 128 |
} |
| 129 |
|
| 130 |
private int getPreferenceValue(String preference, int defaultValue) { |
| 131 |
String prefString = mPreferences.getString(preference, null); |
| 132 |
if (prefString == null) { |
| 133 |
return defaultValue; |
| 134 |
} |
| 135 |
return Integer.parseInt(prefString); |
| 136 |
} |
| 137 |
|
| 138 |
public JSONObject toJSON() throws JSONException { |
| 139 |
JSONObject settings = new JSONObject(); |
| 140 |
settings.put(PRESET_VARIANT, getVoiceVariant().toString()); |
| 141 |
settings.put(PRESET_RATE, getRate()); |
| 142 |
settings.put(PRESET_PITCH, getPitch()); |
| 143 |
settings.put(PRESET_PITCH_RANGE, getPitchRange()); |
| 144 |
settings.put(PRESET_VOLUME, getVolume()); |
| 145 |
settings.put(PRESET_PUNCTUATION_CHARACTERS, getPunctuationCharacters()); |
| 146 |
switch (getPunctuationLevel()) { |
| 147 |
case SpeechSynthesis.PUNCT_NONE: |
| 148 |
settings.put(PRESET_PUNCTUATION_LEVEL, PUNCTUATION_NONE); |
| 149 |
break; |
| 150 |
case SpeechSynthesis.PUNCT_SOME: |
| 151 |
settings.put(PRESET_PUNCTUATION_LEVEL, PUNCTUATION_SOME); |
| 152 |
break; |
| 153 |
case SpeechSynthesis.PUNCT_ALL: |
| 154 |
settings.put(PRESET_PUNCTUATION_LEVEL, PUNCTUATION_ALL); |
| 155 |
break; |
| 156 |
} |
| 157 |
return settings; |
| 158 |
} |
| 159 |
} |