using System.Buffers.Binary;
public static void Main()
byte[] toTest = new byte[64] {
0x00, 0x8F, 0x01, 0x7F, 0x00, 0x7F, 0x01, 0x7F, 0x02, 0x7F, 0x03, 0x7F, 0x04, 0x78, 0x05, 0x7F, 0x06, 0x7F, 0x07, 0x7F, 0x08, 0x7F, 0x09, 0x7F, 0x0A, 0x7F, 0x0B, 0x7F, 0x0C, 0x7F, 0x0D, 0x7F,
0x0E, 0x7F, 0x0F, 0x7F, 0x02, 0x7F, 0x03, 0x7F, 0x10, 0x7F, 0x00, 0x7F, 0x00, 0x7F, 0x00, 0x7F, 0x00, 0x7F, 0x00, 0x7F, 0x00, 0x7F, 0x00, 0x7F, 0x00, 0x7F, 0x00, 0x7F, 0x00, 0x7F, 0x00, 0x7F
Program p = new Program();
int byteLen = toTest.Length;
float[] outBuf = new float[len];
int samplesRead = p.ConvertToIEEEFloat(toTest, outBuf, 0, len);
Console.WriteLine("Samples read: "+samplesRead);
for (int i=0; i<byteLen; i+=2)
if (i>0) Console.Write(",");
byte[] toConvert = new byte[2] {toTest[i], toTest[i+1]};
short val = BinaryPrimitives.ReadInt16LittleEndian(toConvert);
Console.Write(val.ToString());
Console.WriteLine("\nfloats:");
for (int i=0; i<samplesRead; ++i)
if (i>0) Console.Write(",");
Console.Write(outBuf[i].ToString());
short[] shortVals = new short[len];
int shortsRead = p.ConvertToWave16(outBuf, shortVals, 0, byteLen);
Console.WriteLine("\nshorts:");
for (int i=0; i<shortsRead; ++i)
if (i>0) Console.Write(",");
Console.Write(shortVals[i].ToString());
public int ConvertToIEEEFloat(byte[] sourceBuffer, float[] buffer, int offset, int count)
int sourceBytesRequired = count * 2;
int bytesRead = sourceBuffer.Length;
for(int n = 0; n < bytesRead; n+=2)
byte[] toConvert = new byte[2] {sourceBuffer[n], sourceBuffer[n+1]};
buffer[outIndex++] = BitConverter.ToInt16(sourceBuffer, n) / 32768f;
public int ConvertToWave16(float[] sourceBuffer, short[] destBuffer, int offset, int numBytes)
int samplesRequired = numBytes / 2;
int sourceSamples = sourceBuffer.Length;
int destOffset = offset / 2;
for (int sample = 0; sample < sourceSamples; sample++)
float sample32 = sourceBuffer[sample];
destBuffer[destOffset++] = (short)(sample32 * 32767);