Skip to content Skip to sidebar Skip to footer

Converting 16bitpcm To .wav, After Switching Endianness, The .wav File Plays Backwards

I am trying to build an Android app that records PCM audio and exports it as a wav file. It worked fine for 8BitPCM, but when I switched to 16BitPCM I got white noise. I finally fi

Solution 1:

I'm an idiot - 16 bits != a byte.

I was reversing the byte array when I should have been reversing a short array.

I ended up replacing LittleEndianToBig with:

publicstaticshort[] convertLittleBytesToBigShorts(byte[] value) {

    short[] shorts = newshort[value.length/2];
    ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);

    return shorts;
}

and the write command with:

for (int i = 0; i < inputByteArray.length; i++)
        {
            outFile.writeShort(inputByteArray[i]);
        }

I'll clean it up, but that was the issue. My audio is correct now.

Post a Comment for "Converting 16bitpcm To .wav, After Switching Endianness, The .wav File Plays Backwards"