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.IOException;
011
012import javax.sound.sampled.AudioFormat;
013
014/**
015 *  Provides an  interface to the audio system for use by freetts.
016 *  Audio is presented to the AudioPlayer as byte arrays.
017 *  Implementations of this AudioPlayer interface will format the data
018 *  based upon the current audio format (as set by
019 *  <code>setAudioFormat</code>) and output the data. 
020 *  
021 *  <p>
022 *  The AudioPlayer
023 *  interface provides a set of potential synchronization points to
024 *  allow a specific AudioPlayer to batch output in various ways.
025 *  <p>
026 *  These synchronization points are in pairs: <code>reset,
027 *  drain</code> are used to bracket output of large amounts of audio
028 *  data. Typically, an implementation will not return from
029 *  <code>drain</code> until all queued audio has been played (or
030 *  cancelled).
031 *  <p>
032 *  The methods: <code> begin, end</code> are used to bracket smaller amounts of
033 *  audio data (typically associated with a single utterance).
034 *
035 *  <h1>Threading Issues</h1>
036 *  Most of the methods in an AudioPlayer must be called from a
037 *  single thread. The only exceptions to this rule are <code> pause,
038 *  resume, cancel, showMetrics, close, getTime, resetTime</code>
039 *  which can be called from other threads.
040 */
041public interface AudioPlayer {
042    
043    /**
044     * Sets the audio format to use for the next set of outputs. Since
045     * an audio player can be shared by a number of voices, and since
046     * voices can have different AudioFormats (sample rates for
047     * example), it is necessary to allow clients to dynamically set
048     * the audio format for the player.
049     *
050     * @param format the audio format
051     */
052    void setAudioFormat(AudioFormat format) ;
053
054    /**
055     * Retrieves the audio format for this player
056     *
057     * @return the current audio format
058     *
059     */
060    AudioFormat getAudioFormat();
061
062    /**
063     * Pauses all audio output on this player. Play can be resumed
064     * with a call to resume
065     */
066    void pause();
067
068    /**
069     * Resumes audio output on this player
070     */
071    void resume();
072
073    /**
074     * Prepares for another batch of output. Larger groups of output
075     * (such as all output associated with a single FreeTTSSpeakable)
076     * should be grouped between a reset/drain pair.
077     */
078    void reset();
079
080    /**
081     * Waits for all queued audio to be played
082     *
083     * @return <code>true</code> if the audio played to completion;
084     *     otherwise <code> false </code> if the audio was stopped
085     */
086    boolean drain(); 
087
088
089    /**
090     *  Starts the output of a set of data. Audio data for a single
091     *  utterance should be grouped between begin/end pairs.
092     *
093     * @param size the size of data in bytes to be output before
094     *    <code>end</code> is called.
095     * @exception IOException
096     *            if an error occurs while preparing the output.
097     */
098    void begin(int size) throws IOException;
099
100    /**
101     *  Signals the end of a set of data. Audio data for a single 
102     *  utterance should be grouped between <code> begin/end </code> pairs.
103     *
104     *  @return <code>true</code> if the audio was output properly, 
105     *          <code> false</code> if the output was canceled 
106     *          or interrupted.
107     * @exception IOException
108     *            if an error occurs while closing the output
109     */
110    boolean end() throws IOException; 
111
112    /**
113     * Cancels all queued output. All 'write' calls until the next
114     * reset will return false.
115     *
116     */
117    void cancel();
118
119    /**
120     * Waits for all audio playback to stop, and closes this AudioPlayer.
121     * @exception IOException
122     *            error closing the audio player
123     */
124    void close() throws IOException;
125
126    /**
127     * Returns the current volume. The volume is specified as a number
128     * between 0.0 and 1.0, where 1.0 is the maximum volume and 0.0 is
129     * the minimum volume.
130     *
131     * @return the current volume (between 0 and 1)
132     */
133    float getVolume();
134
135    /**
136     * Sets the current volume. The volume is specified as a number
137     * between 0.0 and 1.0, where 1.0 is the maximum volume and 0.0 is
138     * the minimum volume.
139     *
140     * @param volume the new volume (between 0 and 1)
141     */
142    void setVolume(float volume);
143
144    /**
145     * Gets the amount of audio played since the last resetTime
146     *
147     * @return the amount of audio in milliseconds
148     */
149    long getTime(); 
150
151    /**
152     * Resets the audio clock
153     */
154    void resetTime();
155
156    /**
157     * Starts the first sample timer
158     */
159    void startFirstSampleTimer();
160
161    /**
162     * Writes the given bytes to the audio stream
163     *
164     * @param audioData audio data to write to the device
165     *
166     * @return <code>true</code> of the write completed successfully, 
167     *          <code> false </code>if the write was cancelled.
168     * @exception IOException
169     *            if an error occurs while writing the audio data
170     */
171    boolean write(byte[] audioData) throws IOException;
172
173    /**
174     * Writes the given bytes to the audio stream
175     *
176     * @param audioData audio data to write to the device
177     * @param offset the offset into the buffer
178     * @param size the number of bytes to write.
179     *
180     * @return <code>true</code> of the write completed successfully, 
181     *          <code> false </code>if the write was cancelled.
182     * @exception IOException
183     *            if an error occurs while writing the audio data
184     */
185    boolean write(byte[] audioData, int offset, int size) throws IOException;
186
187    /**
188     * Shows metrics for this audio player
189     */
190    void showMetrics();
191}