|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectptolemy.media.javasound.SoundReader
public class SoundReader
This class is a buffer that supports the reading of audio samples from a sound file that is specified as a URL. Specifically, this buffer supports the reading of double-valued audio samples. The maximum valid range of sample values is from -1.0 to 1.0. Any values outside of this range will be hard-clipped to fall within this range.
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, specified as a URL, is given as a constructor parameter. The constructor also takes an array length parameter, which is explained below. The constructor will attempt to open the specified file.
After invoking the constructor, the getSamples() method should be repeatedly invoked to read samples from the specified sound file. The getSamples() 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, getSamplesArray[i].length, must be equal to the constructor parameter getSamplesArraySize. Otherwise an exception will occur. When the end of the sound file is reached, this method will return null.
The getChannels(), getSampleRate(), and getBitsPerSample() methods may be invoked at any time to obtain information about the format of the sound file.
When no more samples are desired, the closeFile() method should be invoked to close the sound file. An exception will occur if getSamples() is invoked at any point after closeFile() is invoked.
Security Issues
Applications have no restrictions on the capturing or playback of audio. Applets, however, may by default only capture audio from a file specified as a URL on the same machine as the one the applet was loaded from. Applet code is not allowed to read or write native files. The .java.policy file may be modified to grant applets more privileges.
Note: Requires Java 2 v1.3.0 or later.
SoundWriter
Red (cxh) |
Red (vogel) |
Field Summary | |
---|---|
private double[][] |
_audioInDoubleArray
|
private javax.sound.sampled.AudioInputStream |
_audioInputStream
|
private byte[] |
_b
|
private int |
_bytesPerSample
|
private int |
_channels
|
private byte[] |
_data
|
private boolean |
_debug
|
private double[][] |
_doubleArray
|
private int |
_frameSizeInBytes
|
private boolean |
_isAudioCaptureActive
|
private int |
_productionRate
|
private javax.sound.sampled.AudioInputStream |
_properFormatAudioInputStream
|
private float |
_sampleRate
|
private int |
_sampleSizeInBits
|
Constructor Summary | |
---|---|
SoundReader(java.lang.String sourceURL,
int getSamplesArraySize)
Construct a sound reader object that reads audio samples from a sound file specified as a string describing a URL and open the file at the specified URL. |
|
SoundReader(java.net.URL soundURL,
int getSamplesArraySize)
Construct a sound reader object that reads audio samples from a sound file specified as a URL and open the file at the specified URL. |
Method Summary | |
---|---|
private double[][] |
_byteArrayToDoubleArray(byte[] byteArray,
int bytesPerSample,
int channels)
|
private void |
_openFileFromURL(java.net.URL soundURL)
|
void |
closeFile()
Close the file at the specified URL. |
int |
getBitsPerSample()
Return the number of bits per audio sample. |
int |
getChannels()
Return the number of audio channels. |
float |
getSampleRate()
Return the sampling rate in Hz. |
double[][] |
getSamples()
Return an array of captured audio samples. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private javax.sound.sampled.AudioInputStream _properFormatAudioInputStream
private javax.sound.sampled.AudioInputStream _audioInputStream
private int _productionRate
private double[][] _audioInDoubleArray
private byte[] _data
private int _frameSizeInBytes
private int _sampleSizeInBits
private float _sampleRate
private int _channels
private int _bytesPerSample
private boolean _isAudioCaptureActive
private byte[] _b
private double[][] _doubleArray
private boolean _debug
Constructor Detail |
---|
public SoundReader(java.lang.String sourceURL, int getSamplesArraySize) throws java.io.IOException
sourceURL
- A string describing a URL.getSamplesArraySize
- The number of samples per channel
returned by getSamples().
java.io.IOException
- If opening the sourceURL throws it,
if the file format is not supported or if there is no audio
to play.public SoundReader(java.net.URL soundURL, int getSamplesArraySize) throws java.io.IOException
soundURL
- The URL of a sound file.getSamplesArraySize
- The number of samples per channel
returned by getSamples().
java.io.IOException
- If opening the sourceURL throws it,
if the file format is not supported or if there is no audio
to play.Method Detail |
---|
public int getChannels() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- If this method is called
before openFile() is called or after closeFile()
is called.public float getSampleRate() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- If this method is called
after closeFile()
is called.public double[][] getSamples() throws java.io.IOException, java.lang.IllegalStateException
The array size is set by the getSamplesSize parameter in the constructor.
java.io.IOException
- If there is a problem reading the audio
samples from the input file.
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
file.public int getBitsPerSample() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- If this method is called
after closeFile()
is called.private void _openFileFromURL(java.net.URL soundURL) throws java.io.IOException
java.io.IOException
private double[][] _byteArrayToDoubleArray(byte[] byteArray, int bytesPerSample, int channels)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |