webrtc 源码阅读——audioprocessing

来源:互联网 发布:优化蜜蜡能买吗 编辑:程序博客网 时间:2024/05/29 03:17

webrtc 作为一个流媒体的框架,包含音频、视频、p2p传输一整套流媒体核心技术。最近利用业余时间学习它的音频模块,webrtc支持音频的麦克风采集、编码、RTP打包发送。同时加入了AGC(自动增益)、NS(降噪)、AEC(回声消抑)、VAD(静音检测)等一系列算法。这些算法主要在采集过后,编码之前进行处理,代码集中在audioprocessing module,简称APM。下面是audioprocessing.h文件的内容:

class AudioFrame;template<typename T>class Beamformer;class StreamConfig;class ProcessingConfig;class EchoCancellation;class EchoControlMobile;class GainControl;class HighPassFilter;class LevelEstimator;class NoiseSuppression;class VoiceDetection;

AudioFrame:主要记录了通道基本信息,数据,VAD标志时间戳,采样频率,信道数等。

EchoCancellation:回声消除模块(AEC),在使用外置扬声器的时候应该使用,有些使用耳麦通讯的情况也会存在回声(因为麦克风与扬声器有空间或者电的弱耦合),如果影响了通话也应该开启。

EchoControlMobile:回声抑制模块(AES),这个模块和回声消除模块功能相似,但是实现方法不一样。该模块使用定电话实现,运算量也远远小于回声消除模块。非常适合移动平台使用。但是对语音损伤大。

GainControl:增益控制模块(AGC),这个模块使用了语音的特征对系统硬件音量和输出的信号大小进行调节。硬件上可以控制输入音量。软件上只能调节原来信号的幅度,如果对原来就已经破音的信号,或者本来输入就比较小的信号就无能为力了。

HighPassFilter:高通滤波器,抑制不需要的低频信号。内部是使用定化的IIR实现的。可以根据需要修改参数选择相应的截止频率。对于某些有工频干扰的设备需要使用高通滤波器。

LevelEstimator:估计信号的能量值。

NoiseSuppression:噪声抑制模块(NS/SE),该模块一般应用在有环境噪声的情况,或者是麦克风采集到的数据有明显噪声的情况。

VoiceDetection:语音激活检测模块(VAD),该模块用于检测语音是否出现。用于编解码以及后续相关处理。在语音通话过程中,如果一方在收听,而没有说话,则会检测到输出端没有采集到数据,此时便不会发送数据。这样会根据是否采集到数据,动态调节数据的发送状况,降低了不必要的带宽浪费。

APM分为两个流,一个近端流,一个远端流。近端(Near-end)流是指从麦克风进入的数据;远端(Far-end)流是指接收到的数据。现在分别介绍一下,这部分代码在audio_processing_impl.cc里。

对far-end流的处理:

