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.

VoiceVariant.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 java.util.regex.Pattern;
  18. public class VoiceVariant {
  19. private static final Pattern mVariantPattern = Pattern.compile("-");
  20. public static final String MALE = "male";
  21. public static final String FEMALE = "female";
  22. public final String variant;
  23. public final int gender;
  24. public final int age;
  25. protected VoiceVariant(String variant, int age) {
  26. if (variant.equals(MALE)) {
  27. this.variant = null;
  28. this.gender = SpeechSynthesis.GENDER_MALE;
  29. } else if (variant.equals(FEMALE)) {
  30. this.variant = null;
  31. this.gender = SpeechSynthesis.GENDER_FEMALE;
  32. } else {
  33. this.variant = variant;
  34. this.gender = SpeechSynthesis.GENDER_UNSPECIFIED;
  35. }
  36. this.age = age;
  37. }
  38. @Override
  39. public String toString() {
  40. final String ret;
  41. if (gender == SpeechSynthesis.GENDER_MALE) {
  42. ret = MALE;
  43. } else if (gender == SpeechSynthesis.GENDER_FEMALE) {
  44. ret = FEMALE;
  45. } else {
  46. ret = variant;
  47. }
  48. if (age == SpeechSynthesis.AGE_YOUNG) {
  49. return ret + "-young";
  50. } else if (age == SpeechSynthesis.AGE_OLD) {
  51. return ret + "-old";
  52. }
  53. return ret;
  54. }
  55. public boolean equals(Object o) {
  56. if (o instanceof VoiceVariant) {
  57. VoiceVariant other = (VoiceVariant)o;
  58. if (variant == null || other.variant == null) {
  59. return other.variant == null && variant == null && other.gender == gender && other.age == age;
  60. }
  61. return other.variant.equals(variant) && other.gender == gender && other.age == age;
  62. }
  63. return false;
  64. }
  65. public static VoiceVariant parseVoiceVariant(String value) {
  66. String[] parts = mVariantPattern.split(value);
  67. int age = SpeechSynthesis.AGE_ANY;
  68. switch (parts.length) {
  69. case 1: // variant
  70. break;
  71. case 2: // variant-age
  72. age = parts[1].equals("young") ? SpeechSynthesis.AGE_YOUNG : SpeechSynthesis.AGE_OLD;
  73. break;
  74. default:
  75. return null;
  76. }
  77. return new VoiceVariant(parts[0], age);
  78. }
  79. }