Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Post History

66%
+2 −0
Q&A 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; ...

1 answer  ·  posted 3y ago by deleted user  ·  last activity 3y ago by deleted user

Question java android physics
#1: Initial revision by (deleted user) · 2021-05-20T08:39:01Z (almost 3 years ago)
How to manage decibel meter?
I had build an application using following source code (following [the steps](https://stackoverflow.com/a/11019896/13146129)).

```
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<br/>
72.12480000000001dB<br/>
81.13996800000001dB<br/>
71.78239488000001dB<br/>
66.2051831808dB<br/>
73.232829308928dB<br/>
64.03725268942848dB<br/>
66.16596043030856dB<br/>
66.50655366884938dB<br/>

When I speak 

>101.3610485870159dB <br/>
354.1377677739226dB<br/> 

When i don't speak anymore

> 56.662042843827614dB<br/>
9.06592685501242dB<br/>
1.4505482968019872dB<br/>


When I was trying to check decibel meter using [the sound meter](https://chrome.google.com/webstore/detail/sound-meter-noise-decibel/fhamlklnpkhdfepaipljcngncafnlbfa/related).

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?