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 7.9KB

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