sound
Class AudioBuffer

java.lang.Object
  extended by sound.AudioBuffer

public class AudioBuffer
extends java.lang.Object

An AudioBuffer is a self-describing fragment of audio in memory. It can be read and written in a manner similar to java.io.ByteArrayInputStream and java.io.ByteArrayOutputStream. It knows how to transcode itself from one audio format to another.

GRJ - 11/5/02 - This code can probably be simplifed somewhat, but my initial goal was to break out this functionality. The goal was to make an interface that works. The implementation can be cleaned up in the next version.


Constructor Summary
AudioBuffer()
           
AudioBuffer(javax.sound.sampled.AudioFormat format)
          Construct an empty AudioBuffer which will hold data of the specified format.
AudioBuffer(javax.sound.sampled.AudioFormat format, byte[] audioByteArray)
          Construct an AudioBuffer containing audio from the supplied byte array.
AudioBuffer(javax.sound.sampled.AudioInputStream ais)
          Construct an AudioBuffer containing audio from the supplied AudioInputStream.
AudioBuffer(java.io.File f)
           
 
Method Summary
 javax.sound.sampled.AudioInputStream getAudioInputStream()
          Obtain an AudioInputStream that contains the AudioBuffer's data.
 byte[] getBytes()
          Obtain a byte array that contains the AudioBuffer's data.
 javax.sound.sampled.Clip getClip(javax.sound.sampled.LineListener ll)
           
 javax.sound.sampled.AudioFormat.Encoding getEncoding()
          Get the AudioFormat.Encoding corresponding to the AudioBuffer's audio data.
 javax.sound.sampled.AudioFormat getFormat()
          Get the audio format of the buffer's data as an AudioFormat object.
 int getFrameSize()
          Get the frame size (bytes per sample) of the AudioBuffer's data.
 float getSampleRate()
          Get the sample rate (samples per second) of the AudioBuffer's data.
 javax.sound.sampled.SourceDataLine getSourceDataLine()
           
static void main(java.lang.String[] args)
           
 void play()
           
 void playClip()
           
 int read(byte[] b, int off, int len)
          Retrieve data from the buffer.
 void transcode(javax.sound.sampled.AudioFormat.Encoding destinationEncoding)
          Convert the encoding of the audio in the buffer to the specified encoding.
 void write(byte[] b, int off, int len)
          Add data to the buffer.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AudioBuffer

public AudioBuffer(javax.sound.sampled.AudioFormat format)
Construct an empty AudioBuffer which will hold data of the specified format.


AudioBuffer

public AudioBuffer()
            throws java.io.IOException,
                   javax.sound.sampled.UnsupportedAudioFileException
Throws:
java.io.IOException
javax.sound.sampled.UnsupportedAudioFileException

AudioBuffer

public AudioBuffer(java.io.File f)
            throws java.io.IOException,
                   javax.sound.sampled.UnsupportedAudioFileException
Throws:
java.io.IOException
javax.sound.sampled.UnsupportedAudioFileException

AudioBuffer

public AudioBuffer(javax.sound.sampled.AudioFormat format,
                   byte[] audioByteArray)
Construct an AudioBuffer containing audio from the supplied byte array.


AudioBuffer

public AudioBuffer(javax.sound.sampled.AudioInputStream ais)
Construct an AudioBuffer containing audio from the supplied AudioInputStream.

Method Detail

getClip

public javax.sound.sampled.Clip getClip(javax.sound.sampled.LineListener ll)
                                 throws java.io.IOException,
                                        javax.sound.sampled.UnsupportedAudioFileException,
                                        javax.sound.sampled.LineUnavailableException
Throws:
java.io.IOException
javax.sound.sampled.UnsupportedAudioFileException
javax.sound.sampled.LineUnavailableException

main

public static void main(java.lang.String[] args)

getSourceDataLine

public javax.sound.sampled.SourceDataLine getSourceDataLine()
                                                     throws javax.sound.sampled.LineUnavailableException
Throws:
javax.sound.sampled.LineUnavailableException

play

public void play()

playClip

public void playClip()
              throws java.io.IOException,
                     javax.sound.sampled.LineUnavailableException,
                     javax.sound.sampled.UnsupportedAudioFileException
Throws:
java.io.IOException
javax.sound.sampled.LineUnavailableException
javax.sound.sampled.UnsupportedAudioFileException

getEncoding

public javax.sound.sampled.AudioFormat.Encoding getEncoding()
Get the AudioFormat.Encoding corresponding to the AudioBuffer's audio data.


getSampleRate

public float getSampleRate()
Get the sample rate (samples per second) of the AudioBuffer's data.


getFrameSize

public int getFrameSize()
Get the frame size (bytes per sample) of the AudioBuffer's data.


getFormat

public javax.sound.sampled.AudioFormat getFormat()
Get the audio format of the buffer's data as an AudioFormat object.


getBytes

public byte[] getBytes()
Obtain a byte array that contains the AudioBuffer's data.


getAudioInputStream

public javax.sound.sampled.AudioInputStream getAudioInputStream()
Obtain an AudioInputStream that contains the AudioBuffer's data.


transcode

public void transcode(javax.sound.sampled.AudioFormat.Encoding destinationEncoding)
Convert the encoding of the audio in the buffer to the specified encoding.

GRJ - 11/5/02 - This is inefficient code. See disclaimer above.

Also, there seems to be a bug in the implementation here (or in the transcoding version of AudioSystem.getAudioInputStream used by the implementation?). The available() method of AudioInputStream does not return a different length after the transcode if the number of bytes per sample changes. I had to do a workaround.


read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Retrieve data from the buffer.

Throws:
java.io.IOException

write

public void write(byte[] b,
                  int off,
                  int len)
Add data to the buffer.