Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/audio_processing/level_controller/level_controller.h" | 11 #include "webrtc/modules/audio_processing/level_controller/level_controller.h" |
| 12 | 12 |
| 13 #include <math.h> | 13 #include <math.h> |
| 14 #include <algorithm> | 14 #include <algorithm> |
| 15 #include <numeric> | 15 #include <numeric> |
| 16 | 16 |
| 17 #include "webrtc/base/array_view.h" | 17 #include "webrtc/base/array_view.h" |
| 18 #include "webrtc/base/arraysize.h" | 18 #include "webrtc/base/arraysize.h" |
| 19 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
| 20 #include "webrtc/modules/audio_processing/audio_buffer.h" | 20 #include "webrtc/modules/audio_processing/audio_buffer.h" |
| 21 #include "webrtc/modules/audio_processing/level_controller/gain_applier.h" | 21 #include "webrtc/modules/audio_processing/level_controller/gain_applier.h" |
| 22 #include "webrtc/modules/audio_processing/level_controller/gain_selector.h" | 22 #include "webrtc/modules/audio_processing/level_controller/gain_selector.h" |
| 23 #include "webrtc/modules/audio_processing/level_controller/noise_level_estimator .h" | 23 #include "webrtc/modules/audio_processing/level_controller/noise_level_estimator .h" |
| 24 #include "webrtc/modules/audio_processing/level_controller/peak_level_estimator. h" | 24 #include "webrtc/modules/audio_processing/level_controller/peak_level_estimator. h" |
| 25 #include "webrtc/modules/audio_processing/level_controller/saturating_gain_estim ator.h" | 25 #include "webrtc/modules/audio_processing/level_controller/saturating_gain_estim ator.h" |
| 26 #include "webrtc/modules/audio_processing/level_controller/signal_classifier.h" | 26 #include "webrtc/modules/audio_processing/level_controller/signal_classifier.h" |
| 27 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" | 27 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" |
| 28 #include "webrtc/system_wrappers/include/logging.h" | |
| 28 #include "webrtc/system_wrappers/include/metrics.h" | 29 #include "webrtc/system_wrappers/include/metrics.h" |
| 29 | 30 |
| 30 namespace webrtc { | 31 namespace webrtc { |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 void UpdateAndRemoveDcLevel(float forgetting_factor, | 34 void UpdateAndRemoveDcLevel(float forgetting_factor, |
| 34 float* dc_level, | 35 float* dc_level, |
| 35 rtc::ArrayView<float> x) { | 36 rtc::ArrayView<float> x) { |
| 36 RTC_DCHECK(!x.empty()); | 37 RTC_DCHECK(!x.empty()); |
| 37 float mean = | 38 float mean = |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 void LevelController::Metrics::Reset() { | 87 void LevelController::Metrics::Reset() { |
| 87 metrics_frame_counter_ = 0; | 88 metrics_frame_counter_ = 0; |
| 88 gain_sum_ = 0.f; | 89 gain_sum_ = 0.f; |
| 89 peak_level_sum_ = 0.f; | 90 peak_level_sum_ = 0.f; |
| 90 noise_energy_sum_ = 0.f; | 91 noise_energy_sum_ = 0.f; |
| 91 max_gain_ = 0.f; | 92 max_gain_ = 0.f; |
| 92 max_peak_level_ = 0.f; | 93 max_peak_level_ = 0.f; |
| 93 max_noise_energy_ = 0.f; | 94 max_noise_energy_ = 0.f; |
| 94 } | 95 } |
| 95 | 96 |
| 96 void LevelController::Metrics::Update(float peak_level, | 97 void LevelController::Metrics::Update(float long_term_peak_level, |
| 97 float noise_energy, | 98 float noise_energy, |
| 98 float gain) { | 99 float gain, |
| 100 float frame_peak_level) { | |
| 99 const float kdBFSOffset = 90.3090f; | 101 const float kdBFSOffset = 90.3090f; |
| 100 gain_sum_ += gain; | 102 gain_sum_ += gain; |
| 101 peak_level_sum_ += peak_level; | 103 peak_level_sum_ += long_term_peak_level; |
| 102 noise_energy_sum_ += noise_energy; | 104 noise_energy_sum_ += noise_energy; |
| 103 max_gain_ = std::max(max_gain_, gain); | 105 max_gain_ = std::max(max_gain_, gain); |
| 104 max_peak_level_ = std::max(max_peak_level_, peak_level); | 106 max_peak_level_ = std::max(max_peak_level_, long_term_peak_level); |
| 105 max_noise_energy_ = std::max(max_noise_energy_, noise_energy); | 107 max_noise_energy_ = std::max(max_noise_energy_, noise_energy); |
| 106 | 108 |
| 107 ++metrics_frame_counter_; | 109 ++metrics_frame_counter_; |
| 108 if (metrics_frame_counter_ == kMetricsFrameInterval) { | 110 if (metrics_frame_counter_ == kMetricsFrameInterval) { |
| 109 RTC_HISTOGRAM_COUNTS( | 111 int max_noise_power_dbfs = static_cast<int>( |
|
hlundin-webrtc
2016/08/26 07:53:28
I would argue that you should make all the local v
peah-webrtc
2016/08/26 08:33:04
Done.
| |
| 110 "WebRTC.Audio.LevelControl.MaxNoisePower", | 112 10 * log10(max_noise_energy_ / frame_length_ + 1e-10f) - kdBFSOffset); |
|
hlundin-webrtc
2016/08/26 07:53:28
You are dividing by frame_length_ in a few places.
peah-webrtc
2016/08/26 08:33:04
Done.
| |
| 111 static_cast<int>(10 * log10(max_noise_energy_ / frame_length_ + 1e-10f) | 113 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.LevelControl.MaxNoisePower", |
| 112 - kdBFSOffset), | 114 max_noise_power_dbfs, -90, 0, 50); |
| 113 -90, 0, 50); | |
| 114 RTC_HISTOGRAM_COUNTS( | |
| 115 "WebRTC.Audio.LevelControl.AverageNoisePower", | |
| 116 static_cast<int>(10 * log10(noise_energy_sum_ / | |
| 117 (frame_length_ * kMetricsFrameInterval) + | |
| 118 1e-10f) - kdBFSOffset), | |
| 119 -90, 0, 50); | |
| 120 | 115 |
| 121 RTC_HISTOGRAM_COUNTS( | 116 int average_noise_power_dbfs = static_cast<int>( |
| 122 "WebRTC.Audio.LevelControl.MaxPeakLevel", | 117 10 * log10(noise_energy_sum_ / (frame_length_ * kMetricsFrameInterval) + |
| 123 static_cast<int>(10 * log10(max_peak_level_ * max_peak_level_ + 1e-10f) | 118 1e-10f) - |
| 124 - kdBFSOffset), | 119 kdBFSOffset); |
| 125 -90, 0, 50); | 120 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.LevelControl.AverageNoisePower", |
| 126 RTC_HISTOGRAM_COUNTS( | 121 average_noise_power_dbfs, -90, 0, 50); |
| 127 "WebRTC.Audio.LevelControl.AveragePeakLevel", | 122 |
| 128 static_cast<int>(10 * log10(peak_level_sum_ * peak_level_sum_ / | 123 int max_peak_level_dbfs = static_cast<int>( |
| 129 (kMetricsFrameInterval * | 124 10 * log10(max_peak_level_ * max_peak_level_ + 1e-10f) - kdBFSOffset); |
| 130 kMetricsFrameInterval) + | 125 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.LevelControl.MaxPeakLevel", |
| 131 1e-10f) - kdBFSOffset), | 126 max_peak_level_dbfs, -90, 0, 50); |
| 132 -90, 0, 50); | 127 |
| 128 int average_peak_level_dbfs = static_cast<int>( | |
| 129 10 * log10(peak_level_sum_ * peak_level_sum_ / | |
| 130 (kMetricsFrameInterval * kMetricsFrameInterval) + | |
| 131 1e-10f) - | |
| 132 kdBFSOffset); | |
| 133 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.LevelControl.AveragePeakLevel", | |
| 134 average_peak_level_dbfs, -90, 0, 50); | |
| 133 | 135 |
| 134 RTC_DCHECK_LE(1.f, max_gain_); | 136 RTC_DCHECK_LE(1.f, max_gain_); |
| 135 RTC_DCHECK_LE(1.f, gain_sum_ / kMetricsFrameInterval); | 137 RTC_DCHECK_LE(1.f, gain_sum_ / kMetricsFrameInterval); |
| 136 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.LevelControl.MaxGain", | 138 |
| 137 static_cast<int>(10 * log10(max_gain_ * max_gain_)), | 139 int max_gain_db = static_cast<int>(10 * log10(max_gain_ * max_gain_)); |
| 138 0, 33, 30); | 140 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.LevelControl.MaxGain", max_gain_db, 0, |
| 141 33, 30); | |
| 142 | |
| 143 int average_gain_db = static_cast<int>( | |
| 144 10 * log10(gain_sum_ * gain_sum_ / | |
| 145 (kMetricsFrameInterval * kMetricsFrameInterval))); | |
| 139 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.LevelControl.AverageGain", | 146 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.LevelControl.AverageGain", |
| 140 static_cast<int>(10 * log10(gain_sum_ * gain_sum_ / | 147 average_gain_db, 0, 33, 30); |
| 141 (kMetricsFrameInterval * | 148 |
| 142 kMetricsFrameInterval))), | 149 int long_term_peak_level_dbfs = static_cast<int>( |
| 143 0, 33, 30); | 150 10 * log10(long_term_peak_level * long_term_peak_level + 1e-10f) - |
| 151 kdBFSOffset); | |
| 152 | |
| 153 int frame_peak_level_dbfs = static_cast<int>( | |
| 154 10 * log10(frame_peak_level * frame_peak_level + 1e-10f) - kdBFSOffset); | |
| 155 | |
| 156 LOG(LS_INFO) << "Level Controller metrics: " << std::endl | |
| 157 << "Max noise power: " << max_noise_power_dbfs | |
| 158 << " dBFS" << std::endl | |
| 159 << "Average noise power: " << average_noise_power_dbfs | |
| 160 << " dBFS" << std::endl | |
| 161 << "Max long term peak level: " << max_peak_level_dbfs | |
| 162 << " dBFS" << std::endl | |
| 163 << "Average long term peak level: " << average_peak_level_dbfs | |
| 164 << " dBFS" << std::endl | |
| 165 << "Max gain: " << max_gain_db << " dB" | |
| 166 << std::endl | |
| 167 << "Average gain: " << average_gain_db << " dB" | |
| 168 << std::endl | |
| 169 << "Last term peak level: " | |
|
hlundin-webrtc
2016/08/26 07:53:29
Last -> Long
peah-webrtc
2016/08/26 08:33:04
Done.
| |
| 170 << long_term_peak_level_dbfs << " dBFS" << std::endl | |
| 171 << "Last frame peak level: " << frame_peak_level_dbfs | |
| 172 << " dBFS"; | |
| 173 | |
| 144 Reset(); | 174 Reset(); |
| 145 } | 175 } |
| 146 } | 176 } |
| 147 | 177 |
| 148 LevelController::LevelController() | 178 LevelController::LevelController() |
| 149 : data_dumper_(new ApmDataDumper(instance_count_)), | 179 : data_dumper_(new ApmDataDumper(instance_count_)), |
| 150 gain_applier_(data_dumper_.get()), | 180 gain_applier_(data_dumper_.get()), |
| 151 signal_classifier_(data_dumper_.get()) { | 181 signal_classifier_(data_dumper_.get()) { |
| 152 Initialize(AudioProcessing::kSampleRate48kHz); | 182 Initialize(AudioProcessing::kSampleRate48kHz); |
| 153 ++instance_count_; | 183 ++instance_count_; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 SignalClassifier::SignalType signal_type; | 223 SignalClassifier::SignalType signal_type; |
| 194 signal_classifier_.Analyze(*audio, &signal_type); | 224 signal_classifier_.Analyze(*audio, &signal_type); |
| 195 int tmp = static_cast<int>(signal_type); | 225 int tmp = static_cast<int>(signal_type); |
| 196 data_dumper_->DumpRaw("lc_signal_type", 1, &tmp); | 226 data_dumper_->DumpRaw("lc_signal_type", 1, &tmp); |
| 197 | 227 |
| 198 // Estimate the noise energy. | 228 // Estimate the noise energy. |
| 199 float noise_energy = | 229 float noise_energy = |
| 200 noise_level_estimator_.Analyze(signal_type, FrameEnergy(*audio)); | 230 noise_level_estimator_.Analyze(signal_type, FrameEnergy(*audio)); |
| 201 | 231 |
| 202 // Estimate the overall signal peak level. | 232 // Estimate the overall signal peak level. |
| 203 float peak_level = | 233 float frame_peak_level = PeakLevel(*audio); |
|
hlundin-webrtc
2016/08/26 07:53:29
const
peah-webrtc
2016/08/26 08:33:04
Done.
| |
| 204 peak_level_estimator_.Analyze(signal_type, PeakLevel(*audio)); | 234 float long_term_peak_level = |
|
hlundin-webrtc
2016/08/26 07:53:29
const
peah-webrtc
2016/08/26 08:33:04
Done.
| |
| 235 peak_level_estimator_.Analyze(signal_type, frame_peak_level); | |
| 205 | 236 |
| 206 float saturating_gain = saturating_gain_estimator_.GetGain(); | 237 float saturating_gain = saturating_gain_estimator_.GetGain(); |
| 207 | 238 |
| 208 // Compute the new gain to apply. | 239 // Compute the new gain to apply. |
| 209 last_gain_ = gain_selector_.GetNewGain(peak_level, noise_energy, | 240 last_gain_ = gain_selector_.GetNewGain(long_term_peak_level, noise_energy, |
| 210 saturating_gain, signal_type); | 241 saturating_gain, signal_type); |
| 211 | 242 |
| 212 // Apply the gain to the signal. | 243 // Apply the gain to the signal. |
| 213 int num_saturations = gain_applier_.Process(last_gain_, audio); | 244 int num_saturations = gain_applier_.Process(last_gain_, audio); |
| 214 | 245 |
| 215 // Estimate the gain that saturates the overall signal. | 246 // Estimate the gain that saturates the overall signal. |
| 216 saturating_gain_estimator_.Update(last_gain_, num_saturations); | 247 saturating_gain_estimator_.Update(last_gain_, num_saturations); |
| 217 | 248 |
| 218 // Update the metrics. | 249 // Update the metrics. |
| 219 metrics_.Update(peak_level, noise_energy, last_gain_); | 250 metrics_.Update(long_term_peak_level, noise_energy, last_gain_, |
| 251 frame_peak_level); | |
| 220 | 252 |
| 221 data_dumper_->DumpRaw("lc_selected_gain", 1, &last_gain_); | 253 data_dumper_->DumpRaw("lc_selected_gain", 1, &last_gain_); |
| 222 data_dumper_->DumpRaw("lc_noise_energy", 1, &noise_energy); | 254 data_dumper_->DumpRaw("lc_noise_energy", 1, &noise_energy); |
| 223 data_dumper_->DumpRaw("lc_peak_level", 1, &peak_level); | 255 data_dumper_->DumpRaw("lc_peak_level", 1, &long_term_peak_level); |
| 224 data_dumper_->DumpRaw("lc_saturating_gain", 1, &saturating_gain); | 256 data_dumper_->DumpRaw("lc_saturating_gain", 1, &saturating_gain); |
| 225 | 257 |
| 226 data_dumper_->DumpWav("lc_output", audio->num_frames(), | 258 data_dumper_->DumpWav("lc_output", audio->num_frames(), |
| 227 audio->channels_f()[0], *sample_rate_hz_, 1); | 259 audio->channels_f()[0], *sample_rate_hz_, 1); |
| 228 } | 260 } |
| 229 | 261 |
| 230 } // namespace webrtc | 262 } // namespace webrtc |
| OLD | NEW |