mediaframe.mpeg1.audio.player
Class JavaSoundAudioDevice

java.lang.Object
  |
  +--mediaframe.mpeg1.audio.player.JavaSoundAudioDevice
All Implemented Interfaces:
AudioDevice

public class JavaSoundAudioDevice
extends java.lang.Object
implements AudioDevice

The JavaSoundAudioDevice class implements an audio device by using the JavaSound API.

Since:
0.0.8
Author:
Mat McGowan

Field Summary
private static int bufferMs
          Constanta, equals to four seconds in milliseconds.
private  byte[] byteBuf
          The temporary buffer for storing the audio data.
private  mediaframe.mpeg1.audio.decoder.Decoder decoder
          The decoder that provides audio data to this audio device.
private  javax.sound.sampled.AudioFormat fmt
          The Audio Format for the MPEG audio stream.
private  float masterGain
          The master gain volume value of the Java Sound audio device.
private  float oldSampleRate
          The sample rate (speed) of the current audio playback (0 - the playback is paused, > 0 otherwise).
private  boolean open
          True if this audio device is opened.
private  boolean playing
          True if the audio player is playing the sound, false if the playback is paused.
private  javax.sound.sampled.SourceDataLine source
          The source audio line that plays the audio stream.
private  boolean stopped
          True if this audio device is stoped.
 
Constructor Summary
JavaSoundAudioDevice()
           
 
Method Summary
 void close()
          Closes this audio device.
protected  void createSource()
          Obtains and opens the source audio line using the Audio Format of the MPEG audio stream.
 void endOfFile()
          Is called if the end of file has been reached.
 void flush()
          Waits for any buffered audio samples to be played by the audio device.
protected  javax.sound.sampled.AudioFormat getAudioFormat()
          Gets the Audio Format for the MPEG audio stream.
protected  byte[] getByteArray(int length)
          Gets the buffer that able to store up to length bytes.
 boolean isEightBitEightKhzMuLaw()
          Returns false because the JavaSoundAudioDevice audio device is not a crappy eight-bit eight khz ulaw decoder (browser).
protected  int millisecondsToBytes(javax.sound.sampled.AudioFormat _fmt, int time)
          Calculates the number of bytes required for storing the audio samples for the specified time and the audio format.
 void open(mediaframe.mpeg1.audio.decoder.Decoder decoder)
          Opens this audio device.
 void pause()
          Stop player playing.
 void play()
          Start player playing.
 void setMute(boolean mute)
          Sets the mute state of the audio playback.
 void setVolume(float volume)
          Sets the volume (in the range 0..1) of the audio playback.
 void stop()
          Stop player playing.
protected  byte[] toByteArray(short[] samples, int offs, int len)
          Returns the byte array with up to len audio samples (two bytes each) from the samples array starting at offset offset.
 void write(short[] samples, int offs, int len)
          Writes audio data to this audio device.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

source

private javax.sound.sampled.SourceDataLine source
The source audio line that plays the audio stream.


fmt

private javax.sound.sampled.AudioFormat fmt
The Audio Format for the MPEG audio stream.


byteBuf

private byte[] byteBuf
The temporary buffer for storing the audio data.


playing

private boolean playing
True if the audio player is playing the sound, false if the playback is paused.


oldSampleRate

private float oldSampleRate
The sample rate (speed) of the current audio playback (0 - the playback is paused, > 0 otherwise).


bufferMs

private static final int bufferMs
Constanta, equals to four seconds in milliseconds.

See Also:
Constant Field Values

open

private boolean open
True if this audio device is opened.


stopped

private boolean stopped
True if this audio device is stoped.


decoder

private mediaframe.mpeg1.audio.decoder.Decoder decoder
The decoder that provides audio data to this audio device.


masterGain

private float masterGain
The master gain volume value of the Java Sound audio device.

Constructor Detail

JavaSoundAudioDevice

public JavaSoundAudioDevice()
Method Detail

open

public void open(mediaframe.mpeg1.audio.decoder.Decoder decoder)
Opens this audio device.

Specified by:
open in interface AudioDevice
Parameters:
decoder - the decoder that will provide audio data to this audio device.

isEightBitEightKhzMuLaw

public boolean isEightBitEightKhzMuLaw()
Returns false because the JavaSoundAudioDevice audio device is not a crappy eight-bit eight khz ulaw decoder (browser).

Specified by:
isEightBitEightKhzMuLaw in interface AudioDevice

getAudioFormat

protected javax.sound.sampled.AudioFormat getAudioFormat()
Gets the Audio Format for the MPEG audio stream.


createSource

protected void createSource()
                     throws mediaframe.mpeg1.audio.decoder.JavaLayerException
Obtains and opens the source audio line using the Audio Format of the MPEG audio stream.

Throws:
mediaframe.mpeg1.audio.decoder.JavaLayerException - if the source data line could not be obtained and opened.

millisecondsToBytes

protected int millisecondsToBytes(javax.sound.sampled.AudioFormat _fmt,
                                  int time)
Calculates the number of bytes required for storing the audio samples for the specified time and the audio format.

Parameters:
_fmt - the format of the audio stream.
time - the time in milliseconds that should be buffered.
Returns:
the number of bytes required for storing the audio samples.

close

public void close()
Closes this audio device. If the device is currently playing audio, playback is stopped immediately without flushing any buffered audio data.

Specified by:
close in interface AudioDevice

write

public void write(short[] samples,
                  int offs,
                  int len)
           throws mediaframe.mpeg1.audio.decoder.JavaLayerException
Writes audio data to this audio device. Audio data is assumed to be in the output format of the decoder. This method may return before the data has actually been sounded by the device if the device buffers audio samples.

If the audio device is not open, this method does nothing.

Specified by:
write in interface AudioDevice
Parameters:
samples - the samples to write to the audio device.
offs - the offset into the array of the first sample to write.
len - the number of samples from the array to write.
Throws:
mediaframe.mpeg1.audio.decoder.JavaLayerException - if the audio data could not be written to the audio device.

getByteArray

protected byte[] getByteArray(int length)
Gets the buffer that able to store up to length bytes.

Parameters:
length - min size of the temporary buffer.
Returns:
the buffer for storing the bytes.

toByteArray

protected byte[] toByteArray(short[] samples,
                             int offs,
                             int len)
Returns the byte array with up to len audio samples (two bytes each) from the samples array starting at offset offset.

Parameters:
samples - the audio data.
offs - the start offset in the data.
len - the number of samples to copy.
Returns:
the byte array with the audio data.

flush

public void flush()
Waits for any buffered audio samples to be played by the audio device. This method should only be called prior to closing the device.

Specified by:
flush in interface AudioDevice

play

public void play()
Start player playing.

Specified by:
play in interface AudioDevice

setVolume

public void setVolume(float volume)
Sets the volume (in the range 0..1) of the audio playback.

Specified by:
setVolume in interface AudioDevice
Parameters:
volume - the volume to set.

setMute

public void setMute(boolean mute)
Sets the mute state of the audio playback.

Specified by:
setMute in interface AudioDevice
Parameters:
mute - the mute state (true if the mute on and the player doesn't play the audio).

stop

public void stop()
Stop player playing.

Specified by:
stop in interface AudioDevice

pause

public void pause()
Stop player playing.

Specified by:
pause in interface AudioDevice

endOfFile

public void endOfFile()
Is called if the end of file has been reached.

Specified by:
endOfFile in interface AudioDevice