Package adams.data.audio
Class SimpleAudioConversion
- java.lang.Object
-
- adams.data.audio.SimpleAudioConversion
-
public class SimpleAudioConversion extends Object
Performs rudimentary audio format conversion.Example usage:
AudioInputStream ais = ... ; SourceDataLine line = ... ; AudioFormat fmt = ... ; // do prep for (int blen = 0; (blen = ais.read(bytes)) > -1;) { int slen; slen = SimpleAudioConversion.unpack(bytes, samples, blen, fmt); // do something with samples blen = SimpleAudioConversion.pack(samples, bytes, slen, fmt); line.write(bytes, 0, blen); }- Author:
- Radiodef
- See Also:
- Overview on StackOverflow.com
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intbytesPerSample(int bitsPerSample)Computes the block-aligned bytes per sample of the audio format, with(int) ceil(bitsPerSample / 8.0).static doublefullScale(int bitsPerSample)Computes the largest magnitude representable by the audio format, withpow(2.0, bitsPerSample - 1).static intpack(float[] samples, byte[] bytes, int slen, AudioFormat fmt)Converts: from an audio sample array (float[]) to a byte array (byte[]).static intunpack(byte[] bytes, gnu.trove.list.TFloatList samples, int blen, AudioFormat fmt)Converts: from a byte array (byte[]) to an audio sample array (float[]).
-
-
-
Method Detail
-
unpack
public static int unpack(byte[] bytes, gnu.trove.list.TFloatList samples, int blen, AudioFormat fmt)Converts:- from a byte array (
byte[]) - to an audio sample array (
float[]).
- Parameters:
bytes- the byte array, filled by theInputStream.samples- an array to fill up with audio samples.blen- the return value ofInputStream.read.fmt- the sourceAudioFormat.- Returns:
- the number of valid audio samples converted.
- Throws:
NullPointerException- ifbytes,samplesorfmtisnullArrayIndexOutOfBoundsException- if(bytes.length < blen)or(samples.length < blen / bytesPerSample(fmt.getBitsPerSample())).
- from a byte array (
-
pack
public static int pack(float[] samples, byte[] bytes, int slen, AudioFormat fmt)Converts:- from an audio sample array (
float[]) - to a byte array (
byte[]).
- Parameters:
samples- an array of audio samples to encode.bytes- an array to fill up with bytes.slen- the return value ofunpack.fmt- the destinationAudioFormat.- Returns:
- the number of valid bytes converted.
- Throws:
NullPointerException- ifsamples,bytesorfmtisnullArrayIndexOutOfBoundsException- if(samples.length < slen)or(bytes.length < slen * bytesPerSample(fmt.getSampleSizeInBits()))
- from an audio sample array (
-
bytesPerSample
public static int bytesPerSample(int bitsPerSample)
Computes the block-aligned bytes per sample of the audio format, with(int) ceil(bitsPerSample / 8.0).This is generally equivalent to the optimization
((bitsPerSample + 7) >>> 3). (Except for the invalid argumentbitsPerSample <= 0.)Round towards the ceiling because formats that allow bit depths in non-integral multiples of 8 typically pad up to the nearest integral multiple of 8. So for example, a 31-bit AIFF file will actually store 32-bit blocks.
- Parameters:
bitsPerSample- the return value ofAudioFormat.getSampleSizeInBits.- Returns:
- The block-aligned bytes per sample of the audio format.
-
fullScale
public static double fullScale(int bitsPerSample)
Computes the largest magnitude representable by the audio format, withpow(2.0, bitsPerSample - 1).For
bitsPerSample < 64, this is generally equivalent to the optimization(1L << (bitsPerSample - 1L)). (Except for the invalid argumentbitsPerSample <= 0.)The result is returned as a
doublebecause, in the case thatbitsPerSample == 64, alongwould overflow.- Parameters:
bitsPerSample- the return value ofAudioFormat.getBitsPerSample.- Returns:
- the largest magnitude representable by the audio format.
-
-