peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/aec3/echo_remover_metrics.h" |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 12 | |
| 13 | #include <math.h> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 16 | #include <algorithm> |
Mirko Bonadei | dbce090 | 2019-03-15 07:39:02 +0100 | [diff] [blame] | 17 | #include <cmath> |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 18 | #include <numeric> |
| 19 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 21 | #include "rtc_base/numerics/safe_minmax.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "system_wrappers/include/metrics.h" |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 26 | EchoRemoverMetrics::DbMetric::DbMetric() : DbMetric(0.f, 0.f, 0.f) {} |
| 27 | EchoRemoverMetrics::DbMetric::DbMetric(float sum_value, |
| 28 | float floor_value, |
| 29 | float ceil_value) |
| 30 | : sum_value(sum_value), floor_value(floor_value), ceil_value(ceil_value) {} |
| 31 | |
| 32 | void EchoRemoverMetrics::DbMetric::Update(float value) { |
| 33 | sum_value += value; |
| 34 | floor_value = std::min(floor_value, value); |
| 35 | ceil_value = std::max(ceil_value, value); |
| 36 | } |
| 37 | |
Gustaf Ullberg | 2723fb1 | 2017-11-23 14:46:48 +0100 | [diff] [blame] | 38 | void EchoRemoverMetrics::DbMetric::UpdateInstant(float value) { |
| 39 | sum_value = value; |
| 40 | floor_value = std::min(floor_value, value); |
| 41 | ceil_value = std::max(ceil_value, value); |
| 42 | } |
| 43 | |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 44 | EchoRemoverMetrics::EchoRemoverMetrics() { |
| 45 | ResetMetrics(); |
| 46 | } |
| 47 | |
| 48 | void EchoRemoverMetrics::ResetMetrics() { |
Gustaf Ullberg | 2723fb1 | 2017-11-23 14:46:48 +0100 | [diff] [blame] | 49 | erl_time_domain_ = DbMetric(0.f, 10000.f, 0.000f); |
Gustaf Ullberg | 2723fb1 | 2017-11-23 14:46:48 +0100 | [diff] [blame] | 50 | erle_time_domain_ = DbMetric(0.f, 0.f, 1000.f); |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 51 | saturated_capture_ = false; |
| 52 | } |
| 53 | |
| 54 | void EchoRemoverMetrics::Update( |
| 55 | const AecState& aec_state, |
| 56 | const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum, |
| 57 | const std::array<float, kFftLengthBy2Plus1>& suppressor_gain) { |
| 58 | metrics_reported_ = false; |
| 59 | if (++block_counter_ <= kMetricsCollectionBlocks) { |
Gustaf Ullberg | 2723fb1 | 2017-11-23 14:46:48 +0100 | [diff] [blame] | 60 | erl_time_domain_.UpdateInstant(aec_state.ErlTimeDomain()); |
Jesús de Vicente Peña | e9a7e90 | 2018-09-27 11:49:39 +0200 | [diff] [blame] | 61 | erle_time_domain_.UpdateInstant(aec_state.FullBandErleLog2()); |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 62 | saturated_capture_ = saturated_capture_ || aec_state.SaturatedCapture(); |
| 63 | } else { |
| 64 | // Report the metrics over several frames in order to lower the impact of |
| 65 | // the logarithms involved on the computational complexity. |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 66 | switch (block_counter_) { |
| 67 | case kMetricsCollectionBlocks + 1: |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 68 | RTC_HISTOGRAM_BOOLEAN( |
| 69 | "WebRTC.Audio.EchoCanceller.UsableLinearEstimate", |
| 70 | static_cast<int>(aec_state.UsableLinearEstimate() ? 1 : 0)); |
Per Åhgren | 0e6d2f5 | 2017-12-20 22:19:56 +0100 | [diff] [blame] | 71 | RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.EchoCanceller.FilterDelay", |
Per Åhgren | 8718afb | 2019-10-15 10:31:35 +0200 | [diff] [blame] | 72 | aec_state.MinDirectPathFilterDelay(), 0, 30, |
| 73 | 31); |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 74 | RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.EchoCanceller.CaptureSaturation", |
| 75 | static_cast<int>(saturated_capture_ ? 1 : 0)); |
Gustaf Ullberg | 2723fb1 | 2017-11-23 14:46:48 +0100 | [diff] [blame] | 76 | break; |
Sam Zackrisson | 7bc3356 | 2020-10-20 12:02:33 +0200 | [diff] [blame] | 77 | case kMetricsCollectionBlocks + 2: |
Gustaf Ullberg | 2723fb1 | 2017-11-23 14:46:48 +0100 | [diff] [blame] | 78 | RTC_HISTOGRAM_COUNTS_LINEAR( |
| 79 | "WebRTC.Audio.EchoCanceller.Erl.Value", |
| 80 | aec3::TransformDbMetricForReporting(true, 0.f, 59.f, 30.f, 1.f, |
| 81 | erl_time_domain_.sum_value), |
| 82 | 0, 59, 30); |
| 83 | RTC_HISTOGRAM_COUNTS_LINEAR( |
| 84 | "WebRTC.Audio.EchoCanceller.Erl.Max", |
| 85 | aec3::TransformDbMetricForReporting(true, 0.f, 59.f, 30.f, 1.f, |
| 86 | erl_time_domain_.ceil_value), |
| 87 | 0, 59, 30); |
| 88 | RTC_HISTOGRAM_COUNTS_LINEAR( |
| 89 | "WebRTC.Audio.EchoCanceller.Erl.Min", |
| 90 | aec3::TransformDbMetricForReporting(true, 0.f, 59.f, 30.f, 1.f, |
| 91 | erl_time_domain_.floor_value), |
| 92 | 0, 59, 30); |
| 93 | break; |
Sam Zackrisson | 7bc3356 | 2020-10-20 12:02:33 +0200 | [diff] [blame] | 94 | case kMetricsCollectionBlocks + 3: |
Gustaf Ullberg | 2723fb1 | 2017-11-23 14:46:48 +0100 | [diff] [blame] | 95 | RTC_HISTOGRAM_COUNTS_LINEAR( |
| 96 | "WebRTC.Audio.EchoCanceller.Erle.Value", |
| 97 | aec3::TransformDbMetricForReporting(false, 0.f, 19.f, 0.f, 1.f, |
| 98 | erle_time_domain_.sum_value), |
| 99 | 0, 19, 20); |
| 100 | RTC_HISTOGRAM_COUNTS_LINEAR( |
| 101 | "WebRTC.Audio.EchoCanceller.Erle.Max", |
| 102 | aec3::TransformDbMetricForReporting(false, 0.f, 19.f, 0.f, 1.f, |
| 103 | erle_time_domain_.ceil_value), |
| 104 | 0, 19, 20); |
| 105 | RTC_HISTOGRAM_COUNTS_LINEAR( |
| 106 | "WebRTC.Audio.EchoCanceller.Erle.Min", |
| 107 | aec3::TransformDbMetricForReporting(false, 0.f, 19.f, 0.f, 1.f, |
| 108 | erle_time_domain_.floor_value), |
| 109 | 0, 19, 20); |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 110 | metrics_reported_ = true; |
| 111 | RTC_DCHECK_EQ(kMetricsReportingIntervalBlocks, block_counter_); |
| 112 | block_counter_ = 0; |
| 113 | ResetMetrics(); |
| 114 | break; |
| 115 | default: |
Artem Titov | d325196 | 2021-11-15 16:57:07 +0100 | [diff] [blame] | 116 | RTC_DCHECK_NOTREACHED(); |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 117 | break; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | namespace aec3 { |
| 123 | |
| 124 | void UpdateDbMetric(const std::array<float, kFftLengthBy2Plus1>& value, |
| 125 | std::array<EchoRemoverMetrics::DbMetric, 2>* statistic) { |
| 126 | RTC_DCHECK(statistic); |
| 127 | // Truncation is intended in the band width computation. |
| 128 | constexpr int kNumBands = 2; |
| 129 | constexpr int kBandWidth = 65 / kNumBands; |
| 130 | constexpr float kOneByBandWidth = 1.f / kBandWidth; |
| 131 | RTC_DCHECK_EQ(kNumBands, statistic->size()); |
| 132 | RTC_DCHECK_EQ(65, value.size()); |
| 133 | for (size_t k = 0; k < statistic->size(); ++k) { |
| 134 | float average_band = |
| 135 | std::accumulate(value.begin() + kBandWidth * k, |
| 136 | value.begin() + kBandWidth * (k + 1), 0.f) * |
| 137 | kOneByBandWidth; |
| 138 | (*statistic)[k].Update(average_band); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | int TransformDbMetricForReporting(bool negate, |
| 143 | float min_value, |
| 144 | float max_value, |
| 145 | float offset, |
| 146 | float scaling, |
| 147 | float value) { |
Mirko Bonadei | dbce090 | 2019-03-15 07:39:02 +0100 | [diff] [blame] | 148 | float new_value = 10.f * std::log10(value * scaling + 1e-10f) + offset; |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 149 | if (negate) { |
| 150 | new_value = -new_value; |
| 151 | } |
kwiberg | 0703856 | 2017-06-12 11:40:47 -0700 | [diff] [blame] | 152 | return static_cast<int>(rtc::SafeClamp(new_value, min_value, max_value)); |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | } // namespace aec3 |
| 156 | |
| 157 | } // namespace webrtc |