How to convert a sample WAV audio data to a double type?

I am working on an application that processes audio data. I use java (I added MP3SPI, Jlayer and Tritonus). I am extracting audio data from a WAV file into an array of bytes. The audio samples I'm working with are 16 bit stereo.

According to what I read, the format is for one example:

AABBCCDD

where AABB is the left channel and the CCDD channel (2 bytes for each channel). I need to convert this pattern to a double value type. I read about the data format. Java uses Big endian, .wav files use a bit of endian. I am a bit confused. Could you help me in the conversion process? Thank you all

+4
source share
4 answers

Warning: integers and bytes are signed. Perhaps you need to mask the lower bytes when packing them:

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 ... } 
+3
source

When you use ByteBuffer ( java.nio.ByteBuffer ), you can use the order of the methods;

[to order]

public final order ByteBuffer (ByteOrder bo)

 Modifies this buffer byte order. Parameters: bo - The new byte order, either BIG_ENDIAN or LITTLE_ENDIAN Returns: This buffer 

After that, you can get the above values ​​with

GetChar () getShort () GetInt () GetFloat () getDouble ()

Great Java language ;-)

+4
source

Little Endian means the data is in the form of BBAA and DDCC. You would just change it.

From the beginning of the frame:

 int left = (bytes[i+1] << 8) + bytes[i]; int right = (bytes[i+3] << 8) + bytes[i+2]; 

where I am your sample index.

0
source

I would personally look for a library that does an endian replacement for you. There are assumptions about reliability in each format of audio files for you, and obtaining this right is difficult for all depths / data types supported by wave files:

  • 8bit - uint8
  • 16bit - int16
  • 24bit - int32
  • 32bit - int32 as float
  • 32bit - float
  • 64bit - double

If you want to support most of the common wave file types, you will need endian conversion for all of these data types.

I would look at ByteSwapper , which will give you bytes for most of the types listed above.

Too bad Java does not have authority fields in its IO file classes. The ability to simply open a whos edianness file, large or small, is a much easier solution to this problem.

0
source

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


All Articles