Debian Codegraph @

android/src/com/reecedunn/espeak/VoiceVariant.java

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