Decibel Meter using Media Recorder

2019-08-28 01:10发布

问题:

I was referring to a code that I found previously and tried it myself. It works perfectly however the decibel measured from the code is extremely high even in a quiet room. The value ranged from 0 to 30000. I was expecting the decibel be around 30 ~ 40 when I am in a quiet room. Can someone please tell me what's wrong with the code? Maybe the algorithm in the code is wrong because "soundDB()" is not used. The decibel shown is the app is from "getAmplitudeEMA()" instead.

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

}

回答1:

The problem is the following line:

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

You are setting as text the value returned from getAmplitudeEMA() but this is not a dB value, it's the amplitude returned by mRecorder.getMaxAmplitude() which is then modified by getAmplitudeEMA().

To "convert" amplitude to dB you need to include your soundDb(double ampl) method in this way:

public void updateTv(){
    mStatusView.setText(Double.toString(soundDb(getAmplitudeEMA()))+ "dB");
}