sound.player
Class LiveSound

java.lang.Object
  extended by sound.player.LiveSound

public class LiveSound
extends java.lang.Object

Created by IntelliJ IDEA. User: Douglas Lyon Date: Dec 13, 2004 Time: 8:25:22 PM Copyright DocJava, Inc.


Constructor Summary
LiveSound()
           
 
Method Summary
static void addLiveSoundListener(LiveSoundListener listener)
          Add a live sound listener.
static int getBitsPerSample()
          Return the number of bits per audio sample, which is set by the setBitsPerSample() method.
static int getBufferSize()
          Return the size of the internal capture and playback audio buffers, in samples per channel.
static int getChannels()
          Return the number of audio channels, which is set by the setChannels() method.
static int getSampleRate()
          Return the current sampling rate in Hz, which is set by the setSampleRate() method.
static double[][] getSamples(java.lang.Object consumer)
          Return an array of captured audio samples.
static int getTransferSize()
          Get the array length (in samples per channel) to use for capturing and playing samples via the putSamples() and getSamples() methods.
static boolean isCaptureActive()
          Return true if audio capture is currently active.
static boolean isPlaybackActive()
          Return true if audio playback is currently active.
static void putSamples(java.lang.Object producer, double[][] samplesArray)
          Play an array of audio samples.
static void removeLibeSoundListener(LiveSoundListener listener)
          Remove a live sound listener.
static void resetCapture()
          Stop audio capture.
static void resetPlayback()
          Stop audio playback.
static void setBitsPerSample(int bitsPerSample)
          Set the number of bits per sample to use for audio capture and playback and notify an registered listeners of the change.
static void setBufferSize(int bufferSize)
          Set the size of the internal capture and playback audio buffers, in samples per channel and notify an registered listeners of the change.
static void setChannels(int channels)
          Set the number of audio channels to use for capture and playback and notify an registered listeners of the change.
static void setSampleRate(int sampleRate)
          Set the sample rate to use for audio capture and playback and notify an registered listeners of the change.
static void setTransferSize(int transferSize)
          Set the array length (in samples per channel) to use for capturing and playing samples via the putSamples() and getSamples() methods.
static void startCapture(java.lang.Object consumer)
          Start audio capture.
static void startPlayback(java.lang.Object producer)
          Start audio playback.
static void stopCapture(java.lang.Object consumer)
          Stop audio capture.
static void stopPlayback(java.lang.Object producer)
          Stop audio playback.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LiveSound

public LiveSound()
Method Detail

addLiveSoundListener

public static void addLiveSoundListener(LiveSoundListener listener)
Add a live sound listener. The listener will be notified of all changes in live audio parameters. If the listener is already listening, then do nothing.

Parameters:
listener - The LiveSoundListener to add.

removeLibeSoundListener

public static void removeLibeSoundListener(LiveSoundListener listener)
Remove a live sound listener. If the listener is is not listening, then do nothing.

Parameters:
listener - The LiveSoundListener to remove.

getSampleRate

public static int getSampleRate()
Return the current sampling rate in Hz, which is set by the setSampleRate() method. The default value of this parameter is 8000 Hz.

Returns:
The sample rate in Hz.

getBitsPerSample

public static int getBitsPerSample()
Return the number of bits per audio sample, which is set by the setBitsPerSample() method. The default value of this parameter is 16 bits.

Returns:
The sample size in bits.

getChannels

public static int getChannels()
Return the number of audio channels, which is set by the setChannels() method. The default value of this parameter is 1 (for mono audio).

Returns:
The number of audio channels.

getBufferSize

public static int getBufferSize()
Return the size of the internal capture and playback audio buffers, in samples per channel. This parameter is set by the setBufferSize() method. The default value of this parameter is 4096.

Returns:
The internal buffer size in samples per channel.

isCaptureActive

public static boolean isCaptureActive()
Return true if audio capture is currently active. Otherwise return false.

Returns:
True If audio capture is currently active. Otherwise return false.

isPlaybackActive

public static boolean isPlaybackActive()
Return true if audio playback is currently active. Otherwise return false.

Returns:
True If audio playback is currently active. Otherwise return false.

setSampleRate

public static void setSampleRate(int sampleRate)
                          throws java.io.IOException
Set the sample rate to use for audio capture and playback and notify an registered listeners of the change. Allowable values for this parameter are 8000, 11025, 22050, 44100, and 48000 Hz. If this method is not invoked, then the default value of 8000 Hz is used.

