001/**
002 * Copyright 2001 Sun Microsystems, Inc.
003 * 
004 * See the file "license.terms" for information on usage and
005 * redistribution of this file, and for a DISCLAIMER OF ALL 
006 * WARRANTIES.
007 */
008package com.sun.speech.freetts.audio;
009
010import java.io.BufferedOutputStream;
011import java.io.FileOutputStream;
012import java.io.IOException;
013
014import javax.sound.sampled.AudioFormat;
015
016import com.sun.speech.freetts.util.Utilities;
017
018
019/**
020 * Provides an implementation of <code>AudioPlayer</code> that sends
021 * all audio data to the given file. 
022 */
023public class RawFileAudioPlayer implements AudioPlayer {
024
025    private AudioFormat audioFormat;
026    private float volume;
027    private BufferedOutputStream os;
028    private String path;
029    
030    /**
031     * Creates a default audio player for an AudioFileFormat of type
032     * WAVE.  Reads the "com.sun.speech.freetts.AudioPlayer.baseName"
033     * property for the base filename to use, and will produce files
034     * of the form &lt;baseName>.raw.  The default value for the
035     * base name is "freetts".
036     */
037    public RawFileAudioPlayer() throws IOException {
038        this(Utilities.getProperty(
039                 "com.sun.speech.freetts.AudioPlayer.baseName", "freetts")
040             + ".raw");
041    }
042    
043    /**
044     * Constructs a NullAudioPlayer
045     */
046    public RawFileAudioPlayer(String path) throws IOException {
047        this.path = path;
048        os = new BufferedOutputStream(new FileOutputStream(path));
049    }
050    
051
052    /**
053     * Sets the audio format for this player
054     *
055     * @param format the audio format
056     */
057    public void setAudioFormat(AudioFormat format) {
058        this.audioFormat = format;
059    }
060
061    /**
062     * Retrieves the audio format for this player
063     *
064     * @return the current audio format.
065     */
066    public AudioFormat getAudioFormat() {
067        return audioFormat;
068    }
069
070    /**
071     * Cancels all queued output. Current 'write' call will return
072     * false
073     *
074     */
075    public void cancel() {
076    }
077
078
079    /**
080     * Pauses the audio output
081     */
082    public void pause() {
083    }
084
085
086    /**
087     * Prepares for another batch of output. Larger groups of output
088     * (such as all output associated with a single FreeTTSSpeakable)
089     * should be grouped between a reset/drain pair.
090     */
091    public void reset() {
092    }
093
094
095    /**
096     * Resumes audio output
097     */
098    public void resume() {
099    }
100        
101
102
103
104    /**
105     * Waits for all audio playback to stop, and closes this AudioPlayer.
106     */
107    public void close() throws IOException {
108        os.flush();
109        os.close();
110        System.out.println("Wrote synthesized speech to " + path);
111    }
112        
113
114    /**
115     * Returns the current volume.
116     *
117     * @return the current volume (between 0 and 1)
118     */
119    public float getVolume() {
120        return volume;
121    }         
122
123    /**
124     * Sets the current volume.
125     *
126     * @param volume  the current volume (between 0 and 1)
127     */
128    public void setVolume(float volume) {
129        this.volume = volume;
130    }         
131
132
133    /**
134     * {@inheritDoc}
135     */
136    public boolean write(byte[] audioData) throws IOException {
137        return write(audioData, 0, audioData.length);
138    }
139
140
141    /**
142     *  Starts the output of a set of data
143     *
144     * @param size the size of data between now and the end
145     *
146     */
147    public void begin(int size) {
148    }
149
150    /**
151     *  Marks the end of a set of data
152     *
153     */
154    public boolean  end()  {
155        return true;
156    }
157
158    /**
159     * {@inheritDoc}
160     */
161    public boolean write(byte[] bytes, int offset, int size)
162        throws IOException {
163        os.write(bytes, offset, size);
164        return true;
165    }
166
167    /**
168     * Starts the first sample timer
169     */
170    public void startFirstSampleTimer() {
171    }
172
173    /**
174     * Waits for all queued audio to be played
175     *
176     * @return <code>true</code> if the audio played to completion,
177     *          <code> false </code>if the audio was stopped
178     */
179    public boolean drain()  {
180        return true;
181    }
182
183    /**
184     * Gets the amount of played since the last resetTime
185     * Currently not supported.
186     *
187     * @return the amount of audio in milliseconds
188     */
189    public long getTime()  {
190        return -1L;
191    }
192
193
194    /**
195     * Resets the audio clock
196     */
197    public void resetTime() {
198    }
199
200    /**
201     * Shows metrics for this audio player
202     */
203    public void showMetrics() {
204    }
205}