upstream version 1.2.2
[debian/freetts] / de / dfki / lt / freetts / en / us / MbrolaVoice.java
1 /**
2  * Copyright 2002 DFKI GmbH.
3  * Portions Copyright 2002 Sun Microsystems, Inc.
4  * All Rights Reserved.  Use is subject to license terms.
5  *
6  * See the file "license.terms" for information on usage and
7  * redistribution of this file, and for a DISCLAIMER OF ALL
8  * WARRANTIES.
9  */
10
11 package de.dfki.lt.freetts.en.us;
12
13 import java.io.File;
14 import java.io.IOException;
15 import java.net.URL;
16 import java.util.Locale;
17
18 import com.sun.speech.freetts.Age;
19 import com.sun.speech.freetts.Gender;
20 import com.sun.speech.freetts.UtteranceProcessor;
21 import com.sun.speech.freetts.en.us.CMULexicon;
22 import com.sun.speech.freetts.en.us.CMUVoice;
23 import com.sun.speech.freetts.util.Utilities;
24
25 import de.dfki.lt.freetts.mbrola.MbrolaAudioOutput;
26 import de.dfki.lt.freetts.mbrola.MbrolaCaller;
27 import de.dfki.lt.freetts.mbrola.ParametersToMbrolaConverter;
28
29 /**
30  * Defines an unlimited-domain diphone synthesis based voice using
31  * the MBROLA synthesis.
32  */
33 public class MbrolaVoice extends CMUVoice {
34
35     private String databaseDirectory; // where the voice database is
36     private String database;          // name of the voice database
37
38     private static final String MRPA_TO_SAMPA_RENAME_LIST =
39         "V ah i iy I ih U uh { ae @ ax r= er A aa O ao u uw E eh EI ey AI ay OI oy aU aw @U ow j y h hh N ng S sh T th Z zh D dh tS ch dZ jh _ pau";
40     
41     /**
42      * Creates an MbrolaVoice.
43      *
44      * @param databaseDirectory the directory within the MBROLA directory
45      *    where the voice database of this voice is located
46      * @param database the name of the voice database of this voice
47      * @param rate the rate of the voice
48      * @param pitch the pitch of the voice
49      * @param range the range of the voice
50      * @param name the name of the voice
51      * @param gender the gender of the voice
52      * @param age the age of the voice
53      * @param description a human-readable string providing a
54      * description that can be displayed for the users.
55      * @param locale the locale of the voice
56      * @param domain the domain of this voice.  For example,
57      * @param organization the organization which created the voice
58      * @param lexicon the lexicon to use
59      */
60     public MbrolaVoice(String databaseDirectory, 
61                        String database, float rate, float pitch, float range,
62                        String name, Gender gender, Age age,
63                        String description, Locale locale, String domain,
64                        String organization, CMULexicon lexicon) {
65         super(name, gender, age, description, locale,
66               domain, organization, lexicon);
67         setRate(rate);
68         setPitch(pitch);
69         setPitchRange(range);
70         this.databaseDirectory = databaseDirectory;
71         this.database = database;
72     }
73
74     //[[Providing the Mbrola classes via getUnitSelector() and
75     // getUnitConcatenator() is just a hack allowing us to use
76     // the current CMUVoice.java framework. It only means that
77     // after the Durator and the ContourGenerator, the classes
78     // process the utterance (Selector before Concatenator).]]
79     /**
80      * Returns the unit selector to be used by this voice.
81      * Derived voices typically override this to customize behaviors.
82      * 
83      * @return the unit selector
84      * 
85      * @throws IOException if an IO error occurs while getting
86      *     processor
87      */
88     protected UtteranceProcessor getUnitSelector() throws IOException {
89         return new ParametersToMbrolaConverter();
90     }
91
92     /**
93      * Returns the command line that invokes the MBROLA executable.
94      * The command will be in the form of:
95      *
96      * <pre> {mbrolaExecutable} -e -R {mbrolaRenameList} {mbrolaVoiceDB} 
97      * - -.raw </pre>
98      */
99     protected String[] getMbrolaCommand() {
100
101         // Construct the mbrola command in such a way that
102         // mbrola reads from stdin and writes raw, headerless audio data
103         // to stdout; translates CMU us radio to sampa phonetic symbols;
104         // and only complains, but does not abort, when encountering an
105         // unknown diphone:
106         String[] cmd = 
107              {getMbrolaBinary(), "-e", "-R",  getRenameList(), 
108              getDatabase(), "-", "-.raw"};
109
110          if (false) {
111              for (int i = 0; i < cmd.length; i++) {
112                  System.out.println(cmd[i]);
113              }
114          }
115
116         return cmd;
117     }
118
119     /**
120      * Returns the absolute name of the MBROLA directory.
121      *
122      * @return the absolute name of the MBROLA directory
123      */
124     public String getMbrolaBase() {
125         return Utilities.getProperty("mbrola.base", ".");
126     }
127
128     /**
129      * Returns the absolute file name of the MBROLA binary.
130      *
131      * @return the absolute file name of the MBROLA binary
132      */
133     public String getMbrolaBinary() {
134         // In windows environments, executables typically end with exe, so
135         // we add this suffix if we are running under windows.
136         StringBuffer executable = new StringBuffer();
137         executable.append("mbrola");
138         String os = System.getProperty("os.name");
139         if (os.indexOf("Windows") >= 0) {
140             executable.append(".exe");
141         }
142         return getMbrolaBase() + File.separator + executable.toString();
143     }
144
145     /**
146      * Returns the absolute file name of the MBROLA phonetic symbols
147      * rename table.
148      *
149      * @return the absolute file name of the rename table
150      */
151     public String getRenameList() {
152         return MRPA_TO_SAMPA_RENAME_LIST;
153     }
154
155     /**
156      * Returns the absolute file name of the Voice database
157      * this MbrolaVoice uses.
158      *
159      * @return the absolute file name of the Voice database
160      */
161     public String getDatabase() {
162         return getMbrolaBase() + File.separator + 
163             databaseDirectory + File.separator + database;
164     }
165
166 //    /**
167 //     * Returns the unit concatenator to be used by this voice.
168 //     * Derived voices typically override this to customize behaviors.
169 //     * 
170 //     * @return the unit conatenator
171 //     * 
172 //     * @throws IOException if an IO error occurs while getting
173 //     *     processor
174 //     */
175 //    protected UtteranceProcessor getUnitConcatenator() throws IOException {
176 //        return null;
177 //    }
178
179     //[[Providing the Mbrola classes via getUnitSelector() and
180     // getUnitConcatenator() is just a hack allowing us to use
181     // the current CMUVoice.java framework. It only means that
182     // after the Durator and the ContourGenerator, the classes
183     // process the utterance (Selector before Concatenator).]]
184     /**
185      * Returns the unit concatenator to be used by this voice.
186      * This method constructs the command line with which the
187      * MBROLA binary will be called, and initialises the
188      * MbrolaCaller accordingly.
189      *
190      * @return the unit conatenator
191      *
192      * @throws IOException if an IO error occurs while getting
193      *     processor
194      */
195     protected UtteranceProcessor getUnitConcatenator() throws IOException {
196         return new MbrolaCaller(getMbrolaCommand());
197     }
198
199     /**
200      * Returns the audio output used by this voice.
201      *
202      * @return the audio output used by this voice
203      *
204      * @throws IOException if an I/O error occurs
205      */
206     protected UtteranceProcessor getAudioOutput() throws IOException {
207         return new MbrolaAudioOutput();
208     }
209
210     /**
211      * Get a resource for this voice.  Resources for this voice are located in
212      * the package <code>com.sun.speech.freetts.en.us</code>.
213      */
214     protected URL getResource(String resource) {
215         return com.sun.speech.freetts.en.us.CMUVoice.class.
216             getResource(resource);
217     }
218
219     /**
220      * Converts this object to a string
221      * 
222      * @return a string representation of this object
223      */
224     public String toString() {
225         return "MbrolaVoice";
226     }
227 }
228