public class Audio
extends java.lang.Object
The format of an audio file is:
byte | type | field name | field value |
---|---|---|---|
0x00 | byte | magic[4] | 0x2E736E64 '.snd' in ASCII |
0x04 | int | offset | offset of audio data relative to the start of the stream |
0x08 | int | size | number of bytes of data |
0x0C | int | format | format code: 1 for 8-bit u-law |
0x10 | int | samplingRate | the sampling rate |
0x14 | int | numChannels | the number of channels |
0x18 | byte | info[] | optional text information |
The design of this class is based on the web page of Billy Donahue. http://www.cooper.edu/~donahu/auformat/auFormat.html. That page no longer exists. This file was created on 1998-08-31, The Wayback machine yields this page for that date: http://web.archive.org/web/19980709194800/http://www.cooper.edu/~donahu/auformat/auFormat.html.
Note that this class serves the same role as the class by the same name in the sun.audio package, but is much more public about its information. For example, the Sun version does not give any access to the audio data itself.
Red (cxh) |
Red (eal) |
Modifier and Type | Field and Description |
---|---|
byte[][] |
audio
Audio data, by channel.
|
int |
format
Format code; 1 for 8-bit mu-law.
|
byte[] |
info
Four byte info field.
|
byte[] |
magic
The file type identifier, 0x2E736E64 or '.snd' in ASCII.
|
int |
numChannels
The number of channels.
|
int |
offset
Offset of audio data relative to the start of the stream.
|
int |
sampleRate
The sampling rate.
|
int |
size
Number of bytes of audio data.
|
Constructor and Description |
---|
Audio(byte[] audio)
Construct an instance initialized with the audio
signal given by the argument.
|
Audio(java.io.DataInputStream input)
Construct an instance and initialize it by reading
the specified stream.
|
Audio(double[] audio)
Construct an instance initialized with the audio
signal given by the argument.
|
Modifier and Type | Method and Description |
---|---|
static byte |
lin2mu(int sample)
Convert an integer linear representation of an audio sample
into a mu-255 companded representation.
|
static int |
mu2lin(byte b)
Convert mu-255 companded representation of an audio sample
into an integer linear representation.
|
static double[] |
readAudio(java.io.DataInputStream input)
Read Sun audio file (.au) format and return the audio data as an array.
|
static void |
setZeroTrap(boolean boole)
Configure all instances of this class to use the MIL-STD zero trap.
|
double[] |
toDouble(int channel)
Convert the audio data to linear double encoding (from mu-law).
|
int[] |
toLinear(int channel)
Convert the audio data to linear integer encoding (from mu-law).
|
java.lang.String |
toString()
Return a readable representation of the header data.
|
void |
write(java.io.DataOutputStream output)
Write the audio data to an output stream in the Sun audio format.
|
static void |
writeAudio(double[] audio,
java.io.DataOutputStream output)
Write Sun audio file (.au) format from an array.
|
void |
writeRaw(java.io.DataOutputStream output)
Write the raw audio data to an output stream.
|
public byte[] magic
public int offset
public int size
public int format
public int sampleRate
public int numChannels
public byte[] info
public byte[][] audio
public Audio(byte[] audio)
audio
- An audio signal.public Audio(double[] audio)
audio
- An audio signal.public Audio(java.io.DataInputStream input) throws java.io.IOException
input
- The input stream.java.io.IOException
- If an error occurs reading the input data
(e.g. a premature end of file).public static byte lin2mu(int sample)
The integer argument is a 16-bit representation of the sample. Anything outside the range -32635 to 32635 will be clipped to within that range.
The mu-255 representation is a byte SEEEMMMM where S is the sign bit, EEE is the three-bit exponent, and MMMM is the four-bit mantissa. The bits are flipped, so that the binary 10000000 is the largest positive number and 00000000 is the largest negative number. If you have called that static method setZeroTrap() with a true argument, then per MIL-STD-188-113, the 00000000 representation is never used, replaced instead with 00000010 (0x02). By default, this trap is not used.
This implementation was written by Anthony Hursh, who included with it the following information:
Copyright 1997 by Anthony Hursh <hursha@saturn.math.uaa.alaska.edu> This code may be freely used as long as proper credit is given. It was originally written in C by Craig Reese (IDA/Supercomputing Research Center) and Joe Campbell (Department of Defense), and ported to Java by Tony Hursh, January 1997. References:
sample
- A linear representation of the sample.public static int mu2lin(byte b)
The mu-255 representation is a byte SEEEMMMM where S is the sign bit, EEE is the three-bit exponent, and MMMM is the four-bit mantissa. The bits are flipped, so that the binary 10000000 is the largest positive number and 00000000 is the largest negative number.
If you have called setZeroTrap() with a true argument, then this will not be an exact inverse of lin2mu because the zero code is interpreted as being the largest negative number, -31616.
b
- A mu-255 representation of the sample.public static double[] readAudio(java.io.DataInputStream input) throws java.io.IOException
double audio[] = readAudio(new DataInputStream(url.openStream());
The returned values lie in the range -1.0 to 1.0.
input
- The input stream.java.io.IOException
- If an I/O error occurs reading the stream.public static void setZeroTrap(boolean boole)
boole
- If true, use zero-trap encoding.public double[] toDouble(int channel)
channel
- The channel number.public int[] toLinear(int channel)
channel
- The channel number.public java.lang.String toString()
toString
in class java.lang.Object
public void write(java.io.DataOutputStream output) throws java.io.IOException
output
- The output stream.java.io.IOException
- If an error occurs writing to the stream.public void writeRaw(java.io.DataOutputStream output) throws java.io.IOException
// The constructor argument below is optional ByteArrayOutputStream out = new ByteArrayOutputStream(sound.size); try { sound.writeRaw(new DataOutputStream(out)); } catch (IOException ex) { throw new RuntimeException("Audio output failed"); } byte[] iobuffer = out.toByteArray(); ByteArrayInputStream instream = new ByteArrayInputStream(_iobuffer); AudioPlayer.player.start(instream);The above code assumes we have an sun.audio.AudioData object called "sound". Although it would seem reasonable to include a "play" method in this class to do this, we wish to avoid a dependence on the sun.audio package in this Ptolemy package, so you will have to implement the above code yourself.
output
- The output stream.java.io.IOException
- If an error occurs writing to the stream.public static void writeAudio(double[] audio, java.io.DataOutputStream output) throws java.io.IOException
audio
- The audio data, as an array of doubles.output
- The output stream.java.io.IOException
- If an I/O error occurs writing to the stream.