2 * Portions Copyright 2001 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.
7 * See the file "license.terms" for information on usage and
8 * redistribution of this file, and for a DISCLAIMER OF ALL
11 package com.sun.speech.freetts;
13 import java.io.BufferedReader;
14 import java.io.IOException;
15 import java.io.InputStreamReader;
16 import java.util.Calendar;
17 import java.util.GregorianCalendar;
18 import java.util.logging.ConsoleHandler;
19 import java.util.logging.Handler;
20 import java.util.logging.Level;
21 import java.util.logging.Logger;
22 import java.util.regex.Pattern;
25 * Standalone utility that tells the time.
27 * Defaults to "alan" voice.
30 public class FreeTTSTime extends FreeTTS {
32 private final static String VERSION =
33 "FreeTTSTime Version 1.1, August 1, 2003";
38 public FreeTTSTime() {
39 super(VoiceManager.getInstance().getVoice("alan"));
45 * @param voice Voice to say time with
47 public FreeTTSTime(Voice voice) {
53 * Prints the usage message for FreeTTSTime.
55 public static void usage() {
56 System.out.println(VERSION);
57 System.out.println("Usage:");
58 System.out.println(" -dumpASCII file : dump the final wave to file");
59 System.out.println(" -dumpAudio file : dump audio to file ");
60 System.out.println(" -help : shows usage information");
61 System.out.println(" -detailedMetrics: turn on detailed metrics");
62 System.out.println(" -dumpRelations : dump the relations ");
63 System.out.println(" -dumpUtterance : dump the final utterance");
64 System.out.println(" -metrics : turn on metrics");
65 System.out.println(" -run name : sets the name of the run");
66 System.out.println(" -silent : don't say anything");
67 System.out.println(" -verbose : verbose output");
68 System.out.println(" -version : shows version number");
69 System.out.println(" -timeTest : runs a lengthy time test");
70 System.out.println(" -iter count : run for count iterations");
71 System.out.println(" -time XX:XX : speak the given time");
72 System.out.println(" -time now : speak the current time");
73 System.out.println(" -period secs : period of iter");
74 System.out.println(" -clockMode : tells time every 5 mins");
75 System.out.println(" -voice VOICE : " +
76 VoiceManager.getInstance().toString());
81 * Starts interactive mode on the given FreeTTSTime. Reads text
82 * from the console and gives it to FreeTTSTime to speak.
83 * terminates on end of file.
85 * @param freetts the engine that speaks
87 private static void interactiveMode(FreeTTSTime freetts) {
91 BufferedReader reader;
92 reader = new BufferedReader(
93 new InputStreamReader(System.in));
94 System.out.print("Enter time: ");
96 time = reader.readLine();
97 if ((time == null) || (time.length() == 0)
98 || time.equals("quit") ) {
102 freetts.getVoice().startBatch();
103 freetts.safeTimeToSpeech(time);
104 freetts.getVoice().endBatch();
107 } catch (IOException e) {
112 * Returns a phrase that conveys the exactness
115 * @param hour the hour of the time
116 * @param min the minute of the time
118 * @return a string phrase
120 private static String timeApprox(int hour, int min) {
125 if ((mm == 0) || (mm == 4)) {
127 } else if (mm == 1) {
129 } else if (mm == 2) {
130 return "a little after";
137 * Returns a phrase that conveys the minutes in relation
140 * @param hour the hour of the time
141 * @param min the minute of the time
143 * @return a string phrase.
145 private static String timeMin(int hour, int min) {
159 } else if (mm == 5) {
161 } else if (mm == 10) {
163 } else if (mm == 15) {
164 return "quarter past";
165 } else if (mm == 20) {
166 return "twenty past";
167 } else if (mm == 25) {
168 return "twenty-five past";
169 } else if (mm == 30) {
171 } else if (mm == 35) {
172 return "twenty-five to";
173 } else if (mm == 40) {
175 } else if (mm == 45) {
177 } else if (mm == 50) {
179 } else if (mm == 55) {
187 * Returns a phrase that conveys the hour in relation
190 * @param hour the hour of the time
191 * @param min the minute of the time
193 * @return a string phrase.
195 private static String timeHour(int hour, int min) {
199 if (min > 32) { // PBL: fixed from flite_time
211 } else if (hh == 1) {
213 } else if (hh == 2) {
215 } else if (hh == 3) {
217 } else if (hh == 4) {
219 } else if (hh == 5) {
221 } else if (hh == 6) {
223 } else if (hh == 7) {
225 } else if (hh == 8) {
227 } else if (hh == 9) {
229 } else if (hh == 10) {
231 } else if (hh == 11) {
233 } else if (hh == 12) {
241 * Returns a phrase that conveys the time of day.
243 * @param hour the hour of the time
244 * @param min the minute of the time
246 * @return a string phrase
248 private static String timeOfDay(int hour, int min) {
256 } else if (hh > 17) {
257 return "in the evening";
258 } else if (hh > 11) {
259 return "in the afternoon";
261 return "in the morning";
266 * Returns a string that corresponds to the given time.
268 * @param time the time in the form HH:MM
270 * @return the time in string, null if the given time is not in the
273 public static String timeToString(String time) {
274 String theTime = null;
275 if (Pattern.matches("[012][0-9]:[0-5][0-9]", time)) {
276 int hour = Integer.parseInt(time.substring(0, 2));
277 int min = Integer.parseInt(time.substring(3));
279 theTime = timeToString(hour, min);
285 * Returns a string that corresponds to the given time.
287 * @param hour the hour
288 * @param min the minutes
290 * @return the time in string, null if the given time out of range
292 public static String timeToString(int hour, int min) {
293 String theTime = "The time is now, " +
294 timeApprox(hour, min) + " " +
295 timeMin(hour, min) + " " +
296 timeHour(hour, min) + ", " +
297 timeOfDay(hour, min) + "." ;
303 * Speaks the given time. Time should be in the exact form
304 * HH:MM where HH is the hour 00 to 23, and MM is the minute 00 to
307 * @param time the time in the form HH:MM
309 * @throws IllegalArgumentException if time is not in the form
312 public void timeToSpeech(String time) {
313 String theTime = timeToString(time);
314 if (theTime != null) {
315 textToSpeech(theTime);
317 throw new IllegalArgumentException("Bad time format");
322 * Speaks the time given the hour and minute.
324 * @param hour the hour of the day (0 to 23)
325 * @param min the minute of the hour (0 to 59)
327 public void timeToSpeech(int hour, int min) {
328 if (hour < 0 || hour > 23) {
329 throw new IllegalArgumentException("Bad time format: hour");
332 if (min < 0 || min > 59) {
333 throw new IllegalArgumentException("Bad time format: min");
335 String theTime = timeToString(hour, min);
336 textToSpeech(theTime);
341 * Speaks the given time. Prints an error message if the time
344 * @param time the time in the form HH:MM
346 public void safeTimeToSpeech(String time) {
348 if (time.equals("now")) {
353 } catch (IllegalArgumentException iae) {
354 System.err.println("Bad time format");
359 * Tells the current time.
361 public void speakNow() {
362 Calendar cal = new GregorianCalendar();
363 int hour = cal.get(Calendar.HOUR_OF_DAY);
364 int min = cal.get(Calendar.MINUTE);
365 timeToSpeech(hour, min);
369 * The main entry point for FreeTTSTime.
371 public static void main(String[] args) {
373 String time = null; // default is interactive mode
377 boolean setMetrics = false;
378 boolean setDetailedMetrics = false;
379 boolean setVerbose = false;
380 boolean setDumpUtterance = false;
381 boolean setDumpRelations = false;
382 String waveDumpFile = null;
383 String runTitle = null;
385 boolean setSilentMode = false;
386 String audioFile = null;
387 boolean setInputMode = false;
389 String voiceName = null;
391 for (int i = 0; i < args.length; i++) {
392 if (args[i].equals("-metrics")) {
394 } else if (args[i].equals("-detailedMetrics")) {
395 setDetailedMetrics = true;
396 } else if (args[i].equals("-silent")) {
397 setSilentMode = true;
398 } else if (args[i].equals("-period")) {
399 if (++i < args.length) {
401 delay = Integer.parseInt(args[i]);
402 } catch (NumberFormatException nfe) {
403 System.out.println("Bad clock period");
407 } else if (args[i].equals("-verbose")) {
409 } else if (args[i].equals("-dumpUtterance")) {
410 setDumpUtterance = true;
411 } else if (args[i].equals("-dumpRelations")) {
412 setDumpRelations = true;
413 } else if (args[i].equals("-clockMode")) {
414 iterations = Integer.MAX_VALUE;
416 } else if (args[i].equals("-timeTest")) {
418 } else if (args[i].equals("-dumpAudio")) {
419 if (++i < args.length) {
424 } else if (args[i].equals("-iter")) {
425 if (++i < args.length) {
427 iterations = Integer.parseInt(args[i]);
428 } catch (NumberFormatException nfe) {
429 System.out.println("Bad iteration format");
435 } else if (args[i].equals("-dumpASCII")) {
436 if (++i < args.length) {
437 waveDumpFile = args[i];
441 } else if (args[i].equals("-version")) {
442 System.out.println(VERSION);
443 } else if (args[i].equals("-help")) {
445 } else if (args[i].equals("-time")) {
447 if (++i < args.length) {
452 } else if (args[i].equals("-run")) {
453 if (++i < args.length) {
458 } else if (args[i].equals("-voice")) {
459 if (++i < args.length) {
465 System.out.println("Unknown option:" + args[i]);
469 if (voiceName == null) {
473 FreeTTSTime freetts = new
474 FreeTTSTime(VoiceManager.getInstance().getVoice(voiceName));
475 Voice voice = freetts.getVoice();
478 voice.setMetrics(true);
481 if (setDetailedMetrics) {
482 voice.setDetailedMetrics(true);
486 Handler handler = new ConsoleHandler();
487 handler.setLevel(Level.ALL);
488 Logger.getLogger("com.sun").addHandler(handler);
489 Logger.getLogger("com.sun").setLevel(Level.ALL);
492 if (setDumpUtterance) {
493 voice.setDumpUtterance(true);
496 if (setDumpRelations) {
497 voice.setDumpRelations(true);
500 if (waveDumpFile != null) {
501 voice.setWaveDumpFile(waveDumpFile);
504 if (runTitle != null) {
505 voice.setRunTitle(runTitle);
509 freetts.setSilentMode(true);
512 if (audioFile != null) {
513 freetts.setAudioFile(audioFile);
517 freetts.setInputMode(InputMode.TEXT);
524 freetts.getVoice().startBatch();
525 for (int i = 0; i < iterations; i++) {
526 freetts.safeTimeToSpeech(time);
528 Thread.sleep(delay * 1000L);
529 } catch (InterruptedException ie) {
533 freetts.getVoice().endBatch();
535 interactiveMode(freetts);