Android如何找到分贝

Jar*_*red 1 audio android microphone decibel

我正试图从麦克风中获取分贝,并且到处寻找如何正确地做到这一点,但它们似乎无法正常工作.

我得到这样的幅度

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;
}
Run Code Online (Sandbox Code Playgroud)

}

然后在我的其他活动中,我调用getAmplitude方法,它返回幅度.要将其转换为分贝,我使用:

dB =  20 * Math.log10(soundMeter.getAmplitude() / 32767);
Run Code Online (Sandbox Code Playgroud)

我已经为32767尝试了许多不同的值,但它们似乎都没有给我一个真实的分贝答案.它通常是负面的,有时是无限的.如果您知道如何以正确的方式找到分贝,请提供帮助.

jak*_*ket 8

getMaxAmplitude返回0到32767之间的数字.要将其转换为dB,您需要先将其缩放到0到-1之间的值. 20*log10(1)==020*log10(0)==-inf.

如果您正在获取-inf,那么这只能是因为您将0传递给日志函数.这很可能是因为你正在进行整数除法.将分母更改为双精度以强制浮点除法.

double dB = 20*log10(x / 32767.0);
Run Code Online (Sandbox Code Playgroud)