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
8 package com.sun.speech.freetts.audio;
10 import com.sun.speech.freetts.util.Utilities;
12 import java.io.IOException;
13 import java.io.BufferedOutputStream;
14 import java.io.FileOutputStream;
15 import javax.sound.sampled.AudioFormat;
19 * Provides an implementation of <code>AudioPlayer</code> that sends
20 * all audio data to the given file.
22 public class RawFileAudioPlayer implements AudioPlayer {
24 private AudioFormat audioFormat;
26 private BufferedOutputStream os;
30 * Creates a default audio player for an AudioFileFormat of type
31 * WAVE. Reads the "com.sun.speech.freetts.AudioPlayer.baseName"
32 * property for the base filename to use, and will produce files
33 * of the form <baseName>.raw. The default value for the
34 * base name is "freetts".
36 public RawFileAudioPlayer() throws IOException {
37 this(Utilities.getProperty(
38 "com.sun.speech.freetts.AudioPlayer.baseName", "freetts")
43 * Constructs a NullAudioPlayer
45 public RawFileAudioPlayer(String path) throws IOException {
47 os = new BufferedOutputStream(new FileOutputStream(path));
52 * Sets the audio format for this player
54 * @param format the audio format
56 public void setAudioFormat(AudioFormat format) {
57 this.audioFormat = format;
61 * Retrieves the audio format for this player
63 * @return the current audio format.
65 public AudioFormat getAudioFormat() {
70 * Cancels all queued output. Current 'write' call will return
74 public void cancel() {
79 * Pauses the audio output
86 * Prepares for another batch of output. Larger groups of output
87 * (such as all output associated with a single FreeTTSSpeakable)
88 * should be grouped between a reset/drain pair.
95 * Resumes audio output
97 public void resume() {
104 * Waits for all audio playback to stop, and closes this AudioPlayer.
106 public void close() {
110 System.out.println("Wrote synthesized speech to " + path);
111 } catch (IOException ioe) {
112 ioe.printStackTrace();
118 * Returns the current volume.
120 * @return the current volume (between 0 and 1)
122 public float getVolume() {
127 * Sets the current volume.
129 * @param volume the current volume (between 0 and 1)
131 public void setVolume(float volume) {
132 this.volume = volume;
137 * Writes the given bytes to the audio stream
139 * @param audioData array of audio data
141 * @return <code>true</code> of the write completed successfully,
142 * <code> false </code>if the write was cancelled.
144 public boolean write(byte[] audioData) {
145 return write(audioData, 0, audioData.length);
150 * Starts the output of a set of data
152 * @param size the size of data between now and the end
155 public void begin(int size) {
159 * Marks the end of a set of data
162 public boolean end() {
167 * Writes the given bytes to the audio stream
169 * @param bytes audio data to write to the device
170 * @param offset the offset into the buffer
171 * @param size the size into the buffer
173 * @return <code>true</code> of the write completed successfully,
174 * <code> false </code>if the write was cancelled.
176 public boolean write(byte[] bytes, int offset, int size) {
178 os.write(bytes, offset, size);
179 } catch (IOException ioe) {
186 * Starts the first sample timer
188 public void startFirstSampleTimer() {
192 * Waits for all queued audio to be played
194 * @return <code>true</code> if the audio played to completion,
195 * <code> false </code>if the audio was stopped
197 public boolean drain() {
202 * Gets the amount of played since the last resetTime
203 * Currently not supported.
205 * @return the amount of audio in milliseconds
207 public long getTime() {
213 * Resets the audio clock
215 public void resetTime() {
219 * Shows metrics for this audio player
221 public void showMetrics() {