Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I was trying to get the amplitude level of a microphone on Android like so:

MediaRecorder recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC);  Timer timer = new Timer(); timer.scheduleAtFixedRate(new RecorderTask(recorder), 0, 1000);  private class RecorderTask extends TimerTask {     private MediaRecorder recorder;      public RecorderTask(MediaRecorder recorder) {         this.recorder = recorder;     }      public void run() {         Log.v("MicInfoService", "amplitude: " + recorder.getMaxAmplitude());     } } 

Unfortunately, this only returns 0 all the time.

It appears that for this to work I have to actually start recording. Is that correct?

If so, do I need to record for 500ms, get amplitude, stop recording and repeat?

Finally, do I have to record to a file? I do not need to save this audio file, can't I just get the current amplitude or highest amplitude since last call of the current live microphone input without recording?

Any help is appreciated, thanks.

like image 326
Tom Avatar asked Jan 23 '11 22:01

Tom


People also ask

Which microphone is better for On Location dialogue recording?

Medium shotgun microphone is better to use it to record on location. In comparison to the short shotgun microphone, the medium shotgun microphone has a more directional pickup pattern and rejects more off-axis sound.

How do I choose my microphone on Android?

To start, tap the settings cog in the bottom right, then choose "Audio." Here, you'll find your microphone options as the first menu in the window. Tap the arrows to move through the mic options.

How do I set audio output as MIC input Android?

This is not as easy as it sounds:Start QJackCtl. Click the "Setup..." button. Choose "alsa" from the "Driver:" dropdown. Choose your soundcard/soundcards that you want to work with from the "Input Device" and "Output Device" dropdowns.


1 Answers

The solution from Toumal works, however I wasn't able to get a high enough refresh rate for my needs. So I ended up using the SoundMeter.java class that Toumal linked but modified it to use the code from this answer

Here is the code I used, which provides a much better refresh rate:

import android.media.AudioFormat; import android.media.AudioRecord; import android.media.MediaRecorder;  public class SoundMeter {      private AudioRecord ar = null;     private int minSize;      public void start() {         minSize= AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);         ar = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,minSize);         ar.startRecording();     }      public void stop() {         if (ar != null) {             ar.stop();         }     }      public double getAmplitude() {         short[] buffer = new short[minSize];         ar.read(buffer, 0, minSize);         int max = 0;         for (short s : buffer)         {             if (Math.abs(s) > max)             {                 max = Math.abs(s);             }         }         return max;     }  } 
like image 77
Benjamin Kaiser Avatar answered Sep 29 '22 12:09

Benjamin Kaiser