7

Im quite new to android and i have searched about this for quite a while. I would like to build an application that is something like a decibel meter. In realtime it shows the sound level. It there is much noise in the room, there will be something indicating that, if its quiet something will indicate that!.

I don't have any idea at all how to do this. Could anyone explain what the basics of the microphone-sound-level application? If its possible, maybe provide some code?

Thanks!

Seb
  • 611
  • 4
  • 11
  • 13

3 Answers3

9

You can use MediaRecorder.getMaxAmplitude().

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO},
            RECORD_AUDIO);
}
  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) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mRecorder.start();
    
  2. Start the MediaRecorder,

    private Runnable mSleepTask = new Runnable() {
        public void run() {
            //Log.i("Noise", "runnable mSleepTask");
            mSensor.start();
            if (!mWakeLock.isHeld()) {
                 mWakeLock.acquire();
            }
            //Noise monitoring start
            // Runnable(mPollTask) will execute after POLL_INTERVAL
            mHandler.postDelayed(mPollTask, POLL_INTERVAL);
        }
    };
    
  3. Create Runnable Thread to check the noise level frequently,

    private Runnable mPollTask = new Runnable() {
        public void run() {
            double amp = mSensor.getAmplitude();
            //Log.i("Noise", "runnable mPollTask");
            // Runnable(mPollTask) will again execute after POLL_INTERVAL
            mHandler.postDelayed(mPollTask, POLL_INTERVAL);
        }
    };
    

Convert the Amplitude to decibel using the following formula,

return 20 * Math.log10(mRecorder.getMaxAmplitude() / 2700.0);
  1. Monitoring the Voice and Alert for the Louder Noise.

    // Create runnable thread to Monitor Voice
    private Runnable mPollTask = new Runnable() {
        public void run() {
            double amp = mSensor.getAmplitude();
            //Log.i("Noise", "runnable mPollTask");
            updateDisplay("Monitoring Voice...", amp);
    
            if ((amp > mThreshold)) {
                callForHelp(amp);
                //Log.i("Noise", "==== onCreate ===");
            }
            // Runnable(mPollTask) will again execute after POLL_INTERVAL
            mHandler.postDelayed(mPollTask, POLL_INTERVAL);
        }
    };
    
Simon Corcos
  • 962
  • 14
  • 31
Tung Duong
  • 1,156
  • 7
  • 19
  • @AshwinBalani you can check here https://github.com/Santosh-Gupta/SitcomSimulator/blob/master/noisealert/src/com/androidexample/noisealert/NoiseAlert.java – Tung Duong Sep 03 '19 at 07:34
1

This question has been addressed generally for Java, and the required classes are available in Android.

The basic idea is to sample the data line for the microphone, and calculate the level from the returned buffer.

How to calculate the level/amplitude/db of audio signal in java?

You can also have a look at the Visualizer class which does FFT frequency analysis, however the permissions for microphone may not be consistent across various devices. You may also have to connect it to the Equalizer class to access the mic.

https://developer.android.com/reference/android/media/audiofx/Visualizer.html

Dominic Cerisano
  • 3,522
  • 1
  • 31
  • 44
0

There's a great app in the marketplace called Audalyzer

http://code.google.com/p/moonblink/wiki/Audalyzer

Also check this discussion..

Android: sample microphone without recording to get live amplitude/level?

Community
  • 1
  • 1
timemirror
  • 586
  • 4
  • 11
  • 5
    This is not the answer, the guy is clearly asking how to build the app. He's not looking for apps – chntgomez Sep 02 '17 at 16:49
  • 1
    @chntgomez I think timemirror gave the app example because it has the code on the link, so one cananalyze it and learn how to do it (it's not as direct as code here though) – Edw590 Feb 28 '23 at 17:22