upstream version 1.2.2
[debian/freetts] / demo / freetts / Emacspeak / FreeTTSEmacspeakHandler.java
1 /**
2  * Copyright 2001 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.Voice;
10
11 import com.sun.speech.freetts.util.Utilities;
12 import java.net.Socket;
13 import java.util.Vector;
14
15
16 /**
17  * Implements a simplified version of the Emacspeak speech server.
18  */
19 public class FreeTTSEmacspeakHandler extends EmacspeakProtocolHandler {
20
21     private SpeakCommandHandler speakCommandHandler;
22
23
24     /**
25      * Constructs a Emacspeak ProtocolHandler
26      *
27      * @param freetts the FreeTTS that this FreeTTSEmacspeakHandler belongs
28      * @param socket the Socket that holds the TCP connection
29      */
30     public FreeTTSEmacspeakHandler(Socket socket, Voice voice) {
31         setSocket(socket);
32         this.speakCommandHandler = new SpeakCommandHandler(voice);
33         this.speakCommandHandler.start();
34         setDebug(Utilities.getBoolean("debug"));
35     }
36
37
38     /**
39      * Speaks the given input text.
40      *
41      * @param input the input text to speak.
42      */
43     public void speak(String input) {
44         // split around "[*]"
45         String[] parts = input.split(PARENS_STAR_REGEX);
46         for (int i = 0; i < parts.length; i++) {
47             speakCommandHandler.add(parts[i]);
48         }
49     }
50
51
52     /**
53      * Removes all the queued text.
54      */
55     public void cancelAll() {
56         speakCommandHandler.removeAll();
57     }
58
59
60     /**
61      * Sets the speaking rate.
62      *
63      * @param wpm the new speaking rate (words per minute)
64      */
65     public void setRate(float wpm) {
66         speakCommandHandler.setRate(wpm);
67     }
68     
69     
70     /**
71      * This thread is used to separate the handling of Voice.speak() from
72      * the thread that accepts commands from the client, so that the 
73      * latter won't be blocked by the former.
74      */
75     class SpeakCommandHandler extends Thread {
76         
77         private Voice voice;
78         private boolean done = false;
79         private Vector commandList = new Vector();
80         
81         
82         /**
83          * Constructs a default SpeakCommandHandler object.
84          *
85          * @param voice the Voice object use to speak
86          */
87         public SpeakCommandHandler(Voice voice) {
88             this.voice = voice;
89         }
90         
91         
92         /**
93          * Implements the run() method of the Thread class.
94          */
95         public void run() {
96             while (!getSocket().isClosed() || commandList.size() > 0) {
97                 Object firstCommand = null;
98                 synchronized (commandList) {
99                     while (commandList.size() == 0 &&
100                            !getSocket().isClosed()) {
101                         try {
102                             commandList.wait();
103                         } catch (InterruptedException ie) {
104                             ie.printStackTrace();
105                         }
106                     }
107                     if (commandList.size() > 0) {
108                         firstCommand = commandList.remove(0);
109                     }
110                 }
111                 if (firstCommand != null) {
112                     voice.speak((String) firstCommand);
113                     debugPrintln("SPEAK: \"" + firstCommand + "\"");
114                 }
115             }
116             debugPrintln("SpeakCommandHandler: thread terminated");
117         }
118         
119         
120         /**
121          * Adds the given command to this Handler.
122          *
123          * @param command the text to be spoken
124          */
125         public void add(String command) {
126             synchronized (commandList) {
127                 commandList.add(command);
128                 commandList.notifyAll();
129             }
130         }
131     
132     
133         /**
134          * Removes all the commands from this Handler.
135          */
136         public void removeAll() {
137             synchronized (commandList) {
138                 voice.getAudioPlayer().cancel();
139                 commandList.removeAllElements();
140             }
141         }
142         
143         
144         /**
145          * Sets the speaking rate.
146          *
147          * @param wpm the new speaking rate (words per minute)
148          */
149         public void setRate(float wpm) {
150             voice.setRate(wpm);
151         }
152         
153         
154         /**
155          * Terminates this SpeakCommandHandler thread.
156          *
157          * @param done true to terminate this thread
158          */
159         public synchronized void setDone(boolean done) {
160             this.done = done;
161         }
162     }
163 }