ch.aplu.util
Class SoundRecorder

java.lang.Object
  extended by ch.aplu.util.SoundRecorder

public class SoundRecorder
extends java.lang.Object

Class for recording sound and save it to a WAV file.


Constructor Summary
SoundRecorder(javax.sound.sampled.AudioFormat audioFormat)
          Same as SoundRecorder(bufSize, audioFormat, mixerIndex) with default buffer size (10000 bytes) and default sound recording device.
SoundRecorder(javax.sound.sampled.AudioFormat audioFormat, int mixerIndex)
          Same as SoundRecorder(bufSize, audioFormat, mixerIndex) with default buffer size (10000 bytes).
SoundRecorder(int bufSize, javax.sound.sampled.AudioFormat audioFormat)
          Same as SoundRecorder(bufSize, audioFormat, mixerIndex) with default sound recording device.
SoundRecorder(int bufSize, javax.sound.sampled.AudioFormat audioFormat, int mixerIndex)
          Create a recorder instance with given audioFormat using the given mixer index (sound device).
 
Method Summary
 void addSoundSampleListener(SoundSampleListener listener)
          Register a SoundSampleLister to get notifications for each recorded sound sample (default sample size: 10000 bytes).
 void capture()
          Same as capture(ByteArrayOutputStream data), but store data in an internal buffer that can be read-out by getRecordedData().
 void capture(java.io.ByteArrayOutputStream data)
          Start capturing the sound in a separate capture thread and store data in the given stream.
static java.lang.String[] getAvailableMixers()
          Return a list of the names of available mixers (sound devices).
 byte[] getCapturedBytes()
          Return a reference to the internal sound recording buffer.
 javax.sound.sampled.AudioFormat getFormat()
          Return the current audio format.
 int getMixerIndex()
           
 void stopCapture()
          Stop the capturing process and return after the capture thread terminates.
 boolean writeWavFile(byte[] data, java.io.File file)
          Write the sound data from byte array into the given file in WAV format.
 boolean writeWavFile(byte[] data, java.lang.String filename)
          Write the sound data in the given byte array into file with given name in WAV format.
 boolean writeWavFile(java.io.ByteArrayOutputStream data, java.io.File file)
          Write the sound data the given stream into the given file in WAV format.
 boolean writeWavFile(java.io.ByteArrayOutputStream data, java.lang.String filename)
          Write the sound data in the given stream into file with given name in WAV format.
 boolean writeWavFile(int[] data, java.io.File file)
          Write the sound data from int array into the given file in WAV format.
 boolean writeWavFile(int[] data, java.lang.String filename)
          Write the sound data from int array into the given file with given name in WAV format.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SoundRecorder

public SoundRecorder(javax.sound.sampled.AudioFormat audioFormat)
Same as SoundRecorder(bufSize, audioFormat, mixerIndex) with default buffer size (10000 bytes) and default sound recording device.


SoundRecorder

public SoundRecorder(int bufSize,
                     javax.sound.sampled.AudioFormat audioFormat)
Same as SoundRecorder(bufSize, audioFormat, mixerIndex) with default sound recording device.


SoundRecorder

public SoundRecorder(javax.sound.sampled.AudioFormat audioFormat,
                     int mixerIndex)
Same as SoundRecorder(bufSize, audioFormat, mixerIndex) with default buffer size (10000 bytes).


SoundRecorder

public SoundRecorder(int bufSize,
                     javax.sound.sampled.AudioFormat audioFormat,
                     int mixerIndex)
Create a recorder instance with given audioFormat using the given mixer index (sound device). To find the mixer indices of all installed sound devices call getAvailableMixers().
Normally an installed sound device has two entries, one for playing and one for recording. You must use the index for the recording device.

Method Detail

addSoundSampleListener

public void addSoundSampleListener(SoundSampleListener listener)
Register a SoundSampleLister to get notifications for each recorded sound sample (default sample size: 10000 bytes).


capture

public void capture()
             throws javax.sound.sampled.LineUnavailableException
Same as capture(ByteArrayOutputStream data), but store data in an internal buffer that can be read-out by getRecordedData().

Throws:
javax.sound.sampled.LineUnavailableException

getCapturedBytes

public byte[] getCapturedBytes()
Return a reference to the internal sound recording buffer.


capture

public void capture(java.io.ByteArrayOutputStream data)
             throws javax.sound.sampled.LineUnavailableException
Start capturing the sound in a separate capture thread and store data in the given stream. After the capture process is started, the method returns.

Throws:
javax.sound.sampled.LineUnavailableException - if the sound card is not available

stopCapture

public void stopCapture()
Stop the capturing process and return after the capture thread terminates.


writeWavFile

public boolean writeWavFile(int[] data,
                            java.lang.String filename)
Write the sound data from int array into the given file with given name in WAV format. Return true if successful; otherwise false


writeWavFile

public boolean writeWavFile(byte[] data,
                            java.lang.String filename)
Write the sound data in the given byte array into file with given name in WAV format. Return true if successful; otherwise false


writeWavFile

public boolean writeWavFile(java.io.ByteArrayOutputStream data,
                            java.lang.String filename)
Write the sound data in the given stream into file with given name in WAV format.


writeWavFile

public boolean writeWavFile(java.io.ByteArrayOutputStream data,
                            java.io.File file)
Write the sound data the given stream into the given file in WAV format. Return true if successful; otherwise false


writeWavFile

public boolean writeWavFile(byte[] data,
                            java.io.File file)
Write the sound data from byte array into the given file in WAV format. Return true if successful; otherwise false


writeWavFile

public boolean writeWavFile(int[] data,
                            java.io.File file)
Write the sound data from int array into the given file in WAV format. The int samples are converted to 2 bytes data depending on big or little endian format. Return true if successful; otherwise false


getAvailableMixers

public static java.lang.String[] getAvailableMixers()
Return a list of the names of available mixers (sound devices). To select one of these mixers, use its index in this list when constructing the SoundRecorder instance.
Normally an installed sound device has two entries, one for playing and one for recording.


getMixerIndex

public int getMixerIndex()

getFormat

public javax.sound.sampled.AudioFormat getFormat()
Return the current audio format.