Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
767 views
in Technique[技术] by (71.8m points)

java - Convert wav audio format byte array to floating point

I am reading a .wav audio file using Java AudioInputStream.The audio file is 16 bit PCM signed, with samplerate = 44100, framesize = 2, framelength= 114048. I managed to get the Audio data in the form of a byte array but I am not sure how much size should I assign to this byte array so that I can convert it to floatinf point values. I am doing some audio manipulation using Goertzel algorithm which takes input of float array, something like "float[] x". Below is some code fragment that I am using. Thanks in advance.

 try {
 AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(fileIn);
  }

while ( numBytesRead != -1) {
numBytesRead = audioInputStream.read(audioBytes);

// Logic goes here
floatValue = byteArrayToFloat(audioBytes);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The audio file is 16 bit PCM signed, with samplerate = 44100, framesize = 2, framelength= 114048.

I'm assuming from the above that you have only a single channel (2 byte samples * 1 channel = 2 byte frames).

First step is to get the data as a sequence of a 16-bit integral type, which is short in Java.

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ShortBuffer;

...

byte[] audioBytes = ...

ShortBuffer sbuf =
    ByteBuffer.wrap(audioBytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
short[] audioShorts = new short[sbuf.capacity()];
sbuf.get(audioShorts);

Now how you convert that to floats depends on how downstream functions expect the audio to be represented. For example if they expect floating point numbers >= -1 and <= 1, then you can do this:

float[] audioFloats = new float[audioShorts.length];
for (int i = 0; i < audioShorts.length; i++) {
    audioFloats[i] = ((float)audioShorts[i])/0x8000;
}

Unfortunately there are a lot of ways to represent audio.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...