mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-13 13:50:40 +01:00

This CL adds functionality that allows adjusting the audio levels internally in APM. The main purpose of the functionality is to allow APM to optionally be moved to an integration that does not provide an analog gain to control, and the implementation of this has been tailored specifically to meet the requirements for that. More specifically, this CL does -Add a new variant of the pre-amplifier gain that is intended to replace the pre-amplifier gain (but at the moment can coexist with that). The main differences with the pre-amplifier gain is that an attenuating gain is allowed, the gain is applied jointly with any emulated analog gain, and that its packaging fits better with the post gain. -Add an emulation of an analog microphone gain. The emulation is designed to match the analog mic gain functionality in Chrome OS (which is digital) but should be usable also on other platforms. -Add a post-gain which is applied after all processing has been applied. The purpose of this gain is for it to work well with the integration in ChromeOS, and be used to compensate for the offset that there is applied on some USB audio devices. Bug: b/177830918 Change-Id: I0f312996e4088c9bd242a713a703eaaeb17f188a Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/209707 Commit-Queue: Per Åhgren <peah@webrtc.org> Reviewed-by: Gustaf Ullberg <gustaf@webrtc.org> Reviewed-by: Alessio Bazzica <alessiob@webrtc.org> Cr-Commit-Position: refs/heads/master@{#33466}
96 lines
3.3 KiB
C++
96 lines
3.3 KiB
C++
/*
|
|
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
#include "modules/audio_processing/capture_levels_adjuster/capture_levels_adjuster.h"
|
|
|
|
#include "modules/audio_processing/audio_buffer.h"
|
|
#include "rtc_base/checks.h"
|
|
#include "rtc_base/numerics/safe_minmax.h"
|
|
|
|
namespace webrtc {
|
|
|
|
namespace {
|
|
|
|
constexpr int kMinAnalogMicGainLevel = 0;
|
|
constexpr int kMaxAnalogMicGainLevel = 255;
|
|
|
|
float ComputeLevelBasedGain(int emulated_analog_mic_gain_level) {
|
|
static_assert(
|
|
kMinAnalogMicGainLevel == 0,
|
|
"The minimum gain level must be 0 for the maths below to work.");
|
|
static_assert(kMaxAnalogMicGainLevel > 0,
|
|
"The minimum gain level must be larger than 0 for the maths "
|
|
"below to work.");
|
|
constexpr float kGainToLevelMultiplier = 1.f / kMaxAnalogMicGainLevel;
|
|
|
|
RTC_DCHECK_GE(emulated_analog_mic_gain_level, kMinAnalogMicGainLevel);
|
|
RTC_DCHECK_LE(emulated_analog_mic_gain_level, kMaxAnalogMicGainLevel);
|
|
return kGainToLevelMultiplier * emulated_analog_mic_gain_level;
|
|
}
|
|
|
|
float ComputePreGain(float pre_gain,
|
|
int emulated_analog_mic_gain_level,
|
|
bool emulated_analog_mic_gain_enabled) {
|
|
return emulated_analog_mic_gain_enabled
|
|
? pre_gain * ComputeLevelBasedGain(emulated_analog_mic_gain_level)
|
|
: pre_gain;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
CaptureLevelsAdjuster::CaptureLevelsAdjuster(
|
|
bool emulated_analog_mic_gain_enabled,
|
|
int emulated_analog_mic_gain_level,
|
|
float pre_gain,
|
|
float post_gain)
|
|
: emulated_analog_mic_gain_enabled_(emulated_analog_mic_gain_enabled),
|
|
emulated_analog_mic_gain_level_(emulated_analog_mic_gain_level),
|
|
pre_gain_(pre_gain),
|
|
pre_adjustment_gain_(ComputePreGain(pre_gain_,
|
|
emulated_analog_mic_gain_level_,
|
|
emulated_analog_mic_gain_enabled_)),
|
|
pre_scaler_(pre_adjustment_gain_),
|
|
post_scaler_(post_gain) {}
|
|
|
|
void CaptureLevelsAdjuster::ApplyPreLevelAdjustment(AudioBuffer& audio_buffer) {
|
|
pre_scaler_.Process(audio_buffer);
|
|
}
|
|
|
|
void CaptureLevelsAdjuster::ApplyPostLevelAdjustment(
|
|
AudioBuffer& audio_buffer) {
|
|
post_scaler_.Process(audio_buffer);
|
|
}
|
|
|
|
void CaptureLevelsAdjuster::SetPreGain(float pre_gain) {
|
|
pre_gain_ = pre_gain;
|
|
UpdatePreAdjustmentGain();
|
|
}
|
|
|
|
void CaptureLevelsAdjuster::SetPostGain(float post_gain) {
|
|
post_scaler_.SetGain(post_gain);
|
|
}
|
|
|
|
void CaptureLevelsAdjuster::SetAnalogMicGainLevel(int level) {
|
|
RTC_DCHECK_GE(level, kMinAnalogMicGainLevel);
|
|
RTC_DCHECK_LE(level, kMaxAnalogMicGainLevel);
|
|
int clamped_level =
|
|
rtc::SafeClamp(level, kMinAnalogMicGainLevel, kMaxAnalogMicGainLevel);
|
|
|
|
emulated_analog_mic_gain_level_ = clamped_level;
|
|
UpdatePreAdjustmentGain();
|
|
}
|
|
|
|
void CaptureLevelsAdjuster::UpdatePreAdjustmentGain() {
|
|
pre_adjustment_gain_ =
|
|
ComputePreGain(pre_gain_, emulated_analog_mic_gain_level_,
|
|
emulated_analog_mic_gain_enabled_);
|
|
pre_scaler_.SetGain(pre_adjustment_gain_);
|
|
}
|
|
|
|
} // namespace webrtc
|