Correct way to Convert 16bit PCM Wave data to float

Posted by fredley on Stack Overflow See other posts from Stack Overflow or by fredley
Published on 2011-01-07T20:58:01Z Indexed on 2011/01/07 21:53 UTC
Read the original article Hit count: 275

Filed under:
|
|
|

I have a wave file in 16bit PCM form. I've got the raw data in a byte[] and a method for extracting samples, and I need them in float format, i.e. a float[] to do a Fourier Transform. Here's my code, does this look right? I'm working on Android so javax.sound.sampled etc. is not available.

private static short getSample(byte[] buffer, int position) {
  return (short) (((buffer[position + 1] & 0xff) << 8) | (buffer[position] & 0xff));
}

...

float[] samples = new float[samplesLength];
  for (int i = 0;i<input.length/2;i+=2){
    samples[i/2] = (float)getSample(input,i) / (float)Short.MAX_VALUE;
  }

© Stack Overflow or respective owner

Related posts about java

Related posts about android