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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. public class VoiceSettings {
  19. private final SharedPreferences mPreferences;
  20. private final SpeechSynthesis mEngine;
  21. public static final String PREF_DEFAULT_GENDER = "default_gender";
  22. public static final String PREF_VARIANT = "espeak_variant";
  23. public static final String PREF_DEFAULT_RATE = "default_rate";
  24. public static final String PREF_RATE = "espeak_rate";
  25. public static final String PREF_DEFAULT_PITCH = "default_pitch";
  26. public static final String PREF_PITCH = "espeak_pitch";
  27. public static final String PREF_PITCH_RANGE = "espeak_pitch_range";
  28. public static final String PREF_VOLUME = "espeak_volume";
  29. public static final String PREF_PUNCTUATION_LEVEL = "espeak_punctuation_level";
  30. public VoiceSettings(SharedPreferences preferences, SpeechSynthesis engine) {
  31. mPreferences = preferences;
  32. mEngine = engine;
  33. }
  34. public VoiceVariant getVoiceVariant() {
  35. String variant = mPreferences.getString(PREF_VARIANT, null);
  36. if (variant == null) {
  37. int gender = getPreferenceValue(PREF_DEFAULT_GENDER, SpeechSynthesis.GENDER_MALE);
  38. if (gender == SpeechSynthesis.GENDER_FEMALE) {
  39. return VoiceVariant.parseVoiceVariant(VoiceVariant.FEMALE);
  40. }
  41. return VoiceVariant.parseVoiceVariant(VoiceVariant.MALE);
  42. }
  43. return VoiceVariant.parseVoiceVariant(variant);
  44. }
  45. public int getRate() {
  46. int min = mEngine.Rate.getMinValue();
  47. int max = mEngine.Rate.getMaxValue();
  48. int rate = getPreferenceValue(PREF_RATE, Integer.MIN_VALUE);
  49. if (rate == Integer.MIN_VALUE) {
  50. rate = (int)((float)getPreferenceValue(PREF_DEFAULT_RATE, 100) / 100 * (float)mEngine.Rate.getDefaultValue());
  51. }
  52. if (rate > max) rate = max;
  53. if (rate < min) rate = min;
  54. return rate;
  55. }
  56. public int getPitch() {
  57. int min = mEngine.Pitch.getMinValue();
  58. int max = mEngine.Pitch.getMaxValue();
  59. int pitch = getPreferenceValue(PREF_PITCH, Integer.MIN_VALUE);
  60. if (pitch == Integer.MIN_VALUE) {
  61. pitch = getPreferenceValue(PREF_DEFAULT_PITCH, 100) / 2;
  62. }
  63. if (pitch > max) pitch = max;
  64. if (pitch < min) pitch = min;
  65. return pitch;
  66. }
  67. public int getPitchRange() {
  68. int min = mEngine.PitchRange.getMinValue();
  69. int max = mEngine.PitchRange.getMaxValue();
  70. int range = getPreferenceValue(PREF_PITCH_RANGE, mEngine.PitchRange.getDefaultValue());
  71. if (range > max) range = max;
  72. if (range < min) range = min;
  73. return range;
  74. }
  75. public int getVolume() {
  76. int min = mEngine.Volume.getMinValue();
  77. int max = mEngine.Volume.getMaxValue();
  78. int range = getPreferenceValue(PREF_VOLUME, mEngine.Volume.getDefaultValue());
  79. if (range > max) range = max;
  80. if (range < min) range = min;
  81. return range;
  82. }
  83. public int getPunctuationLevel() {
  84. int min = mEngine.Punctuation.getMinValue();
  85. int max = mEngine.Punctuation.getMaxValue();
  86. int level = getPreferenceValue(PREF_PUNCTUATION_LEVEL, mEngine.Punctuation.getDefaultValue());
  87. if (level > max) level = max;
  88. if (level < min) level = min;
  89. return level;
  90. }
  91. private int getPreferenceValue(String preference, int defaultValue) {
  92. String prefString = mPreferences.getString(preference, null);
  93. if (prefString == null) {
  94. return defaultValue;
  95. }
  96. return Integer.parseInt(prefString);
  97. }
  98. }