eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TtsSettingsActivity.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C) 2013 Reece H. Dunn
  3. * Copyright (C) 2011 The Android Open Source Project
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.reecedunn.espeak;
  18. import android.content.Context;
  19. import android.content.SharedPreferences;
  20. import android.os.Build;
  21. import android.os.Bundle;
  22. import android.preference.ListPreference;
  23. import android.preference.Preference;
  24. import android.preference.Preference.OnPreferenceChangeListener;
  25. import android.preference.PreferenceActivity;
  26. import android.preference.PreferenceFragment;
  27. import android.preference.PreferenceGroup;
  28. import android.preference.PreferenceManager;
  29. import com.reecedunn.espeak.preference.ImportVoicePreference;
  30. import com.reecedunn.espeak.preference.SeekBarPreference;
  31. import com.reecedunn.espeak.preference.SpeakPunctuationPreference;
  32. import com.reecedunn.espeak.preference.VoiceVariantPreference;
  33. public class TtsSettingsActivity extends PreferenceActivity {
  34. @Override
  35. @SuppressWarnings("deprecation")
  36. protected void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. // Migrate old eyes-free settings to the new settings:
  39. final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  40. final SharedPreferences.Editor editor = prefs.edit();
  41. String pitch = prefs.getString(VoiceSettings.PREF_PITCH, null);
  42. if (pitch == null) {
  43. // Try the old eyes-free setting:
  44. pitch = prefs.getString(VoiceSettings.PREF_DEFAULT_PITCH, "100");
  45. int pitchValue = Integer.parseInt(pitch) / 2;
  46. editor.putString(VoiceSettings.PREF_PITCH, Integer.toString(pitchValue));
  47. }
  48. String rate = prefs.getString(VoiceSettings.PREF_RATE, null);
  49. if (rate == null) {
  50. // Try the old eyes-free setting:
  51. SpeechSynthesis engine = new SpeechSynthesis(this, null);
  52. int defaultValue = engine.Rate.getDefaultValue();
  53. int maxValue = engine.Rate.getMaxValue();
  54. rate = prefs.getString(VoiceSettings.PREF_DEFAULT_RATE, "100");
  55. int rateValue = (Integer.parseInt(rate) / 100) * defaultValue;
  56. if (rateValue < defaultValue) rateValue = defaultValue;
  57. if (rateValue > maxValue) rateValue = maxValue;
  58. editor.putString(VoiceSettings.PREF_RATE, Integer.toString(rateValue));
  59. }
  60. String variant = prefs.getString(VoiceSettings.PREF_VARIANT, null);
  61. if (variant == null) {
  62. String gender = prefs.getString(VoiceSettings.PREF_DEFAULT_GENDER, "0");
  63. if (gender.equals("2")) {
  64. editor.putString(VoiceSettings.PREF_VARIANT, VoiceVariant.FEMALE);
  65. } else {
  66. editor.putString(VoiceSettings.PREF_VARIANT, VoiceVariant.MALE);
  67. }
  68. }
  69. editor.commit();
  70. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
  71. {
  72. getFragmentManager().beginTransaction().replace(
  73. android.R.id.content,
  74. new PrefsEspeakFragment()).commit();
  75. }
  76. else
  77. {
  78. addPreferencesFromResource(R.xml.preferences);
  79. createPreferences(TtsSettingsActivity.this, getPreferenceScreen());
  80. }
  81. }
  82. public static class PrefsEspeakFragment extends PreferenceFragment {
  83. @Override
  84. public void onCreate(Bundle savedInstanceState) {
  85. super.onCreate(savedInstanceState);
  86. addPreferencesFromResource(R.xml.preferences);
  87. createPreferences(getActivity(), getPreferenceScreen());
  88. }
  89. }
  90. private static Preference createImportVoicePreference(Context context) {
  91. final String title = context.getString(R.string.import_voice_title);
  92. final ImportVoicePreference pref = new ImportVoicePreference(context);
  93. pref.setTitle(title);
  94. pref.setDialogTitle(title);
  95. pref.setOnPreferenceChangeListener(mOnPreferenceChanged);
  96. pref.setDescription(R.string.import_voice_description);
  97. return pref;
  98. }
  99. private static Preference createVoiceVariantPreference(Context context, VoiceSettings settings, int titleRes) {
  100. final String title = context.getString(titleRes);
  101. final VoiceVariantPreference pref = new VoiceVariantPreference(context);
  102. pref.setTitle(title);
  103. pref.setDialogTitle(title);
  104. pref.setOnPreferenceChangeListener(mOnPreferenceChanged);
  105. pref.setPersistent(true);
  106. pref.setVoiceVariant(settings.getVoiceVariant());
  107. return pref;
  108. }
  109. private static Preference createSpeakPunctuationPreference(Context context, VoiceSettings settings, int titleRes) {
  110. final String title = context.getString(titleRes);
  111. final SpeakPunctuationPreference pref = new SpeakPunctuationPreference(context);
  112. pref.setTitle(title);
  113. pref.setDialogTitle(title);
  114. pref.setOnPreferenceChangeListener(mOnPreferenceChanged);
  115. pref.setPersistent(true);
  116. pref.setVoiceSettings(settings);
  117. return pref;
  118. }
  119. private static Preference createSeekBarPreference(Context context, SpeechSynthesis.Parameter parameter, String key, int titleRes) {
  120. final String title = context.getString(titleRes);
  121. final int defaultValue = parameter.getDefaultValue();
  122. final SeekBarPreference pref = new SeekBarPreference(context);
  123. pref.setTitle(title);
  124. pref.setDialogTitle(title);
  125. pref.setKey(key);
  126. pref.setOnPreferenceChangeListener(mOnPreferenceChanged);
  127. pref.setPersistent(true);
  128. switch (parameter.getUnitType())
  129. {
  130. case Percentage:
  131. pref.setFormatter(context.getString(R.string.formatter_percentage));
  132. break;
  133. case WordsPerMinute:
  134. pref.setFormatter(context.getString(R.string.formatter_wpm));
  135. break;
  136. default:
  137. throw new IllegalStateException("Unsupported unit type for the parameter.");
  138. }
  139. pref.setMin(parameter.getMinValue());
  140. pref.setMax(parameter.getMaxValue());
  141. pref.setDefaultValue(defaultValue);
  142. final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
  143. final String prefString = prefs.getString(key, null);
  144. if (prefString == null) {
  145. pref.setProgress(defaultValue);
  146. } else {
  147. pref.setProgress(Integer.parseInt(prefString));
  148. }
  149. return pref;
  150. }
  151. /**
  152. * Since the "%s" summary is currently broken, this sets the preference
  153. * change listener for all {@link ListPreference} views to fill in the
  154. * summary with the current entry value.
  155. */
  156. private static void createPreferences(Context context, PreferenceGroup group) {
  157. SpeechSynthesis engine = new SpeechSynthesis(context, null);
  158. VoiceSettings settings = new VoiceSettings(PreferenceManager.getDefaultSharedPreferences(context), engine);
  159. group.addPreference(createImportVoicePreference(context));
  160. group.addPreference(createVoiceVariantPreference(context, settings, R.string.espeak_variant));
  161. group.addPreference(createSpeakPunctuationPreference(context, settings, R.string.espeak_speak_punctuation));
  162. group.addPreference(createSeekBarPreference(context, engine.Rate, VoiceSettings.PREF_RATE, R.string.setting_default_rate));
  163. group.addPreference(createSeekBarPreference(context, engine.Pitch, VoiceSettings.PREF_PITCH, R.string.setting_default_pitch));
  164. group.addPreference(createSeekBarPreference(context, engine.PitchRange, VoiceSettings.PREF_PITCH_RANGE, R.string.espeak_pitch_range));
  165. group.addPreference(createSeekBarPreference(context, engine.Volume, VoiceSettings.PREF_VOLUME, R.string.espeak_volume));
  166. }
  167. private static final OnPreferenceChangeListener mOnPreferenceChanged =
  168. new OnPreferenceChangeListener() {
  169. @Override
  170. public boolean onPreferenceChange(Preference preference, Object newValue) {
  171. if (newValue instanceof String) {
  172. String summary = "";
  173. if (preference instanceof ListPreference) {
  174. final ListPreference listPreference = (ListPreference) preference;
  175. final int index = listPreference.findIndexOfValue((String) newValue);
  176. final CharSequence[] entries = listPreference.getEntries();
  177. if (index >= 0 && index < entries.length) {
  178. summary = entries[index].toString();
  179. }
  180. } else if (preference instanceof SeekBarPreference) {
  181. final SeekBarPreference seekBarPreference = (SeekBarPreference) preference;
  182. String formatter = seekBarPreference.getFormatter();
  183. summary = String.format(formatter, (String)newValue);
  184. } else {
  185. summary = (String)newValue;
  186. }
  187. preference.setSummary(summary);
  188. }
  189. return true;
  190. }
  191. };
  192. }