This project has moved and is read-only. For the latest updates, please go here.

Data format when capturing in IEEE float WaveFormat

Mar 21, 2013 at 9:11 AM
Edited Mar 21, 2013 at 9:12 AM

I am capturing soung from the microphone and willing to have it sampled as IEEE float
I think the right way to do it is, isn't it ?
waveIn = new WaveIn();
waveIn.DataAvailable += waveIn_DataAvailable;
int sampleRate = 44100; // 44,1 kHz
int channels = 2; // stereo
waveIn.WaveFormat = WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channels);
What makes me confused is that I was expecting to receive a float array in the callback
        void waveIn_DataAvailable(object sender, WaveInEventArgs e)
            audioBuffer = e.Buffer; 
And I get a cast exception. It seems that e.Buffer is a Byte array... So how to move from this byte array to a float array ?
Mar 21, 2013 at 12:09 PM
yes, this is a frustrating thing about designing APIs in C# as you can't just cast a byte array to a float array.
You can either loop through using BitConverter.ToSingle on every four bytes, or you can use Buffer.BlockCopy to copy the entire e.Buffer into a float array. Finally if you are comfortable with unsafe code you can use pointers and loop through in C/C++ style.