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

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 an assignment error Created 4 years, 5 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 forgetting_factor,
31 float* dc_level,
32 rtc::ArrayView<float> x) {
33 RTC_DCHECK(!x.empty());
34 float mean =
35 std::accumulate(x.begin(), x.end(), 0) / static_cast<float>(x.size());
36 *dc_level += forgetting_factor * (mean - *dc_level);
37
38 for (float& v : x) {
39 v -= *dc_level;
40 }
41 }
42
43 float FrameEnergy(const AudioBuffer& audio) {
44 float energy = 0.f;
45 for (size_t k = 0; k < audio.num_channels(); ++k) {
46 float channel_energy =
47 std::accumulate(audio.channels_const_f()[k],
48 audio.channels_const_f()[k] + audio.num_frames(), 0,
49 [](float a, float b) -> float { return a + b * b; });
50 energy = std::max(channel_energy, energy);
51 }
52 return energy;
53 }
54
55 float PeakLevel(const AudioBuffer& audio) {
56 float peak_level = 0.f;
57 for (size_t k = 0; k < audio.num_channels(); ++k) {
58 auto channel_peak_level = std::max_element(
59 audio.channels_const_f()[k],
60 audio.channels_const_f()[k] + audio.num_frames(),
61 [](float a, float b) { return std::abs(a) < std::abs(b); });
62 peak_level = std::max(*channel_peak_level, peak_level);
63 }
64 return peak_level;
65 }
66
67 } // namespace
68
69 int LevelController::instance_count_ = 0;
70
71 LevelController::LevelController()
72 : data_dumper_(new ApmDataDumper(instance_count_)),
73 gain_applier_(data_dumper_.get()),
74 signal_classifier_(data_dumper_.get()) {
75 Initialize(AudioProcessing::kSampleRate48kHz);
76 ++instance_count_;
77 }
78
79 LevelController::~LevelController() {}
80
81 void LevelController::Initialize(int sample_rate_hz) {
82 RTC_DCHECK(sample_rate_hz == AudioProcessing::kSampleRate8kHz ||
83 sample_rate_hz == AudioProcessing::kSampleRate16kHz ||
84 sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
85 sample_rate_hz == AudioProcessing::kSampleRate48kHz);
86 data_dumper_->InitiateNewSetOfRecordings();
87 gain_selector_.Initialize(sample_rate_hz);
88 gain_applier_.Initialize(sample_rate_hz);
89 signal_classifier_.Initialize(sample_rate_hz);
90 noise_level_estimator_.Initialize(sample_rate_hz);
91 peak_level_estimator_.Initialize();
92 saturating_gain_estimator_.Initialize();
93
94 sample_rate_hz_ = rtc::Optional<int>(sample_rate_hz);
95 dc_forgetting_factor_ = 0.01f * sample_rate_hz / 48000.f;
96 }
97
98 void LevelController::Process(AudioBuffer* audio) {
99 RTC_DCHECK_LT(0u, audio->num_channels());
100 RTC_DCHECK_GE(2u, audio->num_channels());
101 RTC_DCHECK_NE(0.f, dc_forgetting_factor_);
102 RTC_DCHECK(sample_rate_hz_);
103 data_dumper_->DumpWav("lc_input", audio->num_frames(),
104 audio->channels_const_f()[0], *sample_rate_hz_, 1);
105
106 // Remove DC level.
107 for (size_t k = 0; k < audio->num_channels(); ++k) {
108 UpdateAndRemoveDcLevel(
109 dc_forgetting_factor_, &dc_level_[k],
110 rtc::ArrayView<float>(audio->channels_f()[k], audio->num_frames()));
111 }
112
113 SignalClassifier::SignalType signal_type;
114 signal_classifier_.Analyze(*audio, &signal_type);
115 int tmp = static_cast<int>(signal_type);
116 data_dumper_->DumpRaw("lc_signal_type", 1, &tmp);
117
118 // Estimate the noise energy.
119 float noise_energy =
120 noise_level_estimator_.Analyze(signal_type, FrameEnergy(*audio));
121
122 // Estimate the overall signal peak level.
123 float peak_level =
124 peak_level_estimator_.Analyze(signal_type, PeakLevel(*audio));
125
126 float saturating_gain = saturating_gain_estimator_.GetGain();
127
128 // Compute the new gain to apply.
129 float new_gain = gain_selector_.GetNewGain(peak_level, noise_energy,
130 saturating_gain, signal_type);
131
132 // Apply the gain to the signal.
133 int num_saturations = gain_applier_.Process(new_gain, audio);
134
135 // Estimate the gain that saturates the overall signal.
136 saturating_gain_estimator_.Update(new_gain, num_saturations);
137
138 data_dumper_->DumpRaw("lc_selected_gain", 1, &new_gain);
139 data_dumper_->DumpRaw("lc_noise_energy", 1, &noise_energy);
140 data_dumper_->DumpRaw("lc_peak_level", 1, &peak_level);
141 data_dumper_->DumpRaw("lc_saturating_gain", 1, &saturating_gain);
142
143 data_dumper_->DumpWav("lc_output", audio->num_frames(),
144 audio->channels_f()[0], *sample_rate_hz_, 1);
145 }
146
147 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698