Decibel Sound Meter for Android

I am new to Android and want to write an application for measuring decibel sound level . The idea is that when the sound reaches a certain level, the user receives a warning. It. Can anyone help me with this. Can I do this using HTML5 / Javascript? Any help would be appreciated.

+3
source share
3 answers

Adapted from Reading Decibels Android Media Player

To calculate decibels based on orroid / java based for 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"); mRecorder.prepare(); mRecorder.start(); public double getAmplitude() { if (mRecorder != null) return (mRecorder.getMaxAmplitude()); else return 0; } 

To calculate the Db value:

  powerDb = 20 * log10(getAmplitude() / referenceAmp); 

Link: http://en.wikipedia.org/wiki/Decibel#Field_quantities

Not sure if you can do this in HTML5 on android

+10
source

I think you need to clarify what you mean by "deciBel". There are several different types of dB that mean completely different things. An example of the fact that Soham gives you the calculation of the dB peak relative to the reference amplitude, the proposed article proposes to use a maximum digital value of 1.0. This means that the value you select will be in the range of -96 dB to 0 dB for 16-bit audio capture.

I suspect that what you want to do is measure the sound pressure level (range where speech is about 50 dB, inkjet load 120 dB). This is called 'db (SPL)' . You cannot do this on your device if you do not have the means to calibrate the device against certain power levels.

There are additional considerations you need to make. One is that you need peak or RMS power levels (peak for instant events, RMS for continuous sound such as music). You also need to know how to turn off automatic gain control on the device, as this will make any measurements pointless.

+4
source

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; } } 
+1
source

Source: https://habr.com/ru/post/1382832/


All Articles