org.tritonus.share.sampled.convert
Class TSynchronousFilteredAudioInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by javax.sound.sampled.AudioInputStream
          extended by org.tritonus.share.sampled.convert.TAudioInputStream
              extended by org.tritonus.share.sampled.convert.TSynchronousFilteredAudioInputStream
All Implemented Interfaces:
Closeable, FloatSampleInput

public abstract class TSynchronousFilteredAudioInputStream
extends TAudioInputStream
implements FloatSampleInput

Base class for types of audio filter/converter that translate one frame to another frame.
It provides all the transformation of frame sizes.
It does NOT handle different sample rates of original stream and this stream !

Author:
Florian Bomers

Field Summary
protected  byte[] m_buffer
          The intermediate buffer used during convert actions (if not convertInPlace is used).
 
Fields inherited from class javax.sound.sampled.AudioInputStream
format, frameLength, framePos, frameSize, stream
 
Constructor Summary
TSynchronousFilteredAudioInputStream(AudioInputStream audioInputStream, AudioFormat newFormat)
           
 
Method Summary
 int available()
           
 void close()
           
protected abstract  int convert(byte[] inBuffer, byte[] outBuffer, int outByteOffset, int inFrameCount)
          Override this method to do the actual conversion.
protected  void convert(FloatSampleBuffer buffer, int offset, int count)
          Override this method to do the actual conversion in the FloatSampleBuffer.
protected  void convertInPlace(byte[] buffer, int byteOffset, int frameCount)
          Override this method to provide in-place conversion of samples.
protected  boolean enableConvertInPlace()
          descendant classes should call this method if they have implemented convertInPlace().
protected  void enableFloatConversion()
          Descendant classes should call this method if they have implemented convert(FloatSampleBuffer).
 int getChannels()
           
 AudioFormat getOriginalFormat()
           
 AudioInputStream getOriginalStream()
           
 float getSampleRate()
           
 boolean isDone()
          Determine if this stream has reached its end.
 void mark(int readlimit)
           
 boolean markSupported()
           
 int read()
           
 int read(byte[] abData, int nOffset, int nLength)
          Read nLength bytes that will be the converted samples of the original InputStream.
 void read(FloatSampleBuffer buffer)
          Fill the entire buffer with audio data.
 void read(FloatSampleBuffer buffer, int offset, int sampleCount)
          read sampleCount converted samples at the specified offset.
 void reset()
           
 long skip(long nSkip)
           
 
Methods inherited from class org.tritonus.share.sampled.convert.TAudioInputStream
properties, setProperty
 
Methods inherited from class javax.sound.sampled.AudioInputStream
getFormat, getFrameLength, read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_buffer

protected byte[] m_buffer
The intermediate buffer used during convert actions (if not convertInPlace is used). It remains until this audioStream is closed or destroyed and grows with the time - it always has the size of the largest intermediate buffer ever needed.

Constructor Detail

TSynchronousFilteredAudioInputStream

public TSynchronousFilteredAudioInputStream(AudioInputStream audioInputStream,
                                            AudioFormat newFormat)
Method Detail

enableConvertInPlace

protected boolean enableConvertInPlace()
descendant classes should call this method if they have implemented convertInPlace(). ConvertInPlace will only be used if the converted frame size is larger than the original frame size.


enableFloatConversion

protected void enableFloatConversion()
Descendant classes should call this method if they have implemented convert(FloatSampleBuffer). That convert method will only be called if this class' FloatSampleInput.read() is used.


convert

protected abstract int convert(byte[] inBuffer,
                               byte[] outBuffer,
                               int outByteOffset,
                               int inFrameCount)
Override this method to do the actual conversion. inBuffer starts always at index 0 (it is an internal buffer) You should always override this. inFrameCount is the number of frames in inBuffer. These frames are of the format originalFormat.

Returns:
the resulting number of frames converted and put into outBuffer. The return value is in the format of this stream.

convertInPlace

protected void convertInPlace(byte[] buffer,
                              int byteOffset,
                              int frameCount)
Override this method to provide in-place conversion of samples. To use it, call "enableConvertInPlace()". It will only be used when input bytes per frame >= output bytes per frame. This method must always convert frameCount frames, so no return value is necessary.


convert

protected void convert(FloatSampleBuffer buffer,
                       int offset,
                       int count)
Override this method to do the actual conversion in the FloatSampleBuffer. Use buffer's methods to shrink the number of samples, if necessary. This method will only be called if this stream is accessed by way of FloatSampleInput methods.

Parameters:
buffer - the buffer to convert
offset - the offset in buffer in samples
count - the number of samples in buffer to convert

read

public int read()
         throws IOException
Overrides:
read in class AudioInputStream
Throws:
IOException

getOriginalStream

public AudioInputStream getOriginalStream()

getOriginalFormat

public AudioFormat getOriginalFormat()

read

public final int read(byte[] abData,
                      int nOffset,
                      int nLength)
               throws IOException
Read nLength bytes that will be the converted samples of the original InputStream. When nLength is not an integral number of frames, this method may read less than nLength bytes.

Overrides:
read in class AudioInputStream
Throws:
IOException

skip

public long skip(long nSkip)
          throws IOException
Overrides:
skip in class AudioInputStream
Throws:
IOException

available

public int available()
              throws IOException
Overrides:
available in class AudioInputStream
Throws:
IOException

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class AudioInputStream
Throws:
IOException

mark

public void mark(int readlimit)
Overrides:
mark in class AudioInputStream

reset

public void reset()
           throws IOException
Overrides:
reset in class AudioInputStream
Throws:
IOException

markSupported

public boolean markSupported()
Overrides:
markSupported in class AudioInputStream

getChannels

public int getChannels()
Specified by:
getChannels in interface FloatSampleInput
Returns:
the number of audio channels of the audio data that this stream provides. If it can support a variable number of channels, this method returns AudioSystem.NOT_SPECIFIED.

getSampleRate

public float getSampleRate()
Specified by:
getSampleRate in interface FloatSampleInput
Returns:
the sample rate of the audio data that this stream provides. If it can support different sample rates, this method returns a negative number, e.g. AudioSystem.NOT_SPECIFIED.

isDone

public boolean isDone()
Description copied from interface: FloatSampleInput
Determine if this stream has reached its end. If true, subsequent calls to read() will return 0-sized buffers.

Specified by:
isDone in interface FloatSampleInput
Returns:
true if this stream reached its end.

read

public void read(FloatSampleBuffer buffer,
                 int offset,
                 int sampleCount)
read sampleCount converted samples at the specified offset. The current implementation requires that offset is 0 and sampleCount == buffer.getSampleCount().

Specified by:
read in interface FloatSampleInput
Parameters:
buffer - the buffer to be filled
offset - the start index, in samples, where to start filling the buffer
sampleCount - the number fo samples to fill into the buffer

read

public void read(FloatSampleBuffer buffer)
Description copied from interface: FloatSampleInput
Fill the entire buffer with audio data. If fewer samples are read, this method will use buffer.changeSampleCount() to adjust the size of the buffer. If no samples can be written to the buffer, the buffer's sample count will be set to 0.

The buffer's channel count and sample rate may not be changed by the implementation of this method.

Specified by:
read in interface FloatSampleInput
Parameters:
buffer - the buffer to be filled


Copyright © 2012. All Rights Reserved.