mediaframe.mpeg1.audio.player
Class EightBitAudioDevice

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

public class EightBitAudioDevice
extends java.lang.Object
implements java.lang.Runnable, AudioDevice

The EightBitAudioDevice class implements an audio device by using the sun.audio.AudioPlayer class.

Author:
Alan Blount

Field Summary
private  short[] byteBuf
          The temporary buffer for storing the audio samples.
private  mediaframe.mpeg1.audio.decoder.Decoder decoder
          The decoder that provides audio data to this audio device.
private  boolean open
          True if this audio device is opened, false otherwise.
private static int outputCapacity
          The size of the circle buffer in the pipe.
private  BigPipedInputStream pis
          The input stream in the pipe.
private  BigPipedOutputStream pos
          The output stream in the pipe.
 
Constructor Summary
EightBitAudioDevice()
           
 
Method Summary
 void close()
          Closes this audio device.
 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  short[] getShortArray(int length)
          Gets the temporary buffer that able to store up to length audio samples.
 boolean isEightBitEightKhzMuLaw()
          Returns true because the EightBitAudioDevice audio device is a crappy eight-bit eight khz ulaw decoder (browser).
 void open(mediaframe.mpeg1.audio.decoder.Decoder decoder)
          Opens this audio device.
 void pause()
          Pause the Player.
 void play()
          Start player playing.
 void run()
          Starts the sun.audio.AudioPlayer in the "Eight Bit Audio Device" thread.
 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.
(package private)  int toShortArray(short[] samples, int offset, int len, short[] b, boolean stereoToMono)
          Copies up to len audio samples from the samples array starting at the offset offset into the b array.
 void write(short[] samples, int offset, 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

outputCapacity

private static final int outputCapacity
The size of the circle buffer in the pipe.

See Also:
Constant Field Values

byteBuf

private short[] byteBuf
The temporary buffer for storing the audio samples.


pis

private BigPipedInputStream pis
The input stream in the pipe.


pos

private BigPipedOutputStream pos
The output stream in the pipe.


open

private boolean open
True if this audio device is opened, false otherwise.


decoder

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

Constructor Detail

EightBitAudioDevice

public EightBitAudioDevice()
Method Detail

open

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

Specified by:
open in interface AudioDevice
Parameters:
decoder - the decoder that will provide audio data to this audio device.
Throws:
mediaframe.mpeg1.audio.decoder.JavaLayerException - if the audio device could not be opened.

isEightBitEightKhzMuLaw

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

Specified by:
isEightBitEightKhzMuLaw in interface AudioDevice

run

public void run()
Starts the sun.audio.AudioPlayer in the "Eight Bit Audio Device" thread. This method is called by the JRE after this audio device is opened and the "Eight Bit Audio Device" thread is started.

Specified by:
run in interface java.lang.Runnable

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 offset,
                  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.
offset - 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.

getShortArray

protected short[] getShortArray(int length)
Gets the temporary buffer that able to store up to length audio samples.

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

toShortArray

int toShortArray(short[] samples,
                 int offset,
                 int len,
                 short[] b,
                 boolean stereoToMono)
Copies up to len audio samples from the samples array starting at the offset offset into the b array. Mix stereo samples into the mono sample if the audio decoder provides stereo data.

Parameters:
samples - the audio data.
offset - the start offset in the data.
len - the number of samples to copy.
b - the destination array of audio data into which the data is copied.
stereoToMono - true if the audio decoder provides stereo data
Returns:
the total number of samples copied into the b array.

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

pause

public void pause()
Pause the Player.

Specified by:
pause in interface AudioDevice

stop

public void stop()
Stop player playing.

Specified by:
stop 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).

endOfFile

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

Specified by:
endOfFile in interface AudioDevice