I am receiving a string representing an array of audio samples from a browser captured via getUserMedia. getUserMedia is recording at 48000, and I am interleaving 2 channels before I send the string values to the server. I turn this string into a float[] like so:
string[] raw = buffer.ToString().Split(new char[]{ ',' });
float[] fArray = new float[raw.Length];
for (int x = 0; x < raw.Length; x++)
{
float sampleVal = float.Parse(raw[x]);
fArray[x] = sampleVal;
}
What I would like to do is convert the float[] array into a byte[] array so that I can pass it to a BufferedWaveProvider (48000, 16, 1) for playback. Here's how I am currently trying to do the conversion:
byte[] bSamples = new byte[fArray.Length * 2];
for (int x = 0; x < fArray.Length; x += 2)
{
short sSample = (short)Math.Floor(fArray[x] * 32767);
byte[] tmp = BitConverter.GetBytes(sSample);
bSamples[x] = tmp[0];
bSamples[x + 1] = tmp[1];
}
Using the code above, only is garbage produced. Can anyone point me in the right direction for doing such a conversion?
I've seen this, but it didn't get me where I need to go.