net.beadsproject.beads.core
Class AudioUtils

java.lang.Object
  extended by net.beadsproject.beads.core.AudioUtils

public final class AudioUtils
extends java.lang.Object

AudioUtils provides basic conversion of buffers of audio data between different formats, interleaving, and some other miscellaneous audio functions.

Author:
ollie

Field Summary
static java.util.Random rng
          A handy random number generator.
 
Constructor Summary
AudioUtils()
           
 
Method Summary
static void byteToFloat(float[] out, byte[] in, boolean bigEndian)
          Converts a buffer of bytes to a buffer of floats with a given byte order.
static void byteToFloat(float[] out, byte[] in, boolean bigEndian, int numFloats)
          Converts a buffer of bytes to a buffer of floats with a given byte order.
static void byteToFloat(float[] out, byte[] in, boolean bigEndian, int startIndexInByteArray, int numFloats)
          Converts a buffer of bytes to a buffer of floats with a given byte order.
static void byteToFloat(float[] out, byte[] in, boolean bigEndian, int startIndexInByteArray, int startIndexInFloatArray, int numFloats)
          Converts a buffer of bytes to a buffer of floats with a given byte order.
static void deinterleave(float[] source, int nChannels, int nFrames, float[][] result)
          De-interleave an interleaved buffer of floats to form a 2D array of floats of size nChannels x nFrames.
static double fastPow01(double a, double b)
          Does a freaky shortcut for calculating pow (limited to base with range 0-1), faster but less accurate than regular Math.pow().
static java.io.File fileFromString(java.lang.String s)
          Equivalent to fileFromURL(urlFromString(s)).
static java.io.File fileFromURL(java.net.URL url)
          Attempts to get a File from a URL, suppressing warnings.
static void floatToByte(byte[] out, float[] in, boolean bigEndian)
          Converts a buffer of floats to a buffer of bytes with a given byte order.
static void floatToByte(byte[] out, int outstart, float[] in, int instart, int inlength, boolean bigEndian)
          Converts a buffer of floats to a buffer of bytes with a given byte order.
static void floatToShort(short[] out, float[] in)
          Converts a buffer of floats to a buffer of shorts.
static void interleave(float[][] source, int nChannels, int nFrames, float[] result)
          Interleave a 2D array of floats of size nChannels x nFrames to form a single interleaved buffer of floats.
static void interleave(float[][] source, int nChannels, int nFrames, int offset, float[] result)
          Interleave a 2D array of floats of size nChannels x nFrames to form a single interleaved buffer of floats.
static void reverseBuffer(float[][] buffer)
          Reverse the data in each channel.
static void shortToFloat(float[] out, short[] in)
          Converts a buffer of shorts to a buffer of floats.
static void stretchBuffer(float[][] source, float[][] dest)
          "Stretches" source into dest.
static java.net.URL urlFromString(java.lang.String s)
          Attempts to determine a URL given a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rng

public static java.util.Random rng
A handy random number generator.

Constructor Detail

AudioUtils

public AudioUtils()
Method Detail

shortToFloat

public static final void shortToFloat(float[] out,
                                      short[] in)
Converts a buffer of shorts to a buffer of floats.

Parameters:
out - buffer of floats.
in - buffer of shorts.

floatToShort

public static final void floatToShort(short[] out,
                                      float[] in)
Converts a buffer of floats to a buffer of shorts.

Parameters:
out - buffer of shorts.
in - buffer of floats.

floatToByte

public static final void floatToByte(byte[] out,
                                     float[] in,
                                     boolean bigEndian)
Converts a buffer of floats to a buffer of bytes with a given byte order.

Parameters:
out - buffer of bytes.
in - buffer of floats.
bigEndian - true for big endian byte order, false otherwise.

floatToByte

public static final void floatToByte(byte[] out,
                                     int outstart,
                                     float[] in,
                                     int instart,
                                     int inlength,
                                     boolean bigEndian)
Converts a buffer of floats to a buffer of bytes with a given byte order.

Parameters:
out - Output array
outstart - Start index of output
in - Input array
instart - Start index of input
inlength - Number of floats to copy
bigEndian - Format

byteToFloat

public static final void byteToFloat(float[] out,
                                     byte[] in,
                                     boolean bigEndian)
Converts a buffer of bytes to a buffer of floats with a given byte order.

