Android webrtc中如何做到声音AGC麦克风自动调控,ANS背景噪音处理,以及AEC回音控制呢?在google的webrtc中其实都已经有了对应的处理方法,先了解声音AGC,ANS,AEC等的基本概念。
AGC是自动增益补偿功能(Automatic Gain Control),AGC可以自动调麦克风的收音量,使与会者收到一定的音量水平,不会因发言者与麦克风的距离改变时,声音有忽大忽小声的缺点。
ANS是背景噪音抑制功能(Automatic Noise Suppression),ANS可探测出背景固定频率的杂音并消除背景噪音,例如:风扇、空调声自动滤除。呈现出与会者清晰的声音。
AEC是回声消除器(Acoustic Echo Chancellor),AEC可以消除各种延迟的回声。
webrtc想要实现回声控制,噪音处理,声音补偿,可以参照下方的方法进行。
创建PeerConnectionFactory的时候,将AudioDeviceModule添加到PeerConnectionFactory:
private PeerConnectionFactory createPeerConnectionFactory () { AudioDeviceModule audioDeviceModule = JavaAudioDeviceModule.builder ( mContext ) .setUseHardwareAcousticEchoCanceler ( false ) .setUseHardwareNoiseSuppressor ( false ) .createAudioDeviceModule (); PeerConnectionFactory.InitializationOptions initializationOptions = PeerConnectionFactory.InitializationOptions.builder ( mContext ) .createInitializationOptions (); PeerConnectionFactory.initialize ( initializationOptions ); peerConnectionFactory = PeerConnectionFactory.builder () .setAudioDeviceModule ( audioDeviceModule ) .createPeerConnectionFactory (); return peerConnectionFactory; }
创建PeerConnection对象:
public PeerConnection createPeerConnection() { PeerConnection.RTCConfiguration rtcConfig = new PeerConnection.RTCConfiguration(mIceLis); if (mFactory != null) { return mPeerConnectionFactory.createPeerConnection(rtcConfig, this); } else { return null; } }
然后对本地语音流进行AGC,ANS,AEC处理,都设置为true即可:
private void createLocalStream () { WebRtcAudioUtils.setWebRtcBasedNoiseSuppressor ( true ); WebRtcAudioUtils.setWebRtcBasedAcousticEchoCanceler ( true ); WebRtcAudioUtils.setWebRtcBasedAutomaticGainControl ( true ); MediaConstraints localMediaConstraints = new MediaConstraints (); AudioSource localAudioSource = peerConnectionFactory.createAudioSource ( localMediaConstraints ); localTrack = peerConnectionFactory.createAudioTrack ( LOCAL_AUDIO_TRACK, localAudioSource ); localTrack.setEnabled ( true ); localMediaStream = peerConnectionFactory.createLocalMediaStream ( LOCAL_STREAM ); localMediaStream.addTrack ( localTrack ); peerConnection.addStream ( localMediaStream ); }