upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / audio / RawFileAudioPlayer.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 com.sun.speech.freetts.util.Utilities;
11
12 import java.io.IOException;
13 import java.io.BufferedOutputStream;
14 import java.io.FileOutputStream;
15 import javax.sound.sampled.AudioFormat;
16
17
18 /**
19  * Provides an implementation of <code>AudioPlayer</code> that sends
20  * all audio data to the given file. 
21  */
22 public class RawFileAudioPlayer implements AudioPlayer {
23
24     private AudioFormat audioFormat;
25     private float volume;
26     private BufferedOutputStream os;
27     private String path;
28     
29     /**
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 &lt;baseName>.raw.  The default value for the
34      * base name is "freetts".
35      */
36     public RawFileAudioPlayer() throws IOException {
37         this(Utilities.getProperty(
38                  "com.sun.speech.freetts.AudioPlayer.baseName", "freetts")
39              + ".raw");
40     }
41     
42     /**
43      * Constructs a NullAudioPlayer
44      */
45     public RawFileAudioPlayer(String path) throws IOException {
46         this.path = path;
47         os = new BufferedOutputStream(new FileOutputStream(path));
48     }
49     
50
51     /**
52      * Sets the audio format for this player
53      *
54      * @param format the audio format
55      */
56     public void setAudioFormat(AudioFormat format) {
57         this.audioFormat = format;
58     }
59
60     /**
61      * Retrieves the audio format for this player
62      *
63      * @return the current audio format.
64      */
65     public AudioFormat getAudioFormat() {
66         return audioFormat;
67     }
68
69     /**
70      * Cancels all queued output. Current 'write' call will return
71      * false
72      *
73      */
74     public void cancel() {
75     }
76
77
78     /**
79      * Pauses the audio output
80      */
81     public void pause() {
82     }
83
84
85     /**
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.
89      */
90     public void reset() {
91     }
92
93
94     /**
95      * Resumes audio output
96      */
97     public void resume() {
98     }
99         
100
101
102
103     /**
104      * Waits for all audio playback to stop, and closes this AudioPlayer.
105      */
106     public void close() {
107         try {
108             os.flush();
109             os.close();
110             System.out.println("Wrote synthesized speech to " + path);
111         } catch (IOException ioe) {
112             ioe.printStackTrace();
113         }
114     }
115         
116
117     /**
118      * Returns the current volume.
119      *
120      * @return the current volume (between 0 and 1)
121      */
122     public float getVolume() {
123         return volume;
124     }         
125
126     /**
127      * Sets the current volume.
128      *
129      * @param volume  the current volume (between 0 and 1)
130      */
131     public void setVolume(float volume) {
132         this.volume = volume;
133     }         
134
135
136     /**
137      * Writes the given bytes to the audio stream
138      *
139      * @param audioData array of audio data
140      *
141      * @return <code>true</code> of the write completed successfully, 
142      *          <code> false </code>if the write was cancelled.
143      */
144     public boolean write(byte[] audioData) {
145         return write(audioData, 0, audioData.length);
146     }
147
148
149     /**
150      *  Starts the output of a set of data
151      *
152      * @param size the size of data between now and the end
153      *
154      */
155     public void begin(int size) {
156     }
157
158     /**
159      *  Marks the end of a set of data
160      *
161      */
162     public boolean  end()  {
163         return true;
164     }
165
166     /**
167      * Writes the given bytes to the audio stream
168      *
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
172      *
173      * @return <code>true</code> of the write completed successfully, 
174      *          <code> false </code>if the write was cancelled.
175      */
176     public boolean write(byte[] bytes, int offset, int size) {
177         try {
178             os.write(bytes, offset, size);
179         } catch (IOException ioe) {
180             return false;
181         }
182         return true;
183     }
184
185     /**
186      * Starts the first sample timer
187      */
188     public void startFirstSampleTimer() {
189     }
190
191     /**
192      * Waits for all queued audio to be played
193      *
194      * @return <code>true</code> if the audio played to completion,
195      *          <code> false </code>if the audio was stopped
196      */
197     public boolean drain()  {
198         return true;
199     }
200
201     /**
202      * Gets the amount of played since the last resetTime
203      * Currently not supported.
204      *
205      * @return the amount of audio in milliseconds
206      */
207     public long getTime()  {
208         return -1L;
209     }
210
211
212     /**
213      * Resets the audio clock
214      */
215     public void resetTime() {
216     }
217
218     /**
219      * Shows metrics for this audio player
220      */
221     public void showMetrics() {
222     }
223 }