1. AGC 初識(shí)
自動(dòng)增益控制電路的作用是:當(dāng)輸入信號(hào)電壓變化很大時(shí),保持接收機(jī)輸出電壓恒定或基本不變。具體地說(shuō),當(dāng)輸入信號(hào)很弱時(shí),接收機(jī)的增益大,自動(dòng)增益控制電路不起作用;當(dāng)輸入信號(hào)很強(qiáng)時(shí),自動(dòng)增益控制電路進(jìn)行控制,使接收機(jī)的增益減小。這樣,當(dāng)接收信號(hào)強(qiáng)度變化時(shí),接收機(jī)的輸出端的電壓或功率基本不變或保持恒定。因此對(duì)AGC電路的要求是:在輸入信號(hào)較小時(shí),AGC電路不起作用,只有當(dāng)輸入信號(hào)增大到一定程度后,AGC電路才起控制作用,使增益隨輸入信號(hào)的增大而減少。
為實(shí)現(xiàn)上述要求,必須有一個(gè)能隨外來(lái)信號(hào)強(qiáng)弱而變化的控制電壓或電流信號(hào),利用這個(gè)信號(hào)對(duì)放大器的增益自動(dòng)進(jìn)行控制。由上述分析可知,調(diào)幅中頻信號(hào)經(jīng)幅度檢波后,在它的輸出中除音頻信號(hào)外,還含有直流分量。直流分量大小與中頻載波的振幅成正比,也即與外來(lái)高頻信號(hào)成正比。因此,可將檢波器輸出的直流分量作為AGC控制信號(hào)。
AGC電路工作原理:可以分為增益受控放大電路和控制電壓形成電路。增益受控放大電路位于正向放大通路,其增益隨控制電壓U0而改變??刂齐妷盒纬呻娐返幕静考茿GC整流器和低通平滑濾波器,有時(shí)也包含門電路和直流放大器等部件。

2. webrtc 的 AGC算法
AGC是自動(dòng)增益補(bǔ)償功能(Automatic Gain Control),AGC可以自動(dòng)調(diào)麥克風(fēng)的收音量,使與會(huì)者收到一定的音量水平,不會(huì)因發(fā)言者與麥克風(fēng)的距離改變時(shí),聲音有忽大忽小聲的缺點(diǎn)。
webbrtc中的結(jié)構(gòu)如下:


