com.groovemanager.spi.floatEncoding
Class FloatConversionInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.groovemanager.spi.floatEncoding.FloatConversionInputStream
All Implemented Interfaces:
java.io.Closeable

class FloatConversionInputStream
extends java.io.InputStream

This class is used for converion of PCM_SIGNED encoded audio data to PCM_FLOAT encoding and vice versa. As an additional bonus it can provide PCM_SIGNED to PCM_SIGNED and PCM_FLOAT to PCM_FLOAT conversion.

Author:
Manu Robledo

Field Summary
private static float MAXINT
          Constants used for conversion
private static float MAXINTBACK
          Constants used for conversion
private static double MAXLONG
          Constants used for conversion
private static double MAXLONGBACK
          Constants used for conversion
private  byte[] readTemp
          Temporary buffer for reading from the source stream
private  javax.sound.sampled.AudioInputStream source
          The source stream to be converted
private  javax.sound.sampled.AudioFormat sourceFormat
          AudioFormat of the source data
private  javax.sound.sampled.AudioFormat targetFormat
          AudioFormat of the source data
 
Constructor Summary
FloatConversionInputStream(javax.sound.sampled.AudioInputStream stream, javax.sound.sampled.AudioFormat targetFormat)
          Constructs a new ConversionInputStream from the given AudioInputStream being converted to the given Format
 
Method Summary
 int available()
           
 void close()
           
private  void convert(byte[] from, byte[] to, int off, int length)
          Convert the data depending on the source and target format while copying it from the source array to the target array
private static void deleteBytes(byte[] from, byte[] to, int off, int length, int sampleSize, int zeroCount, boolean isBigEndian)
          Decrease the sample size of the given audio data by copying it into another smaller array and cutting out the least significant bytes
private static void doubleToLong(byte[] from, int off, int length, boolean isBigEndian)
          Convert the data from 64 Bit double to 64 Bit long
private static void floatToInt(byte[] from, int off, int length, boolean isBigEndian)
          Convert the data from 32 Bit float to 32 Bit integer
private  long getConvertedLength(long len)
          Get the length in bytes that will be needed for converting the given length of unconverted data.
private  long getUnconvertedLength(long len)
          Get the length in bytes of unconverted data which leads to converted data of the given length
private static void insertZeros(byte[] from, byte[] to, int off, int length, int sampleSize, int zeroCount, boolean isBigEndian)
          Increase the sample size of the given audio data by copying it into another larger array and filling the samples up with zeros in the least significant bytes
private static void intToFloat(byte[] from, int off, int length, boolean isBigEndian)
          Convert the data from 32 Bit integer to 32 Bit float
private static void longToDouble(byte[] from, int off, int length, boolean isBigEndian)
          Convert the data from 64 Bit long to 64 Bit double
 void mark(int readlimit)
           
 boolean markSupported()
           
 int read()
          Frame size is at least 2 bytes so this method mustn't be called
 int read(byte[] b)
           
 int read(byte[] b, int off, int len)
           
 void reset()
           
 long skip(long n)
           
private static void swap(byte[] from, int off, int length, int sampleSize)
          Swap the the samples inside the given array from little endian to big endian or vice versa.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAXINT

private static final float MAXINT
Constants used for conversion

See Also:
Constant Field Values

MAXINTBACK

private static final float MAXINTBACK
Constants used for conversion

See Also:
Constant Field Values

MAXLONG

private static final double MAXLONG
Constants used for conversion

See Also:
Constant Field Values

MAXLONGBACK

private static final double MAXLONGBACK
Constants used for conversion

See Also:
Constant Field Values

readTemp

private byte[] readTemp
Temporary buffer for reading from the source stream


source

private final javax.sound.sampled.AudioInputStream source
The source stream to be converted


sourceFormat

private javax.sound.sampled.AudioFormat sourceFormat
AudioFormat of the source data


targetFormat

private javax.sound.sampled.AudioFormat targetFormat
AudioFormat of the source data

Constructor Detail

FloatConversionInputStream

public FloatConversionInputStream(javax.sound.sampled.AudioInputStream stream,
                                  javax.sound.sampled.AudioFormat targetFormat)
Constructs a new ConversionInputStream from the given AudioInputStream being converted to the given Format

Parameters:
stream - The Source AudioInputStream to be converted
targetFormat - The Format into which the Stream should be converted
Method Detail

available

public int available()
              throws java.io.IOException
Overrides:
available in class java.io.InputStream
Throws:
java.io.IOException
See Also:
InputStream.available()

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.io.IOException
See Also:
InputStream.close()

convert

