upstream version 1.2.2
[debian/freetts] / demo / JSAPI / Emacspeak / JSAPIEmacspeakHandler.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 java.net.Socket;
10
11 import javax.speech.synthesis.Synthesizer;
12 import com.sun.speech.freetts.util.Utilities;
13
14
15 /**
16  * Implements a very simplified version of the Emacspeak speech server.
17  */
18 public class JSAPIEmacspeakHandler extends EmacspeakProtocolHandler {
19
20     // synthesizer related variables
21     private Synthesizer synthesizer;
22     
23
24     /**
25      * Constructs a JSAPIEmacspeakHandler.
26      *
27      * @param socket the Socket that holds the TCP connection
28      * @param synthesizer the JSAPI synthesizer to use
29      */
30     public JSAPIEmacspeakHandler(Socket socket, Synthesizer synthesizer) {
31         setSocket(socket);
32         this.synthesizer = synthesizer;
33         setDebug(Utilities.getBoolean("debug"));
34     }
35
36
37     /**
38      * Speaks the given input text.
39      *
40      * @param input the input text to speak.
41      */
42     public void speak(String input) {
43         // split around "[*]"
44         String[] parts = input.split(PARENS_STAR_REGEX);
45         for (int i = 0; i < parts.length; i++) {
46             debugPrintln(parts[i]);
47             synthesizer.speakPlainText(parts[i], null);
48         }
49     }
50
51
52     /**
53      * Removes all the queued text.
54      */
55     public void cancelAll() {
56         synthesizer.cancelAll();
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         try {
67             synthesizer.getSynthesizerProperties().setSpeakingRate(wpm);
68         } catch (java.beans.PropertyVetoException e) {
69             // ignore and do nothing
70         }
71     }
72 }