mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-13 05:40:42 +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}
92 lines
3.3 KiB
C++
92 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/audio_samples_scaler.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include "api/array_view.h"
|
|
#include "modules/audio_processing/audio_buffer.h"
|
|
#include "rtc_base/checks.h"
|
|
#include "rtc_base/numerics/safe_minmax.h"
|
|
|
|
namespace webrtc {
|
|
|
|
AudioSamplesScaler::AudioSamplesScaler(float initial_gain)
|
|
: previous_gain_(initial_gain), target_gain_(initial_gain) {}
|
|
|
|
void AudioSamplesScaler::Process(AudioBuffer& audio_buffer) {
|
|
if (static_cast<int>(audio_buffer.num_frames()) != samples_per_channel_) {
|
|
// Update the members depending on audio-buffer length if needed.
|
|
RTC_DCHECK_GT(audio_buffer.num_frames(), 0);
|
|
samples_per_channel_ = static_cast<int>(audio_buffer.num_frames());
|
|
one_by_samples_per_channel_ = 1.f / samples_per_channel_;
|
|
}
|
|
|
|
if (target_gain_ == 1.f && previous_gain_ == target_gain_) {
|
|
// If only a gain of 1 is to be applied, do an early return without applying
|
|
// any gain.
|
|
return;
|
|
}
|
|
|
|
float gain = previous_gain_;
|
|
if (previous_gain_ == target_gain_) {
|
|
// Apply a non-changing gain.
|
|
for (size_t channel = 0; channel < audio_buffer.num_channels(); ++channel) {
|
|
rtc::ArrayView<float> channel_view(audio_buffer.channels()[channel],
|
|
samples_per_channel_);
|
|
for (float& sample : channel_view) {
|
|
sample *= gain;
|
|
}
|
|
}
|
|
} else {
|
|
const float increment =
|
|
(target_gain_ - previous_gain_) * one_by_samples_per_channel_;
|
|
|
|
if (increment > 0.f) {
|
|
// Apply an increasing gain.
|
|
for (size_t channel = 0; channel < audio_buffer.num_channels();
|
|
++channel) {
|
|
gain = previous_gain_;
|
|
rtc::ArrayView<float> channel_view(audio_buffer.channels()[channel],
|
|
samples_per_channel_);
|
|
for (float& sample : channel_view) {
|
|
gain = std::min(gain + increment, target_gain_);
|
|
sample *= gain;
|
|
}
|
|
}
|
|
} else {
|
|
// Apply a decreasing gain.
|
|
for (size_t channel = 0; channel < audio_buffer.num_channels();
|
|
++channel) {
|
|
gain = previous_gain_;
|
|
rtc::ArrayView<float> channel_view(audio_buffer.channels()[channel],
|
|
samples_per_channel_);
|
|
for (float& sample : channel_view) {
|
|
gain = std::max(gain + increment, target_gain_);
|
|
sample *= gain;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
previous_gain_ = target_gain_;
|
|
|
|
// Saturate the samples to be in the S16 range.
|
|
for (size_t channel = 0; channel < audio_buffer.num_channels(); ++channel) {
|
|
rtc::ArrayView<float> channel_view(audio_buffer.channels()[channel],
|
|
samples_per_channel_);
|
|
for (float& sample : channel_view) {
|
|
constexpr float kMinFloatS16Value = -32768.f;
|
|
constexpr float kMaxFloatS16Value = 32767.f;
|
|
sample = rtc::SafeClamp(sample, kMinFloatS16Value, kMaxFloatS16Value);
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace webrtc
|