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
9 import com.sun.speech.freetts.audio.AudioPlayer;
10 import com.sun.speech.freetts.util.Utilities;
12 import java.io.DataOutputStream;
13 import java.io.IOException;
15 import java.net.Socket;
17 import javax.sound.sampled.AudioFormat;
21 * Implements the AudioPlayer for the freetts Client/Server demo.
22 * This SocketAudioPlayer basically sends synthesized wave bytes to the
25 public class SocketAudioPlayer implements AudioPlayer {
27 private AudioFormat audioFormat;
28 private Socket socket;
29 private DataOutputStream dataOutputStream;
30 private boolean debug = false;
31 private int bytesToPlay = 0;
32 private int bytesPlayed = 0;
33 private boolean firstByteSent = false;
34 private long firstByteTime = -1;
38 * Constructs a SocketAudioPlayer that will send wave bytes to the
41 * @param socket the Socket to which synthesized wave bytes will be sent
43 public SocketAudioPlayer(Socket socket) {
46 this.dataOutputStream = new DataOutputStream
47 (socket.getOutputStream());
48 debug = Utilities.getBoolean("debug");
49 } catch (IOException ioe) {
50 ioe.printStackTrace();
56 * Sets the audio format to use for the next set of outputs. Since
57 * an audio player can be shared by a number of voices, and since
58 * voices can have different AudioFormats (sample rates for
59 * example), it is necessary to allow clients to dynamically set
60 * the audio format for the player.
62 * @param format the audio format
64 public void setAudioFormat(AudioFormat format) {
65 this.audioFormat = format;
70 * Retrieves the audio format for this player
72 * @return the current audio format
75 public AudioFormat getAudioFormat() {
76 return this.audioFormat;
81 * Pauses all audio output on this player. Play can be resumed
82 * with a call to resume. Not implemented in this Player.
84 public void pause() {}
88 * Resumes audio output on this player. Not implemented in this Player.
90 public void resume() {}
94 * Prepares for another batch of output. Larger groups of output
95 * (such as all output associated with a single FreeTTSSpeakable)
96 * should be grouped between a reset/drain pair.
98 public void reset() {}
102 * Flushes all the audio data to the Socket.
104 * @return <code>true</code> all the time
106 public boolean drain() {
108 dataOutputStream.flush();
109 } catch (IOException ioe) {
110 ioe.printStackTrace();
117 * Starts the output of a set of data. Audio data for a single
118 * utterance should be grouped between begin/end pairs.
120 * @param size the size of data in bytes to be output before
121 * <code>end</code> is called.
123 public void begin(int size) {
126 firstByteSent = false;
127 dataOutputStream.writeBytes(String.valueOf(size) + "\n");
128 dataOutputStream.flush();
130 System.out.println("begin: " + size);
132 } catch (IOException ioe) {
133 ioe.printStackTrace();
139 * Starts the first sample timer (none in this player)
141 public void startFirstSampleTimer() {
146 * Signals the end of a set of data. Audio data for a single
147 * utterance should be groupd between <code> begin/end </code> pairs.
149 * @return <code>true</code> if the audio was output properly,
150 * <code> false</code> if the output was cancelled
154 public boolean end() {
156 System.out.println("end");
158 if (bytesPlayed < bytesToPlay) {
159 int bytesNotPlayed = bytesToPlay - bytesPlayed;
160 write(new byte[bytesNotPlayed], 0, bytesNotPlayed);
170 * Cancels all queued output. All 'write' calls until the next
171 * reset will return false. Not implemented in this Player.
174 public void cancel() {}
178 * Waits for all audio playback to stop, and closes this AudioPlayer.
179 * Not implemented in this Player.
181 public void close() {}
185 * Returns the current volume. The volume is specified as a number
186 * between 0.0 and 1.0, where 1.0 is the maximum volume and 0.0 is
187 * the minimum volume. Not implemented in this Player.
189 * @return the current volume (between 0 and 1)
191 public float getVolume() {
197 * Sets the current volume. The volume is specified as a number
198 * between 0.0 and 1.0, where 1.0 is the maximum volume and 0.0 is
199 * the minimum volume. Not implemented in this Player.
201 * @param volume the new volume (between 0 and 1)
203 public void setVolume(float volume) {}
207 * Gets the amount of audio played since the last resetTime.
208 * Not implemented in this Player.
210 * @returns the amount of audio in milliseconds
212 public long getTime() {
218 * Resets the audio clock. Not implemented in this Player.
220 public void resetTime() {}
224 * Writes the given bytes to the audio stream
226 * @param audioData audio data to write to the device
228 * @return <code>true</code> of the write completed successfully,
229 * <code> false </code>if the write was cancelled.
231 public boolean write(byte[] audioData) {
232 return write(audioData, 0, audioData.length);
237 * Writes the given bytes to the audio stream
239 * @param audioData audio data to write to the device
240 * @param offset the offset into the buffer
241 * @param size the number of bytes to write.
243 * @return <code>true</code> of the write completed successfully,
244 * <code> false </code>if the write was cancelled.
246 public boolean write(byte[] audioData, int offset, int size) {
248 if (!firstByteSent) {
249 firstByteTime = System.currentTimeMillis();
250 firstByteSent = true;
254 dataOutputStream.write(audioData, offset, size);
255 dataOutputStream.flush();
258 System.out.println("sent " + size + " bytes " +
259 audioData[0] + " " + audioData[size/2]);
262 } catch (IOException ioe) {
263 ioe.printStackTrace();
270 * Shows metrics for this audio player. Not implemented in this Player.
272 public void showMetrics() {}
276 * Returns the first byte sent time in milliseconds, the last time it
279 * @return the last first byte sent time in milliseconds
281 public long getFirstByteSentTime() {
282 return firstByteTime;