public class SoundPlayback
extends java.lang.Object
Depending on available system resources, it may be possible to run an instance of this class and an instance of SoundCapture concurrently. This allows for the concurrent capture, signal processing, and playback of audio data.
After calling the appropriate constructor, startPlayback() must be called to initialize the audio system. The putSamples() or putSamplesInt() method should then be repeatedly called to deliver the audio data to the audio output device (speaker or file). The audio samples delivered to putSamples() should be in the proper range, or clipping will occur. putSamples() expects the samples to be in the range (-1, 1). putSamplesInt() expects the samples to be in the range (-2^(bits_per_sample/2), 2^(bits_per_sample/2)), where bits_per_sample is the number of bits per sample. Note that it is possible (but probably not useful) to interleave calls to putSamples() and putSamplesInt(). Finally, after no more audio playback is desired, stopPlayback() should be called to free up audio system resources.
Note: Requires Java 2 v1.3.0 or later.
SoundCapture
Yellow (cxh) |
Yellow (vogel) |
Constructor and Description |
---|
SoundPlayback(float sampleRate,
int sampleSizeInBits,
int channels,
int bufferSize,
int putSamplesSize)
Construct a sound playback object that plays audio through the
computer's speaker.
|
SoundPlayback(java.lang.String fileName,
float sampleRate,
int sampleSizeInBits,
int channels,
int bufferSize,
int putSamplesSize)
Construct a sound playback object that writes audio to
a sound file with the specified name.
|
Modifier and Type | Method and Description |
---|---|
void |
putSamples(double[][] putSamplesArray)
Play an array of audio samples.
|
void |
putSamplesInt(int[][] putSamplesArray)
Play an array of audio samples.
|
void |
startPlayback()
Perform initialization for the playback of audio data.
|
void |
stopPlayback()
Stop playing/writing audio.
|
public SoundPlayback(float sampleRate, int sampleSizeInBits, int channels, int bufferSize, int putSamplesSize)
sampleRate
- Sample rate in Hz. Must be in the range: 8000
to 48000.sampleSizeInBits
- Number of bits per sample (valid choices are
8 or 16).channels
- Number of audio channels. 1 for mono, 2 for
stereo, etc.bufferSize
- Requested size of the internal audio input
buffer in samples. This controls the latency (delay from
the time putSamples() is called until the audio is
actually heard). A lower bound on the latency is given by
(bufferSize / sampleRate) seconds.
Ideally, the
smallest value that gives acceptable performance (no underflow)
should be used. Typical values are about 1/10 th the sample
rate. For example, at 44100 Hz sample rate, a typical buffer
size value might be 4410.putSamplesSize
- Size of the array parameter of
putSamples(). For performance reasons, the size should
be chosen smaller than bufferSize. Typical values
are 1/2 to 1/16 th of bufferSize.public SoundPlayback(java.lang.String fileName, float sampleRate, int sampleSizeInBits, int channels, int bufferSize, int putSamplesSize)
Note that the audio data will not actually be saved to file, fileName, until stopPlayback() is called. If an unknown audio format is used, an exception will be thrown in stopPlayback().
fileName
- The file name to create. If the file already
exists, overwrite it. Valid sound file formats are WAVE (.wav),
AIFF (.aif, .aiff), AU (.au). The file format to write is
determined automatically from the file extension.sampleRate
- Sample rate in Hz. Must be in the range: 8000
to 48000.sampleSizeInBits
- Number of bits per sample (valid choices are
8 or 16).channels
- Number of audio channels. 1 for mono, 2 for
stereo.bufferSize
- Requested size of the internal audio input
buffer in samples. This controls the latency (delay from
the time putSamples() is called until the audio is
actually heard). A lower bound on the latency is given by
(bufferSize / sampleRate) seconds.
Ideally, the
smallest value that gives acceptable performance (no underflow)
should be used. Typical values are about 1/10 th the sample
rate. For example, at 44100 Hz sample rate, a typical buffer
size value might be 4410.putSamplesSize
- Size of the array parameter of
putSamples(). There is no restriction on the value of
this parameter, but typical values are 64-2024.public void putSamples(double[][] putSamplesArray) throws java.io.IOException, java.lang.IllegalStateException
If the "write audio to file" constructor was used, then append the audio data contained in putSamplesArray to the sound file specified in the constructor. Note that underflow cannot occur for this case.
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.
putSamplesArray
- A two dimensional array containing
the samples to play or write to a file. The first index
represents the channel number (0 for first channel, 1 for
second channel, etc.). 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. putSamplesArray should be a
rectangular array such that putSamplesArray.length() gives
the number of channels and putSamplesArray[n].length() is
equal to putSamplesSize, for all channels n. This
is not actually checked, however.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.public void putSamplesInt(int[][] putSamplesArray) throws java.io.IOException, java.lang.IllegalStateException
If the "write audio to file" constructor was used, then append the audio data contained in putSamplesArray to the sound file specified in the constructor.
The samples should be in the range (-2^(bits_per_sample/2), 2^(bits_per_sample/2)). Samples that are outside this range will be hard-clipped.
putSamplesArray
- A two dimensional array containing
the samples to play or write to a file. The first index
represents the channel number (0 for first channel, 1 for
second channel, etc.). 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. putSamplesArray should be a
rectangular array such that putSamplesArray.length() gives
the number of channels and putSamplesArray[n].length() is
equal to putSamplesSize, for all channels n. This
is not actually checked, however.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.public void startPlayback() throws java.io.IOException, java.lang.IllegalStateException
java.io.IOException
- If there is a problem setting up
the system for audio playback. This will occur if
a file cannot be opened or if the audio out port cannot
be accessed.java.lang.IllegalStateException
- If this method is called
more than once between invocations of stopCapture().public void stopPlayback() throws java.io.IOException
If the "write audio data to file" constructor was used, then the sound file specified by the constructor is saved and closed.
java.io.IOException
- If there is a problem closing the
audio resources, or if the "write audio data
to file" constructor was used and the sound file has an
unsupported format.