AudioBuffer Format

Jan 30, 2013 at 4:01 PM
Edited Jan 30, 2013 at 4:25 PM

I'm reading from a WAV file using NAudio, which produces a byte array. I want to convert that byte array into a float array, so I can use it as the audioBuffer. I've used the method below to do the job. Will this produce the correct results that your algorithm will be able to use?

// Create a temporary byte buffer, and read the file into it

byte[] buffer = new byte[reader.Length];
int read = reader.Read(buffer, 0, buffer.Length);

// Convert the byte array into the float array
audioBuffer = new float[read / 2];
for (int i = 0, j = 0; i < read / 2; i += 2, j++)
{
	// convert two bytes to one short
	short s = BitConverter.ToInt16(buffer, i);
	// convert to range from -1 to (just below) 1
	audioBuffer[j] =  s / 32768.0f;
}