Convert signed ints (2 bytes, 16 bits) in double format. With Java

I have a problem. In Java, I need to read samples from a wav file. File format: wav, PCM_SIGNED, signed int 2bytes = 16bits, little endian ... The object reads the audio samples in BYTES, and I need to convert these two bytes to one double value. I tried using this formula, but this is not entirely correct:

mono = (double)((audioBytes[k] & 0xFF) | (audioBytes[k + 1] << 8));

Comparing the results with Matlab, I always notice the differences between the real value in Matlab and the converted in Java. Can someone help me please? Thanks Dave

+3
source share
3 answers

, , Matlab Java. [-32768..32767] [-1.1], , , . java: -3.0517578125E-5 -1: -1/32768. , Matlab . , Matlab.

( , ), BIG-ENDIAN vs LITTLE-ENDIAN , java :

import java.nio.*;
...
ByteBuffer buf = ByteBuffer.wrap(audioBytes);
buf.order(ByteOrder.LITTLE_ENDIAN);

while (buf.remaining() >= 2) {
    short s = buf.getShort();
    double mono = (double) s;
    double mono_norm = mono / 32768.0;
    ...
}

ByteBuffer.getShort() , Little-Endian , getXXX().

+2

:

double sampleValue = (double)(( bytes[0]<<8 ) | ( bytes[1]&0x00FF ));

( /)

0

most_significant byte * 256 + minimum_significant_byte, double?

-1
source

Source: https://habr.com/ru/post/1783879/


All Articles