2 * Copyright 2003 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 java.io.IOException;
10 import java.net.ServerSocket;
11 import java.net.Socket;
13 import java.util.Locale;
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;
25 * Provides text-to-speech server for Emacspeak.
27 public class EmacspeakServer extends TTSServer {
29 // synthesizer related variables
30 private Synthesizer synthesizer;
31 private String voiceName;
36 * Constructs a EmacspeakServer.
38 public EmacspeakServer(String voiceName) {
39 loadSynthesizer(voiceName);
43 * Creates and loads the synthesizer.
45 private void loadSynthesizer(String voiceName) {
47 voice = new Voice(voiceName,
48 Voice.GENDER_DONT_CARE,
52 SynthesizerModeDesc modeDesc = new SynthesizerModeDesc(
53 null, "general", Locale.US, null, null);
56 synthesizer = Central.createSynthesizer(modeDesc);
58 if (synthesizer == null) {
59 System.err.println(noSynthesizerMessage());
63 synthesizer.allocate();
65 synthesizer.getSynthesizerProperties().setVolume(1.0f);
66 synthesizer.getSynthesizerProperties().setVoice(voice);
67 } catch (Exception e) {
68 System.out.println("Error creating synthesizer");
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>.
79 * @return a no synthesizer message
81 static private String noSynthesizerMessage() {
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";
100 * Spawns a ProtocolHandler depending on the current protocol.
102 * @param socket the socket that the spawned protocol handler will use
104 protected void spawnProtocolHandler(Socket socket) {
106 JSAPIEmacspeakHandler handler =
107 new JSAPIEmacspeakHandler(socket, synthesizer);
108 (new Thread(handler)).start();
109 } catch (Exception e) {
116 * Sets the speaking rate of the voice.
118 * @param wpm the speaking rate (words per minute)
120 public void setRate(float wpm) {
122 synthesizer.getSynthesizerProperties().setSpeakingRate(wpm);
123 } catch (java.beans.PropertyVetoException e) {
124 // ignore and do nothing
130 * Starts this TTS Server.
132 public static void main(String[] args) {
133 String voiceName = (args.length > 0)
137 System.out.println();
138 System.out.println("Using voice: " + voiceName);
139 System.out.println();
141 EmacspeakServer server = new EmacspeakServer(voiceName);
143 if (args.length > 1) {
144 float wpm = Float.parseFloat(args[1]);
148 (new Thread(server)).start();