yogesh kumar yogesh kumar - 25 days ago 14
Android Question

Some Devices not able to generate audio stream/distorted voice from mic

I am using Webrtc(Android Version) for peer to peer communication in a calling app. It is working fine for most of the devices and the voice quality is excellent but in some devices(eg. Samsung GT-I8262) it is not able to generate audio. After online search, we came to know that some devices take different sampling_rate, frame_rate and buffer than the android guaranteed sampling rate of 44.1 khz.

We tried to hard code these values inside the source code of webrtc and after using the produced lib we are getting very distorted/Robotic type voice(But now we are able to generate the audio stream).

We have tested some calling applications which are producing the audio stream correctly from this device. This shows there must be some solution for this. Please guide me

Answer

this issue is occurring because of frame rate according to Sample rate supported by devices. as you mentioned that android guaranteed sampling rate of 44.1 kHz(very true).and I found out that all phones support either 44100 Hz, 48000 Hz, or both. but some older devices like Device Model name started with Samsung GT-* ** does support different ones.so you need to change the sample rate which is supported by your device.

public void getValidSampleRates() {
for (int rate : new int[] {8000, 11025, 16000, 22050, 44100}) {  // add the rates you wish to check against
    int bufferSize = AudioRecord.getMinBufferSize(rate, AudioFormat.CHANNEL_CONFIGURATION_DEFAULT, AudioFormat.ENCODING_PCM_16BIT);
    if (bufferSize > 0) {
        // buffer size is valid, Sample rate supported

    }
}  }

by above code, you will get Valid Sample Rate by the device. then you need to change DEFAULT_SAMPLE_RATE_HZ inside WebRtcAudioUtils.java initially value is 16kHz as the default sample rate.so A higher sample rate might prevent from supporting communication mode on some older (e.g. ICS) devices. so instead of doing hard code value inside the source code of web RTC you need to call a function

  WebRtcAudioUtils.setDefaultSampleRateHz(8000);

This will override the default sample rate for webrtc. The webrtc lib automatically calculating the best frame_rate for particular sample rate. this function can be useful on some devices where the available Android APIs are known to return invalid results. You need to call this function before any audio/video initialization(or recording). hopefully, this will solve your issue. please have a closer look at WebRtcAudioUtils.java

Comments