說(shuō)明:gain_control.h是包裝的頭文件,在apm里頭gain_control_impl調(diào)用。主要包括了接口定義函數(shù)和參數(shù)配置。
3. 主要配置
include/gain_control.h 里面定義了agc的三種模式,kAdaptiveAnalog、kAdaptiveDigital和kFixedDigital。其中,kAdaptiveAnalog帶有模擬音量調(diào)節(jié)的功能。kAdaptiveDigital是可變?cè)鲆鎍gc,但是不調(diào)節(jié)系統(tǒng)音量。kFixedDigital是固定增益的agc。
enum Mode {
// Adaptive mode intended for use if an analog volume control is available
// on the capture device. It will require the user to provide coupling
// between the OS mixer controls and AGC through the |stream_analog_level()|
// functions.
//
// It consists of an analog gain prescription for the audio device and a
// digital compression stage.
kAdaptiveAnalog,
// Adaptive mode intended for situations in which an analog volume control
// is unavailable. It operates in a similar fashion to the adaptive analog
// mode, but with scaling instead applied in the digital domain. As with
// the analog mode, it additionally uses a digital compression stage.
kAdaptiveDigital,
// Fixed mode which enables only the digital compression stage also used by
// the two adaptive modes.
//
// It is distinguished from the adaptive modes by considering only a
// short time-window of the input signal. It applies a fixed gain through
// most of the input level range, and compresses (gradually reduces gain
// with increasing level) the input signal at higher levels. This mode is
// preferred on embedded devices where the capture signal level is
// predictable, so that a known gain can be applied.
kFixedDigital
};
analog_agc.h 定義了配置targetLevelDbfs和compressionGaindB用于調(diào)節(jié)agc的動(dòng)態(tài)范圍
typedef struct {
// Configurable parameters/variables
uint32_t fs; // Sampling frequency
int16_t compressionGaindB; // Fixed gain level in dB
int16_t targetLevelDbfs; // Target level in -dBfs of envelope (default -3)
int16_t agcMode; // Hard coded mode (adaptAna/adaptDig/fixedDig)
uint8_t limiterEnable; // Enabling limiter (on/off (default off))
WebRtcAgcConfig defaultConfig;
WebRtcAgcConfig usedConfig;
// General variables
int16_t initFlag;
int16_t lastError;
// Target level parameters
// Based on the above: analogTargetLevel = round((32767*10^(-22/20))^2*16/2^7)
int32_t analogTargetLevel; // = RXX_BUFFER_LEN * 846805; -22 dBfs
int32_t startUpperLimit; // = RXX_BUFFER_LEN * 1066064; -21 dBfs
int32_t startLowerLimit; // = RXX_BUFFER_LEN * 672641; -23 dBfs
int32_t upperPrimaryLimit; // = RXX_BUFFER_LEN * 1342095; -20 dBfs
int32_t lowerPrimaryLimit; // = RXX_BUFFER_LEN * 534298; -24 dBfs
int32_t upperSecondaryLimit; // = RXX_BUFFER_LEN * 2677832; -17 dBfs
int32_t lowerSecondaryLimit; // = RXX_BUFFER_LEN * 267783; -27 dBfs
uint16_t targetIdx; // Table index for corresponding target level
#ifdef MIC_LEVEL_FEEDBACK
uint16_t targetIdxOffset; // Table index offset for level compensation
#endif
int16_t analogTarget; // Digital reference level in ENV scale
// Analog AGC specific variables
int32_t filterState[8]; // For downsampling wb to nb
int32_t upperLimit; // Upper limit for mic energy
int32_t lowerLimit; // Lower limit for mic energy
int32_t Rxx160w32; // Average energy for one frame
int32_t Rxx16_LPw32; // Low pass filtered subframe energies
int32_t Rxx160_LPw32; // Low pass filtered frame energies
int32_t Rxx16_LPw32Max; // Keeps track of largest energy subframe
int32_t Rxx16_vectorw32[RXX_BUFFER_LEN]; // Array with subframe energies
int32_t Rxx16w32_array[2][5]; // Energy values of microphone signal
int32_t env[2][10]; // Envelope values of subframes
int16_t Rxx16pos; // Current position in the Rxx16_vectorw32
int16_t envSum; // Filtered scaled envelope in subframes
int16_t vadThreshold; // Threshold for VAD decision
int16_t inActive; // Inactive time in milliseconds
int16_t msTooLow; // Milliseconds of speech at a too low level
int16_t msTooHigh; // Milliseconds of speech at a too high level
int16_t changeToSlowMode; // Change to slow mode after some time at target
int16_t firstCall; // First call to the process-function
int16_t msZero; // Milliseconds of zero input
int16_t msecSpeechOuterChange; // Min ms of speech between volume changes
int16_t msecSpeechInnerChange; // Min ms of speech between volume changes
int16_t activeSpeech; // Milliseconds of active speech
int16_t muteGuardMs; // Counter to prevent mute action
int16_t inQueue; // 10 ms batch indicator
// Microphone level variables
int32_t micRef; // Remember ref. mic level for virtual mic
uint16_t gainTableIdx; // Current position in virtual gain table
int32_t micGainIdx; // Gain index of mic level to increase slowly
int32_t micVol; // Remember volume between frames
int32_t maxLevel; // Max possible vol level, incl dig gain
int32_t maxAnalog; // Maximum possible analog volume level
int32_t maxInit; // Initial value of "max"
int32_t minLevel; // Minimum possible volume level
int32_t minOutput; // Minimum output volume level
int32_t zeroCtrlMax; // Remember max gain => don't amp low input
int32_t lastInMicLevel;
int16_t scale; // Scale factor for internal volume levels
#ifdef MIC_LEVEL_FEEDBACK
int16_t numBlocksMicLvlSat;
uint8_t micLvlSat;
#endif
// Structs for VAD and digital_agc
AgcVad vadMic;
DigitalAgc digitalAgc;
#ifdef WEBRTC_AGC_DEBUG_DUMP
FILE* fpt;
FILE* agcLog;
int32_t fcount;
#endif
int16_t lowLevelSignal;
} LegacyAgc;
相關(guān)源代碼如下:
int GainControlImpl::AnalyzeCaptureAudio(AudioBuffer* audio) {
if (!enabled_) {
return AudioProcessing::kNoError;
}
RTC_DCHECK(num_proc_channels_);
RTC_DCHECK_GE(AudioBuffer::kMaxSplitFrameLength,
audio->num_frames_per_band());
RTC_DCHECK_EQ(audio->num_channels(), *num_proc_channels_);
RTC_DCHECK_LE(*num_proc_channels_, gain_controllers_.size());
int16_t split_band_data[AudioBuffer::kMaxNumBands]
[AudioBuffer::kMaxSplitFrameLength];
int16_t* split_bands[AudioBuffer::kMaxNumBands] = {
split_band_data[0], split_band_data[1], split_band_data[2]};
if (mode_ == kAdaptiveAnalog) {
int capture_channel = 0;
for (auto& gain_controller : gain_controllers_) {
gain_controller->set_capture_level(analog_capture_level_);
audio->ExportSplitChannelData(capture_channel, split_bands);
int err =
WebRtcAgc_AddMic(gain_controller->state(), split_bands,
audio->num_bands(), audio->num_frames_per_band());
audio->ImportSplitChannelData(capture_channel, split_bands);
if (err != AudioProcessing::kNoError) {
return AudioProcessing::kUnspecifiedError;
}
++capture_channel;
}
} else if (mode_ == kAdaptiveDigital) {
int capture_channel = 0;
for (auto& gain_controller : gain_controllers_) {
int32_t capture_level_out = 0;
audio->ExportSplitChannelData(capture_channel, split_bands);
int err =
WebRtcAgc_VirtualMic(gain_controller->state(), split_bands,
audio->num_bands(), audio->num_frames_per_band(),
analog_capture_level_, &capture_level_out);
audio->ImportSplitChannelData(capture_channel, split_bands);
gain_controller->set_capture_level(capture_level_out);
if (err != AudioProcessing::kNoError) {
return AudioProcessing::kUnspecifiedError;
}
++capture_channel;
}
}
return AudioProcessing::kNoError;
}
4. 主要接口
analog_agc.h包括模擬的agc結(jié)構(gòu)體聲明,而gain_control.h中的接口函數(shù)在analog_agc.c中實(shí)現(xiàn)。
WebRtcAgc_AddFarend 計(jì)算遠(yuǎn)端信號(hào)的語(yǔ)音活度VAD
WebRtcAgc_AddMic 計(jì)算麥克風(fēng)輸入的語(yǔ)音活度,對(duì)于非常小的信號(hào)會(huì)乘增益系數(shù)
WebRtcAgc_VirtualMic 用虛擬的麥克風(fēng)音量來(lái)調(diào)節(jié)幅度
WebRtcAgc_Process vad核心處理
WebRtcAgc_set_config 設(shè)置VAD參數(shù)
在analog_agc.c還包括以下函數(shù):
WebRtcAgc_UpdateAgcThresholds
WebRtcAgc_SaturationCtrl
WebRtcAgc_ZeroCtrl
WebRtcAgc_SpeakerInactiveCtrl
WebRtcAgc_ExpCurve
WebRtcAgc_ProcessAnalog
digital_agc.h包括數(shù)字的agc結(jié)構(gòu)體聲明,Vad結(jié)構(gòu)聲明,而gain_control.h中的接口函數(shù)在analog_agc.c中實(shí)現(xiàn)。
WebRtcAgc_ProcessDigital
WebRtcAgc_AddFarendToDigital
WebRtcAgc_InitVad
WebRtcAgc_ProcessVad
WebRtcAgc_CalculateGainTable
參考文章:
Webrtc AGC 算法(二)