Parameters:
out - buffer of floats.
in - buffer of bytes.
bigEndian - true for big endian byte order, false otherwise.

byteToFloat

public static final void byteToFloat(float[] out,
                                     byte[] in,
                                     boolean bigEndian,
                                     int numFloats)
Converts a buffer of bytes to a buffer of floats with a given byte order. Will copy numFloat floats to out.

Parameters:
out - buffer of floats.
in - buffer of bytes.
bigEndian - true for big endian byte order, false otherwise.
numFloats - number of elements to copy into out

byteToFloat

public static final void byteToFloat(float[] out,
                                     byte[] in,
                                     boolean bigEndian,
                                     int startIndexInByteArray,
                                     int numFloats)
Converts a buffer of bytes to a buffer of floats with a given byte order. Will copy numFloat floats to out.

Parameters:
out - buffer of floats.
in - buffer of bytes.
bigEndian - true for big endian byte order, false otherwise.
startIndexInByteArray - where to start copying from
numFloats - number of elements to copy into out

byteToFloat

public static final void byteToFloat(float[] out,
                                     byte[] in,
                                     boolean bigEndian,
                                     int startIndexInByteArray,
                                     int startIndexInFloatArray,
                                     int numFloats)
Converts a buffer of bytes to a buffer of floats with a given byte order. Will copy numFloat floats to out.

Parameters:
out - buffer of floats.
in - buffer of bytes.
bigEndian - true for big endian byte order, false otherwise.
startIndexInByteArray - where to start copying from
startIndexInFloatArray - where to start copying to
numFloats - number of elements to copy into out

deinterleave

public static final void deinterleave(float[] source,
                                      int nChannels,
                                      int nFrames,
                                      float[][] result)
De-interleave an interleaved buffer of floats to form a 2D array of floats of size nChannels x nFrames.

Parameters:
source - interleaved buffer of floats.
nChannels - first dimension of resulting 2D array.
nFrames - second dimension of resulting 2D array.
result - the result

interleave

public static final void interleave(float[][] source,
                                    int nChannels,
                                    int nFrames,
                                    float[] result)
Interleave a 2D array of floats of size nChannels x nFrames to form a single interleaved buffer of floats.

Parameters:
source - 2D array of floats.
nChannels - first dimension of input 2D array.
nFrames - second dimension of input 2D array.
result - the result

interleave

public static final void interleave(float[][] source,
                                    int nChannels,
                                    int nFrames,
                                    int offset,
                                    float[] result)
Interleave a 2D array of floats of size nChannels x nFrames to form a single interleaved buffer of floats.

Parameters:
source - 2D array of floats.
nChannels - first dimension of input 2D array.
nFrames - second dimension of input 2D array.
offset - the number of frames offset
result - the result

stretchBuffer

public static final void stretchBuffer(float[][] source,
                                       float[][] dest)
"Stretches" source into dest. Linearly interpolates.

Parameters:
source -
dest -

reverseBuffer

public static final void reverseBuffer(float[][] buffer)
Reverse the data in each channel.

Parameters:
buffer -

fastPow01

public static double fastPow01(double a,
                               double b)
Does a freaky shortcut for calculating pow (limited to base with range 0-1), faster but less accurate than regular Math.pow().

CREDIT: this method is copied directly from http://martin.ankerl.com/2007/10/04/optimized-pow-approximation-for-java-and-c-c/

Parameters:
a - the base.
b - the exponent.
Returns:
the result

urlFromString

public static java.net.URL urlFromString(java.lang.String s)
Attempts to determine a URL given a String. Firstly the String is interpreted as a System Resource. Failing that, it is interpreted as a proper URL. Failing that it is interpreted as a file path. All Exceptions are suppressed but the method returns null.

Parameters:
s - String to interpret as System Resource, URL or file path.
Returns:
a URL if successful, null otherwise.

fileFromURL

public static java.io.File fileFromURL(java.net.URL url)
Attempts to get a File from a URL, suppressing warnings. Assumes UTF-8 encoding.

Parameters:
url - to get File from.
Returns:
a File if successful, null otherwise.

fileFromString

public static java.io.File fileFromString(java.lang.String s)
Equivalent to fileFromURL(urlFromString(s)). See these methods.

Parameters:
s - the String to interpret as System Resource, URL or file path.
Returns:
a File if successful, null otherwise.