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

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