Search code examples
androidaudiosignal-processingnoise

How to analyze the value returned by the mediaRecorder.getMaxAmplitude() in Android


I am building an android application that should start recording the audio when the recorded sound is above a certain threshold.

I researched and found out that the method getMaxAmplitude() of the MediaRecorder class gives an estimate of the loudness of the noise as a measure of sound pressure. It returns a value between 0 to +32767.

Here is the code that I have written.

MediaRecorder mediaRecorder = new MediaRecorder();
while(True){
   if (mediaRecorder.getMaxAmplitude() > THRESHOLD)
      System.out.println("Recording sound");
   else
      System.out.println("Sound recording not above the threshold level);
}

I have trouble figuring out the THRESHOLD value since there is no standard unit for the value returned by the getMaxAmplitude() function. It would be preferred if someone could suggest a conversion from the value returned by the getMaxAmplitude() function to decibel (dB) or any other useful unit.


Solution

  • There is no easy way in which you could interpret the output of getMaxAmplitude() as it will just give you the maximum value your phone's specific hardware is getting. Unfortunately the fact that a manufacturer complies with Android OS requirements doesn't ensure that you'll get the same response from every microphone. As a matter of fact not even all the devices from the same manufacturer have the same kind of microphone and thus you won't get the same frequency response or sensibility. If you want to convert to dB from this adimentional (but hardware dependent sound intensity proportional) units getMaxAmplitude() is returning, just use dB = 20log10(getMaxAmplitude()). Now if you want this to work on a wide range of devices I would advice you to include a calibration stage in your app so you can set a dynamic device dependent threshold instead of an absolute value. Maybe you can set your threshold as the relationship between the intensity you get when you calibrate and the intensity you get on the moment you're trying to capture. I don't know the specifics of your app but if I were you I would have at least one of these two things in mind to compare with. The moment when the microphone input is most quite and when it's most saturated (maybe you can generate your own loud sound from the speaker and record it)