Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate loudness level from the microphone on android [closed]

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.

               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 < 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;
                }

            }   
like image 712
Foreverniu Avatar asked Mar 11 '13 08:03

Foreverniu


1 Answers

There are two problems here. The first is that you are calculating the RMS from a single sample, as you aren't calculating the sum across the whole buffer - you're resetting it every sample. So sum contains the square-magnitude for one sample, but then you're dividing by the sample length.

The second problem is that you won't easily be able to create a meter to measure loudness. The decibel value you calculate will only be a power ratio where the maximum value of 0dB indicates a peak. It doesn't have any correlation with physical sound pressure, which is generally what people mean when they say 'loudness' (dB(SPL) is the scale where 50dB is the loudness of speech, 110dB a rock concert, etc).

See also: sound meter android

like image 195
the_mandrill Avatar answered Nov 18 '22 16:11

the_mandrill