upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / en / us / CMUDiphoneVoice.java
1 /**
2  * Portions Copyright 2001 Sun Microsystems, Inc.
3  * Portions Copyright 1999-2001 Language Technologies Institute, 
4  * Carnegie Mellon University.
5  * All Rights Reserved.  Use is subject to license terms.
6  * 
7  * See the file "license.terms" for information on usage and
8  * redistribution of this file, and for a DISCLAIMER OF ALL 
9  * WARRANTIES.
10  */
11 package com.sun.speech.freetts.en.us;
12
13 import com.sun.speech.freetts.diphone.DiphonePitchmarkGenerator;
14 import com.sun.speech.freetts.diphone.DiphoneUnitSelector;
15 import com.sun.speech.freetts.Item;
16 import com.sun.speech.freetts.ProcessException;
17 import com.sun.speech.freetts.Relation;
18 import com.sun.speech.freetts.relp.UnitConcatenator;
19 import com.sun.speech.freetts.Utterance;
20 import com.sun.speech.freetts.UtteranceProcessor;
21 import com.sun.speech.freetts.Voice;
22
23 import com.sun.speech.freetts.Gender;
24 import com.sun.speech.freetts.Age;
25
26 import de.dfki.lt.freetts.ConcatenativeVoice;
27
28 import java.util.Locale;
29
30 import java.io.IOException;
31
32 import java.net.URL;
33
34
35 /**
36  * Defines an unlimited-domain diphone synthesis based voice 
37  */
38 public class CMUDiphoneVoice extends CMUVoice implements ConcatenativeVoice {
39
40     protected URL database;
41
42     /**
43      * Creates a simple voice.  This is merely for backwards
44      * compatibility with versions of FreeTTS earlier than v1.2
45      * (i.e., before the voice manager was introduced).
46      */
47     public CMUDiphoneVoice() {
48         this(null, null, null, null, null, null, null, null, null);
49     }
50     
51     /**
52      * Creates a simple voice
53      *
54      * @param name the name of the voice
55      * @param gender the gender of the voice
56      * @param age the age of the voice
57      * @param description a human-readable string providing a
58      * description that can be displayed for the users.
59      * @param locale the locale of the voice
60      * @param domain the domain of this voice.  For example,
61      * @param organization the organization which created the voice
62      * "general", "time", or
63      * "weather".
64      * @param lexicon the lexicon to load
65      * @param database a url to the unit database file for this voice
66      */
67     public CMUDiphoneVoice(String name, Gender gender,
68             Age age, String description, Locale locale, String domain,
69             String organization, CMULexicon lexicon, URL database) {
70         super(name, gender, age, description, locale,
71                 domain, organization, lexicon);
72         setRate(150f);
73         setPitch(100F);
74         setPitchRange(11F);
75         this.database = database;
76     }
77
78     /**
79      * Gets the url to the database that defines the unit data for this
80      * voice.
81      *
82      * @return a url to the database
83      */
84     public URL getDatabase() {
85         if (database == null) {
86             /* This is merely for backwards compatibility with
87              * versions of FreeTTS earlier than v1.2 (i.e.,
88              * before the voice manager was introduced).
89              */
90             String name = getFeatures().getString(Voice.DATABASE_NAME);
91             database = this.getClass().getResource(name);
92         }
93         return database;
94     }
95
96     /**
97      * Sets the FeatureSet for this Voice.
98      *
99      * @throws IOException if an I/O error occurs
100      */
101     protected void setupFeatureSet() throws IOException {
102         super.setupFeatureSet();
103     }
104
105     /**
106      * Returns the post lexical processor to be used by this voice.
107      * Derived voices typically override this to customize behaviors.
108      * 
109      * @return the Unit selector
110      * 
111      * @throws IOException if an IO error occurs while getting
112      *     processor
113      */
114     protected UtteranceProcessor getPostLexicalAnalyzer() throws IOException {
115         return new CMUDiphoneVoicePostLexicalAnalyzer();
116     }
117
118     /**
119      * Returns the pitch mark generator to be used by this voice.
120      * Derived voices typically override this to customize behaviors.
121      * This voice uses a DiphonePitchMark generator to generate
122      * pitchmarks.
123      * 
124      * @return the pitchmark processor
125      * 
126      * @throws IOException if an IO error occurs while getting
127      *     processor
128      */
129     public UtteranceProcessor getPitchmarkGenerator() throws IOException {
130         return new DiphonePitchmarkGenerator();
131     }
132
133     /**
134      * Returns the unit concatenator to be used by this voice.
135      * Derived voices typically override this to customize behaviors.
136      * This voice uses a relp.UnitConcatenator to concatenate units.
137      * 
138      * @return the unit concatenator processor
139      * 
140      * @throws IOException if an IO error occurs while getting
141      *     processor
142      */
143     public UtteranceProcessor getUnitConcatenator() throws IOException {
144         return new UnitConcatenator();
145     }
146
147
148     /**
149      * Returns the unit selector to be used by this voice.
150      * Derived voices typically override this to customize behaviors.
151      * This voice uses the DiphoneUnitSelector to select units. The
152      * unit selector requires the name of a diphone database. If no
153      * diphone database has been specified then an Error is thrown.
154      * 
155      * @return the unit selector processor
156      * 
157      * @throws IOException if an IO error occurs while getting
158      *     processor
159      */
160     public UtteranceProcessor getUnitSelector() throws IOException {
161         return new DiphoneUnitSelector(getDatabase());
162     }
163
164     
165     /**
166      * Converts this object to a string
167      * 
168      * @return a string representation of this object
169      */
170     public String toString() {
171         return "CMUDiphoneVoice";
172     }
173 }
174
175
176 /**
177  * Annotates the utterance with post lexical information. Converts AH
178  * phonemes to AA phoneme in addition to the standard english postlex
179  * processing.
180  */
181 class CMUDiphoneVoicePostLexicalAnalyzer implements UtteranceProcessor {
182     UtteranceProcessor englishPostLex =
183         new com.sun.speech.freetts.en.PostLexicalAnalyzer();
184
185     /**
186      * performs the processing
187      * @param  utterance  the utterance to process/tokenize
188      * @throws ProcessException if an IOException is thrown during the
189      *         processing of the utterance
190      */
191     public void processUtterance(Utterance utterance) throws ProcessException {
192         fixPhoneme_AH(utterance);
193         englishPostLex.processUtterance(utterance);
194     }
195
196
197     /**
198      * Turns all AH phonemes into AA phonemes.
199      * This should really be done in the index itself
200      * @param utterance the utterance to fix
201      */
202     private void fixPhoneme_AH(Utterance utterance) {
203         for (Item item = utterance.getRelation(Relation.SEGMENT).getHead();
204                 item != null;
205                 item = item.getNext()) {
206             if (item.getFeatures().getString("name").equals("ah")) {
207                 item.getFeatures().setString("name", "aa");
208             }
209         }
210     }
211
212     // inherited from Object
213     public String toString() {
214         return "PostLexicalAnalyzer";
215     }
216 }