0

我指的是我之前找到并自己尝试过的代码。它工作得很好,但是即使在安静的房间里,从代码测量的分贝也非常高。数值范围从 0 到 30000。当我在一个安静的房间里时,我预计分贝在 30 到 40 左右。有人可以告诉我代码有什么问题吗?也许代码中的算法是错误的,因为没有使用“soundDB()”。显示的分贝是应用程序来自“getAmplitudeEMA()”。

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

public class Noise extends Activity {

TextView mStatusView;
MediaRecorder mRecorder;
Thread runner;
private static double mEMA = 0.0;
static final private double EMA_FILTER = 0.6;

final Runnable updater = new Runnable(){

    public void run(){          
        updateTv();
    };
};
final Handler mHandler = new Handler();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.noiselevel);
    mStatusView = (TextView) findViewById(R.id.status);


    if (runner == null)
    { 
        runner = new Thread(){
            public void run()
            {
                while (runner != null)
                {
                    try
                    {
                        Thread.sleep(1000);
                        Log.i("Noise", "Tock");
                    } catch (InterruptedException e) { };
                    mHandler.post(updater);
                }
            }
        };
        runner.start();
        Log.d("Noise", "start runner()");
    }
}

public void onResume()
{
    super.onResume();
    startRecorder();
}

public void onPause()
{
    super.onPause();
    stopRecorder();
}

 public void startRecorder(){
    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 (java.io.IOException ioe) {
            android.util.Log.e("[Monkey]", "IOException: " + 
android.util.Log.getStackTraceString(ioe));

        }catch (java.lang.SecurityException e) {
            android.util.Log.e("[Monkey]", "SecurityException: " +   
android.util.Log.getStackTraceString(e));
        }
        try{           
            mRecorder.start();
        }catch (java.lang.SecurityException e) {
            android.util.Log.e("[Monkey]", "SecurityException: " +    
android.util.Log.getStackTraceString(e));
        }

        //mEMA = 0.0;
    }

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

public void updateTv(){
    mStatusView.setText(Double.toString((getAmplitudeEMA())) + " dB");
}
public double soundDb(double ampl){
    return  20 * Math.log10(getAmplitudeEMA() / ampl);
}
public double getAmplitude() {
    if (mRecorder != null)
        return  (mRecorder.getMaxAmplitude());
    else
        return 0;
    }
public double getAmplitudeEMA() {
    double amp =  getAmplitude();
    mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA;
    return mEMA;
}

}
4

2 回答 2

0

问题是以下行:

mStatusView.setText(Double.toString((getAmplitudeEMA())) + " dB");

您正在将返回的值设置为文本,getAmplitudeEMA()但这不是 dB 值,它是返回的幅度mRecorder.getMaxAmplitude(),然后由getAmplitudeEMA().

要将幅度“转换”为 dB,您需要以soundDb(double ampl)这种方式包含您的方法:

public void updateTv(){
    mStatusView.setText(Double.toString(soundDb(getAmplitudeEMA()))+ "dB");
}
于 2018-05-31T15:24:07.600 回答
0

您必须调用函数 soundDb,它将幅度转换为 dB FS。

    mStatusView.setText(soundDb(32767.0) + " dB");
于 2020-01-08T19:47:24.940 回答