This project has moved. For the latest updates, please go here.

Extracting volume data from WaveIn error

Mar 25, 2011 at 11:52 AM

I'm trying to feed my VU meter with level values.
Basically I'm using solution I've seen with ProcessData method from NAudioDemo, values I get are 0.0 or NaN. Where have I gone wrong?

code:

void ProcessData(byte[] buffer, int offset, int count)
 {
      int index = 0, sampleCount = 0;
      while (index < count)
      {
                for (int channel = 0; channel < 2; channel++)
                {
                    float sampleValue = Math.Abs(BitConverter.ToSingle(buffer, offset + index));
                    if (float.IsNaN(sampleValue))
                    {
                        //checj
                        Debug.WriteLine("Seronja =/");
                        continue;
                    }
                    _maxSamples[channel] = Math.Max(_maxSamples[channel], sampleValue);
                    index += 4;
                }
                sampleCount++;
              
                if (sampleCount >= _sampleCount)
                {
                    OnVolumeNotification();
                    sampleCount = 0;
                    Array.Clear(_maxSamples, 0, _maxSamples.Length);
                }            
     }
}


I call it during WaveIn.DataAvailable event.
WaveFormat is 44100,16,2.
Any ideas?

 

 

Coordinator
Mar 26, 2011 at 1:54 PM

You are recording 16 bit so it should be BitConverter.ToShort instead.

Divide by 32768.0f if you want to normalise to a -1.0f to 1.0f range

Mark