title: How to get Microphone volume in Windows Phone 8
body: <p>I want to get the volume level from recording microphone data in windows phone 8.</p>
  
  <pre><code>microphone = Microphone.Default;
  microphone.BufferDuration = TimeSpan.FromMilliseconds(150);
  microphone.BufferReady += microphone_BufferReady;
  
  private void microphone_BufferReady(object sender, EventArgs e)
  {
        // Get buffer from microphone and add to collection
        int size = microphone.GetSampleSizeInBytes(microphone.BufferDuration);
        byte[] buffer = new byte[size];
        int bytes = microphone.GetData(buffer);
        MicrophoneDuration += microphone.GetSampleDuration(size);
        bufferCollection.Add(buffer);
        //Get the volume of microphone
        MicrophoneVolume = GetMicrophoneVolume(buffer);
  }
  
  /// &lt;summary&gt;
  /// Get the microphone volume, from 0 up to 100.
  /// 
  /// The sum of data[i] square divided by the total length of the data.
  /// volume = sum(data[i] * data[i])/Length/10000
  /// &lt;/summary&gt;
  /// &lt;param name="data"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public static double GetMicrophoneVolume(byte[] data)
  {
      double value = 0;
  
      for (int i = 0; i &lt; data.Length; i++)
      {
          value += data[i] * data[i];
      }
      return ConvertTo(value / data.Length / 10000);
  }
  
  /// &lt;summary&gt;
  ///  x/100 = value/MaxiMicrophoneVolume/// &lt;/summary&gt;
  /// &lt;param name="value"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  private static double ConvertTo(double value)
  {
      if (value &gt; MaxiMicrophoneVolume)
          value = MaxiMicrophoneVolume;
      if (value &lt; 0)
          value = 0;
  
      return (100 * value) / MaxiMicrophoneVolume;
  }
  </code></pre>
  
  <p>But in this method, the volume  depends on the sum of each data in data[]. At the beginning, when  sum is low, when we speak loudly the sum is larger, but when the sound decreases, the sum doesn't change, so the volume which calculated by the <code>GetMicrophoneVolume</code> doesn't change.</p>
  
  <p>Anyone knows how to correctly get the microphone volume?<br>
  Or is there some  problem with my solution?<br>
  Also why doesn't the sum of data decreases when the sound decreases?<br>
  A better way to solve the problem will be greatly appreciated.</p>