Hedge Hedge - 4 years ago 359
C# Question

Convert memorystream to double-array?

I've got a pcm-stream of raw music data from a wave-file and would like to convert it to a double-array (to apply a fft afterwards).

The result I got now contains very high or low double-numbers (1.0E-200 and 1.0E+300) and I'm not sure whether these can be correct.

This is the code I'm using right now:

WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(mp3);
double[] real = new double[pcm.Length];
byte[] buffer = new byte[8];
int count = 0;

while ((read = pcm.Read(buffer, 0, buffer.Length)) > 0)
real[count] = BitConverter.ToDouble(buffer, 0);

Answer Source

Your PCM stream is almost certainly 16 bit. So instead of BitConverter.ToDouble use ToInt16 instead. Then divide by 32768.0 to get into the the range +/- 1.0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download