1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
/**
* Copyright 2003 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and
* redistribution of this file, and for a DISCLAIMER OF ALL
* WARRANTIES.
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.regex.Pattern;
import javax.speech.Central;
import javax.speech.Engine;
import javax.speech.EngineList;
import javax.speech.EngineException;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;
import javax.speech.synthesis.SynthesizerProperties;
import javax.speech.synthesis.Voice;
/**
* Simple program showing how to use the Limited Domain (time)
* FreeTTS synthesizer.
*/
public class JTime {
Synthesizer synthesizer;
/**
* Returns a "no synthesizer" message, and asks
* the user to check if the "speech.properties" file is
* at <code>user.home</code> or <code>java.home/lib</code>.
*
* @return a no synthesizer message
*/
static private String noSynthesizerMessage() {
String message =
"No synthesizer created. This may be the result of any\n" +
"number of problems. It's typically due to a missing\n" +
"\"speech.properties\" file that should be at either of\n" +
"these locations: \n\n";
message += "user.home : " + System.getProperty("user.home") + "\n";
message += "java.home/lib: " + System.getProperty("java.home") +
File.separator + "lib\n\n" +
"Another cause of this problem might be corrupt or missing\n" +
"voice jar files in the freetts lib directory. This problem\n" +
"also sometimes arises when the freetts.jar file is corrupt\n" +
"or missing. Sorry about that. Please check for these\n" +
"various conditions and then try again.\n";
return message;
}
/**
* Example of how to list all the known voices for a specific
* mode using just JSAPI. FreeTTS maps the domain name to the
* JSAPI mode name. The currently supported domains are
* "general," which means general purpose synthesis for tasks
* such as reading e-mail, and "time" which means a domain that's
* only good for speaking the time of day.
*/
public static void listAllVoices(String modeName) {
System.out.println();
System.out.println(
"All " + modeName + " Mode JSAPI Synthesizers and Voices:");
/* Create a template that tells JSAPI what kind of speech
* synthesizer we are interested in. In this case, we're
* just looking for a general domain synthesizer for US
* English.
*/
SynthesizerModeDesc required = new SynthesizerModeDesc(
null, // engine name
modeName, // mode name
Locale.US, // locale
null, // running
null); // voices
/* Contact the primary entry point for JSAPI, which is
* the Central class, to discover what synthesizers are
* available that match the template we defined above.
*/
EngineList engineList = Central.availableSynthesizers(required);
for (int i = 0; i < engineList.size(); i++) {
SynthesizerModeDesc desc = (SynthesizerModeDesc) engineList.get(i);
System.out.println(" " + desc.getEngineName()
+ " (mode=" + desc.getModeName()
+ ", locale=" + desc.getLocale() + "):");
Voice[] voices = desc.getVoices();
for (int j = 0; j < voices.length; j++) {
System.out.println(" " + voices[j].getName());
}
}
}
/**
* Construct a default JTime object. It creates the Limited Domain
* synthesizer, speaks the current time, and asks the user to input
* a new time in the format HH:MM.
*/
public JTime(String voiceName) {
try {
/* Find a synthesizer that has the general domain voice
* we are looking for. NOTE: this uses the Central class
* of JSAPI to find a Synthesizer. The Central class
* expects to find a speech.properties file in user.home
* or java.home/lib.
*
* If your situation doesn't allow you to set up a
* speech.properties file, you can circumvent the Central
* class and do a very non-JSAPI thing by talking to
* FreeTTSEngineCentral directly. See the WebStartClock
* demo for an example of how to do this.
*/
SynthesizerModeDesc desc = new SynthesizerModeDesc(
null, // engine name
"time", // mode name
Locale.US, // locale
null, // running
null); // voice
synthesizer = Central.createSynthesizer(desc);
/* Just an informational message to guide users that didn't
* set up their speech.properties file.
*/
if (synthesizer == null) {
System.err.println(noSynthesizerMessage());
System.exit(1);
}
/* Get the synthesizer ready to speak
*/
synthesizer.allocate();
synthesizer.resume();
/* Choose the voice.
*/
desc = (SynthesizerModeDesc) synthesizer.getEngineModeDesc();
Voice[] voices = desc.getVoices();
Voice voice = null;
for (int i = 0; i < voices.length; i++) {
if (voices[i].getName().equals(voiceName)) {
voice = voices[i];
break;
}
}
if (voice == null) {
System.err.println(
"Synthesizer does not have a voice named "
+ voiceName + ".");
System.exit(1);
}
synthesizer.getSynthesizerProperties().setVoice(voice);
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* Starts interactive mode. Reads text from the console and gives
* it to the synthesizer to speak. Terminates on end of file.
*/
public void interactiveMode() {
try {
while (true) {
String text;
BufferedReader reader = new BufferedReader
(new InputStreamReader(System.in));
System.out.print("Enter time (HH:MM): ");
System.out.flush();
text = reader.readLine();
if ((text == null) || (text.length() == 0)) {
break;
} else {
timeToSpeech(text);
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* Speaks the given time. Time should be in the exact form
* HH:MM where HH is the hour 00 to 23, and MM is the minute 00 to
* 59.
*
* @param time the time in the form HH:MM
*
* @throws IllegalArgumentException if time is not in the form
* HH:MM
*/
public void timeToSpeech(String time) {
String theTime = TimeUtils.timeToString(time);
if (theTime != null) {
synthesizer.speakPlainText(theTime, null);
} else {
// throw new IllegalArgumentException("Bad time format");
System.out.println("Bad time format. The format should be HH:MM");
}
}
/**
* Speaks the time given the hour and minute.
*
* @param hour the hour of the day (0 to 23)
* @param min the minute of the hour (0 to 59)
*/
public void timeToSpeech(int hour, int min) {
if (hour < 0 || hour > 23) {
throw new IllegalArgumentException("Bad time format: hour");
}
if (min < 0 || min > 59) {
throw new IllegalArgumentException("Bad time format: min");
}
String theTime = TimeUtils.timeToString(hour, min);
synthesizer.speakPlainText(theTime, null);
}
/**
* Speaks the given time. Prints an error message if the time
* is ill-formed.
*
* @param time the time in the form HH:MM
*/
public void safeTimeToSpeech(String time) {
try {
if (time.equals("now")) {
speakNow();
} else {
timeToSpeech(time);
}
} catch (IllegalArgumentException iae) {
System.err.println("Bad time format");
}
}
/**
* Tells the current time.
*/
public void speakNow() {
long now = System.currentTimeMillis();
Calendar cal = new GregorianCalendar();
int hour = cal.get(Calendar.HOUR_OF_DAY);
int min = cal.get(Calendar.MINUTE);
timeToSpeech(hour, min);
}
/**
* Closes things down
*/
public void close() {
try {
synthesizer.deallocate();
} catch (EngineException ee) {
System.out.println("Trouble deallocating synthesizer: " + ee);
}
}
public static void main(String[] args) {
/* List all the "time" domain voices, which are voices that
* are only capable of speaking the time of day.
*/
listAllVoices("time");
String voiceName = (args.length > 0)
? args[0]
: "alan";
System.out.println();
System.out.println("Using voice: " + voiceName);
try {
JTime jtime = new JTime(voiceName);
jtime.speakNow();
jtime.interactiveMode();
jtime.close();
}
catch (Exception e) {
e.printStackTrace();
}
System.exit(0);
}
}
|