Parameters:
sampleRate - Sample rate in Hz.
Throws:
java.io.IOException - If the specified sample rate is not supported by the audio hardware or by Java.

setBitsPerSample

public static void setBitsPerSample(int bitsPerSample)
                             throws java.io.IOException
Set the number of bits per sample to use for audio capture and playback and notify an registered listeners of the change. Allowable values include 8 and 16 bits. If this method is not invoked, then the default value of 16 bits is used.

Parameters:
bitsPerSample - The number of bits per sample.
Throws:
java.io.IOException - If the specified bits per sample is not supported by the audio hardware or by Java.

setChannels

public static void setChannels(int channels)
                        throws java.io.IOException
Set the number of audio channels to use for capture and playback and notify an registered listeners of the change. Allowable values are 1 (for mono) and 2 (for stereo). If this method is not invoked, the default value of 1 audio channel is used. Note that this method sets the size of the first dimension of the 2-dimensional array used by the putSamples() and getSamples() methods.

Parameters:
channels - The number audio channels.
Throws:
java.io.IOException - If the specified number of channels is not supported by the audio hardware or by Java.

setBufferSize

public static void setBufferSize(int bufferSize)
                          throws java.io.IOException
Set the size of the internal capture and playback audio buffers, in samples per channel and notify an registered listeners of the change. If this method is not invoked, the default value of 4096 is used.

Parameters:
bufferSize - The size of the internal capture and playback audio buffers, in samples per channel.
Throws:
java.io.IOException - If the specified number of channels is not supported by the audio hardware or by Java.

setTransferSize

public static void setTransferSize(int transferSize)
                            throws java.lang.IllegalStateException
Set the array length (in samples per channel) to use for capturing and playing samples via the putSamples() and getSamples() methods. This method sets the size of the 2nd dimension of the 2-dimensional array used by the putSamples() and getSamples() methods. If this method is not invoked, the default value of 128 is used.

This method should only be called while audio capture and playback are inactive. Otherwise an exception will occur.

Parameters:
transferSize - The size of the 2nd dimension of the 2-dimensional array used by the putSamples() and getSamples() methods
Throws:
java.lang.IllegalStateException - If this method is called while audio capture or playback are active.

getTransferSize

public static int getTransferSize()
Get the array length (in samples per channel) to use for capturing and playing samples via the putSamples() and getSamples() methods. This method gets the size of the 2nd dimension of the 2-dimensional array used by the putSamples() and getSamples() methods. This method returns the value that was set by the setTransferSize(). If setTransferSize() was not invoked, the default value of 128 is returns.

This method should only be called while audio capture and playback are inactive. Otherwise an exception will occur.

Returns:
The size of the 2nd dimension of the 2-dimensional array used by the putSamples() and getSamples() methods.

resetCapture

public static void resetCapture()
Stop audio capture. If audio capture is already inactive, then do nothing. This method should generally not be used, but it may be needed to turn of audio capture for the case where an ill-behaved application exits without calling stopCapture(). The preferred way of stopping audio capture is by calling the stopCapture() method.


resetPlayback

public static void resetPlayback()
Stop audio playback. If audio playback is already inactive, then do nothing. This method should generally not be used, but it may be needed to turn of audio playback for the case where an ill-behaved application exits without calling stopPlayback(). The preferred way of stopping audio playback is by calling the stopPlayback() method.


startCapture

public static void startCapture(java.lang.Object consumer)
                         throws java.io.IOException,
                                java.lang.IllegalStateException
Start audio capture. The specified object will be given an exclusive lock on the audio capture resources until the stopCapture() method is called with the same object reference. After this method returns, the getSamples() method may be repeatedly invoked (using the object reference as a parameter) to capture audio.

If audio capture is already active, then an exception will occur.

Parameters:
consumer - The object to be given exclusive access to the captured audio resources.
Throws:
java.lang.IllegalStateException - If this method is called while audio capture is already active.
java.io.IOException

stopCapture

public static void stopCapture(java.lang.Object consumer)
                        throws java.io.IOException,
                               java.lang.IllegalStateException
Stop audio capture. If the specified object has the lock on audio capture when this method is invoked, then stop audio capture. Otherwise an exception will occur.

