upstream version 1.2.2
[debian/freetts] / demo / freetts / Emacspeak / FreeTTSEmacspeakServer.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
9 import com.sun.speech.freetts.ValidationException;
10 import com.sun.speech.freetts.Validator;
11 import com.sun.speech.freetts.Voice;
12 import com.sun.speech.freetts.VoiceManager;
13 import com.sun.speech.freetts.audio.JavaClipAudioPlayer;
14 import com.sun.speech.freetts.audio.JavaStreamingAudioPlayer;
15
16 import java.net.Socket;
17
18 /**
19  * Provides text-to-speech server for Emacspeak.
20  */
21 public class FreeTTSEmacspeakServer extends TTSServer {
22
23     private Voice emacsVoice;
24     
25     /**
26      * Constructs a EmacspeakServer.
27      */
28     public FreeTTSEmacspeakServer(String voiceName) {
29         System.setProperty
30             ("com.sun.speech.freetts.audio.AudioPlayer.cancelDelay", "0");
31         createVoice(voiceName);
32     }
33
34
35     /**
36      * Creates and loads the Voice.
37      */
38     private void createVoice(String voiceName) {
39         VoiceManager voiceManager = VoiceManager.getInstance();
40         emacsVoice = voiceManager.getVoice(voiceName);
41         if (emacsVoice == null) {
42             System.err.println("No such voice with the name: " + voiceName);
43             System.exit(1);
44         }
45         emacsVoice.allocate();
46     }
47
48
49     /**
50      * Spawns a ProtocolHandler depending on the current protocol.
51      * This method is inherited from TTSServer.
52      *
53      * @param socket the socket that the spawned protocol handler will use
54      */
55     protected void spawnProtocolHandler(Socket socket) {
56         try {
57             FreeTTSEmacspeakHandler handler =
58                 new FreeTTSEmacspeakHandler(socket, emacsVoice);
59             (new Thread(handler)).start();
60         } catch (Exception e) {
61             e.printStackTrace();
62         }
63     }
64
65
66     /**
67      * Sets the speaking rate of the voice.
68      *
69      * @param wpm the speaking rate (words per minute)
70      */
71     public void setRate(float wpm) {
72         emacsVoice.setRate(wpm);
73     }
74     
75         
76     /**
77      * Starts this TTS Server.
78      *
79      * Usage: FreeTTSEmacspeakServer [voicename [speaking rate]]
80      */
81     public static void main(String[] args) {
82         String voiceName = (args.length > 0)
83             ? args[0]
84             : "kevin16";
85         
86         System.out.println();
87         System.out.println("Using voice: " + voiceName);
88         System.out.println();
89
90         FreeTTSEmacspeakServer server = new FreeTTSEmacspeakServer(voiceName);
91
92         if (args.length > 1) {
93             float wpm = Float.parseFloat(args[1]);
94             server.setRate(wpm);
95         }
96         
97         (new Thread(server)).start();
98     }
99 }