audiometer

measuring decibels with mobile phone


Good Evening to everybody!

i have the following trouble: i'm tryin to measure the decibels(of the voice) using the microphone of my mobile phone but dont know why it doesn´t work!!! any suggestions??thanks for help!!

The program is this:

`package com.dani;



import java.io.IOException;


import android.media.MediaRecorder;
import android.os.Bundle;
import android.app.Activity;

import android.widget.TextView;

public class Pruebita2 extends Activity {

TextView TextView;
StringBuilder builder=new StringBuilder();
MediaRecorder  mRecorder;
double powerDb;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pruebita2);
TextView=new TextView(this);
setContentView(TextView);

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 e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
mRecorder.start();

}
public double getAmplitude() {
if (mRecorder != null)
        return  (mRecorder.getMaxAmplitude());
else
        return 0;


}
 powerDb = 20 * log10(getAmplitude() / referenceAmp);//obtain the DECIBELS
}`

Solution

  • this code works for me:

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