superpoweredSDK / Low-Latency-Android-iOS-Linux-Windows-tvOS-macOS-Interactive-Audio-Platform

🇸Superpowered Audio, Networking and Cryptographics SDKs. High performance and cross platform on Android, iOS, macOS, tvOS, Linux, Windows and modern web browsers.
https://superpowered.com
1.35k stars 284 forks source link

Initialize of SuperpoweredAndroidAudioIO sporadically failing to record from microphone #316

Closed jdebner closed 7 years ago

jdebner commented 7 years ago

We have a fairly simple processing loop that runs a voice activity detector to detect human voice. We load the superpowered library from our activity but we're seeing sporadic issues when we attempt to initialize SuperpoweredAndroidAudioIO.

SuperpoweredCPU::setSustainedPerformanceMode(true);
new SuperpoweredAndroidAudioIO(sampleRate, bufferSize, true, false, audioProcessing, NULL, -1, SL_ANDROID_STREAM_MEDIA, bufferSize * 2);

While passing in sample rate of 16k (we've tried other sample rates) and buffer size of 512 (been experimenting with buffer sizes) we sometimes find that the SuperpoweredAndroidAudioIO fails to begin sending audio samples to our processing callback. While stepping through the open source side of superpoweredSDK everything seems to be successful but in the logs we see a log indicating AudioRecord failed to start.

E/AudioRecord: start() status -38

This looks like a failure when SuperPowered is attempting to begin getting data from the microphone. Do you have any ways to consistently ensure this recording begins?

Is there something we need to set up or make sure we configure with the device in order to make sure recording will be successful? We've confirmed microphone permissions and it does work in additional attempts within the same app session with the same parameters.


Happy to share more if it helps but we've based our design on the Android sample projects and we have an implementation on iOS that does seem to be working correctly. One difference we noticed is iOS has a "preferred" sample rate so we do a conversion if it ends up recording a different sample rate than what our VAD supports. We never get confirmation back from Android if sample rate is supported other than passing in a constant and hoping it works.

superpoweredSDK commented 7 years ago

I recommend to set up audio I/O with the preferred sample rate and a multiple of the preferred buffer size, just like how it's shown in the example projects. Then handle sample rate conversion with the Superpowered resampler, and buffer sizes with your own code (like in iOS). Android devices are not very reliable when it comes to arbitrary sample rates and buffer sizes.