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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.SeekBarPreference;
  30. import com.reecedunn.espeak.preference.SpeakPunctuationPreference;
  31. import com.reecedunn.espeak.preference.VoiceVariantPreference;
  32. public class TtsSettingsActivity extends PreferenceActivity {
  33. @Override
  34. @SuppressWarnings("deprecation")
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. // Migrate old eyes-free settings to the new settings:
  38. final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  39. final SharedPreferences.Editor editor = prefs.edit();
  40. String pitch = prefs.getString(VoiceSettings.PREF_PITCH, null);
  41. if (pitch == null) {
  42. // Try the old eyes-free setting:
  43. pitch = prefs.getString(VoiceSettings.PREF_DEFAULT_PITCH, "100");
  44. int pitchValue = Integer.parseInt(pitch) / 2;
  45. editor.putString(VoiceSettings.PREF_PITCH, Integer.toString(pitchValue));
  46. }
  47. String rate = prefs.getString(VoiceSettings.PREF_RATE, null);
  48. if (rate == null) {
  49. // Try the old eyes-free setting:
  50. SpeechSynthesis engine = new SpeechSynthesis(this, null);
  51. int defaultValue = engine.Rate.getDefaultValue();
  52. int maxValue = engine.Rate.getMaxValue();
  53. rate = prefs.getString(VoiceSettings.PREF_DEFAULT_RATE, "100");
  54. int rateValue = (Integer.parseInt(rate) / 100) * defaultValue;
  55. if (rateValue < defaultValue) rateValue = defaultValue;
  56. if (rateValue > maxValue) rateValue = maxValue;
  57. editor.putString(VoiceSettings.PREF_RATE, Integer.toString(rateValue));
  58. }
  59. String variant = prefs.getString(VoiceSettings.PREF_VARIANT, null);
  60. if (variant == null) {
  61. String gender = prefs.getString(VoiceSettings.PREF_DEFAULT_GENDER, "0");
  62. if (gender.equals("2")) {
  63. editor.putString(VoiceSettings.PREF_VARIANT, VoiceVariant.FEMALE);
  64. } else {
  65. editor.putString(VoiceSettings.PREF_VARIANT, VoiceVariant.MALE);
  66. }
  67. }
  68. editor.commit();
  69. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
  70. {
  71. getFragmentManager().beginTransaction().replace(
  72. android.R.id.content,
  73. new PrefsEspeakFragment()).commit();
  74. }
  75. else
  76. {
  77. addPreferencesFromResource(R.xml.preferences);
  78. createPreferences(TtsSettingsActivity.this, getPreferenceScreen());
  79. }
  80. }
  81. public static class PrefsEspeakFragment extends PreferenceFragment {
  82. @Override
  83. public void onCreate(Bundle savedInstanceState) {
  84. super.onCreate(savedInstanceState);
  85. addPreferencesFromResource(R.xml.preferences);
  86. createPreferences(getActivity(), getPreferenceScreen());
  87. }
  88. }
  89. private static Preference createVoiceVariantPreference(Context context, VoiceSettings settings, int titleRes) {
  90. final String title = context.getString(titleRes);
  91. final VoiceVariantPreference pref = new VoiceVariantPreference(context);
  92. pref.setTitle(title);
  93. pref.setDialogTitle(title);
  94. pref.setOnPreferenceChangeListener(mOnPreferenceChanged);
  95. pref.setPersistent(true);
  96. pref.setVoiceVariant(settings.getVoiceVariant());
  97. return pref;
  98. }
  99. private static Preference createSpeakPunctuationPreference(Context context, VoiceSettings settings, int titleRes) {
  100. final String title = context.getString(titleRes);
  101. final SpeakPunctuationPreference pref = new SpeakPunctuationPreference(context);
  102. pref.setTitle(title);
  103. pref.setDialogTitle(title);
  104. pref.setOnPreferenceChangeListener(mOnPreferenceChanged);
  105. pref.setPersistent(true);
  106. pref.setVoiceSettings(settings);
  107. return pref;
  108. }
  109. private static Preference createSeekBarPreference(Context context, SpeechSynthesis.Parameter parameter, String key, int titleRes) {
  110. final String title = context.getString(titleRes);
  111. final int defaultValue = parameter.getDefaultValue();
  112. final SeekBarPreference pref = new SeekBarPreference(context);
  113. pref.setTitle(title);
  114. pref.setDialogTitle(title);
  115. pref.setKey(key);
  116. pref.setOnPreferenceChangeListener(mOnPreferenceChanged);
  117. pref.setPersistent(true);
  118. switch (parameter.getUnitType())
  119. {
  120. case Percentage:
  121. pref.setFormatter(context.getString(R.string.formatter_percentage));
  122. break;
  123. case WordsPerMinute:
  124. pref.setFormatter(context.getString(R.string.formatter_wpm));
  125. break;
  126. default:
  127. throw new IllegalStateException("Unsupported unit type for the parameter.");
  128. }
  129. pref.setMin(parameter.getMinValue());
  130. pref.setMax(parameter.getMaxValue());
  131. pref.setDefaultValue(defaultValue);
  132. final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
  133. final String prefString = prefs.getString(key, null);
  134. if (prefString == null) {
  135. pref.setProgress(defaultValue);
  136. } else {
  137. pref.setProgress(Integer.parseInt(prefString));
  138. }
  139. return pref;
  140. }
  141. /**
  142. * Since the "%s" summary is currently broken, this sets the preference
  143. * change listener for all {@link ListPreference} views to fill in the
  144. * summary with the current entry value.
  145. */
  146. private static void createPreferences(Context context, PreferenceGroup group) {
  147. SpeechSynthesis engine = new SpeechSynthesis(context, null);
  148. VoiceSettings settings = new VoiceSettings(PreferenceManager.getDefaultSharedPreferences(context), engine);
  149. group.addPreference(createVoiceVariantPreference(context, settings, R.string.espeak_variant));
  150. group.addPreference(createSpeakPunctuationPreference(context, settings, R.string.espeak_speak_punctuation));
  151. group.addPreference(createSeekBarPreference(context, engine.Rate, VoiceSettings.PREF_RATE, R.string.setting_default_rate));
  152. group.addPreference(createSeekBarPreference(context, engine.Pitch, VoiceSettings.PREF_PITCH, R.string.setting_default_pitch));
  153. group.addPreference(createSeekBarPreference(context, engine.PitchRange, VoiceSettings.PREF_PITCH_RANGE, R.string.espeak_pitch_range));
  154. group.addPreference(createSeekBarPreference(context, engine.Volume, VoiceSettings.PREF_VOLUME, R.string.espeak_volume));
  155. }
  156. private static final OnPreferenceChangeListener mOnPreferenceChanged =
  157. new OnPreferenceChangeListener() {
  158. @Override
  159. public boolean onPreferenceChange(Preference preference, Object newValue) {
  160. if (newValue instanceof String) {
  161. String summary = "";
  162. if (preference instanceof ListPreference) {
  163. final ListPreference listPreference = (ListPreference) preference;
  164. final int index = listPreference.findIndexOfValue((String) newValue);
  165. final CharSequence[] entries = listPreference.getEntries();
  166. if (index >= 0 && index < entries.length) {
  167. summary = entries[index].toString();
  168. }
  169. } else if (preference instanceof SeekBarPreference) {
  170. final SeekBarPreference seekBarPreference = (SeekBarPreference) preference;
  171. String formatter = seekBarPreference.getFormatter();
  172. summary = String.format(formatter, (String)newValue);
  173. } else {
  174. summary = (String)newValue;
  175. }
  176. preference.setSummary(summary);
  177. }
  178. return true;
  179. }
  180. };
  181. }