Parameters:
consumer - The object that held on exclusive lock on the captured audio resources when this method was invoked.
Throws:
java.lang.IllegalStateException - If the specified object did not hold an exclusive lock on the captured audio resources when this method was invoked.
java.io.IOException

startPlayback

public static void startPlayback(java.lang.Object producer)
                          throws java.io.IOException,
                                 java.lang.IllegalStateException
Start audio playback. The specified object will be given an exclusive lock on the audio playback resources until the stopPlayback() method is called with the same object reference. After this method returns, the putSamples() method may be repeatedly invoked (using the object reference as a parameter) to playback audio.

If audio playback is already active, then an exception will occur.

Parameters:
producer - The object to be given exclusive access to the playback playback resources.
Throws:
java.lang.IllegalStateException - If this method is called while audio playback is already active.
java.io.IOException

stopPlayback

public static void stopPlayback(java.lang.Object producer)
                         throws java.io.IOException,
                                java.lang.IllegalStateException
Stop audio playback. If the specified object has the lock on audio playback when this method is invoked, then stop audio playback. Otherwise an exception will occur.

Parameters:
producer - The object that held on exclusive lock on the playback audio resources when this method was invoked.
Throws:
java.lang.IllegalStateException - If the specified object did not hold an exclusive lock on the playback audio resources when this method was invoked.
java.io.IOException

getSamples

public static double[][] getSamples(java.lang.Object consumer)
                             throws java.io.IOException,
                                    java.lang.IllegalStateException
Return an array of captured audio samples. This method should be repeatedly called to obtain audio data. The returned audio samples will have values in the range [-1, 1], regardless of the audio bit resolution (bits per sample). This method should be called often enough to prevent overflow of the internal audio buffer. If overflow occurs, some audio data will be lost but no exception or other error condition will occur. If the audio data is not yet available, then this method will block until the data is available.

The first index of the returned array represents the channel number (0 for first channel, 1 for second channel). The number of channels is set by the setChannels() method. The second index represents the sample index within a channel. For example, returned array[n][m] contains the (m+1)th sample of the (n+1)th channel. For each channel, n, the length of returned array[n] is equal to the value returned by the getTransferSize() method. The size of the 2nd dimension of the returned array is set by the setTransferSize() method.

Note that only the object with the exclusive lock on the captured audio resources is allowed to invoked this method. An exception will occur if the specified object does not have the lock on the captured audio resources.

Parameters:
consumer - The object that has an exclusive lock on the captured audio resources.
Returns:
Two dimensional array of captured audio samples.
Throws:
java.io.IOException - If there is a problem capturing audio.
java.lang.IllegalStateException - If audio capture is currently inactive, or if the specified object does not hold the lock on the captured audio resources.

putSamples

public static void putSamples(java.lang.Object producer,
                              double[][] samplesArray)
                       throws java.io.IOException,
                              java.lang.IllegalStateException
Play an array of audio samples. There will be a delay before the audio data is actually heard, since the audio data in samplesArray is queued to an internal audio buffer. The size of the internal buffer is set by the setTransferSize() method. A lower bound on the latency is given by (bufferSize / sampleRate) seconds. This method should be invoked often enough to prevent underflow of the internal audio buffer. Underflow is undesirable since it will cause audible gaps in audio playback, but no exception or error condition will occur. If the caller attempts to write more data than can be written, this method blocks until the data can be written to the internal audio buffer.

The samples should be in the range (-1, 1). Samples that are outside this range will be hard-clipped so that they fall within this range.

The first index of the specified array represents the channel number (0 for first channel, 1 for second channel, etc.). The number of channels is set by the setChannels() method. The second index represents the sample index within a channel. For example, putSamplesArray[n][m] contains the (m+1)th sample of the (n+1)th channel. samplesArray should be a rectangular array such that samplesArray.length() gives the number of channels and samplesArray[n].length() is equal to samplesArray, for all channels n. This is not actually checked, however.

Note that only the object with the exclusive lock on the playback audio resources is allowed to invoked this method. An exception will occur if the specified object does not have the lock on the playback audio resources.

Parameters:
producer - The object that has an exclusive lock on the playback audio resources.
samplesArray - A two dimensional array containing the samples to play or write to a file.
Throws:
java.io.IOException - If there is a problem playing audio.
java.lang.IllegalStateException - If audio playback is currently inactive. That is, If startPlayback() has not yet been called or if stopPlayback() has already been called.