本文介绍了Android如何查找分贝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从麦克风上获得分贝,并且到处都在寻找如何正确执行分贝的方法,但是它们似乎没有用.

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.

我得到这样的振幅

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;
}

}

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

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);

我已经为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.

推荐答案

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

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.

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

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);

这篇关于Android如何查找分贝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 16:58