Search code examples
javaaudiosignal-processingwavsampling

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


Solution

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