GF4J 0.9.4 Beta

gameframe.sound
Class RiffWavFile

java.lang.Object
  |
  +--gameframe.sound.RiffWavFile

public class RiffWavFile
extends java.lang.Object

Class for reading RIFF WAV audio files. Originally taken from my own small audio class library and changed to only handle the reading as other features are not needed in the GameFrame context.

Since:
GameFrame for Java 0.9
Version:
GameFrame for Java 0.9.4

Field Summary
private  short[] m_aWavBuffer
          The wave buffer.
private  int m_averageBytePerSecond
          The average bytes per second in the RIFF file.
private  int m_bitsPerSample
          The bits per sample in the RIFF file.
private  int m_blockAlignment
          The block alignment in the RIFF file.
private  int m_bytesPerSample
          The bytes per sample value in the RIFF file.
private  int m_dataLength
          The length of the RIFF data field.
private  int m_formatLength
          The RIFF format length.
private  java.io.DataInputStream m_inputStream
          The input stream used for reading the file.
private  short m_maxValue
          The maximum sample value in the RIFF file.
private  short m_minValue
          The minimum sample value in the RIFF file.
private  int m_numChannels
          The number of channels in the RIFF file.
private  int m_riffLength
          The RIFF length.
private  int m_samplesPerSecond
          The samples per second in the RIFF file.
private  java.lang.String m_strFileName
          The name of the file.
private  int m_wavFormatTag
          The RIFF wave format tag.
static int WAVE_FORMAT_PCM
          Format tag for Microsoft Pulse Code Modulation (PCM) format (non-proprietary).
 
Constructor Summary
RiffWavFile()
          Constructs a new RIFF wave file without any filename.
 
Method Summary
private  boolean findTag(java.io.DataInputStream inStream, java.lang.String stringToFind)
          Tries to find the specified tag from the specified inputstream.
 short[] getPcmData()
          Returns the raw linear PCM sample data in 16-bit mono format.
 void loadFromInputStream(java.lang.String strFilename)
           
 void loadWAV(java.lang.String filename)
          Loads the RIFFF WAVE file from the specified file.
 void loadWAVURL(java.lang.String strFileName)
          Loads the RIFFF WAVE file from the specified URL.
 void readHeader()
          Reads and parses the RIFF WAVE header.
private  int readIntelInt(java.io.DataInputStream inStream)
          Reads an "Intel formatted" 32-bit integer from the given data input stream.
private  short readIntelShort(java.io.DataInputStream inStream)
          Reads an "Intel formatted" 16-bit short from the given data input stream.
 void resampleTo(int targetSampleRate)
          Sets the sampling rate of this wav data to the given sampling rate
private  short[] resampleTo(short[] a16BitData, int originalSampleRate, int targetSampleRate)
          Resamples the given 16-bit linear PCM sample data from the given sample rate to the given sample rate.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

WAVE_FORMAT_PCM

public static final int WAVE_FORMAT_PCM
Format tag for Microsoft Pulse Code Modulation (PCM) format (non-proprietary).

m_inputStream

private java.io.DataInputStream m_inputStream
The input stream used for reading the file.

m_aWavBuffer

private short[] m_aWavBuffer
The wave buffer.

m_riffLength

private int m_riffLength
The RIFF length.

m_formatLength

private int m_formatLength
The RIFF format length.

m_averageBytePerSecond

private int m_averageBytePerSecond
The average bytes per second in the RIFF file.

m_blockAlignment

private int m_blockAlignment
The block alignment in the RIFF file.

m_bitsPerSample

private int m_bitsPerSample
The bits per sample in the RIFF file.

m_wavFormatTag

private int m_wavFormatTag
The RIFF wave format tag.

m_numChannels

private int m_numChannels
The number of channels in the RIFF file.

m_samplesPerSecond

private int m_samplesPerSecond
The samples per second in the RIFF file.

m_bytesPerSample

private int m_bytesPerSample
The bytes per sample value in the RIFF file.

m_dataLength

private int m_dataLength
The length of the RIFF data field.

m_minValue

private short m_minValue
The minimum sample value in the RIFF file.

m_maxValue

private short m_maxValue
The maximum sample value in the RIFF file.

m_strFileName

private java.lang.String m_strFileName
The name of the file.
Constructor Detail

RiffWavFile

public RiffWavFile()
Constructs a new RIFF wave file without any filename.
Method Detail

loadWAV

public void loadWAV(java.lang.String filename)
             throws java.io.FileNotFoundException,
                    GameFrameException
Loads the RIFFF WAVE file from the specified file.
Parameters:
filename - The name of the RIFF WAVE file.
Returns:
True if load successfull, false if not.

loadWAVURL

public void loadWAVURL(java.lang.String strFileName)
                throws java.io.FileNotFoundException,
                       GameFrameException
Loads the RIFFF WAVE file from the specified URL.
Parameters:
filename - The name of the RIFF WAVE file.

loadFromInputStream

public void loadFromInputStream(java.lang.String strFilename)
                         throws java.io.FileNotFoundException,
                                GameFrameException

resampleTo

public void resampleTo(int targetSampleRate)
Sets the sampling rate of this wav data to the given sampling rate
Parameters:
frequency - The target sampling rate.

getPcmData

public short[] getPcmData()
Returns the raw linear PCM sample data in 16-bit mono format. Note: Returns the reference to the internal data structure of this object, modifying the data in the data structure will propably make this objects state illegal.
Returns:
Linear PCM sample data.

readHeader

public void readHeader()
                throws GameFrameException
Reads and parses the RIFF WAVE header. When this method returns the size of the actual sample data field has been read and the m_inputStream is about to read the first actual sample data.
Returns:
True if header is can be parsed and format is supported, false otherwise.
Throws:
GameFrameException - Thrown if the header is not correctly read.

resampleTo

private short[] resampleTo(short[] a16BitData,
                           int originalSampleRate,
                           int targetSampleRate)
Resamples the given 16-bit linear PCM sample data from the given sample rate to the given sample rate.
Parameters:
a16BitData - The linear PCM sample data to be resampled.
originalSampleRate - The original sample rate.
targetSampleRate - The targetted sample rate.

readIntelInt

private int readIntelInt(java.io.DataInputStream inStream)
                  throws java.io.IOException
Reads an "Intel formatted" 32-bit integer from the given data input stream.
Parameters:
inStream - The input stream to read from.
Returns:
The integer value that was read.

readIntelShort

private short readIntelShort(java.io.DataInputStream inStream)
                      throws java.io.IOException
Reads an "Intel formatted" 16-bit short from the given data input stream.
Parameters:
inStream - The input stream to read from.
Returns:
The short value that was read.

findTag

private boolean findTag(java.io.DataInputStream inStream,
                        java.lang.String stringToFind)
                 throws java.io.IOException
Tries to find the specified tag from the specified inputstream. If the tag is found this method will exit and the input stream will point to the next value after the tag. If the tag is not found this method will exit with false and the input stream will point to the end of itself.
Parameters:
inStream - The data input stream to read from.
tagToFind - The tag we are looking for.
Returns:
true if the tag was found, false if not.

GF4J 0.9.4 Beta