comirva.audio.util
Class ReducedAudioInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by javax.sound.sampled.AudioInputStream
          extended by comirva.audio.util.ReducedAudioInputStream
All Implemented Interfaces:
Closeable

public class ReducedAudioInputStream
extends AudioInputStream

Reduced Audio Input Stream

Description:

This wrapper tries to downsample a PCM audio stream to 11025 Hz and 1 channel. Downsampling can only be performed for interger valued ratios. Channel reduction is only supported form two channel to one. The number of bits per sample is reduced to 16.

This class makes the whole audio package more independent from tritonus_remaining.jar. Thanks to its specialized functionality this class is about 10% faster and more stable than the Tritonus package.


Field Summary
protected  int frameSize
           
protected  AudioInputStream in
           
protected  int INITIAL_BUFFER_SIZE
           
protected  int sampleSize
           
protected  AudioFormat sourceFormat
           
protected  AudioFormat targetFormat
           
 
Fields inherited from class javax.sound.sampled.AudioInputStream
format, frameLength, framePos
 
Constructor Summary
ReducedAudioInputStream(AudioInputStream ais, float targetSampleRate)
          Creates a new ReducedAudioInputStream for a given AudioInputStream and for a given target sample rate.
 
Method Summary
 int available()
          Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.
 void close()
          Closes this input stream and releases any system resources associated with the stream.
 AudioFormat getFormat()
          Obtains the audio format of the sound data in this audio input stream.
 long getFrameLength()
          Obtains the length of the stream, expressed in sample frames rather than bytes.
 boolean markSupported()
          Tests if this input stream supports the mark and reset methods.
 int read()
          Reads the next byte of data from the input stream.
 int read(byte[] b)
          Reads some number of bytes from the input stream and stores them into the buffer array b.
 int read(byte[] b, int off, int len)
          Reads up to len bytes of data from the input stream into an array of bytes.
 long skip(long n)
          Skips over and discards n bytes of data from this input stream.
 
Methods inherited from class javax.sound.sampled.AudioInputStream
mark, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

in

protected AudioInputStream in

sourceFormat

protected AudioFormat sourceFormat

targetFormat

protected AudioFormat targetFormat

frameSize

protected int frameSize

sampleSize

protected int sampleSize

INITIAL_BUFFER_SIZE

protected final int INITIAL_BUFFER_SIZE
See Also:
Constant Field Values
Constructor Detail

ReducedAudioInputStream

public ReducedAudioInputStream(AudioInputStream ais,
                               float targetSampleRate)
Creates a new ReducedAudioInputStream for a given AudioInputStream and for a given target sample rate.

Parameters:
ais - AudioInputStream the original audio input stream
targetSampleRate - float the target sample rate
Method Detail

available

public int available()
              throws IOException
Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.

Overrides:
available in class AudioInputStream
Returns:
the number of bytes that can be read from this input stream without blocking.
Throws:
IOException - if an I/O error occurs.

close

public void close()
           throws IOException
Closes this input stream and releases any system resources associated with the stream.

Specified by:
close in interface Closeable
Overrides:
close in class AudioInputStream
Throws:
IOException - if an I/O error occurs.

markSupported

public boolean markSupported()
Tests if this input stream supports the mark and reset methods.

Overrides:
markSupported in class AudioInputStream
Returns:
true if this stream instance supports the mark and reset methods; false otherwise.

read

public int read()
         throws IOException
Reads the next byte of data from the input stream.

Overrides:
read in class AudioInputStream
Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
IOException - if an I/O error occurs.

read

public int read(byte[] b)
         throws IOException
Reads some number of bytes from the input stream and stores them into the buffer array b.

Overrides:
read in class AudioInputStream
Parameters:
b - the buffer into which the data is read.
Returns:
the total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached.
Throws:
IOException - if an I/O error occurs.

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Reads up to len bytes of data from the input stream into an array of bytes.

Overrides:
read in class AudioInputStream
Parameters:
b - the buffer into which the data is read.
off - the start offset in array b at which the data is written.
len - the maximum number of bytes to read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
IOException - if an I/O error occurs.

skip

public long skip(long n)
          throws IOException
Skips over and discards n bytes of data from this input stream. This method only skips on frame basis, if the original stream is encoded. Therefore you cannot skip a precise number of bytes. You should at least skip one frame, or no bytes will be skipped at all. The real number of skipped bytes will be returned.

Overrides:
skip in class AudioInputStream
Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
IOException - if an I/O error occurs.

getFormat

public AudioFormat getFormat()
Obtains the audio format of the sound data in this audio input stream.

Overrides:
getFormat in class AudioInputStream
Returns:
an audio format object describing this stream's format

getFrameLength

public long getFrameLength()
Obtains the length of the stream, expressed in sample frames rather than bytes.

Overrides:
getFrameLength in class AudioInputStream
Returns:
the length in sample frames