com.ritolaaudio.simplewavio
Class Utils

java.lang.Object
  extended by com.ritolaaudio.simplewavio.Utils

public class Utils
extends java.lang.Object

General utilities for reading and writing WAV (RIFF) files.

Author:
chuck

Constructor Summary
Utils()
           
 
Method Summary
static void flipEndian(byte[] original, byte[] output)
          Reverse the order of a byte array and write that reordered array to the given output.
static void floatsToWAV(float[][] buffer, java.io.File destFile, float sampleRate)
          Write an array of float[] frames as a RIFF (.WAV) file, expecting floats within the range [-1,1].
static void toByteArray(int value, int numBytes, byte[] dest, int off)
          Converts a signed int into an array of bytes and writes it to a byte array t the given offset.
static float[][] WAVToFloats(java.io.File input)
          Open a .WAV (RIFF) file and return its contents as a 2-dimensional array of floats.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utils

public Utils()
Method Detail

toByteArray

public static final void toByteArray(int value,
                                     int numBytes,
                                     byte[] dest,
                                     int off)
Converts a signed int into an array of bytes and writes it to a byte array t the given offset.

Parameters:
value - The integral value to byte-ify.
numBytes - The number of bytes desired to describe this value (up to 4)
dest - The byte array to which to write said bytes.
off - The offset index within said byte array from which to start writing said bytes.
Since:
Jul 14, 2012

flipEndian

public static final void flipEndian(byte[] original,
                                    byte[] output)
Reverse the order of a byte array and write that reordered array to the given output. Intra-byte bit order is unchanged.

Parameters:
original - input array to reverse (does not get changed)
output - output array for results of byte order reversal.
Since:
Jul 14, 2012

WAVToFloats

public static float[][] WAVToFloats(java.io.File input)
                             throws java.io.IOException
Open a .WAV (RIFF) file and return its contents as a 2-dimensional array of floats.

Parameters:
srcFile -
Returns:
A 2-dimensional float array, where the first dimension represents single-sample-wide frames, and the second dimension representing a single-sample channel of that frame. i.e. a mono clip 44000 samples long would have the dimensions [44000][1]
Throws:
java.io.FileNotFoundException - Failure to find the file specified; read aborted.
javax.sound.sampled.UnsupportedAudioFileException - Java Media Framework could not recognize the format of the specified file.
java.io.IOException - Some general IO error occurred while trying to process the file. Probably a bug in SimpleWAVIO.
Since:
Jul 11, 2012

floatsToWAV

public static void floatsToWAV(float[][] buffer,
                               java.io.File destFile,
                               float sampleRate)
                        throws java.io.IOException
Write an array of float[] frames as a RIFF (.WAV) file, expecting floats within the range [-1,1]. If they are outside of this range they will be subjected to a floor or ceiling (hard clipped) to fit within the range [-1,1].

Parameters:
buffer - 2-dimensional float array, where the first dimension represents single sample-wide frames, and the second dimension representing a single-sample channel of that frame. i.e. a mono clip 44000 samples long would have the dimensions [44000][1]
destFile - Destination File. Does not need to exist beforehand.
sampleRate -
Throws:
java.io.IOException
Since:
Jul 11, 2012