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 java.io.ByteArrayInputStream;
12 import java.io.IOException;
14 import javax.sound.sampled.AudioFileFormat;
15 import javax.sound.sampled.AudioFormat;
16 import javax.sound.sampled.AudioInputStream;
17 import javax.sound.sampled.AudioSystem;
19 import com.sun.speech.freetts.util.Utilities;
22 * Streams audio to multiple files, one per utterance.
26 public class MultiFileAudioPlayer implements AudioPlayer {
27 private AudioFormat currentFormat = null;
28 private int fileCount = 0;
29 private String baseName;
30 private byte[] outputData;
31 private int curIndex = 0;
32 private AudioFileFormat.Type outputType;
35 * Creates a default audio player for an AudioFileFormat of type
36 * WAVE. Reads the "com.sun.speech.freetts.AudioPlayer.baseName"
37 * property for the base filename to use, and will produce files
38 * of the form <baseName>1.wav. The default value for the
39 * base name is "freetts".
41 public MultiFileAudioPlayer() {
42 this(Utilities.getProperty(
43 "com.sun.speech.freetts.AudioPlayer.baseName", "freetts"),
44 AudioFileFormat.Type.WAVE);
48 * Constructs a MultiFileAudioPlayer
50 * @param baseName the base name of the audio file
51 * @param type the type of audio output
54 public MultiFileAudioPlayer(String baseName, AudioFileFormat.Type type) {
55 this.baseName = baseName;
56 this.outputType = type;
60 * Sets the audio format for this player
62 * @param format the audio format
64 * @throws UnsupportedOperationException if the line cannot be opened with
67 public synchronized void setAudioFormat(AudioFormat format) {
68 currentFormat = format;
73 * Gets the audio format for this player
75 * @return format the audio format
77 public AudioFormat getAudioFormat() {
89 * Resumes audio output
91 public synchronized void resume() {
95 * Starts the first sample timer
97 public void startFirstSampleTimer() {
102 * Cancels currently playing audio
104 public synchronized void cancel() {
108 * Prepares for another batch of output. Larger groups of output
109 * (such as all output associated with a single FreeTTSSpeakable)
110 * should be grouped between a reset/drain pair.
112 public synchronized void reset() {
116 * Closes this audio player
118 public synchronized void close() {
122 * Returns the current volume.
124 * @return the current volume (between 0 and 1)
126 public float getVolume() {
131 * Sets the current volume.
133 * @param volume the current volume (between 0 and 1)
135 public void setVolume(float volume) {
142 * Starts the output of a set of data. Audio data for a single
143 * utterance should be grouped between begin/end pairs.
145 * @param size the size of data between now and the end
147 public void begin(int size) {
148 outputData = new byte[size];
153 * Marks the end of a set of data. Audio data for a single
154 * utterance should be groupd between begin/end pairs.
156 * @return true if the audio was output properly, false if the
157 * output was cancelled or interrupted.
160 public boolean end() {
161 ByteArrayInputStream bais = new ByteArrayInputStream(outputData);
162 AudioInputStream ais = new AudioInputStream(bais,
163 currentFormat, outputData.length/currentFormat.getFrameSize());
164 String name = baseName;
165 name = name + fileCount;
166 name = name + "." + outputType.getExtension();
167 File file = new File(name);
169 AudioSystem.write(ais, outputType, file);
170 System.out.println("Wrote synthesized speech to " + name);
171 } catch (IOException ioe) {
172 System.err.println("Can't write audio to " + file);
174 } catch (IllegalArgumentException iae) {
175 System.err.println("Can't write audio type " + outputType);
184 * Waits for all queued audio to be played
186 * @return true if the audio played to completion, false if
187 * the audio was stopped
189 public boolean drain() {
194 * Gets the amount of played since the last mark
196 * @return the amount of audio in milliseconds
198 public synchronized long getTime() {
204 * Resets the audio clock
206 public synchronized void resetTime() {
212 * Writes the given bytes to the audio stream
214 * @param audioData audio data to write to the device
216 * @return <code>true</code> of the write completed successfully,
217 * <code> false </code>if the write was cancelled.
219 public boolean write(byte[] audioData) {
220 return write(audioData, 0, audioData.length);
224 * Writes the given bytes to the audio stream
226 * @param bytes audio data to write to the device
227 * @param offset the offset into the buffer
228 * @param size the size into the buffer
230 * @return <code>true</code> of the write completed successfully,
231 * <code> false </code>if the write was cancelled.
233 public boolean write(byte[] bytes, int offset, int size) {
234 System.arraycopy(bytes, offset, outputData, curIndex, size);
240 * Returns the name of this audioplayer
242 * @return the name of the audio player
244 public String toString() {
245 return "FileAudioPlayer";
250 * Shows metrics for this audio player
252 public void showMetrics() {