2 * Copyright 2003 Sun Microsystems, Inc.
4 * See the file "license.terms" for information on usage and
5 * redistribution of this file, and for a DISCLAIMER OF ALL
9 import java.util.Locale;
10 import java.util.Vector;
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;
21 * Simple program showing how to use FreeTTS using only the Java
24 public class HelloWorld {
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>.
31 * @return a no synthesizer message
33 static private String noSynthesizerMessage() {
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";
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.
58 public static void listAllVoices(String modeName) {
62 "All " + modeName + " Mode JSAPI Synthesizers and Voices:");
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
69 SynthesizerModeDesc required = new SynthesizerModeDesc(
71 modeName, // mode name
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.
80 EngineList engineList = Central.availableSynthesizers(required);
81 for (int i = 0; i < engineList.size(); i++) {
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());
94 public static void main(String[] args) {
96 /* List all the "general" domain voices, which are voices that
97 * are capable of attempting to speak almost any text you
100 listAllVoices("general");
102 String voiceName = (args.length > 0)
106 System.out.println();
107 System.out.println("Using voice: " + voiceName);
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
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.
122 SynthesizerModeDesc desc = new SynthesizerModeDesc(
124 "general", // mode name
128 Synthesizer synthesizer = Central.createSynthesizer(desc);
130 /* Just an informational message to guide users that didn't
131 * set up their speech.properties file.
133 if (synthesizer == null) {
134 System.err.println(noSynthesizerMessage());
138 /* Get the synthesizer ready to speak
140 synthesizer.allocate();
141 synthesizer.resume();
145 desc = (SynthesizerModeDesc) synthesizer.getEngineModeDesc();
146 Voice[] voices = desc.getVoices();
148 for (int i = 0; i < voices.length; i++) {
149 if (voices[i].getName().equals(voiceName)) {
156 "Synthesizer does not have a voice named "
160 synthesizer.getSynthesizerProperties().setVoice(voice);
162 /* The the synthesizer to speak and wait for it to
165 synthesizer.speakPlainText("Hello world!", null);
166 synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
168 /* Clean up and leave.
170 synthesizer.deallocate();
173 } catch (Exception e) {