How to visualize microphone sound/pressure level in Silverlight 4?
I was following [this tutorial][1] on how to make a Silverlight audio recorder. I thought it would be great to add a volume bar indicator to provide user with a feedback on what's happening. However, I can't seem to get this to work properly.

OnSamples method of AudioSink class provides raw PCM data as one of the arguments. Also, I set the [AudioFrameSize][2] property of [AudioCaptureDevice][3] to 40 (1000/40 == 25fps), so [OnSamples][4] is triggered every 40ms.

My question is how to extract the sound volume information from PCM data and display it as percentage in a progress bar [0-100]?

---

This is what I have so far:

    double average = 0;
    for (int a = 0; a < sampleData.Length; ++a)
    {
        average += Math.Abs(sampleData[a]);
    }
    average /= sampleData.Length;
    
    double volume = 20 * Math.Log10(average);

Value of the progress bar is then set to volume:

    progressBar.Value = volume;

My code doesn't work, apparently, since the volume value is almost always at the same level.

Any help is appreciated!

  [1]: http://blog.ondrejsv.com/post/Audio-recorder-Silverlight-4-sample.aspx
  [2]: http://msdn.microsoft.com/en-us/library/system.windows.media.audiocapturedevice.audioframesize%28v=VS.95%29.aspx
  [3]: http://msdn.microsoft.com/en-us/library/system.windows.media.audiocapturedevice%28VS.95%29.aspx
  [4]: http://msdn.microsoft.com/en-us/library/system.windows.media.audiosink.onsamples%28v=VS.96%29.aspx
How to visualize microphone sound/pressure level in Silverlight 4?
===

I was following [this tutorial][1] on how to make a Silverlight audio recorder. I thought it would be great to add a volume bar indicator to provide user with a feedback on what's happening. However, I can't seem to get this to work properly.

OnSamples method of AudioSink class provides raw PCM data as one of the arguments. Also, I set the [AudioFrameSize][2] property of [AudioCaptureDevice][3] to 40 (1000/40 == 25fps), so [OnSamples][4] is triggered every 40ms.

My question is how to extract the sound volume information from PCM data and display it as percentage in a progress bar [0-100]?

---

This is what I have so far:

    double average = 0;
    for (int a = 0; a < sampleData.Length; ++a)
    {
        average += Math.Abs(sampleData[a]);
    }
    average /= sampleData.Length;
    
    double volume = 20 * Math.Log10(average);

Value of the progress bar is then set to volume:

    progressBar.Value = volume;

My code doesn't work, apparently, since the volume value is almost always at the same level.

Any help is appreciated!

  [1]: http://blog.ondrejsv.com/post/Audio-recorder-Silverlight-4-sample.aspx
  [2]: http://msdn.microsoft.com/en-us/library/system.windows.media.audiocapturedevice.audioframesize%28v=VS.95%29.aspx
  [3]: http://msdn.microsoft.com/en-us/library/system.windows.media.audiocapturedevice%28VS.95%29.aspx
  [4]: http://msdn.microsoft.com/en-us/library/system.windows.media.audiosink.onsamples%28v=VS.96%29.aspx