upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / jsapi / FreeTTSVoice.java
1 /**
2  * Copyright 2003 Sun Microsystems, Inc.
3  * 
4  * See the file "license.terms" for information on usage and
5  * redistribution of this file, and for a DISCLAIMER OF ALL 
6  * WARRANTIES.
7  */
8 package com.sun.speech.freetts.jsapi;
9
10 import com.sun.speech.engine.synthesis.BaseVoice;
11 import com.sun.speech.freetts.Validator;
12 import com.sun.speech.freetts.ValidationException;
13
14 /**
15  * Extends the BaseVoice class to encapsulate FreeTTSSynthesizer specific data.
16  */
17 public class FreeTTSVoice extends BaseVoice {
18
19     private com.sun.speech.freetts.Voice freettsVoice;
20     private Validator validator;
21
22     /**
23      * Constructs a FreeTTSVoice
24      *
25      * @param freettsVoice the freetts voice
26      * @param validatorName the classname of the validator to use
27      */
28     public FreeTTSVoice(com.sun.speech.freetts.Voice freettsVoice,
29                         String validatorName) {
30         super(freettsVoice.getName()+Math.random(), freettsVoice.getName(),
31                 genderToInt(freettsVoice.getGender()),
32                 ageToInt(freettsVoice.getAge()), freettsVoice.getStyle(),
33                 freettsVoice.getPitch(), freettsVoice.getPitchRange(),
34                 freettsVoice.getRate(), freettsVoice.getVolume());
35         this.freettsVoice = freettsVoice;
36         
37         if (validatorName != null) {
38             try {
39                 Class clazz = Class.forName(validatorName);
40                 validator = (Validator) clazz.newInstance();
41             } catch (ClassNotFoundException cnfe) {
42                 cnfe.printStackTrace();
43             } catch (IllegalAccessException iae) {
44                 iae.printStackTrace();
45             } catch (InstantiationException ie) {
46                 ie.printStackTrace();
47             }
48         } else {
49             validator = null;
50         }
51     }
52
53     /**
54      * Convert a freetts gender to jsapi gender
55      *
56      * @param gender the freetts gender
57      *
58      * @return the jsapi gender
59      */
60     private static int genderToInt(com.sun.speech.freetts.Gender gender) {
61         if (gender == com.sun.speech.freetts.Gender.MALE) {
62             return javax.speech.synthesis.Voice.GENDER_MALE;
63         } else if (gender == com.sun.speech.freetts.Gender.FEMALE) {
64             return javax.speech.synthesis.Voice.GENDER_FEMALE;
65         } else if (gender == com.sun.speech.freetts.Gender.NEUTRAL) {
66             return javax.speech.synthesis.Voice.GENDER_NEUTRAL;
67         } else if (gender == com.sun.speech.freetts.Gender.DONT_CARE) {
68             return javax.speech.synthesis.Voice.GENDER_DONT_CARE;
69         } else {
70             throw new Error("jaspi does not have an equivalent to gender "
71                     + gender.toString());
72         }
73     }
74
75     /**
76      * Convert a freetts age to jsapi age
77      *
78      * @param age the freetts age
79      *
80      * @return the jsapi age
81      */
82     private static int ageToInt(com.sun.speech.freetts.Age age) {
83         if (age == com.sun.speech.freetts.Age.CHILD) {
84             return javax.speech.synthesis.Voice.AGE_CHILD;
85         } else if (age == com.sun.speech.freetts.Age.TEENAGER) {
86             return javax.speech.synthesis.Voice.AGE_TEENAGER;
87         } else if (age == com.sun.speech.freetts.Age.YOUNGER_ADULT) {
88             return javax.speech.synthesis.Voice.AGE_YOUNGER_ADULT;
89         } else if (age == com.sun.speech.freetts.Age.MIDDLE_ADULT) {
90             return javax.speech.synthesis.Voice.AGE_MIDDLE_ADULT;
91         } else if (age == com.sun.speech.freetts.Age.OLDER_ADULT) {
92             return javax.speech.synthesis.Voice.AGE_OLDER_ADULT;
93         } else if (age == com.sun.speech.freetts.Age.NEUTRAL) {
94             return javax.speech.synthesis.Voice.AGE_NEUTRAL;
95         } else if (age == com.sun.speech.freetts.Age.DONT_CARE) {
96             return javax.speech.synthesis.Voice.AGE_DONT_CARE;
97         } else {
98             throw new Error("jaspi does not have an equivalent to age "
99                     + age.toString());
100         } 
101     }
102
103     /**
104      * Gets the id for this voice.
105      * Should be unique for a synthesizer.
106      *
107      * @return the voice id
108      */
109     public String getId() {
110         return voiceId;
111     }
112
113     /**
114      * Gets a string representation of the object
115      *
116      * @return the name of this voice
117      */
118     public String toString() {
119         return getName();
120     }
121
122
123     /**
124      * Gets a FreeTTS com.sun.speech.freetts.Voice from this JSAPI voice
125      *
126      * @return a FreeTTS Voice or null, if the voice cannot be found
127      */
128     public synchronized com.sun.speech.freetts.Voice getVoice() {
129         return freettsVoice;
130     }
131
132     /**
133      * Sets the id for this voice.
134      *
135      * @param id the new id
136      */
137     public void setId(String id) {
138         voiceId = id;
139     }
140
141     /**
142      * Creates a copy of this <code>BaseVoice</code>.
143      *
144      * @return the cloned object
145      */
146     public Object clone() {
147         return super.clone();
148     }
149
150     /**
151      * Validates this FreeTTSVoice.
152      *
153      * @throws ValidationException if this FreeTTSVoice is invalid
154      */
155     public void validate() throws ValidationException {
156         if (validator != null) {
157             validator.validate();
158         }
159     }
160 }
161