I'm working on an application that processes audio data. I'm using java (I've added MP3SPI, Jlayer, and Tritonus). I'm extracting the audio data from a .wav file to a byte array. The audio data samples I'm working with are 16 bits stereo.
According to what I've read the format for one sample is:
AABBCCDD
where AABB represents left channel and CCDD rigth channel (2 bytes for each channel). I'd need to convert this sample into a double value type. I've reading about data format. Java uses Big endian, .wav files use little endian. I'm a little bit confused. Could you please help me with the conversion process? Thanks you all
In the General Preferences tab, click on Import Settings, located towards the bottom. Click on the menu next to Import Using > WAV Encoder. Then click to change Setting > Custom and a new window will open. In the WAV Encoder window, change the Sample Rate to 44.100 kHz and Sample Size to 16-bit.
What is a WAV file? WAV, known for WAVE (Waveform Audio File Format), is a subset of Microsoft's Resource Interchange File Format (RIFF) specification for storing digital audio files. The format doesn't apply any compression to the bitstream and stores the audio recordings with different sampling rates and bitrates.
Warning: integers and bytes are signed. Maybe you need to mask the low bytes when packing them together:
for (int i =0; i < length; i += 4) {
double left = (double)((bytes [i] & 0xff) | (bytes[i + 1] << 8));
double right = (double)((bytes [i + 2] & 0xff) | (bytes[i + 3] << 8));
... your code here ...
}
When you use the ByteBuffer (java.nio.ByteBuffer) you can use the method order;
[order]
public final ByteBuffer order(ByteOrder bo)
Modifies this buffer's byte order.
Parameters:
bo - The new byte order, either BIG_ENDIAN or LITTLE_ENDIAN
Returns:
This buffer
After this you can get the above mentioned values with;
getChar() getShort() getInt() getFloat() getDouble()
What a great language is Java ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With