upstream version 1.2.2
[debian/freetts] / demo / JSAPI / Emacspeak / EmacspeakServer.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 import java.io.File;
9 import java.io.IOException;
10 import java.net.ServerSocket;
11 import java.net.Socket;
12
13 import java.util.Locale;
14
15 import javax.speech.Central;
16 import javax.speech.Engine;
17 import javax.speech.EngineException;
18 import javax.speech.synthesis.Synthesizer;
19 import javax.speech.synthesis.SynthesizerModeDesc;
20 import javax.speech.synthesis.SynthesizerProperties;
21 import javax.speech.synthesis.Voice;
22
23
24 /**
25  * Provides text-to-speech server for Emacspeak.
26  */
27 public class EmacspeakServer extends TTSServer {
28
29     // synthesizer related variables
30     private Synthesizer synthesizer;
31     private String voiceName;
32     private Voice voice;
33
34
35     /**
36      * Constructs a EmacspeakServer.
37      */
38     public EmacspeakServer(String voiceName) {
39         loadSynthesizer(voiceName);
40     }
41
42     /**
43      * Creates and loads the synthesizer.
44      */
45     private void loadSynthesizer(String voiceName) {
46
47         voice = new Voice(voiceName, 
48                           Voice.GENDER_DONT_CARE, 
49                           Voice.AGE_DONT_CARE, 
50                           null);
51
52         SynthesizerModeDesc modeDesc = new SynthesizerModeDesc(
53             null, "general", Locale.US, null, null);
54
55         try {
56             synthesizer = Central.createSynthesizer(modeDesc);
57
58             if (synthesizer == null) {
59                 System.err.println(noSynthesizerMessage());
60                 System.exit(1);
61             }
62
63             synthesizer.allocate();
64             synthesizer.resume();
65             synthesizer.getSynthesizerProperties().setVolume(1.0f);
66             synthesizer.getSynthesizerProperties().setVoice(voice);
67         } catch (Exception e) {
68             System.out.println("Error creating synthesizer");
69             System.exit(1);
70         }
71     }
72
73
74     /**
75      * Returns a "no synthesizer" message, and asks 
76      * the user to check if the "speech.properties" file is
77      * at <code>user.home</code> or <code>java.home/lib</code>.
78      *
79      * @return a no synthesizer message
80      */
81     static private String noSynthesizerMessage() {
82         String message =
83             "No synthesizer created.  This may be the result of any\n" +
84             "number of problems.  It's typically due to a missing\n" +
85             "\"speech.properties\" file that should be at either of\n" +
86             "these locations: \n\n";
87         message += "user.home    : " + System.getProperty("user.home") + "\n";
88         message += "java.home/lib: " + System.getProperty("java.home") +
89             File.separator + "lib\n\n" +
90             "Another cause of this problem might be corrupt or missing\n" +
91             "voice jar files in the freetts lib directory.  This problem\n" +
92             "also sometimes arises when the freetts.jar file is corrupt\n" +
93             "or missing.  Sorry about that.  Please check for these\n" +
94             "various conditions and then try again.\n";
95         return message;
96     }
97
98
99     /**
100      * Spawns a ProtocolHandler depending on the current protocol.
101      *
102      * @param socket the socket that the spawned protocol handler will use
103      */
104     protected void spawnProtocolHandler(Socket socket) {
105         try {
106             JSAPIEmacspeakHandler handler =
107                 new JSAPIEmacspeakHandler(socket, synthesizer);
108             (new Thread(handler)).start();
109         } catch (Exception e) {
110             e.printStackTrace();
111         }
112     }
113
114
115     /**
116      * Sets the speaking rate of the voice.
117      *
118      * @param wpm the speaking rate (words per minute)
119      */
120     public void setRate(float wpm) {
121         try {
122             synthesizer.getSynthesizerProperties().setSpeakingRate(wpm);
123         } catch (java.beans.PropertyVetoException e) {
124             // ignore and do nothing
125         }
126     }
127     
128         
129     /**
130      * Starts this TTS Server.
131      */
132     public static void main(String[] args) {
133         String voiceName = (args.length > 0)
134             ? args[0]
135             : "kevin16";
136         
137         System.out.println();
138         System.out.println("Using voice: " + voiceName);
139         System.out.println();
140
141         EmacspeakServer server = new EmacspeakServer(voiceName);
142
143         if (args.length > 1) {
144             float wpm = Float.parseFloat(args[1]);
145             server.setRate(wpm);
146         }
147         
148         (new Thread(server)).start();
149     }
150 }