private void convert(byte[] from,
                     byte[] to,
                     int off,
                     int length)
Convert the data depending on the source and target format while copying it from the source array to the target array

Parameters:
from - The array of audio data that should be converted
to - The array to copy the converted data to
off - offset inside the target array
length - The number of bytes to be processed

deleteBytes

private static void deleteBytes(byte[] from,
                                byte[] to,
                                int off,
                                int length,
                                int sampleSize,
                                int zeroCount,
                                boolean isBigEndian)
Decrease the sample size of the given audio data by copying it into another smaller array and cutting out the least significant bytes

Parameters:
from - The source array
to - The target array
off - offset inside the target array
length - The length in bytes of the data to be processed
sampleSize - The new sampleSize
zeroCount - The number of bytes to cut from each sample
isBigEndian - Indicates whether the bytes should be cut from the left or from the right side

doubleToLong

private static void doubleToLong(byte[] from,
                                 int off,
                                 int length,
                                 boolean isBigEndian)
Convert the data from 64 Bit double to 64 Bit long

Parameters:
from - The data to be converted
off - Offset inside the data array
length - The number of bytes to be processed
isBigEndian - Indicates whether the samples are stored in big endian or little endian

floatToInt

private static void floatToInt(byte[] from,
                               int off,
                               int length,
                               boolean isBigEndian)
Convert the data from 32 Bit float to 32 Bit integer

Parameters:
from - The data to be converted
off - Offset inside the data array
length - The number of bytes to be processed
isBigEndian - Indicates whether the samples are stored in big endian or little endian

getConvertedLength

private long getConvertedLength(long len)
Get the length in bytes that will be needed for converting the given length of unconverted data.

Parameters:
len - The length in bytes of the unconverted data.
Returns:
The length in bytes of the converted data.

getUnconvertedLength

private long getUnconvertedLength(long len)
Get the length in bytes of unconverted data which leads to converted data of the given length

Parameters:
len - The length in bytes of the converted data.
Returns:
The length in bytes of the unconverted data.

insertZeros

private static void insertZeros(byte[] from,
                                byte[] to,
                                int off,
                                int length,
                                int sampleSize,
                                int zeroCount,
                                boolean isBigEndian)
Increase the sample size of the given audio data by copying it into another larger array and filling the samples up with zeros in the least significant bytes

Parameters:
from - The source array
to - The target array
off - offset inside the target array
length - The length in bytes of the data to be processed
sampleSize - The new sampleSize
zeroCount - The number of bytes to fill up per sample
isBigEndian - Indicates whether the zeros should be filled from the left or from the right side

intToFloat

private static void intToFloat(byte[] from,
                               int off,
                               int length,
                               boolean isBigEndian)
Convert the data from 32 Bit integer to 32 Bit float

Parameters:
from - The data to be converted
off - Offset inside the data array
length - The number of bytes to be processed
isBigEndian - Indicates whether the samples are stored in big endian or little endian

longToDouble

private static void longToDouble(byte[] from,
                                 int off,
                                 int length,
                                 boolean isBigEndian)
Convert the data from 64 Bit long to 64 Bit double

Parameters:
from - The data to be converted
off - Offset inside the data array
length - The number of bytes to be processed
isBigEndian - Indicates whether the samples are stored in big endian or little endian

mark

public void mark(int readlimit)
Overrides:
mark in class java.io.InputStream
See Also:
InputStream.mark(int)

markSupported

public boolean markSupported()
Overrides:
markSupported in class java.io.InputStream
See Also:
InputStream.markSupported()

read

public int read()
         throws java.io.IOException
Frame size is at least 2 bytes so this method mustn't be called

Specified by:
read in class java.io.InputStream
Throws:
java.io.IOException
See Also:
InputStream.read()

read

public int read(byte[] b)
         throws java.io.IOException
Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException
See Also:
InputStream.read(byte[])

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException
See Also:
InputStream.read(byte[], int, int)

reset

public void reset()
           throws java.io.IOException
Overrides:
reset in class java.io.InputStream
Throws:
java.io.IOException
See Also:
InputStream.reset()

skip

public long skip(long n)
          throws java.io.IOException
Overrides:
skip in class java.io.InputStream
Throws:
java.io.IOException
See Also:
InputStream.skip(long)

swap

private static void swap(byte[] from,
                         int off,
                         int length,
                         int sampleSize)
Swap the the samples inside the given array from little endian to big endian or vice versa.

Parameters:
from - The array of audio data
off - The start address inside the array
length - The length in bytes of the data to be processed
sampleSize - The size of one sample in bytes