| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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: |
| 79 |
break; |
| 80 |
case 2: |
| 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 |
} |