upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / en / us / CMUVoice.java
1 /**
2  * Portions Copyright 2003 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 java.io.IOException;
14 import java.util.List;
15 import java.util.Locale;
16
17 import com.sun.speech.freetts.Age;
18 import com.sun.speech.freetts.FeatureSet;
19 import com.sun.speech.freetts.Gender;
20 import com.sun.speech.freetts.PartOfSpeech;
21 import com.sun.speech.freetts.PartOfSpeechImpl;
22 import com.sun.speech.freetts.PhoneDurations;
23 import com.sun.speech.freetts.PhoneDurationsImpl;
24 import com.sun.speech.freetts.PhoneSet;
25 import com.sun.speech.freetts.PhoneSetImpl;
26 import com.sun.speech.freetts.Segmenter;
27 import com.sun.speech.freetts.Tokenizer;
28 import com.sun.speech.freetts.UtteranceProcessor;
29 import com.sun.speech.freetts.Voice;
30 import com.sun.speech.freetts.cart.CARTImpl;
31 import com.sun.speech.freetts.cart.Durator;
32 import com.sun.speech.freetts.cart.Intonator;
33 import com.sun.speech.freetts.cart.Phraser;
34 import com.sun.speech.freetts.en.ContourGenerator;
35 import com.sun.speech.freetts.en.PartOfSpeechTagger;
36 import com.sun.speech.freetts.en.PauseGenerator;
37 import com.sun.speech.freetts.relp.AudioOutput;
38 import com.sun.speech.freetts.util.BulkTimer;
39
40 /**
41  * Provides generic support for a CMU Voice
42  */
43 public abstract class CMUVoice extends Voice {
44     private PhoneSet phoneSet;
45
46     /**
47      * Creates a simple voice
48      *
49      * @param name the name of the voice
50      * @param gender the gender of the voice
51      * @param age the age of the voice
52      * @param description a human-readable string providing a
53      * description that can be displayed for the users.
54      * @param locale the locale of the voice
55      * @param domain the domain of this voice.  For example,
56      * @param organization the organization which created the voice
57      * "general", "time", or
58      * "weather".
59      * @param lexicon the lexicon to load
60      */
61     public CMUVoice(String name, Gender gender,
62             Age age, String description, Locale locale, String domain,
63             String organization, CMULexicon lexicon) {
64         super(name, gender, age, description, locale, domain,
65                 organization);
66         setLexicon(lexicon);
67     }
68
69     // overrides Voice.loader
70
71     /**
72      * Called by <code> Voice </code>  during loading, derived voices
73      * should override this to provide customized loading.
74      */
75     protected void loader() throws IOException {
76         setupFeatureSet();
77         setupUtteranceProcessors();
78         setupFeatureProcessors();
79     }
80
81     /**
82      * Sets up the FeatureSet for this Voice.
83      *
84      * @throws IOException if an I/O error occurs
85      */
86     protected void setupFeatureSet() throws IOException {
87         BulkTimer.LOAD.start("FeatureSet");
88         FeatureSet features = getFeatures();
89         features.setString(FEATURE_SILENCE, "pau");
90         features.setString("join_type", "simple_join");
91         BulkTimer.LOAD.stop("FeatureSet");
92     }
93
94
95     /**
96      * Sets up the utterance processors for this voice. Subclasses
97      * should define this method to setup the utterance processors for
98      * the voice.
99      *
100      * @throws IOException throws an IOException if an error occurs
101      */
102     protected void setupUtteranceProcessors() throws IOException {
103         List processors = getUtteranceProcessors();
104
105         BulkTimer.LOAD.start("CartLoading");
106         CARTImpl numbersCart = new CARTImpl(getResource("nums_cart.txt"));
107         CARTImpl phrasingCart = new CARTImpl(getResource("phrasing_cart.txt"));
108         CARTImpl accentCart = new CARTImpl(getResource("int_accent_cart.txt"));
109         CARTImpl toneCart = new CARTImpl(getResource("int_tone_cart.txt"));
110         CARTImpl durzCart = new CARTImpl(getResource("durz_cart.txt"));
111         BulkTimer.LOAD.stop("CartLoading");
112
113         BulkTimer.LOAD.start("UtteranceProcessors");
114         PhoneDurations phoneDurations = new PhoneDurationsImpl(
115             getResource("dur_stat.txt"));
116         PronounceableFSM prefixFSM = new PrefixFSM
117             (getResource("prefix_fsm.txt"));
118         PronounceableFSM suffixFSM = new SuffixFSM
119             (getResource("suffix_fsm.txt"));
120         
121         processors.add(new TokenToWords(numbersCart, prefixFSM, suffixFSM));
122         processors.add(new PartOfSpeechTagger());
123         processors.add(new Phraser(phrasingCart));
124         processors.add(new Segmenter());
125         processors.add(new PauseGenerator());
126         processors.add(new Intonator(accentCart, toneCart));
127         processors.add(getPostLexicalAnalyzer());
128         processors.add(new Durator(durzCart, 150.0f, phoneDurations));
129         processors.add(new ContourGenerator
130            (getResource("f0_lr_terms.txt"), 170.0f, 34.0f));
131
132
133         processors.add(getUnitSelector());
134         processors.add(getPitchmarkGenerator());
135         processors.add(getUnitConcatenator());
136         BulkTimer.LOAD.stop("UtteranceProcessors");
137     }
138
139     // [[[TODO: currently a CMUVoice only allows customization of
140     // the postlex, unit selector and wave synthesizer. This may 
141     // grow as time goes on ]]]
142     /**
143      * Returns the post lexical processor to be used by this voice.
144      * Derived voices typically override this to customize behaviors.
145      * 
146      * @return the post lexical analyzer in use by this voice
147      * 
148      * @throws IOException if an IO error occurs while getting
149      *     processor
150      */
151     protected UtteranceProcessor getPostLexicalAnalyzer() throws IOException {
152         return new com.sun.speech.freetts.en.PostLexicalAnalyzer();
153     }
154
155     /**
156      * Returns the unit selector to be used by this voice
157      * Derived voices typically override this to customize behaviors.
158      * 
159      * @return the unit selector in use by this voice
160      * 
161      * @throws IOException if an IO error occurs while getting
162      *     processor
163      */
164     protected UtteranceProcessor getUnitSelector() throws IOException {
165         return null;
166     }
167
168     /**
169      * Returns the pitch mark generator to be used by this voice
170      * Derived voices typically override this to customize behaviors.
171      * 
172      * @return the pitch mark generator to be used by this voice
173      * 
174      * @throws IOException if an IO error occurs while getting
175      *     processor
176      */
177     protected UtteranceProcessor getPitchmarkGenerator() throws IOException {
178         return null;
179     }
180
181     /**
182      * Returns the unit concatenator to be used by this voice
183      * Derived voices typically override this to customize behaviors.
184      * 
185      * @return the Unit concatenator
186      * 
187      * @throws IOException if an IO error occurs while getting
188      *     processor
189      */
190     protected UtteranceProcessor getUnitConcatenator() throws IOException {
191         return null;
192     }
193
194     
195     /**
196      * Sets up the FeatureProcessors for this Voice.
197      *
198      * @throws IOException if an I/O error occurs
199      */
200     protected void setupFeatureProcessors() throws IOException {
201         BulkTimer.LOAD.start("FeatureProcessing");
202         PartOfSpeech pos = new PartOfSpeechImpl( 
203             getResource("part_of_speech.txt"),
204             "content");
205
206         phoneSet  = new PhoneSetImpl(getResource("phoneset.txt"));
207
208         addFeatureProcessor("word_break", new FeatureProcessors.WordBreak());
209         addFeatureProcessor("word_punc", new FeatureProcessors.WordPunc());
210         addFeatureProcessor("gpos", new FeatureProcessors.Gpos(pos));
211         addFeatureProcessor("word_numsyls",new FeatureProcessors.WordNumSyls());
212         addFeatureProcessor("ssyl_in", new FeatureProcessors.StressedSylIn());
213         addFeatureProcessor("syl_in", new FeatureProcessors.SylIn());
214         addFeatureProcessor("syl_out", new FeatureProcessors.SylOut());
215         addFeatureProcessor("ssyl_out", new
216                 FeatureProcessors.StressedSylOut());
217         addFeatureProcessor("syl_break", new FeatureProcessors.SylBreak());
218         addFeatureProcessor("old_syl_break", new FeatureProcessors.SylBreak());
219         addFeatureProcessor("num_digits", new FeatureProcessors.NumDigits());
220         addFeatureProcessor("month_range", new FeatureProcessors.MonthRange());
221         addFeatureProcessor("token_pos_guess", 
222                 new FeatureProcessors.TokenPosGuess());
223         addFeatureProcessor("segment_duration", 
224                 new FeatureProcessors.SegmentDuration());
225         addFeatureProcessor("sub_phrases", new FeatureProcessors.SubPhrases());
226         addFeatureProcessor("asyl_in", new FeatureProcessors.AccentedSylIn());
227         addFeatureProcessor("last_accent", new FeatureProcessors.LastAccent());
228         addFeatureProcessor("pos_in_syl", new FeatureProcessors.PosInSyl());
229         addFeatureProcessor("position_type", new
230                 FeatureProcessors.PositionType());
231
232         addFeatureProcessor("ph_cplace", new FeatureProcessors.PH_CPlace());
233         addFeatureProcessor("ph_ctype", new FeatureProcessors.PH_CType());
234         addFeatureProcessor("ph_cvox", new FeatureProcessors.PH_CVox());
235         addFeatureProcessor("ph_vc", new FeatureProcessors.PH_VC());
236         addFeatureProcessor("ph_vfront", new FeatureProcessors.PH_VFront());
237         addFeatureProcessor("ph_vheight", new FeatureProcessors.PH_VHeight());
238         addFeatureProcessor("ph_vlng", new FeatureProcessors.PH_VLength());
239         addFeatureProcessor("ph_vrnd", new FeatureProcessors.PH_VRnd());
240
241         addFeatureProcessor("seg_coda_fric", new
242                 FeatureProcessors.SegCodaFric());
243         addFeatureProcessor("seg_onset_fric", new
244                 FeatureProcessors.SegOnsetFric());
245
246         addFeatureProcessor("seg_coda_stop", new
247                 FeatureProcessors.SegCodaStop());
248         addFeatureProcessor("seg_onset_stop", new
249                 FeatureProcessors.SegOnsetStop());
250
251         addFeatureProcessor("seg_coda_nasal", new
252                 FeatureProcessors.SegCodaNasal());
253         addFeatureProcessor("seg_onset_nasal", new
254                 FeatureProcessors.SegOnsetNasal());
255
256         addFeatureProcessor("seg_coda_glide", new
257                 FeatureProcessors.SegCodaGlide());
258         addFeatureProcessor("seg_onset_glide", new
259                 FeatureProcessors.SegOnsetGlide());
260
261         addFeatureProcessor("seg_onsetcoda", new
262                 FeatureProcessors.SegOnsetCoda());
263         addFeatureProcessor("syl_codasize", new
264                 FeatureProcessors.SylCodaSize());
265         addFeatureProcessor("syl_onsetsize", new
266                 FeatureProcessors.SylOnsetSize());
267         addFeatureProcessor("accented", new FeatureProcessors.Accented());
268         BulkTimer.LOAD.stop("FeatureProcessing");
269     }
270     
271     /**
272      * Given a phoneme and a feature name, return the feature
273      *
274      * @param phone the phoneme of interest
275      * @param featureName the name of the feature of interest
276      *
277      * @return the feature with the given name
278      */
279     public String getPhoneFeature(String phone, String featureName) {
280         return phoneSet.getPhoneFeature(phone, featureName);
281     }
282
283     /**
284      * Returns the AudioOutput processor to be used by this voice
285      * Derived voices typically override this to customize behaviors.
286      * 
287      * @return the audio output processor
288      * 
289      * @throws IOException if an IO error occurs while getting
290      *     processor
291      */
292     protected UtteranceProcessor getAudioOutput() throws IOException {
293         return new AudioOutput();
294     }
295
296     /**
297      * Gets a tokenizer for this voice
298      *
299      * @return the tokenizer
300      */
301     public Tokenizer getTokenizer() {
302         Tokenizer tokenizer = new com.sun.speech.freetts.en.TokenizerImpl();
303         tokenizer.setWhitespaceSymbols(USEnglish.WHITESPACE_SYMBOLS);
304         tokenizer.setSingleCharSymbols(USEnglish.SINGLE_CHAR_SYMBOLS);
305         tokenizer.setPrepunctuationSymbols(USEnglish.PREPUNCTUATION_SYMBOLS);
306         tokenizer.setPostpunctuationSymbols(USEnglish.PUNCTUATION_SYMBOLS);
307         return tokenizer;
308     }
309
310     /**
311      * Converts this object to its String representation
312      *
313      * @return the string representation of this object
314      */
315     public String toString() {
316         return "CMUVoice";
317     }
318 }
319