upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / audio / SingleFileAudioPlayer.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 import java.io.InputStream;
14 import java.io.SequenceInputStream;
15 import java.util.Vector;
16
17 import javax.sound.sampled.AudioFileFormat;
18 import javax.sound.sampled.AudioFormat;
19 import javax.sound.sampled.AudioInputStream;
20 import javax.sound.sampled.AudioSystem;
21
22 import com.sun.speech.freetts.util.Utilities;
23
24 /**
25  * Streams audio to a file. 
26  *
27  *
28  */
29 public class SingleFileAudioPlayer implements AudioPlayer {
30     private AudioFormat currentFormat = null;
31     private String baseName;
32     private byte[] outputData;
33     private int curIndex = 0;
34     private int totBytes = 0;
35     private AudioFileFormat.Type outputType;
36     private Vector outputList;
37
38
39     /**
40      * Constructs a FileAudioPlayer 
41      *
42      * @param baseName the base name of the audio file
43      * @param type the type of audio output
44      *
45      */
46     public SingleFileAudioPlayer(String baseName, AudioFileFormat.Type type) {
47         this.baseName = baseName + "." + type.getExtension();
48         this.outputType = type;
49
50         outputList = new Vector();
51     }
52
53     /**
54      * Creates a default audio player for an AudioFileFormat of type
55      * WAVE.  Reads the "com.sun.speech.freetts.AudioPlayer.baseName"
56      * property for the base filename to use, and will produce a file
57      * of the form <baseName>.wav.  The default value for the
58      * base name is "freetts".
59      */
60     public SingleFileAudioPlayer() {
61         this(Utilities.getProperty(
62                  "com.sun.speech.freetts.AudioPlayer.baseName", "freetts"),
63              AudioFileFormat.Type.WAVE);
64     }
65
66     /**
67      * Sets the audio format for this player
68      *
69      * @param format the audio format
70      *
71      * @throws UnsupportedOperationException if the line cannot be opened with
72      *     the given format
73      */
74     public synchronized void setAudioFormat(AudioFormat format) {
75         currentFormat = format;
76     }
77
78
79     /**
80      * Gets the audio format for this player
81      *
82      * @return format the audio format
83      */
84     public AudioFormat getAudioFormat() {
85         return currentFormat;
86     }
87
88
89     /**
90      * Pauses audio output
91      */
92     public void pause() {
93     }
94
95     /**
96      * Resumes audio output
97      */
98     public synchronized void resume() {
99     }
100         
101
102
103     /**
104      * Cancels currently playing audio
105      */
106     public synchronized void cancel() {
107     }
108
109     /**
110      * Prepares for another batch of output. Larger groups of output
111      * (such as all output associated with a single FreeTTSSpeakable)
112      * should be grouped between a reset/drain pair.
113      */
114     public synchronized void reset() {
115     }
116
117
118     /**
119      * Starts the first sample timer
120      */
121     public void startFirstSampleTimer() {
122     }
123
124     /**
125      * Closes this audio player
126      */
127     public synchronized void close() {
128         try {
129             File file = new File(baseName);
130             InputStream is = new SequenceInputStream(outputList.elements());
131             AudioInputStream ais = new AudioInputStream(is,
132                     currentFormat, totBytes / currentFormat.getFrameSize());
133             if (false) {
134                 System.out.println("Avail " + ais.available());
135                 System.out.println("totBytes " + totBytes);
136                 System.out.println("FS " + currentFormat.getFrameSize());
137             }
138             System.out.println("Wrote synthesized speech to " + baseName);
139             AudioSystem.write(ais, outputType, file);
140         } catch (IOException ioe) {
141             System.err.println("Can't write audio to " + baseName);
142         } catch (IllegalArgumentException iae) {
143             System.err.println("Can't write audio type " + outputType);
144         }
145     }
146
147
148     /**
149      * Returns the current volume.
150      *
151      * @return the current volume (between 0 and 1)
152      */
153     public float getVolume() {
154         return 1.0f;
155     }         
156
157     /**
158      * Sets the current volume.
159      *
160      * @param volume  the current volume (between 0 and 1)
161      */
162     public void setVolume(float volume) {
163     }         
164
165
166
167
168     /**
169      *  Starts the output of a set of data. Audio data for a single
170      *  utterance should be grouped between begin/end pairs.
171      *
172      * @param size the size of data between now and the end
173      */
174     public void begin(int size) {
175         outputData = new byte[size];
176         curIndex = 0;
177     }
178
179     /**
180      *  Marks the end of a set of data. Audio data for a single 
181      *  utterance should be groupd between begin/end pairs.
182      *
183      *  @return true if the audio was output properly, false if the
184      *      output was cancelled or interrupted.
185      *
186      */
187     public boolean  end()  {
188         outputList.add(new ByteArrayInputStream(outputData));
189         totBytes += outputData.length;
190         return true;
191     }
192
193
194     /**
195      * Waits for all queued audio to be played
196      *
197      * @return true if the audio played to completion, false if
198      *   the audio was stopped
199      */
200     public boolean drain()  {
201         return true;
202     }
203
204     /**
205      * Gets the amount of played since the last mark
206      *
207      * @return the amount of audio in milliseconds
208      */
209     public synchronized long getTime()  {
210         return -1L;
211     }
212
213
214     /**
215      * Resets the audio clock
216      */
217     public synchronized void resetTime() {
218     }
219     
220
221     
222     /**
223      * Writes the given bytes to the audio stream
224      *
225      * @param audioData audio data to write to the device
226      *
227      * @return <code>true</code> of the write completed successfully, 
228      *          <code> false </code>if the write was cancelled.
229      */
230     public boolean write(byte[] audioData) {
231         return write(audioData, 0, audioData.length);
232     }
233     
234     /**
235      * Writes the given bytes to the audio stream
236      *
237      * @param bytes audio data to write to the device
238      * @param offset the offset into the buffer
239      * @param size the size into the buffer
240      *
241      * @return <code>true</code> of the write completed successfully, 
242      *          <code> false </code>if the write was cancelled.
243      */
244     public boolean write(byte[] bytes, int offset, int size) {
245         System.arraycopy(bytes, offset, outputData, curIndex, size);
246         curIndex += size;
247         return true;
248     }
249
250     /**
251      * Returns the name of this audioplayer
252      *
253      * @return the name of the audio player
254      */
255     public String toString() {
256         return "FileAudioPlayer";
257     }
258
259     /**
260      * Shows metrics for this audio player
261      */
262     public void showMetrics() {
263     }
264 }