Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return amplitude of the mic input when not recording

I have the following code and need to be able to listen to the mic on the phone and use that value to be displayed graphically on some sort of visualizer:

Timer micTimer = new Timer();
micTimer.schedule(new TimerTask(){
    @Override
    public void run(){
        int x = MediaRecorder.getAudioSourceMax();
        Log.v("Timer", "" + x);
    }
}, 0, 100);

I know this is probably far from wrong. Later in the class I am using an AudioRecorder object to record the sound for some post processing. Here I don't want to record anything just display the sound on a visualizer. The log from this code just outputs the number 6 every 100ms. Could it have something to do with it auto-adjusting the gain? If so how do I get round this?

like image 962
SamRowley Avatar asked Feb 03 '26 23:02

SamRowley


1 Answers

Well, I'm sorry to say Sam, but you can't get the amplitude from the mic without recording. I developed an app very similar to this. You have to begin recording, call the getMaxAmplitude() function(which will return 0), wait a little bit, then call it again and it will return the maximum amplitude since the function was called last. Look into the NoiseAlert app. It really helped me out.

Also, as a side note, in my research I have found that it's easier to use the MediaRecorder object because it doesn't have the auto-gain; you get what's actually being 'heard' by the microphone.

like image 129
Brandon Avatar answered Feb 05 '26 13:02

Brandon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!