echo_remover_metrics_unittest.cc (6082B)
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 11 #include "modules/audio_processing/aec3/echo_remover_metrics.h" 12 13 #include <algorithm> 14 #include <array> 15 #include <cmath> 16 17 #include "api/audio/echo_canceller3_config.h" 18 #include "api/environment/environment_factory.h" 19 #include "modules/audio_processing/aec3/aec3_common.h" 20 #include "modules/audio_processing/aec3/aec3_fft.h" 21 #include "modules/audio_processing/aec3/aec_state.h" 22 #include "modules/audio_processing/aec3/fft_data.h" 23 #include "rtc_base/checks.h" 24 #include "test/gtest.h" 25 26 namespace webrtc { 27 28 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) 29 30 // Verifies the check for non-null input. 31 TEST(UpdateDbMetricDeathTest, NullValue) { 32 std::array<float, kFftLengthBy2Plus1> value; 33 value.fill(0.f); 34 EXPECT_DEATH(aec3::UpdateDbMetric(value, nullptr), ""); 35 } 36 37 #endif 38 39 // Verifies the updating functionality of UpdateDbMetric. 40 TEST(UpdateDbMetric, Updating) { 41 std::array<float, kFftLengthBy2Plus1> value; 42 std::array<EchoRemoverMetrics::DbMetric, 2> statistic; 43 statistic.fill(EchoRemoverMetrics::DbMetric(0.f, 100.f, -100.f)); 44 constexpr float kValue0 = 10.f; 45 constexpr float kValue1 = 20.f; 46 std::fill(value.begin(), value.begin() + 32, kValue0); 47 std::fill(value.begin() + 32, value.begin() + 64, kValue1); 48 49 aec3::UpdateDbMetric(value, &statistic); 50 EXPECT_FLOAT_EQ(kValue0, statistic[0].sum_value); 51 EXPECT_FLOAT_EQ(kValue0, statistic[0].ceil_value); 52 EXPECT_FLOAT_EQ(kValue0, statistic[0].floor_value); 53 EXPECT_FLOAT_EQ(kValue1, statistic[1].sum_value); 54 EXPECT_FLOAT_EQ(kValue1, statistic[1].ceil_value); 55 EXPECT_FLOAT_EQ(kValue1, statistic[1].floor_value); 56 57 aec3::UpdateDbMetric(value, &statistic); 58 EXPECT_FLOAT_EQ(2.f * kValue0, statistic[0].sum_value); 59 EXPECT_FLOAT_EQ(kValue0, statistic[0].ceil_value); 60 EXPECT_FLOAT_EQ(kValue0, statistic[0].floor_value); 61 EXPECT_FLOAT_EQ(2.f * kValue1, statistic[1].sum_value); 62 EXPECT_FLOAT_EQ(kValue1, statistic[1].ceil_value); 63 EXPECT_FLOAT_EQ(kValue1, statistic[1].floor_value); 64 } 65 66 // Verifies that the TransformDbMetricForReporting method produces the desired 67 // output for values for dBFS. 68 TEST(TransformDbMetricForReporting, DbFsScaling) { 69 std::array<float, kBlockSize> x; 70 FftData X; 71 std::array<float, kFftLengthBy2Plus1> X2; 72 Aec3Fft fft; 73 x.fill(1000.f); 74 fft.ZeroPaddedFft(x, Aec3Fft::Window::kRectangular, &X); 75 X.Spectrum(Aec3Optimization::kNone, X2); 76 77 float offset = -10.f * std::log10(32768.f * 32768.f); 78 EXPECT_NEAR(offset, -90.3f, 0.1f); 79 EXPECT_EQ( 80 static_cast<int>(30.3f), 81 aec3::TransformDbMetricForReporting( 82 true, 0.f, 90.f, offset, 1.f / (kBlockSize * kBlockSize), X2[0])); 83 } 84 85 // Verifies that the TransformDbMetricForReporting method is able to properly 86 // limit the output. 87 TEST(TransformDbMetricForReporting, Limits) { 88 EXPECT_EQ(0, aec3::TransformDbMetricForReporting(false, 0.f, 10.f, 0.f, 1.f, 89 0.001f)); 90 EXPECT_EQ(10, aec3::TransformDbMetricForReporting(false, 0.f, 10.f, 0.f, 1.f, 91 100.f)); 92 } 93 94 // Verifies that the TransformDbMetricForReporting method is able to properly 95 // negate output. 96 TEST(TransformDbMetricForReporting, Negate) { 97 EXPECT_EQ(10, aec3::TransformDbMetricForReporting(true, -20.f, 20.f, 0.f, 1.f, 98 0.1f)); 99 EXPECT_EQ(-10, aec3::TransformDbMetricForReporting(true, -20.f, 20.f, 0.f, 100 1.f, 10.f)); 101 } 102 103 // Verify the Update functionality of DbMetric. 104 TEST(DbMetric, Update) { 105 EchoRemoverMetrics::DbMetric metric(0.f, 20.f, -20.f); 106 constexpr int kNumValues = 100; 107 constexpr float kValue = 10.f; 108 for (int k = 0; k < kNumValues; ++k) { 109 metric.Update(kValue); 110 } 111 EXPECT_FLOAT_EQ(kValue * kNumValues, metric.sum_value); 112 EXPECT_FLOAT_EQ(kValue, metric.ceil_value); 113 EXPECT_FLOAT_EQ(kValue, metric.floor_value); 114 } 115 116 // Verify the Update functionality of DbMetric. 117 TEST(DbMetric, UpdateInstant) { 118 EchoRemoverMetrics::DbMetric metric(0.f, 20.f, -20.f); 119 constexpr float kMinValue = -77.f; 120 constexpr float kMaxValue = 33.f; 121 constexpr float kLastValue = (kMinValue + kMaxValue) / 2.0f; 122 for (float value = kMinValue; value <= kMaxValue; value++) 123 metric.UpdateInstant(value); 124 metric.UpdateInstant(kLastValue); 125 EXPECT_FLOAT_EQ(kLastValue, metric.sum_value); 126 EXPECT_FLOAT_EQ(kMaxValue, metric.ceil_value); 127 EXPECT_FLOAT_EQ(kMinValue, metric.floor_value); 128 } 129 130 // Verify the constructor functionality of DbMetric. 131 TEST(DbMetric, Constructor) { 132 EchoRemoverMetrics::DbMetric metric; 133 EXPECT_FLOAT_EQ(0.f, metric.sum_value); 134 EXPECT_FLOAT_EQ(0.f, metric.ceil_value); 135 EXPECT_FLOAT_EQ(0.f, metric.floor_value); 136 137 metric = EchoRemoverMetrics::DbMetric(1.f, 2.f, 3.f); 138 EXPECT_FLOAT_EQ(1.f, metric.sum_value); 139 EXPECT_FLOAT_EQ(2.f, metric.floor_value); 140 EXPECT_FLOAT_EQ(3.f, metric.ceil_value); 141 } 142 143 // Verify the general functionality of EchoRemoverMetrics. 144 TEST(EchoRemoverMetrics, NormalUsage) { 145 EchoRemoverMetrics metrics; 146 AecState aec_state(CreateEnvironment(), EchoCanceller3Config{}, 1); 147 std::array<float, kFftLengthBy2Plus1> comfort_noise_spectrum; 148 std::array<float, kFftLengthBy2Plus1> suppressor_gain; 149 comfort_noise_spectrum.fill(10.f); 150 suppressor_gain.fill(1.f); 151 for (int j = 0; j < 3; ++j) { 152 for (int k = 0; k < kMetricsReportingIntervalBlocks - 1; ++k) { 153 metrics.Update(aec_state, comfort_noise_spectrum, suppressor_gain); 154 EXPECT_FALSE(metrics.MetricsReported()); 155 } 156 metrics.Update(aec_state, comfort_noise_spectrum, suppressor_gain); 157 EXPECT_TRUE(metrics.MetricsReported()); 158 } 159 } 160 161 } // namespace webrtc