3

I'm trying to get the decibels from the microphone and have looked everywhere how to correctly do it but they don't seem to work.

I get the amplitude like this

public class SoundMeter {
static final private double EMA_FILTER = 0.6;

private MediaRecorder mRecorder = null;
private double mEMA = 0.0;

public void start()  {
    if (mRecorder == null) {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile("/dev/null/");

        try {
            mRecorder.prepare();
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        mRecorder.start();
        mEMA = 0.0;
    }
}

public void stop() {
    if (mRecorder != null) {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }
}

public double getTheAmplitude(){
    if(mRecorder != null)
        return (mRecorder.getMaxAmplitude());
    else
        return 1;
}
public double getAmplitude() {
    if (mRecorder != null)
        return  (mRecorder.getMaxAmplitude()/2700.0);
    else
        return 0;

}

public double getAmplitudeEMA() {
    double amp = getAmplitude();
    mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA;
    return mEMA;
}

}

Then In my other activity I call the getAmplitude method an it returns the amplitude.To convert it to decibels I use this:

dB =  20 * Math.log10(soundMeter.getAmplitude() / 32767);

Ive tried many different values in place for the 32767 but none of them seem to give me a realistic decibel answer. It's usually negative and sometimes -infinity. Please help if you know how to find decibels the right way.

Jared
  • 85
  • 1
  • 9

1 Answers1

10

getMaxAmplitude returns a number between 0 and 32767. To convert that to dB you need to first scale it to to a value between 0 and -1. 20*log10(1)==0 and 20*log10(0)==-inf.

If you're getting -inf then this can only be because you're passing 0 to the log function. This is most likely because you are doing integer division. Change the denominator to a double to force a floating point division.

double dB = 20*log10(x / 32767.0);
jaket
  • 9,140
  • 2
  • 25
  • 44
  • I'm still getting a negative number like -40 – Jared Jun 12 '15 at 04:08
  • 1
    That's good. dB in digital goes from 0 (max) down to -inf. 1.0=0dB, 0.1=-20dB, 0.01=-40dB etc... – jaket Jun 12 '15 at 04:11
  • 1
    Write out the text -40 dBFS. What do you mean? – jaket Jun 12 '15 at 04:14
  • I thought it was 0 and up. And is -20 supposed to be louder than -40? – Jared Jun 12 '15 at 04:16
  • 1
    No it goes down. And yes -20 is louder – jaket Jun 12 '15 at 04:18
  • Well according to the internet it goes from 0 and up. – Jared Jun 12 '15 at 04:20
  • 1
    That's acoustic dBSPL. Totally different thing. If you know the sensitivity of your microphone then you can convert to that but that's a completely different question. http://en.m.wikipedia.org/wiki/DBFS – jaket Jun 12 '15 at 04:22
  • OK. Is there a simple way to convert this to dBSPL? – Jared Jun 12 '15 at 04:23
  • 1
    As I said you need to know the sensitivity of your mic. Or have a microphone calibrator. – jaket Jun 12 '15 at 04:25
  • 1
    So there's no way to convert then. Why do you need it in dBSPL? It's commonplace to display it in dBFS. – jaket Jun 12 '15 at 04:30
  • almost all the sound meter app's dB goes from 0 upwards. I think it's more common and easier for people to understand. – Jared Jun 12 '15 at 04:35
  • 1
    Every single meter that I know of in a digital audio workstation goes down from zero. Google some images of protools or any other daw of your choosing if you don't believe me. If you want to display in dBSPL you simply can't do it without knowing the sensitivity of your mic. Sorry. – jaket Jun 12 '15 at 04:45
  • 1
    cool. I think I answered your original question. If you agree could you please accept the answer. thanks – jaket Jun 12 '15 at 05:09