Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
How to manage decibel meter?
I had build an application using following source code (following the steps).
package com.decibal.level;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView mStatusView;
MediaRecorder mediaRecorder;
Thread runner;
private static double mEMA = 0.0;
static final private double EMA_FILTER = 0.6;
final Runnable updater = new Runnable() {
@Override
public void run() {
updateTv();
};
};
final Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStatusView = findViewById(R.id.statusView);
if (runner == null){
runner = new Thread(){
public void run(){
while(runner!=null){
try{
Thread.sleep(1000);
Log.d("Noise","Tock");
}catch (InterruptedException e){
e.printStackTrace();
}
handler.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 (mediaRecorder == null){
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile("/dev/null");
try{
mediaRecorder.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{
mediaRecorder.start();
}catch (java.lang.SecurityException e){
android.util.Log.e("[Monkey]", "SecurityException: " + android.util.Log.getStackTraceString(e));
}
//mEMA = 0.0;
}
}
public void stopRecorder(){
if (mediaRecorder!=null){
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
}
}
public void updateTv(){
mStatusView.setText(Double.toString((getAmplitudeEMA()))+"dB");
}
public double soundDb(double ampl){
return 20 * Math.log10(getAmplitudeEMA()/ampl);
}
public double getAmplitudeEMA() {
double amp = getAmplitude();
mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA;
return mEMA;
}
public double getAmplitude() {
if (mediaRecorder != null)
return (mediaRecorder.getMaxAmplitude());
else
return 0;
}
}
I am listing decibel meter result below :
59.28dB
72.12480000000001dB
81.13996800000001dB
71.78239488000001dB
66.2051831808dB
73.232829308928dB
64.03725268942848dB
66.16596043030856dB
66.50655366884938dB
When I speak
101.3610485870159dB
354.1377677739226dB
When i don't speak anymore
56.662042843827614dB
9.06592685501242dB
1.4505482968019872dB
When I was trying to check decibel meter using the sound meter.
The meter was at 60-71(When I speak also). The source code is correct but, the equation is wrong(that's what I think). When I don't speak than, I just can hear fan rotation sound (noise) which is not too much. Phone can detect the sound also. But, I worry when I stop speaking than decibel meter drops. How can I manage the decibel meter?
1 answer
I was reading the Wiki of Decibel. Unfortunately, I "forgot" there's different between amplitude ratio and dB level. After reading the wiki. When I looked at my source code, I saw sounddB
was unused. When I tried to get a list from sounddB
, I noticed the decibel meter was at 70. I had write ampl = 10exp(-7)
from the comment. That's basically ok to solve the problem. But, this doesn't print accurately as the extension does. So, you have to change ampl level
.
1 comment thread