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
10 import com.sun.speech.freetts.audio.AudioPlayer;
11 import com.sun.speech.freetts.audio.JavaStreamingAudioPlayer;
12 import com.sun.speech.freetts.util.Utilities;
14 import java.io.BufferedReader;
15 import java.io.DataInputStream;
16 import java.io.InputStreamReader;
17 import java.io.IOException;
18 import java.io.PrintWriter;
20 import java.net.Socket;
22 import javax.sound.sampled.AudioFormat;
26 * Implements a Java Client for the Client/Server demo. For details about
27 * the protocol between client and server, consult the file
28 * <code>Protocol.txt</code>.
32 private String serverAddress = Utilities.getProperty("server", "localhost");
33 private int serverPort = Utilities.getInteger("port", 5555).intValue();
35 private static final int AUDIO_BUFFER_SIZE = 256;
37 private boolean debug = Utilities.getBoolean("debug");
39 private BufferedReader systemInReader; // for reading user input text
40 private BufferedReader reader;
41 private DataInputStream dataReader; // for reading raw bytes
42 private PrintWriter writer;
43 private AudioPlayer audioPlayer;
44 private int sampleRate = Utilities.getInteger("sampleRate", 16000).intValue();
45 private int sampleSize = 16; // in bits
46 private byte[] socketBuffer = new byte[AUDIO_BUFFER_SIZE];
49 private boolean metrics = Boolean.getBoolean("metrics");
50 private long sendTime; // time the text is sent to server
51 private long receiveTime; // time the first byte is received
52 private long firstSoundTime; // time the first play to audio
53 private boolean firstByteReceived = false;
56 private static final String FIRST_SENTENCE =
57 "Type in what you want me to say.";
61 * Constructs a default Client. It connects to the speech server, and
62 * constructs an AudioPlayer.
66 System.out.println("Error connecting to " + serverAddress +
70 this.audioPlayer = new JavaStreamingAudioPlayer();
71 this.audioPlayer.setAudioFormat
72 (new AudioFormat(sampleRate, sampleSize, 1, true, true));
77 * Connects this client to the server.
79 * @return <code>true</code> if successfully connected
80 * <code>false</code> if failed to connect
82 private boolean connect() {
84 Socket socket = new Socket(serverAddress, serverPort);
85 dataReader = new DataInputStream(socket.getInputStream());
86 systemInReader = new BufferedReader
87 (new InputStreamReader(System.in));
88 writer = new PrintWriter(socket.getOutputStream(), true);
90 } catch (IOException ioe) {
91 ioe.printStackTrace();
98 * Reads a line of text from the Socket.
100 * @return a line of text without the end of line character
102 private String readLine() throws IOException {
105 StringBuffer buffer = new StringBuffer();
107 while ((c = (char) dataReader.readByte()) != '\n') {
109 System.out.println(c);
114 int lastCharIndex = buffer.length() - 1;
116 // remove trailing ^M for Windows-based machines
117 byte lastByte = (byte) buffer.charAt(lastCharIndex);
118 if (lastByte == 13) {
119 return buffer.substring(0, lastCharIndex);
121 return buffer.toString();
127 * Sends the given line of text to the Socket, appending an end of
128 * line character to the end.
130 * @param the line of text to send
132 private void sendLine(String line) {
134 System.out.println(line);
137 if (line.length() > 0) {
146 * Run the TTS protocol.
148 public void runTTSProtocol() {
150 String readyLine = readLine();
151 if (readyLine.equals("READY")) {
152 if (!sendTTSRequest(FIRST_SENTENCE)) {
155 System.out.print("Say : ");
157 while ((input = systemInReader.readLine()) != null) {
158 if (input.length() > 0 && !sendTTSRequest(input)) {
161 System.out.print("Say : ");
169 System.out.println("ALL DONE");
171 } catch (Exception e) {
178 * Sends a TTS request on the given text.
180 * @param text the text to do TTS on
182 * @return <code>true</code> if the TTS transaction was successful
183 * <code>false</code> if an error occurred
185 private boolean sendTTSRequest(String text) {
188 sendTime = System.currentTimeMillis();
189 firstByteReceived = false;
192 // send TTS request to server
194 String.valueOf(sampleRate) + "\n" +
198 String numberSamplesStr = null;
199 int numberSamples = 0;
203 numberSamplesStr = readLine();
204 numberSamples = Integer.parseInt(numberSamplesStr);
206 if (numberSamples == -2) { // error
207 System.err.println("Client.sendTTSRequest(): error!");
210 if (numberSamples > 0) {
212 ("Receiving : " + numberSamples + " samples");
213 receiveAndPlay(numberSamples);
215 } catch (IOException ioe) {
216 ioe.printStackTrace();
219 while (numberSamples > 0);
222 System.out.println("FirstByte : " +
223 (receiveTime - sendTime) + " ms");
231 * Reads the given number of bytes from the socket, and plays them
232 * with the AudioPlayer.
234 * @param numberSamples the number of bytes to read from the socket
236 private void receiveAndPlay(int numberSamples) {
241 bytesRemaining = numberSamples;
243 audioPlayer.begin(0);
245 while (bytesRemaining > 0) {
247 // how many more bytes do we have to read?
248 if (bytesRemaining >= AUDIO_BUFFER_SIZE) {
249 bytesToRead = AUDIO_BUFFER_SIZE;
251 bytesToRead = bytesRemaining;
255 // we want to fill the socketBuffer completely before playing
258 int read = dataReader.read
259 (socketBuffer, nRead, bytesToRead);
261 if (metrics && !firstByteReceived) {
262 receiveTime = System.currentTimeMillis();
267 while (bytesToRead > 0);
270 System.err.println("error reading samples");
272 bytesRemaining -= nRead;
274 if (metrics && !firstByteReceived) {
275 firstSoundTime = System.currentTimeMillis();
276 firstByteReceived = true;
278 audioPlayer.write(socketBuffer, 0, nRead);
280 } catch (IOException ioe) {
281 ioe.printStackTrace();
285 System.out.println("BytesRemaining: " + bytesRemaining);
292 System.out.println("finished");
298 * Main program to run the client.
300 public static void main(String[] argv) {
301 Client client = new Client();
302 client.runTTSProtocol();