Calculate microphone volume on Android

I am trying to calculate the volume level from the microphone on Android. I used AudioRecord to get the raw data from the microphone, and also performed some normalization and calculated the decibel after that. But the result is wrong. The decibel values โ€‹โ€‹that I received were not stable and cannot reflect sound. For example, even when I clapped my hands, the decibel value did not reflect the sound. How can I change the code or What if I want to calculate the volume from the microphone in real time? Many thanks.

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; } } 
+8
android audio microphone decibel
source share
1 answer

There are two problems here. Firstly, you calculate the RMS from one sample, since you do not calculate the sum over the entire buffer - you will drop it every sample. Thus, sum contains the square value for one sample, but then you divide by the length of the sample.

The second problem is that it will not be easy for you to create a meter for measuring volume. The calculated decibel value will only be the power ratio, where a maximum value of 0 dB indicates the peak. It has no correlation with physical sound pressure, which people usually mean when they say โ€œvolumeโ€ (dB (SPL) is a scale where 50 dB is the volume of speech, 110 dB is a rock concert, etc.).

See also: android sound meter

+3
source share

All Articles