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;
}
}
AudioRecord
class supports reading directly into ashort[]
or aByteBuffer
- so why read into abyte[]
, wrap is as aByteBuffer
and access it as shorts? – Returnable