public class JavaClipAudioPlayer extends Object implements AudioPlayer
AudioPlayer
that creates
javax.sound.sampled audio clips and outputs them via the
javax.sound API. The interface provides a highly reliable audio
output package. Since audio is batched and not sent to the audio
layer until an entire utterance has been processed, this player has
higher latency (50 msecs for a typical 4 second utterance).Modifier and Type | Class and Description |
---|---|
private class |
JavaClipAudioPlayer.JavaClipLineListener
Provides a LineListener for this clas.
|
Modifier and Type | Field and Description |
---|---|
private AudioInputStream |
audioInput
Audio input stream that is used to play back the audio.
|
private boolean |
audioMetrics |
private boolean |
cancelled |
private int |
curIndex |
private Clip |
currentClip |
private AudioFormat |
currentFormat |
private AudioFormat |
defaultFormat
Default format is 8kHz.
|
private long |
drainDelay |
private boolean |
firstPlay |
private boolean |
firstSample |
private LineListener |
lineListener |
private static Logger |
LOGGER
Logger instance.
|
private long |
openFailDelayMs |
private PipedOutputStream |
outputData
Data buffer to write the pure audio data to.
|
private boolean |
paused |
private BulkTimer |
timer |
private long |
totalOpenFailDelayMs |
private float |
volume
The current volume.
|
Constructor and Description |
---|
JavaClipAudioPlayer()
Constructs a default JavaClipAudioPlayer
|
Modifier and Type | Method and Description |
---|---|
void |
begin(int size)
Starts the output of a set of data.
|
void |
cancel()
Cancels all queued audio.
|
void |
close()
Closes this audio player
[[[ WORKAROUND TODO
The javax.sound.sampled drain is almost working properly.
|
boolean |
drain()
Waits for all queued audio to be played
|
boolean |
end()
Marks the end a set of data.
|
AudioFormat |
getAudioFormat()
Retrieves the audio format for this player
|
private Clip |
getClip()
Lazy instantiation of the clip.
|
long |
getTime()
Returns the current position in the output stream since the
last
resetTime
Currently not supported. |
float |
getVolume()
Returns the current volume.
|
void |
pause()
Pauses audio output.
|
void |
reset()
Prepares for another batch of output.
|
void |
resetTime()
Resets the time for this audio stream to zero
|
void |
resume()
Resumes playing audio after a pause.
|
void |
setAudioFormat(AudioFormat format)
Sets the audio format for this player
|
private void |
setPaused(boolean state)
Sets pause mode
|
private void |
setVolume(Clip clip,
float vol)
Sets the volume on the given clip
|
void |
setVolume(float volume)
Sets the current volume.
|
void |
showMetrics()
Shows metrics for this audio player
|
void |
startFirstSampleTimer()
Starts the first sample timer
|
String |
toString()
Returns the name of this audio player
|
boolean |
write(byte[] audioData)
Writes the given bytes to the audio stream
|
boolean |
write(byte[] bytes,
int offset,
int size)
Writes the given bytes to the audio stream
|
private volatile boolean paused
private volatile boolean cancelled
private volatile Clip currentClip
private float volume
private boolean audioMetrics
private AudioFormat defaultFormat
private AudioFormat currentFormat
private boolean firstSample
private boolean firstPlay
private int curIndex
private final PipedOutputStream outputData
private AudioInputStream audioInput
private final LineListener lineListener
private long drainDelay
private long openFailDelayMs
private long totalOpenFailDelayMs
public JavaClipAudioPlayer()
public void setAudioFormat(AudioFormat format)
setAudioFormat
in interface AudioPlayer
format
- the audio formatUnsupportedOperationException
- if the line cannot be opened with
the given formatpublic AudioFormat getAudioFormat()
getAudioFormat
in interface AudioPlayer
public void pause()
resume
. Output can be
aborted by calling cancel
pause
in interface AudioPlayer
public void resume()
resume
in interface AudioPlayer
public void cancel()
cancel
in interface AudioPlayer
public void reset()
reset
in interface AudioPlayer
public boolean drain()
drain
in interface AudioPlayer
true
if the write completed successfully,
false
if the write was cancelled.public void close()
close
in interface AudioPlayer
public float getVolume()
getVolume
in interface AudioPlayer
public void setVolume(float volume)
setVolume
in interface AudioPlayer
volume
- the current volume (between 0 and 1)private void setPaused(boolean state)
state
- true if we are pausedprivate void setVolume(Clip clip, float vol)
line
- the line to set the volume onvol
- the volume (range 0 to 1)public long getTime()
resetTime
Currently not supported.getTime
in interface AudioPlayer
public void resetTime()
resetTime
in interface AudioPlayer
public void begin(int size) throws IOException
begin
in interface AudioPlayer
size
- the size of data in bytes to be output before
end
is called.IOException
- if an error occurs while preparing the output.private Clip getClip() throws LineUnavailableException
LineUnavailableException
- if the target line is not available.public boolean end()
end
in interface AudioPlayer
true
if the audio was output properly,
false
if the output was canceled or interrupted.public boolean write(byte[] audioData) throws IOException
write
in interface AudioPlayer
audioData
- audio data to write to the devicetrue
of the write completed successfully,
false
if the write was cancelled.IOException
- if an error occurs while writing the audio datapublic boolean write(byte[] bytes, int offset, int size) throws IOException
write
in interface AudioPlayer
bytes
- audio data to write to the deviceoffset
- the offset into the buffersize
- the number of bytes to write.true
of the write completed successfully,
false
if the write was cancelled.IOException
- if an error occurs while writing the audio datapublic void showMetrics()
showMetrics
in interface AudioPlayer
public void startFirstSampleTimer()
startFirstSampleTimer
in interface AudioPlayer
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses