upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / audio / MultiFileAudioPlayer.java
1 /**
2  * Copyright 2001 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 package com.sun.speech.freetts.audio;
9
10 import java.io.ByteArrayInputStream;
11 import java.io.File;
12 import java.io.IOException;
13
14 import javax.sound.sampled.AudioFileFormat;
15 import javax.sound.sampled.AudioFormat;
16 import javax.sound.sampled.AudioInputStream;
17 import javax.sound.sampled.AudioSystem;
18
19 import com.sun.speech.freetts.util.Utilities;
20
21 /**
22  * Streams audio to multiple files, one per utterance. 
23  *
24  *
25  */
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;
33
34     /**
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".
40      */
41     public MultiFileAudioPlayer() {
42         this(Utilities.getProperty(
43                  "com.sun.speech.freetts.AudioPlayer.baseName", "freetts"),
44              AudioFileFormat.Type.WAVE);
45     }       
46
47     /**
48      * Constructs a MultiFileAudioPlayer 
49      *
50      * @param baseName the base name of the audio file
51      * @param type the type of audio output
52      *
53      */
54     public MultiFileAudioPlayer(String baseName, AudioFileFormat.Type type) {
55         this.baseName = baseName;
56         this.outputType = type;
57     }
58
59     /**
60      * Sets the audio format for this player
61      *
62      * @param format the audio format
63      *
64      * @throws UnsupportedOperationException if the line cannot be opened with
65      *     the given format
66      */
67     public synchronized void setAudioFormat(AudioFormat format) {
68         currentFormat = format;
69     }
70
71
72     /**
73      * Gets the audio format for this player
74      *
75      * @return format the audio format
76      */
77     public AudioFormat getAudioFormat() {
78         return currentFormat;
79     }
80
81
82     /**
83      * Pauses audio output
84      */
85     public void pause() {
86     }
87
88     /**
89      * Resumes audio output
90      */
91     public synchronized void resume() {
92     }
93         
94     /**
95      * Starts the first sample timer
96      */
97     public void startFirstSampleTimer() {
98     }
99
100
101     /**
102      * Cancels currently playing audio
103      */
104     public synchronized void cancel() {
105     }
106
107     /**
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.
111      */
112     public synchronized void reset() {
113     }
114
115     /**
116      * Closes this audio player
117      */
118     public synchronized void close() {
119     }
120
121     /**
122      * Returns the current volume.
123      *
124      * @return the current volume (between 0 and 1)
125      */
126     public float getVolume() {
127         return 1.0f;
128     }         
129
130     /**
131      * Sets the current volume.
132      *
133      * @param volume  the current volume (between 0 and 1)
134      */
135     public void setVolume(float volume) {
136     }         
137
138
139
140
141     /**
142      *  Starts the output of a set of data. Audio data for a single
143      *  utterance should be grouped between begin/end pairs.
144      *
145      * @param size the size of data between now and the end
146      */
147     public void begin(int size) {
148         outputData = new byte[size];
149         curIndex = 0;
150     }
151
152     /**
153      *  Marks the end of a set of data. Audio data for a single 
154      *  utterance should be groupd between begin/end pairs.
155      *
156      *  @return true if the audio was output properly, false if the
157      *      output was cancelled or interrupted.
158      *
159      */
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);
168         try {
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);
173             return false;
174         } catch (IllegalArgumentException iae) {
175             System.err.println("Can't write audio type " + outputType);
176             return false;
177         }
178         fileCount++;
179         return true;
180     }
181
182
183     /**
184      * Waits for all queued audio to be played
185      *
186      * @return true if the audio played to completion, false if
187      *   the audio was stopped
188      */
189     public boolean drain()  {
190         return true;
191     }
192
193     /**
194      * Gets the amount of played since the last mark
195      *
196      * @return the amount of audio in milliseconds
197      */
198     public synchronized long getTime()  {
199         return -1L;
200     }
201
202
203     /**
204      * Resets the audio clock
205      */
206     public synchronized void resetTime() {
207     }
208     
209
210     
211     /**
212      * Writes the given bytes to the audio stream
213      *
214      * @param audioData audio data to write to the device
215      *
216      * @return <code>true</code> of the write completed successfully, 
217      *          <code> false </code>if the write was cancelled.
218      */
219     public boolean write(byte[] audioData) {
220         return write(audioData, 0, audioData.length);
221     }
222     
223     /**
224      * Writes the given bytes to the audio stream
225      *
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
229      *
230      * @return <code>true</code> of the write completed successfully, 
231      *          <code> false </code>if the write was cancelled.
232      */
233     public boolean write(byte[] bytes, int offset, int size) {
234         System.arraycopy(bytes, offset, outputData, curIndex, size);
235         curIndex += size;
236         return true;
237     }
238
239     /**
240      * Returns the name of this audioplayer
241      *
242      * @return the name of the audio player
243      */
244     public String toString() {
245         return "FileAudioPlayer";
246     }
247
248
249     /**
250      * Shows metrics for this audio player
251      */
252     public void showMetrics() {
253     }
254 }