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.

VoiceSettings.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (C) 2013 Reece H. Dunn
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.reecedunn.espeak;
  17. import android.content.SharedPreferences;
  18. import org.json.JSONException;
  19. import org.json.JSONObject;
  20. public class VoiceSettings {
  21. private final SharedPreferences mPreferences;
  22. private final SpeechSynthesis mEngine;
  23. public static final String PREF_DEFAULT_GENDER = "default_gender";
  24. public static final String PREF_VARIANT = "espeak_variant";
  25. public static final String PREF_DEFAULT_RATE = "default_rate";
  26. public static final String PREF_RATE = "espeak_rate";
  27. public static final String PREF_DEFAULT_PITCH = "default_pitch";
  28. public static final String PREF_PITCH = "espeak_pitch";
  29. public static final String PREF_PITCH_RANGE = "espeak_pitch_range";
  30. public static final String PREF_VOLUME = "espeak_volume";
  31. public static final String PREF_PUNCTUATION_LEVEL = "espeak_punctuation_level";
  32. public static final String PREF_PUNCTUATION_CHARACTERS = "espeak_punctuation_characters";
  33. public static final String PRESET_VARIANT = "variant";
  34. public static final String PRESET_RATE = "rate";
  35. public static final String PRESET_PITCH = "pitch";
  36. public static final String PRESET_PITCH_RANGE = "pitch-range";
  37. public static final String PRESET_VOLUME = "volume";
  38. public static final String PRESET_PUNCTUATION_LEVEL = "punctuation-level";
  39. public static final String PRESET_PUNCTUATION_CHARACTERS = "punctuation-characters";
  40. public static final String PUNCTUATION_NONE = "none";
  41. public static final String PUNCTUATION_SOME = "some";
  42. public static final String PUNCTUATION_ALL = "all";
  43. public VoiceSettings(SharedPreferences preferences, SpeechSynthesis engine) {
  44. mPreferences = preferences;
  45. mEngine = engine;
  46. }
  47. public VoiceVariant getVoiceVariant() {
  48. String variant = mPreferences.getString(PREF_VARIANT, null);
  49. if (variant == null) {
  50. int gender = getPreferenceValue(PREF_DEFAULT_GENDER, SpeechSynthesis.GENDER_MALE);
  51. if (gender == SpeechSynthesis.GENDER_FEMALE) {
  52. return VoiceVariant.parseVoiceVariant(VoiceVariant.FEMALE);
  53. }
  54. return VoiceVariant.parseVoiceVariant(VoiceVariant.MALE);
  55. }
  56. return VoiceVariant.parseVoiceVariant(variant);
  57. }
  58. public int getRate() {
  59. int min = mEngine.Rate.getMinValue();
  60. int max = mEngine.Rate.getMaxValue();
  61. int rate = getPreferenceValue(PREF_RATE, Integer.MIN_VALUE);
  62. if (rate == Integer.MIN_VALUE) {
  63. rate = (int)((float)getPreferenceValue(PREF_DEFAULT_RATE, 100) / 100 * (float)mEngine.Rate.getDefaultValue());
  64. }
  65. if (rate > max) rate = max;
  66. if (rate < min) rate = min;
  67. return rate;
  68. }
  69. public int getPitch() {
  70. int min = mEngine.Pitch.getMinValue();
  71. int max = mEngine.Pitch.getMaxValue();
  72. int pitch = getPreferenceValue(PREF_PITCH, Integer.MIN_VALUE);
  73. if (pitch == Integer.MIN_VALUE) {
  74. pitch = getPreferenceValue(PREF_DEFAULT_PITCH, 100) / 2;
  75. }
  76. if (pitch > max) pitch = max;
  77. if (pitch < min) pitch = min;
  78. return pitch;
  79. }
  80. public int getPitchRange() {
  81. int min = mEngine.PitchRange.getMinValue();
  82. int max = mEngine.PitchRange.getMaxValue();
  83. int range = getPreferenceValue(PREF_PITCH_RANGE, mEngine.PitchRange.getDefaultValue());
  84. if (range > max) range = max;
  85. if (range < min) range = min;
  86. return range;
  87. }
  88. public int getVolume() {
  89. int min = mEngine.Volume.getMinValue();
  90. int max = mEngine.Volume.getMaxValue();
  91. int range = getPreferenceValue(PREF_VOLUME, mEngine.Volume.getDefaultValue());
  92. if (range > max) range = max;
  93. if (range < min) range = min;
  94. return range;
  95. }
  96. public int getPunctuationLevel() {
  97. int min = mEngine.Punctuation.getMinValue();
  98. int max = mEngine.Punctuation.getMaxValue();
  99. int level = getPreferenceValue(PREF_PUNCTUATION_LEVEL, mEngine.Punctuation.getDefaultValue());
  100. if (level > max) level = max;
  101. if (level < min) level = min;
  102. return level;
  103. }
  104. public String getPunctuationCharacters() {
  105. return mPreferences.getString(PREF_PUNCTUATION_CHARACTERS, null);
  106. }
  107. private int getPreferenceValue(String preference, int defaultValue) {
  108. String prefString = mPreferences.getString(preference, null);
  109. if (prefString == null) {
  110. return defaultValue;
  111. }
  112. return Integer.parseInt(prefString);
  113. }
  114. public JSONObject toJSON() throws JSONException {
  115. JSONObject settings = new JSONObject();
  116. settings.put(PRESET_VARIANT, getVoiceVariant().toString());
  117. settings.put(PRESET_RATE, getRate());
  118. settings.put(PRESET_PITCH, getPitch());
  119. settings.put(PRESET_PITCH_RANGE, getPitchRange());
  120. settings.put(PRESET_VOLUME, getVolume());
  121. settings.put(PRESET_PUNCTUATION_CHARACTERS, getPunctuationCharacters());
  122. switch (getPunctuationLevel()) {
  123. case SpeechSynthesis.PUNCT_NONE:
  124. settings.put(PRESET_PUNCTUATION_LEVEL, PUNCTUATION_NONE);
  125. break;
  126. case SpeechSynthesis.PUNCT_SOME:
  127. settings.put(PRESET_PUNCTUATION_LEVEL, PUNCTUATION_SOME);
  128. break;
  129. case SpeechSynthesis.PUNCT_ALL:
  130. settings.put(PRESET_PUNCTUATION_LEVEL, PUNCTUATION_ALL);
  131. break;
  132. }
  133. return settings;
  134. }
  135. }