public class SoundWriter
extends java.lang.Object
Supported file types
Valid sound file formats are WAVE (.wav), AIFF (.aif, .aiff), AU (.au). Valid sample rates are 8000, 11025, 22050, 44100, and 48000 Hz. Both 8 bit and 16 bit audio are supported. Mono and stereo files are supported.
Usage
The path to the sound file to write is given as a constructor parameter, along with parameters that specify the desired audio format. The constructor also takes an array length parameter, which is explained below.
After invoking the constructor, the putSamples() method should be repeatedly invoked to write samples to the specified sound file. This method is blocking, so it will not return until the samples have been written. The putSamples() method takes a multidimensional array as a parameter. 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 each channel i, the size of the array, putSamplesArray[i].length, must be equal to the constructor parameter putSamplesArraySize. Otherwise an exception will occur. Thus, each call to putSamples() writes putSamplesArraySize on each channel. It should be noted that the putSamples() method does not write samples directly to a sound file, but instead writes samples to an internal array. The internal array of samples is written to the sound file by the closeFile() method.
The closeFile() method should be invoked when no more samples need to be written. This method will write the internal array of samples to the output file and close the file. It is not possible to write any more samples after closeFile() has been called. An exception will occur if putSamples() is invoked at any point after closeFile() is invoked.
Security issues
Applications have no restrictions on the capturing or playback of audio. Applet code is not allowed to write native files by default. The .java.policy file must be modified to grant applets more privileges.
Note: Requires Java 2 v1.3.0 or later.
SoundCapture
Red (cxh) |
Red (vogel) |
Constructor and Description |
---|
SoundWriter(java.lang.String fileName,
float sampleRate,
int bitsPerSample,
int channels,
int putSamplesArraySize)
Construct a sound writer object with the specified name.
|
Modifier and Type | Method and Description |
---|---|
void |
closeFile()
Open a the file specified in the constructor for writing,
write the accumulated audio samples (obtained via
putSamples()) to the file specified in the constructor,
and close the file.
|
void |
putSamples(double[][] putSamplesArray)
Append the audio data contained in putSamplesArray
to an internal array.
|
public SoundWriter(java.lang.String fileName, float sampleRate, int bitsPerSample, int channels, int putSamplesArraySize)
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.bitsPerSample
- Number of bits per sample (valid choices are
8 or 16).channels
- Number of audio channels. 1 for mono, 2 for
stereo.putSamplesArraySize
- 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.lang.IllegalStateException
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. An exception
will occur if this is not the case.java.lang.IllegalStateException
- If closeFile() has already
been called.public void closeFile() throws java.io.IOException
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.