2 * Copyright 2001 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 com.sun.speech.freetts.Voice;
11 import com.sun.speech.freetts.util.Utilities;
12 import java.net.Socket;
13 import java.util.Vector;
17 * Implements a simplified version of the Emacspeak speech server.
19 public class FreeTTSEmacspeakHandler extends EmacspeakProtocolHandler {
21 private SpeakCommandHandler speakCommandHandler;
25 * Constructs a Emacspeak ProtocolHandler
27 * @param freetts the FreeTTS that this FreeTTSEmacspeakHandler belongs
28 * @param socket the Socket that holds the TCP connection
30 public FreeTTSEmacspeakHandler(Socket socket, Voice voice) {
32 this.speakCommandHandler = new SpeakCommandHandler(voice);
33 this.speakCommandHandler.start();
34 setDebug(Utilities.getBoolean("debug"));
39 * Speaks the given input text.
41 * @param input the input text to speak.
43 public void speak(String input) {
45 String[] parts = input.split(PARENS_STAR_REGEX);
46 for (int i = 0; i < parts.length; i++) {
47 speakCommandHandler.add(parts[i]);
53 * Removes all the queued text.
55 public void cancelAll() {
56 speakCommandHandler.removeAll();
61 * Sets the speaking rate.
63 * @param wpm the new speaking rate (words per minute)
65 public void setRate(float wpm) {
66 speakCommandHandler.setRate(wpm);
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.
75 class SpeakCommandHandler extends Thread {
78 private boolean done = false;
79 private Vector commandList = new Vector();
83 * Constructs a default SpeakCommandHandler object.
85 * @param voice the Voice object use to speak
87 public SpeakCommandHandler(Voice voice) {
93 * Implements the run() method of the Thread class.
96 while (!getSocket().isClosed() || commandList.size() > 0) {
97 Object firstCommand = null;
98 synchronized (commandList) {
99 while (commandList.size() == 0 &&
100 !getSocket().isClosed()) {
103 } catch (InterruptedException ie) {
104 ie.printStackTrace();
107 if (commandList.size() > 0) {
108 firstCommand = commandList.remove(0);
111 if (firstCommand != null) {
112 voice.speak((String) firstCommand);
113 debugPrintln("SPEAK: \"" + firstCommand + "\"");
116 debugPrintln("SpeakCommandHandler: thread terminated");
121 * Adds the given command to this Handler.
123 * @param command the text to be spoken
125 public void add(String command) {
126 synchronized (commandList) {
127 commandList.add(command);
128 commandList.notifyAll();
134 * Removes all the commands from this Handler.
136 public void removeAll() {
137 synchronized (commandList) {
138 voice.getAudioPlayer().cancel();
139 commandList.removeAllElements();
145 * Sets the speaking rate.
147 * @param wpm the new speaking rate (words per minute)
149 public void setRate(float wpm) {
155 * Terminates this SpeakCommandHandler thread.
157 * @param done true to terminate this thread
159 public synchronized void setDone(boolean done) {