Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: webrtc/modules/audio_processing/level_controller/level_controller.cc

Issue 2090583002: New module for the adaptive level controlling functionality in the audio processing module (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Corrected the initial behavior for the peak level estimate, and ensured a nonzero minimum peak leveā€¦ Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "webrtc/modules/audio_processing/level_controller/level_controller.h"
12
13 #include <algorithm>
14 #include <numeric>
15
16 #include "webrtc/base/array_view.h"
17 #include "webrtc/base/checks.h"
18 #include "webrtc/modules/audio_processing/audio_buffer.h"
19 #include "webrtc/modules/audio_processing/level_controller/gain_applier.h"
20 #include "webrtc/modules/audio_processing/level_controller/gain_selector.h"
21 #include "webrtc/modules/audio_processing/level_controller/noise_level_estimator .h"
22 #include "webrtc/modules/audio_processing/level_controller/peak_level_estimator. h"
23 #include "webrtc/modules/audio_processing/level_controller/saturating_gain_estim ator.h"
24 #include "webrtc/modules/audio_processing/level_controller/signal_classifier.h"
25 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h"
26
27 namespace webrtc {
28 namespace {
29
30 void UpdateAndRemoveDcLevel(float* dc_level, rtc::ArrayView<float> x) {
31 RTC_DCHECK_LT(0u, x.size());
hlundin-webrtc 2016/06/27 11:21:15 I find it easier to read RTC_DCHECK(!x.empty());
peah-webrtc 2016/06/27 22:51:48 Done.
32 float mean = 0;
33 for (float v : x) {
34 mean += v;
35 }
36 mean /= x.size();
37 *dc_level += 0.01f * (mean - *dc_level);
hlundin-webrtc 2016/06/27 11:21:15 Is the size of x always constant in milliseconds?
peah-webrtc 2016/06/27 22:51:48 Good point! Done.
38
39 for (float& v : x) {
40 v -= *dc_level;
41 }
42 }
43
44 float FrameEnergy(const AudioBuffer& audio) {
45 auto sample_power = [](float a, float b) { return a + b * b; };
hlundin-webrtc 2016/06/27 11:21:15 Nit: "Specify the return type of the lambda explic
peah-webrtc 2016/06/27 22:51:48 Good point! Done.
46
47 float energy = 0.f;
48 for (size_t k = 0; k < audio.num_channels(); ++k) {
49 float channel_energy = 0;
hlundin-webrtc 2016/06/27 11:21:15 No need to initialize this to zero. Just add float
peah-webrtc 2016/06/27 22:51:48 Done.
50 channel_energy = std::accumulate(
51 audio.channels_const_f()[k],
52 audio.channels_const_f()[k] + audio.num_frames(), 0, sample_power);
53 energy = std::max(channel_energy, energy);
54 }
55 return energy;
56 }
57
58 float PeakLevel(const AudioBuffer& audio) {
59 float peak_level = 0.f;
60 auto compare_abs = [](float a, float b) { return std::abs(a) < std::abs(b); };
hlundin-webrtc 2016/06/27 11:21:15 Move this lambda to in-line below.
peah-webrtc 2016/06/27 22:51:48 Done.
61
62 for (size_t k = 0; k < audio.num_channels(); ++k) {
63 auto channel_peak_level = std::max_element(
64 audio.channels_const_f()[k],
65 audio.channels_const_f()[k] + audio.num_frames(), compare_abs);
66 peak_level = std::max(*channel_peak_level, peak_level);
67 }
68 return peak_level;
69 }
70
71 } // namespace
72
73 int LevelController::instance_count_ = 0;
74
75 LevelController::LevelController() {
76 ++instance_count_;
77 data_dumper_.reset(new ApmDataDumper(instance_count_));
78 gain_selector_.reset(new GainSelector());
79 gain_applier_.reset(new GainApplier(data_dumper_.get()));
80 signal_classifier_.reset(new SignalClassifier(data_dumper_.get()));
81 noise_level_estimator_.reset(new NoiseLevelEstimator());
82 peak_level_estimator_.reset(new PeakLevelEstimator());
83 saturating_gain_estimator_.reset(new SaturatingGainEstimator());
84 Initialize(AudioProcessing::kSampleRate48kHz, 1u);
85 }
86
87 // TODO(peah): See if the destructors can be removed.
88 LevelController::~LevelController() {}
89
90 void LevelController::Initialize(int sample_rate_hz, size_t num_channels_) {
91 data_dumper_->InitiateNewSetOfRecordings();
92 gain_selector_->Initialize(sample_rate_hz);
93 gain_applier_->Initialize(sample_rate_hz);
94 signal_classifier_->Initialize(sample_rate_hz);
95 noise_level_estimator_->Initialize(sample_rate_hz);
96 peak_level_estimator_->Initialize();
97 saturating_gain_estimator_->Initialize();
98
99 sample_rate_hz_ = sample_rate_hz;
100 }
101
102 void LevelController::Process(AudioBuffer* audio) {
103 RTC_DCHECK_LT(0u, audio->num_channels());
104 RTC_DCHECK_GE(2u, audio->num_channels());
105 data_dumper_->DumpWav("lc_input", audio->num_frames(),
106 audio->channels_const_f()[0], sample_rate_hz_, 1);
107
108 // Remove DC level.
109 for (size_t k = 0; k < audio->num_channels(); ++k) {
110 UpdateAndRemoveDcLevel(
111 &dc_level_[k],
112 rtc::ArrayView<float>(audio->channels_f()[k], audio->num_frames()));
113 }
114
115 SignalClassifier::SignalType signal_type;
116 signal_classifier_->Analyze(*audio, &signal_type);
117 int tmp = static_cast<int>(signal_type);
118 data_dumper_->DumpRaw("lc_signal_type", 1, &tmp);
119
120 // Estimate the noise energy.
121 float frame_energy = FrameEnergy(*audio);
122 float noise_energy =
123 noise_level_estimator_->Analyze(signal_type, frame_energy);
hlundin-webrtc 2016/06/27 11:21:15 You can just as well call FrameEnergy(*audio) dire
peah-webrtc 2016/06/27 22:51:48 Done.
124
125 // Estimate the overall signal peak level.
126 float frame_peak_level = PeakLevel(*audio);
127 float peak_level =
128 peak_level_estimator_->Analyze(signal_type, frame_peak_level);
hlundin-webrtc 2016/06/27 11:21:15 Same here. Call PeakLevel(*audio) directly as a pa
peah-webrtc 2016/06/27 22:51:48 Done.
129
130 float saturating_gain = saturating_gain_estimator_->GetGain();
131
132 // Compute the new gain to apply.
133 float new_gain =
134 gain_selector_->GetNewGain(peak_level, noise_energy, saturating_gain);
135
136 // Apply the gain to the signal.
137 int num_saturations = gain_applier_->Process(new_gain, audio);
138
139 // Estimate the gain that saturates the overall signal.
140 saturating_gain_estimator_->Update(new_gain, num_saturations);
141
142 data_dumper_->DumpRaw("lc_selected_gain", 1, &new_gain);
143 data_dumper_->DumpRaw("lc_noise_energy", 1, &noise_energy);
144 data_dumper_->DumpRaw("lc_peak_level", 1, &peak_level);
145 data_dumper_->DumpRaw("lc_saturating_gain", 1, &saturating_gain);
146
147 data_dumper_->DumpWav("lc_output", audio->num_frames(),
148 audio->channels_f()[0], sample_rate_hz_, 1);
149 }
150
151 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698