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; } }
android audio microphone decibel
Foreverniu
source share