upstream version 1.2.2
[debian/freetts] / demo / JSAPI / HelloWorld / HelloWorld.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 import java.io.File;
9 import java.util.Locale;
10 import java.util.Vector;
11
12 import javax.speech.Central;
13 import javax.speech.Engine;
14 import javax.speech.EngineList;
15 import javax.speech.synthesis.Synthesizer;
16 import javax.speech.synthesis.SynthesizerModeDesc;
17 import javax.speech.synthesis.SynthesizerProperties;
18 import javax.speech.synthesis.Voice;
19
20 /**
21  * Simple program showing how to use FreeTTS using only the Java
22  * Speech API (JSAPI).
23  */
24 public class HelloWorld {
25
26     /**
27      * Returns a "no synthesizer" message, and asks 
28      * the user to check if the "speech.properties" file is
29      * at <code>user.home</code> or <code>java.home/lib</code>.
30      *
31      * @return a no synthesizer message
32      */
33     static private String noSynthesizerMessage() {
34         String message =
35             "No synthesizer created.  This may be the result of any\n" +
36             "number of problems.  It's typically due to a missing\n" +
37             "\"speech.properties\" file that should be at either of\n" +
38             "these locations: \n\n";
39         message += "user.home    : " + System.getProperty("user.home") + "\n";
40         message += "java.home/lib: " + System.getProperty("java.home") +
41             File.separator + "lib\n\n" +
42             "Another cause of this problem might be corrupt or missing\n" +
43             "voice jar files in the freetts lib directory.  This problem\n" +
44             "also sometimes arises when the freetts.jar file is corrupt\n" +
45             "or missing.  Sorry about that.  Please check for these\n" +
46             "various conditions and then try again.\n";
47         return message;
48     }
49
50     /**
51      * Example of how to list all the known voices for a specific
52      * mode using just JSAPI.  FreeTTS maps the domain name to the
53      * JSAPI mode name.  The currently supported domains are
54      * "general," which means general purpose synthesis for tasks
55      * such as reading e-mail, and "time" which means a domain that's
56      * only good for speaking the time of day. 
57      */
58     public static void listAllVoices(String modeName) {
59         
60         System.out.println();
61         System.out.println(
62             "All " + modeName + " Mode JSAPI Synthesizers and Voices:");
63
64         /* Create a template that tells JSAPI what kind of speech
65          * synthesizer we are interested in.  In this case, we're
66          * just looking for a general domain synthesizer for US
67          * English.
68          */ 
69         SynthesizerModeDesc required = new SynthesizerModeDesc(
70             null,      // engine name
71             modeName,  // mode name
72             Locale.US, // locale
73             null,      // running
74             null);     // voices
75
76         /* Contact the primary entry point for JSAPI, which is
77          * the Central class, to discover what synthesizers are
78          * available that match the template we defined above.
79          */
80         EngineList engineList = Central.availableSynthesizers(required);
81         for (int i = 0; i < engineList.size(); i++) {
82             
83             SynthesizerModeDesc desc = (SynthesizerModeDesc) engineList.get(i);
84             System.out.println("    " + desc.getEngineName()
85                                + " (mode=" + desc.getModeName()
86                                + ", locale=" + desc.getLocale() + "):");
87             Voice[] voices = desc.getVoices();
88             for (int j = 0; j < voices.length; j++) {
89                 System.out.println("        " + voices[j].getName());
90             }
91         }
92     }
93     
94     public static void main(String[] args) {
95
96         /* List all the "general" domain voices, which are voices that
97          * are capable of attempting to speak almost any text you
98          * throw at them.
99          */
100         listAllVoices("general");
101
102         String voiceName = (args.length > 0)
103             ? args[0]
104             : "kevin16";
105         
106         System.out.println();
107         System.out.println("Using voice: " + voiceName);
108         
109         try {
110             /* Find a synthesizer that has the general domain voice
111              * we are looking for.  NOTE:  this uses the Central class
112              * of JSAPI to find a Synthesizer.  The Central class
113              * expects to find a speech.properties file in user.home
114              * or java.home/lib.
115              *
116              * If your situation doesn't allow you to set up a
117              * speech.properties file, you can circumvent the Central
118              * class and do a very non-JSAPI thing by talking to
119              * FreeTTSEngineCentral directly.  See the WebStartClock
120              * demo for an example of how to do this.
121              */
122             SynthesizerModeDesc desc = new SynthesizerModeDesc(
123                 null,          // engine name
124                 "general",     // mode name
125                 Locale.US,     // locale
126                 null,          // running
127                 null);         // voice
128             Synthesizer synthesizer = Central.createSynthesizer(desc);
129
130             /* Just an informational message to guide users that didn't
131              * set up their speech.properties file. 
132              */
133             if (synthesizer == null) {
134                 System.err.println(noSynthesizerMessage());
135                 System.exit(1);
136             }
137
138             /* Get the synthesizer ready to speak
139              */
140             synthesizer.allocate();
141             synthesizer.resume();
142
143             /* Choose the voice.
144              */
145             desc = (SynthesizerModeDesc) synthesizer.getEngineModeDesc();
146             Voice[] voices = desc.getVoices();
147             Voice voice = null;
148             for (int i = 0; i < voices.length; i++) {
149                 if (voices[i].getName().equals(voiceName)) {
150                     voice = voices[i];
151                     break;
152                 }
153             }
154             if (voice == null) {
155                 System.err.println(
156                     "Synthesizer does not have a voice named "
157                     + voiceName + ".");
158                 System.exit(1);
159             }
160             synthesizer.getSynthesizerProperties().setVoice(voice);
161
162             /* The the synthesizer to speak and wait for it to
163              * complete.
164              */
165             synthesizer.speakPlainText("Hello world!", null);
166             synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
167             
168             /* Clean up and leave.
169              */
170             synthesizer.deallocate();
171             System.exit(0);
172             
173         } catch (Exception e) {
174             e.printStackTrace();
175         }
176     }
177 }