Wednesday, 22 January 2014

Detect noise in android example

Detect noise in android example : 

Noise level of the around device is find by using the MediaRecorder. From the MediaRecorder we get the Amplitude from that we can get the noise level in decibel.
Follow the simple steps to do that,
1)Get the noise level using the MediaRecorder,

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 (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}mRecorder.start();
2)Crate Runnable Thread to check the noise level frequently,

private Runnable mPollTask = new Runnable() {
publicvoid run() {
           double amp = mSensor.getAmplitude();
           //Log.i("Noise", "runnable mPollTask");
          // Runnable(mPollTask) will again execute after POLL_INTERVAL
           mHandler.postDelayed(mPollTask, POLL_INTERVAL);
}
};
3)Convert the Amplitude to decibel using the following formula,

return 20 * Math.log10(mRecorder.getMaxAmplitude() / 2700.0);

4)Finally,Update the noise level in the textview and the progressbar,


bar.setProgress((int)signalEMA);
tv_noice.setText(signalEMA+"dB");
Screenshot:

4 comments:

Online wallpaper said...

v nice ... and easy example.. thnx alot

Rajiv said...
This comment has been removed by the author.
Rajiv said...

What is 2700 while conversation to dB??

AlexandreFaust said...

On step 3, the base is 2700.0, where you finded this number? Thanks.