AcousticEchoCanceler on Samsung devices not working
Asked Answered
E

3

14

I have AcousticEchoCanceler working for VoIP calls for every other device type I've tried, but not on any Samsung device. The device reports AcousticEchoCanceler being available but it simply does nothing.

What I've got:

  • acousticEchoCanceler.setEnabled(true);
  • audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
  • Audio session ID passed to AudioTrack
  • Sample rate: 16k
  • Tried both mono and stereo recording
  • <uses-permission android:name="android.permission.RECORD_AUDIO"/>
  • <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

Has anyone got AcousticEchoCanceler to work on a Samsung device?

Erratic answered 4/10, 2017 at 8:56 Comment(1)
I've noticed that it's almost random - in some cases the AEC works on Samsung devices, sometimes it doesn't. I still haven't found any reason for this, the same code just randomly works.Erratic
L
9

I had the same issue recently. The most important part for me was to use audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION) before creating the AudioRecord.

Next the AudioRecord was created with :

mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, mSamplingRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, audioRecordBufferSize);

And finally create and enable the NS and AEC :

mNoiseSuppressor = NoiseSuppressor.create(mAudioRecord.getAudioSessionId());
if (mNoiseSuppressor != null) {
    int res = mNoiseSuppressor.setEnabled(true);
}

mAcousticEchoCanceler = AcousticEchoCanceler.create(mAudioRecord.getAudioSessionId());
if (mAcousticEchoCanceler != null) {
    int res = mAcousticEchoCanceler.setEnabled(true);
}

The AudioTrack doesn't need to be associated to same Audio session ID than AudioTrack (I suppose it should be done automatically).

Logger answered 8/12, 2017 at 8:22 Comment(0)
G
3

I had tried this code a few years ago and remember that it worked. Can't say how it will fare with the latest devices.

int audioSource = MediaRecorder.AudioSource.VOICE_COMMUNICATION;
int sampleFreq = 16000;
int numChannels = 1;
int numBytesPerSample = 2;
int channelConfig = numChannels == 1 ? AudioFormat.CHANNEL_IN_MONO : AudioFormat.CHANNEL_IN_STEREO;
int audioFormat = numBytesPerSample == 2 ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT;
int bufSize = AudioRecord.getMinBufferSize(sampleFreq, channelConfig, audioFormat);
if (bufSize == AudioRecord.ERROR_BAD_VALUE || bufSize == AudioRecord.ERROR) {
        return;
}

AudioRecord audioInRecord   = new AudioRecord(audioSource, sampleFreq, 
                channelConfig, audioFormat, bufSize);
if (audioInRecord.getState() != AudioRecord.STATE_INITIALIZED) {
    return;
}

boolean aecAvailable = AcousticEchoCanceler.isAvailable();
if (aecAvailable) {
    AcousticEchoCanceler instance;
    if((instance = AcousticEchoCanceler.create(audioInRecord.getAudioSessionId())) != null) {
        instance.setEnabled(true);
        Log.d(TAG, "AEC enabled");
    } else {
        Log.d(TAG, "AEC disabled");
    }
}
Grimbal answered 12/10, 2017 at 4:51 Comment(1)
I have a similar code already, but it randomly doesn't work on latest Samsung devices.Erratic
P
1

Try these lines:

if (AcousticEchoCanceler.isAvailable() && WebRtcAudioUtils.isAcousticEchoCancelerSupported()) {
                WebRtcAudioUtils.setWebRtcBasedAcousticEchoCanceler(true);
                WebRtcAudioUtils.useWebRtcBasedAcousticEchoCanceler();
            }
Pyelography answered 9/10, 2017 at 4:41 Comment(4)
Seems that code alone doesn't do much, the whole pipeline needs to be changed to use WebRtc audio. And I'm not completely sure how to do that.Erratic
Actually that line alone when added to the right point of code seemed to fix the issue for now - I'll need more testing but seems promising so far! Also see Twilio's doc about this.Erratic
Except now it broke on Samsung again and nothing's helping it anymoreErratic
Since time is running out I'm awarding this answer the bounty so that it won't get lost and it's the closest one to getting to the source of the issue, but the question is still openErratic

© 2022 - 2024 — McMap. All rights reserved.