Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

measuring decibels with mobile phone

Tags:

audio

meter

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
}`
like image 372
Daniel Hernandez Ramirez Avatar asked Mar 28 '13 23:03

Daniel Hernandez Ramirez


People also ask

Can you measure dB with Android?

Decibel Pro App This decibel meter app is known as one of the best sound measuring apps available for different types of devices. Decibel Pro App is supported on both iOS and Android, so you can download it from Apple App Store or Google Play Market.

Are phone dB readers accurate?

The answer is, “Yes,” but only four apps that were tested measure up. The National Institute for Occupational Safety and Health (NIOSH) evaluated the apps at its acoustic testing lab. The tests also compared results for i-Phones and Android phones.


1 Answers

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

}
like image 183
Daniel Hernandez Ramirez Avatar answered Oct 19 '22 22:10

Daniel Hernandez Ramirez