Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a .WAV audio data sample into an double type?

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

like image 232
dedalo Avatar asked Jun 05 '09 19:06

dedalo


People also ask

How do I convert a WAV file to sample rate?

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 data type is WAV?

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.


2 Answers

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 ...

}
like image 185
G B Avatar answered Sep 21 '22 05:09

G B


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 ;-)

like image 36
Roland Beuker Avatar answered Sep 22 '22 05:09

Roland Beuker