micromod.output
Class JavaSoundOutputDevice

java.lang.Object
  extended bymicromod.output.PCM16StreamOutputDevice
      extended bymicromod.output.HasAvailableOutputDevice
          extended bymicromod.output.JavaSoundOutputDevice
All Implemented Interfaces:
OutputDevice, PCM16, StreamOutputDevice

public class JavaSoundOutputDevice
extends HasAvailableOutputDevice

An OutputDevice for the Java1.3 audio system. The one Java should have had 3 years ago ... Because of the clunky way JavaSound drains it's buffer, this class implements a workaround that alters the behaviour of framesAvailable() to return more fine-grained values. 30 July 2003 (Jon Zeppieri): added getLine(), so we can control volume on the line 18 March 2005 (Jon Zeppieri): inserted a sourceDataLine.flush() into the close() method, to eliminate a delay on the Macintosh


Field Summary
protected  int available
           
protected  int bufferFrames
           
protected  long lastMillis
           
protected  int samplingRate
           
protected  javax.sound.sampled.SourceDataLine sourceDataLine
           
protected  long timeMillis
           
 
Fields inherited from class micromod.output.PCM16StreamOutputDevice
bytesPerFrame, CONVERT_BUFFER_FRAMES, convertBuffer, converter
 
Constructor Summary
JavaSoundOutputDevice(AudioFormatConverter converter, int samplingRate, int bufferTimeMillis)
           
 
Method Summary
protected  int bytesAvailable()
           
 void close()
          Close the output device and release any resources.
 int framesAvailable()
          A frame of PCM audio is one sample.
 javax.sound.sampled.SourceDataLine getLine()
           
 int getSamplingRate()
           
 void pause()
          Pause playback immediately.
 void start()
          Begin playing audio.
 void stop()
          Stop playback.
 void write(byte[] buffer, int length)
          Write length bytes of audio to the output device This method should return only when all of the audio has been written to the device, except if the device is stopped or paused, in which case the method should return immediately.
 
Methods inherited from class micromod.output.PCM16StreamOutputDevice
initialise, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

samplingRate

protected int samplingRate

bufferFrames

protected int bufferFrames

available

protected int available

timeMillis

protected long timeMillis

lastMillis

protected long lastMillis

sourceDataLine

protected javax.sound.sampled.SourceDataLine sourceDataLine
Constructor Detail

JavaSoundOutputDevice

public JavaSoundOutputDevice(AudioFormatConverter converter,
                             int samplingRate,
                             int bufferTimeMillis)
                      throws OutputDeviceException
Parameters:
samplingRate - Try 44100
bufferTimeMillis - The number of milliseconds of audio to buffer.
Method Detail

getLine

public javax.sound.sampled.SourceDataLine getLine()

framesAvailable

public int framesAvailable()
Description copied from class: HasAvailableOutputDevice
A frame of PCM audio is one sample. Eg) 1 frame is 4 bytes with 16 bit stereo audio.

Overrides:
framesAvailable in class HasAvailableOutputDevice
Returns:
the number of frames of audio that can be written directly to the buffer without "blocking" (having to wait until the space becomes available)

write

public void write(byte[] buffer,
                  int length)
Description copied from interface: StreamOutputDevice
Write length bytes of audio to the output device This method should return only when all of the audio has been written to the device, except if the device is stopped or paused, in which case the method should return immediately.


getSamplingRate

public int getSamplingRate()
Returns:
The PCM sampling rate

start

public void start()
Description copied from interface: OutputDevice
Begin playing audio. You should call this if you want any output! You must also call this after pause() or stop() to resume playback..


pause

public void pause()
Description copied from interface: StreamOutputDevice
Pause playback immediately. Calling start() will continue where you left off.


stop

public void stop()
Description copied from interface: OutputDevice
Stop playback. All audio written to the device should be played before this method returns.


close

public void close()
Description copied from interface: OutputDevice
Close the output device and release any resources. Once this method is called, the device must be reopened to allow playback. Reopening could involve reinstantiation or calling a device specific open() method.


bytesAvailable

protected int bytesAvailable()
Specified by:
bytesAvailable in class HasAvailableOutputDevice
Returns:
The number of bytes available in the buffer.