title: Calculate loudness level from the microphone on android
body: <p>I am trying to calculate the loudness level from the microphone on android.
  I used AudioRecord to get the raw data from the microphone and also did some normalization and calculated the decibel after that. But the result is not right. The decibel values I got were not steady and cannot reflect the sound. Such as even when I clapped hands, the decibel value did not reflect the sound.  How should I change the code or What should I do if I want to calculate the loudness from the microphone in real-time?  Thanks a lot.</p>
  
  <pre><code>               recorder = new AudioRecord(AudioSource.MIC, iSampleRate, AudioFormat.CHANNEL_IN_MONO,             AudioFormat.ENCODING_PCM_16BIT, iAudioBufferSize);
  
                 iBufferReadResult = recorder.read(buffer, 0, iAudioBufferSize);
  
                for (int i = 0; i &lt; buffer.length-1; i++)
              {
                  ByteBuffer bb = ByteBuffer.wrap(buffer, i, 2);
                  int isample = bb.getShort();
  
                  double dsample =(double) isample / 32768.0;
                  sum += (dsample*dsample);
  
                  double rms = Math.sqrt(sum/(buffer.length*0.5));        
                  double decibel = 20*Math.log10(rms);
  
                  sum = 0;
                  }
  
              }   
  </code></pre>