Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get microphone soundlevel in android

Tags:

android

Im trying to get the sound level by getting live-input from the microphone.

As apps such as ,Sound meter and deciBel. I found this sample code from the link:

http://code.google.com/p/android-labs/source/browse/trunk/NoiseAlert/src/com/google/android/noisealert/SoundMeter.java

I'm also pasting it here.

package com.google.android.noisealert;


import android.media.MediaRecorder;

public class SoundMeter {
    static final private double EMA_FILTER = 0.6;

    private MediaRecorder mRecorder = null;
    private double mEMA = 0.0;

    public void start() {
            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"); 
                mRecorder.prepare();
                mRecorder.start();
                mEMA = 0.0;
            }
    }

    public void stop() {
            if (mRecorder != null) {
                    mRecorder.stop();       
                    mRecorder.release();
                    mRecorder = null;
            }
    }

    public double getAmplitude() {
            if (mRecorder != null)
                    return  (mRecorder.getMaxAmplitude()/2700.0);
            else
                    return 0;

    }

    public double getAmplitudeEMA() {
            double amp = getAmplitude();
            mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA;
            return mEMA;
    }

}

Does this code do what im trying to do? Thanks!

like image 479
Seb Avatar asked Jul 13 '11 15:07

Seb


People also ask

How do I check the Sound level on my Android?

Decibel X. This highly-rated app turns your smartphone into a pre-calibrated, accurate and portable sound level meter. It has a standard measurement range from 30 to 130 dB. It boasts many features for measuring the intensity of sound around you built into a nicely-designed, intuitive user interface.


1 Answers

it will once you:

  • instantiate the class
  • call its start() method
  • poll its getAmplitude() function from your main class (just as they did it in that sample code). Be aware that you need to do the polling in a Runnable() so that the UI is not affected. (Call the getAmplitude function every 100ms to detect a change in the input volume)

I have used it for that too and the code does the job.

like image 128
user1207504 Avatar answered Oct 17 '22 07:10

user1207504