int AudioProcessingImpl::ProcessReverseStreamLocked() {  AudioBuffer* ra = render_.render_audio.get();  // For brevity.  if (rev_analysis_needed()) {    ra->SplitIntoFrequencyBands();  }  if (capture_nonlocked_.intelligibility_enabled) {    public_submodules_->intelligibility_enhancer->ProcessRenderAudio(        ra->split_channels_f(kBand0To8kHz), capture_nonlocked_.split_rate,        ra->num_channels());  }  RETURN_ON_ERR(public_submodules_->echo_cancellation->ProcessRenderAudio(ra));  RETURN_ON_ERR(      public_submodules_->echo_control_mobile->ProcessRenderAudio(ra));  if (!constants_.use_experimental_agc) {    RETURN_ON_ERR(public_submodules_->gain_control->ProcessRenderAudio(ra));  }  if (rev_synthesis_needed()) {    ra->MergeFrequencyBands();  }  return kNoError;}

1、AEC流程,记录AEC中的farend及其相关运算;

2、AES流程,记录AES中的farend及其相关运算;

3、AGC流程,计算farend及其相关特征。

对near-end流处理

int AudioProcessingImpl::ProcessStreamLocked() {  // Ensure that not both the AEC and AECM are active at the same time.  // TODO(peah): Simplify once the public API Enable functions for these  // are moved to APM.  RTC_DCHECK(!(public_submodules_->echo_cancellation->is_enabled() &&               public_submodules_->echo_control_mobile->is_enabled()));#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP  if (debug_dump_.debug_file->is_open()) {    audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();    msg->set_delay(capture_nonlocked_.stream_delay_ms);    msg->set_drift(        public_submodules_->echo_cancellation->stream_drift_samples());    msg->set_level(gain_control()->stream_analog_level());    msg->set_keypress(capture_.key_pressed);  }#endif  MaybeUpdateHistograms();  AudioBuffer* ca = capture_.capture_audio.get();  // For brevity.  if (constants_.use_experimental_agc &&      public_submodules_->gain_control->is_enabled()) {    private_submodules_->agc_manager->AnalyzePreProcess(        ca->channels()[0], ca->num_channels(),        capture_nonlocked_.fwd_proc_format.num_frames());  }  if (fwd_analysis_needed()) {    ca->SplitIntoFrequencyBands();  }  if (capture_nonlocked_.beamformer_enabled) {    private_submodules_->beamformer->ProcessChunk(*ca->split_data_f(),                                                  ca->split_data_f());    ca->set_num_channels(1);  }  public_submodules_->high_pass_filter->ProcessCaptureAudio(ca);  RETURN_ON_ERR(public_submodules_->gain_control->AnalyzeCaptureAudio(ca));  public_submodules_->noise_suppression->AnalyzeCaptureAudio(ca);  // Ensure that the stream delay was set before the call to the  // AEC ProcessCaptureAudio function.  if (public_submodules_->echo_cancellation->is_enabled() &&      !was_stream_delay_set()) {    return AudioProcessing::kStreamParameterNotSetError;  }  RETURN_ON_ERR(public_submodules_->echo_cancellation->ProcessCaptureAudio(      ca, stream_delay_ms()));  if (public_submodules_->echo_control_mobile->is_enabled() &&      public_submodules_->noise_suppression->is_enabled()) {    ca->CopyLowPassToReference();  }  public_submodules_->noise_suppression->ProcessCaptureAudio(ca);  if (capture_nonlocked_.intelligibility_enabled) {    RTC_DCHECK(public_submodules_->noise_suppression->is_enabled());    int gain_db = public_submodules_->gain_control->is_enabled() ?                  public_submodules_->gain_control->compression_gain_db() :                  0;    public_submodules_->intelligibility_enhancer->SetCaptureNoiseEstimate(        public_submodules_->noise_suppression->NoiseEstimate(), gain_db);  }  // Ensure that the stream delay was set before the call to the  // AECM ProcessCaptureAudio function.  if (public_submodules_->echo_control_mobile->is_enabled() &&      !was_stream_delay_set()) {    return AudioProcessing::kStreamParameterNotSetError;  }  RETURN_ON_ERR(public_submodules_->echo_control_mobile->ProcessCaptureAudio(      ca, stream_delay_ms()));  public_submodules_->voice_detection->ProcessCaptureAudio(ca);  if (constants_.use_experimental_agc &&      public_submodules_->gain_control->is_enabled() &&      (!capture_nonlocked_.beamformer_enabled ||       private_submodules_->beamformer->is_target_present())) {    private_submodules_->agc_manager->Process(        ca->split_bands_const(0)[kBand0To8kHz], ca->num_frames_per_band(),        capture_nonlocked_.split_rate);  }  RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(      ca, echo_cancellation()->stream_has_echo()));  if (fwd_synthesis_needed()) {    ca->MergeFrequencyBands();  }  // TODO(aluebs): Investigate if the transient suppression placement should be  // before or after the AGC.  if (capture_.transient_suppressor_enabled) {    float voice_probability =        private_submodules_->agc_manager.get()            ? private_submodules_->agc_manager->voice_probability()            : 1.f;    public_submodules_->transient_suppressor->Suppress(        ca->channels_f()[0], ca->num_frames(), ca->num_channels(),        ca->split_bands_const_f(0)[kBand0To8kHz], ca->num_frames_per_band(),        ca->keyboard_data(), ca->num_keyboard_frames(), voice_probability,        capture_.key_pressed);  }  // The level estimator operates on the recombined data.  public_submodules_->level_estimator->ProcessStream(ca);  capture_.was_stream_delay_set = false;  return kNoError;}

其中包括七个步骤:1、分频;2、高通滤波;3、AEC;4、NS;5、AES;6、VAD;7、AGC;8、频段合并 9、音量调节。
可见nearend的处理全面,流程清晰。可以更具实际需要打开不同的模块,适应不同场景的需要,对于一般通讯系统来说具有正面的改善效果。但是在实际工作中也发现了一些流程上隐患。另外就是该结构的各个模块处理相对独立耦合低,本来应该是一个优良的特性,然而在复杂情况的信号处理难以到达目标效果。由于低耦合造成的运算量浪费更加是无法避免的。

2 0
原创粉丝点击