upstream version 1.2.2
[debian/freetts] / demo / JSAPI / WebStartClock / JSAPIClock.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
9 import com.sun.speech.freetts.jsapi.FreeTTSEngineCentral; 
10
11 import java.util.Locale;
12
13 import javax.speech.EngineList; 
14 import javax.speech.EngineCreate; 
15 import javax.speech.synthesis.Synthesizer;
16 import javax.speech.synthesis.SynthesizerModeDesc;
17
18 /**
19  * A talking clock powered by FreeTTS.
20  */
21 public class JSAPIClock extends Clock {
22
23     protected Synthesizer synthesizer;
24
25
26     /**
27      * Creates the synthesizer, called by the constructor.
28      */
29     public void createSynthesizer() {
30
31         try {
32             SynthesizerModeDesc desc = 
33                 new SynthesizerModeDesc(null, 
34                                         "time",
35                                         Locale.US, 
36                                         Boolean.FALSE,
37                                         null);
38
39             FreeTTSEngineCentral central = new FreeTTSEngineCentral();
40             EngineList list = central.createEngineList(desc); 
41             
42             if (list.size() > 0) { 
43                 EngineCreate creator = (EngineCreate) list.get(0); 
44                 synthesizer = (Synthesizer) creator.createEngine(); 
45             } 
46             if (synthesizer == null) {
47                 System.err.println("Cannot create synthesizer");
48                 System.exit(1);
49             }
50             synthesizer.allocate();
51             synthesizer.resume();
52
53         } catch (Exception e) {
54             e.printStackTrace();
55         }
56     }
57
58
59     /**
60      * Speaks the given time in full text.
61      *
62      * @param time time in full text
63      */
64     protected void speak(String time) {
65         synthesizer.speakPlainText(time, null);
66     }
67
68
69     /**
70      * main() method to run the JSAPIClock.
71      */
72     public static void main(String args[]) {
73         Clock frame = new JSAPIClock();
74         frame.pack();
75         frame.setVisible(true);
76         frame.createSynthesizer();
77         frame.startClock();
78     }
79 }