com.softsynth.jsyn.util
Class WAVFileWriter

java.lang.Object
  extended by java.io.OutputStream
      extended by com.softsynth.util.RandomOutputStream
          extended by com.softsynth.jsyn.util.WAVFileWriter
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class WAVFileWriter
extends RandomOutputStream

Writes audio data to a random access file in WAV format. Note that if you are overwriting a RandomAccessFile then you should clear it before you start. This will prevent having the remainder of a longer file stuck at the end of a short file. In Java 1.2 you can call setLength(0).


Constructor Summary
WAVFileWriter(java.io.RandomAccessFile file)
           
 
Method Summary
 void fixSizes()
          Fix RIFF and data chunk sizes based on final size.
static void main(java.lang.String[] args)
           
 void write(short[] samples, int channelsPerFrame, int sampleRate)
          Write a complete WAV file containing the array of short samples.
 void writeDataChunk(short[] samples)
          Write a data chunk to the WAV file containing the given short array.
 void writeDataChunkHeader(int size)
          Write a 'data' chunk header to the WAV file.
 void writeFormatChunk(int bitsPerSample, int channelsPerFrame, int sampleRate)
          Write an 'fmt ' chunk to the WAV file containing the given information.
 void writeHeader(int size)
          Write a 'RIFF' file header and a 'WAVE' ID to the WAV file.
 void writeHeader(int channelsPerFrame, int sampleRate)
          Write a simple WAV header for 16 bit PCM data.
 void writeIntLittle(int n)
          Write a 32 bit intgeer to the stream in Little Endian format.
 void writeShortLittle(short n)
          Write a 16 bit intgeer to the stream in Little Endian format.
 
Methods inherited from class com.softsynth.util.RandomOutputStream
getFilePointer, seek, write, write, write
 
Methods inherited from class java.io.OutputStream
close, flush
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WAVFileWriter

public WAVFileWriter(java.io.RandomAccessFile file)
              throws java.io.IOException
Throws:
java.io.IOException
Method Detail

writeIntLittle

public void writeIntLittle(int n)
                    throws java.io.IOException
Write a 32 bit intgeer to the stream in Little Endian format.

Throws:
java.io.IOException

writeShortLittle

public void writeShortLittle(short n)
                      throws java.io.IOException
Write a 16 bit intgeer to the stream in Little Endian format.

Throws:
java.io.IOException

writeFormatChunk

public void writeFormatChunk(int bitsPerSample,
                             int channelsPerFrame,
                             int sampleRate)
                      throws java.io.IOException
Write an 'fmt ' chunk to the WAV file containing the given information.

Throws:
java.io.IOException

writeDataChunk

public void writeDataChunk(short[] samples)
                    throws java.io.IOException
Write a data chunk to the WAV file containing the given short array. Multi-channel data should be interleaved.

Throws:
java.io.IOException

writeDataChunkHeader

public void writeDataChunkHeader(int size)
                          throws java.io.IOException
Write a 'data' chunk header to the WAV file. This should be followed by call to writeShortLittle() to write the data to the chunk. If you do not know the length of the data chunk when calling this method, then pass zero for the size. Then when you finish writing the data call fixSizes().

Throws:
java.io.IOException

writeHeader

public void writeHeader(int size)
                 throws java.io.IOException
Write a 'RIFF' file header and a 'WAVE' ID to the WAV file. If you do not know the length of the file when calling this method, then pass zero for the size. Then when you finish writing the data chunk call fixSizes().

Throws:
java.io.IOException

writeHeader

public void writeHeader(int channelsPerFrame,
                        int sampleRate)
                 throws java.io.IOException
Write a simple WAV header for 16 bit PCM data. Set file and data sizes to zero.

Throws:
java.io.IOException

fixSizes

public void fixSizes()
              throws java.io.IOException
Fix RIFF and data chunk sizes based on final size. Assume data chunk is the last chunk.

Throws:
java.io.IOException

write

public void write(short[] samples,
                  int channelsPerFrame,
                  int sampleRate)
           throws java.io.IOException
Write a complete WAV file containing the array of short samples.

Throws:
java.io.IOException

main

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