Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Emulator Seems to Record Audio at 96khz

My app is recording audio from phone's microphones and does some real time processing on it. It's working fine on physical devices, but acts "funny" in emulator. It records something, but I'm not quite sure what it is it's recording.

It appears that on emulator the audio samples are being read at about double the rate as on actual devices. In the app I have a visual progress widget (a horizontally moving recording head), which moves about twice as fast in emulator.

Here is the recording loop:

int FREQUENCY = 44100;
int BLOCKSIZE = 110;

int bufferSize = AudioRecord.getMinBufferSize(FREQUENCY,
        AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT) * 10;

AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.CAMCORDER,
        FREQUENCY, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT,
        bufferSize);

short[] signal = new short[BLOCKSIZE * 2]; // Times two for stereo

audioRecord.startRecording();

while (!isCancelled()) {
    int bufferReadResult = audioRecord.read(signal, 0, BLOCKSIZE * 2);
    if (bufferReadResult != BLOCKSIZE * 2)
        throw new RuntimeException("Recorded less than BLOCKSIZE x 2 samples:"
                + bufferReadResult);

    // process the `signal` array here
}

audioRecord.stop();
audioRecord.release();

The audio source is set to "CAMCORDER" and it records in stereo. The idea is, if the phone has multiple microphones, the app will process data from both and use whichever has better SNR. But I have the same problems if recording mono from AudioSource.MIC. It reads audio data in a while loop, I am assuming that audioRecord.read() is a blocking call and will not let me read same data twice.

The recorded data looks OK – the record buffer contains 16-bit PCM samples for two channels. The loop just seems to be running at twice the speed as on real devices. Which leads me to think that maybe the emulator is using a higher sampling rate than the specified 44100Hz. If I query the sample rate with audioRecord.getSampleRate() it returns the correct value.

Also there are some interesting audio related messages in logcat while recording:

07-13 12:22:02.282  1187  1531 D AudioFlinger: mixer(0xf44c0000) throttle end: throttle time(154)
(...)
07-13 12:22:02.373  1187  1817 E audio_hw_generic: Error opening input stream format 1, channel_mask 0010, sample_rate 16000
07-13 12:22:02.373  1187  3036 I AudioFlinger: AudioFlinger's thread 0xf3bc0000 ready to run
07-13 12:22:02.403  1187  3036 W AudioFlinger: RecordThread: buffer overflow
(...)
07-13 12:22:24.792  1187  3036 W AudioFlinger: RecordThread: buffer overflow
07-13 12:22:30.677  1187  3036 W AudioFlinger: RecordThread: buffer overflow
07-13 12:22:37.722  1187  3036 W AudioFlinger: RecordThread: buffer overflow

I'm using up-to-date Android Studio and Android SDK, and I have tried emulator images running API levels 21-24. My dev environment is Ubuntu 16.04

Has anybody experienced something similar? Am I doing something wrong in my recording loop?

like image 678
Pēteris Caune Avatar asked Jul 13 '16 09:07

Pēteris Caune


People also ask

Is 96kHz enough for recording?

The verdict. Ultimately for recording, both camps are right. Recording at 96kHz can improve the sound quality but it can also make no difference, depending on your collection of plug-ins and the musical material. In any event, you do have to consider the CPU resources tradeoff.

Can we record audio in emulator?

Note: The Android Emulator cannot record audio.

Which emulator is fastest in Android Studio?

5: MEmu Emulator It is among the fastest open-source Android emulator for playing mobile games on a desktop. It is known to provide excellent performance and an extreme user experience. MEmu allows one to level multiple accounts for the same game, or the user can play many games on one account simultaneously.


1 Answers

I suspect it is caused by AudioFormat.CHANNEL_IN_STEREO. A mic on device is typically a mono audio source. If because of some reason emulator supports stereo, you will be receiving twice as much data on emulator (for both channels). To verify this, try to switch to AudioFormat.CHANNEL_IN_MONO, which is guarantied to work on all devices, and see whether you receive same amount of data on emulator then.

like image 73
sergej shafarenka Avatar answered Oct 04 '22 07:10

sergej shafarenka