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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 VoiceSettings(SharedPreferences preferences, SpeechSynthesis engine) {
  28. mPreferences = preferences;
  29. mEngine = engine;
  30. }
  31. public VoiceVariant getVoiceVariant() {
  32. String variant = mPreferences.getString(PREF_VARIANT, null);
  33. if (variant == null) {
  34. int gender = getPreferenceValue(PREF_DEFAULT_GENDER, SpeechSynthesis.GENDER_MALE);
  35. if (gender == SpeechSynthesis.GENDER_FEMALE) {
  36. return VoiceVariant.parseVoiceVariant(VoiceVariant.FEMALE);
  37. }
  38. return VoiceVariant.parseVoiceVariant(VoiceVariant.MALE);
  39. }
  40. return VoiceVariant.parseVoiceVariant(variant);
  41. }
  42. public int getRate() {
  43. int min = mEngine.Rate.getMinValue();
  44. int max = mEngine.Rate.getMaxValue();
  45. int rate = getPreferenceValue(PREF_RATE, Integer.MIN_VALUE);
  46. if (rate == Integer.MIN_VALUE) {
  47. rate = (int)((float)getPreferenceValue(PREF_DEFAULT_RATE, 100) / 100 * (float)mEngine.Rate.getDefaultValue());
  48. }
  49. if (rate > max) rate = max;
  50. if (rate < min) rate = min;
  51. return rate;
  52. }
  53. public int getPitch() {
  54. int min = mEngine.Pitch.getMinValue();
  55. int max = mEngine.Pitch.getMaxValue();
  56. int pitch = getPreferenceValue(PREF_PITCH, Integer.MIN_VALUE);
  57. if (pitch == Integer.MIN_VALUE) {
  58. pitch = getPreferenceValue(PREF_DEFAULT_PITCH, 100) / 2;
  59. }
  60. if (pitch > max) pitch = max;
  61. if (pitch < min) pitch = min;
  62. return pitch;
  63. }
  64. private int getPreferenceValue(String preference, int defaultValue) {
  65. String prefString = mPreferences.getString(preference, null);
  66. if (prefString == null) {
  67. return defaultValue;
  68. }
  69. return Integer.parseInt(prefString);
  70. }
  71. }