使用Media Recorder的分贝计 [英] Decibel Meter using Media Recorder

查看:183
本文介绍了使用Media Recorder的分贝计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是我之前找到并自己尝试过的代码.它可以完美工作,但是即使在安静的房间中,根据代码测得的分贝也非常高.值的范围从0到30000.当我在安静的房间里时,我期望分贝约为30到40.有人可以告诉我代码有什么问题吗?也许代码中的算法是错误的,因为未使用"soundDB()".显示的分贝是该应用来自"getAmplitudeEMA()".

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

}

推荐答案

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

You have to call the function soundDb, which transforms the amplitude into dB FS.

    mStatusView.setText(soundDb(32767.0) + " dB");

这篇关于使用Media Recorder的分贝计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