blob: c3fc80773a933f381e576f3e54e30131b7fddd16 [file] [log] [blame]
peahe985b3f2017-02-28 22:08:53 -08001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/aec3/echo_remover_metrics.h"
peahe985b3f2017-02-28 22:08:53 -080012
13#include <math.h>
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
peahe985b3f2017-02-28 22:08:53 -080016#include <algorithm>
Mirko Bonadeidbce0902019-03-15 07:39:02 +010017#include <cmath>
peahe985b3f2017-02-28 22:08:53 -080018#include <numeric>
19
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "rtc_base/checks.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010021#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "system_wrappers/include/metrics.h"
peahe985b3f2017-02-28 22:08:53 -080023
24namespace webrtc {
25
peahe985b3f2017-02-28 22:08:53 -080026EchoRemoverMetrics::DbMetric::DbMetric() : DbMetric(0.f, 0.f, 0.f) {}
27EchoRemoverMetrics::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
32void 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 Ullberg2723fb12017-11-23 14:46:48 +010038void 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
peahe985b3f2017-02-28 22:08:53 -080044EchoRemoverMetrics::EchoRemoverMetrics() {
45 ResetMetrics();
46}
47
48void EchoRemoverMetrics::ResetMetrics() {
Gustaf Ullberg2723fb12017-11-23 14:46:48 +010049 erl_time_domain_ = DbMetric(0.f, 10000.f, 0.000f);
Gustaf Ullberg2723fb12017-11-23 14:46:48 +010050 erle_time_domain_ = DbMetric(0.f, 0.f, 1000.f);
peahe985b3f2017-02-28 22:08:53 -080051 saturated_capture_ = false;
52}
53
54void 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 Ullberg2723fb12017-11-23 14:46:48 +010060 erl_time_domain_.UpdateInstant(aec_state.ErlTimeDomain());
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +020061 erle_time_domain_.UpdateInstant(aec_state.FullBandErleLog2());
peahe985b3f2017-02-28 22:08:53 -080062 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.
peahe985b3f2017-02-28 22:08:53 -080066 switch (block_counter_) {
67 case kMetricsCollectionBlocks + 1:
peahe985b3f2017-02-28 22:08:53 -080068 RTC_HISTOGRAM_BOOLEAN(
69 "WebRTC.Audio.EchoCanceller.UsableLinearEstimate",
70 static_cast<int>(aec_state.UsableLinearEstimate() ? 1 : 0));
Per Åhgren0e6d2f52017-12-20 22:19:56 +010071 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.EchoCanceller.FilterDelay",
Per Åhgren8718afb2019-10-15 10:31:35 +020072 aec_state.MinDirectPathFilterDelay(), 0, 30,
73 31);
peahe985b3f2017-02-28 22:08:53 -080074 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.EchoCanceller.CaptureSaturation",
75 static_cast<int>(saturated_capture_ ? 1 : 0));
Gustaf Ullberg2723fb12017-11-23 14:46:48 +010076 break;
Sam Zackrisson7bc33562020-10-20 12:02:33 +020077 case kMetricsCollectionBlocks + 2:
Gustaf Ullberg2723fb12017-11-23 14:46:48 +010078 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 Zackrisson7bc33562020-10-20 12:02:33 +020094 case kMetricsCollectionBlocks + 3:
Gustaf Ullberg2723fb12017-11-23 14:46:48 +010095 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);
peahe985b3f2017-02-28 22:08:53 -0800110 metrics_reported_ = true;
111 RTC_DCHECK_EQ(kMetricsReportingIntervalBlocks, block_counter_);
112 block_counter_ = 0;
113 ResetMetrics();
114 break;
115 default:
Artem Titovd3251962021-11-15 16:57:07 +0100116 RTC_DCHECK_NOTREACHED();
peahe985b3f2017-02-28 22:08:53 -0800117 break;
118 }
119 }
120}
121
122namespace aec3 {
123
124void 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
142int TransformDbMetricForReporting(bool negate,
143 float min_value,
144 float max_value,
145 float offset,
146 float scaling,
147 float value) {
Mirko Bonadeidbce0902019-03-15 07:39:02 +0100148 float new_value = 10.f * std::log10(value * scaling + 1e-10f) + offset;
peahe985b3f2017-02-28 22:08:53 -0800149 if (negate) {
150 new_value = -new_value;
151 }
kwiberg07038562017-06-12 11:40:47 -0700152 return static_cast<int>(rtc::SafeClamp(new_value, min_value, max_value));
peahe985b3f2017-02-28 22:08:53 -0800153}
154
155} // namespace aec3
156
157} // namespace webrtc