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/render_signal_analyzer.h"
12
13 #include <math.h>
14
15 #include <array>
16 #include <cmath>
17 #include <vector>
18
19 #include "api/array_view.h"
20 #include "modules/audio_processing/aec3/aec3_common.h"
21 #include "modules/audio_processing/aec3/aec3_fft.h"
22 #include "modules/audio_processing/aec3/fft_data.h"
23 #include "modules/audio_processing/aec3/render_delay_buffer.h"
24 #include "modules/audio_processing/test/echo_canceller_test_tools.h"
25 #include "rtc_base/random.h"
26 #include "rtc_base/strings/string_builder.h"
27 #include "test/gtest.h"
28
29 namespace webrtc {
30 namespace {
31
32 constexpr float kPi = 3.141592f;
33
ProduceSinusoidInNoise(int sample_rate_hz,size_t sinusoid_channel,float sinusoidal_frequency_hz,Random * random_generator,size_t * sample_counter,std::vector<std::vector<std::vector<float>>> * x)34 void ProduceSinusoidInNoise(int sample_rate_hz,
35 size_t sinusoid_channel,
36 float sinusoidal_frequency_hz,
37 Random* random_generator,
38 size_t* sample_counter,
39 std::vector<std::vector<std::vector<float>>>* x) {
40 // Fill x with low-amplitude noise.
41 for (auto& band : *x) {
42 for (auto& channel : band) {
43 RandomizeSampleVector(random_generator, channel,
44 /*amplitude=*/500.f);
45 }
46 }
47 // Produce a sinusoid of the specified frequency in the specified channel.
48 for (size_t k = *sample_counter, j = 0; k < (*sample_counter + kBlockSize);
49 ++k, ++j) {
50 (*x)[0][sinusoid_channel][j] +=
51 32000.f *
52 std::sin(2.f * kPi * sinusoidal_frequency_hz * k / sample_rate_hz);
53 }
54 *sample_counter = *sample_counter + kBlockSize;
55 }
56
RunNarrowBandDetectionTest(size_t num_channels)57 void RunNarrowBandDetectionTest(size_t num_channels) {
58 RenderSignalAnalyzer analyzer(EchoCanceller3Config{});
59 Random random_generator(42U);
60 constexpr int kSampleRateHz = 48000;
61 constexpr size_t kNumBands = NumBandsForRate(kSampleRateHz);
62 std::vector<std::vector<std::vector<float>>> x(
63 kNumBands, std::vector<std::vector<float>>(
64 num_channels, std::vector<float>(kBlockSize, 0.f)));
65 std::array<float, kBlockSize> x_old;
66 Aec3Fft fft;
67 EchoCanceller3Config config;
68 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
69 RenderDelayBuffer::Create(config, kSampleRateHz, num_channels));
70
71 std::array<float, kFftLengthBy2Plus1> mask;
72 x_old.fill(0.f);
73 constexpr int kSinusFrequencyBin = 32;
74
75 auto generate_sinusoid_test = [&](bool known_delay) {
76 size_t sample_counter = 0;
77 for (size_t k = 0; k < 100; ++k) {
78 ProduceSinusoidInNoise(16000, num_channels - 1,
79 16000 / 2 * kSinusFrequencyBin / kFftLengthBy2,
80 &random_generator, &sample_counter, &x);
81
82 render_delay_buffer->Insert(x);
83 if (k == 0) {
84 render_delay_buffer->Reset();
85 }
86 render_delay_buffer->PrepareCaptureProcessing();
87
88 analyzer.Update(*render_delay_buffer->GetRenderBuffer(),
89 known_delay ? absl::optional<size_t>(0) : absl::nullopt);
90 }
91 };
92
93 generate_sinusoid_test(true);
94 mask.fill(1.f);
95 analyzer.MaskRegionsAroundNarrowBands(&mask);
96 for (int k = 0; k < static_cast<int>(mask.size()); ++k) {
97 EXPECT_EQ(abs(k - kSinusFrequencyBin) <= 2 ? 0.f : 1.f, mask[k]);
98 }
99 EXPECT_TRUE(analyzer.PoorSignalExcitation());
100 EXPECT_TRUE(static_cast<bool>(analyzer.NarrowPeakBand()));
101 EXPECT_EQ(*analyzer.NarrowPeakBand(), 32);
102
103 // Verify that no bands are detected as narrow when the delay is unknown.
104 generate_sinusoid_test(false);
105 mask.fill(1.f);
106 analyzer.MaskRegionsAroundNarrowBands(&mask);
107 std::for_each(mask.begin(), mask.end(), [](float a) { EXPECT_EQ(1.f, a); });
108 EXPECT_FALSE(analyzer.PoorSignalExcitation());
109 }
110
ProduceDebugText(size_t num_channels)111 std::string ProduceDebugText(size_t num_channels) {
112 rtc::StringBuilder ss;
113 ss << "number of channels: " << num_channels;
114 return ss.Release();
115 }
116 } // namespace
117
118 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
119 // Verifies that the check for non-null output parameter works.
TEST(RenderSignalAnalyzerDeathTest,NullMaskOutput)120 TEST(RenderSignalAnalyzerDeathTest, NullMaskOutput) {
121 RenderSignalAnalyzer analyzer(EchoCanceller3Config{});
122 EXPECT_DEATH(analyzer.MaskRegionsAroundNarrowBands(nullptr), "");
123 }
124
125 #endif
126
127 // Verify that no narrow bands are detected in a Gaussian noise signal.
TEST(RenderSignalAnalyzer,NoFalseDetectionOfNarrowBands)128 TEST(RenderSignalAnalyzer, NoFalseDetectionOfNarrowBands) {
129 for (auto num_channels : {1, 2, 8}) {
130 SCOPED_TRACE(ProduceDebugText(num_channels));
131 RenderSignalAnalyzer analyzer(EchoCanceller3Config{});
132 Random random_generator(42U);
133 std::vector<std::vector<std::vector<float>>> x(
134 3, std::vector<std::vector<float>>(
135 num_channels, std::vector<float>(kBlockSize, 0.f)));
136 std::array<float, kBlockSize> x_old;
137 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
138 RenderDelayBuffer::Create(EchoCanceller3Config(), 48000, num_channels));
139 std::array<float, kFftLengthBy2Plus1> mask;
140 x_old.fill(0.f);
141
142 for (size_t k = 0; k < 100; ++k) {
143 for (auto& band : x) {
144 for (auto& channel : band) {
145 RandomizeSampleVector(&random_generator, channel);
146 }
147 }
148
149 render_delay_buffer->Insert(x);
150 if (k == 0) {
151 render_delay_buffer->Reset();
152 }
153 render_delay_buffer->PrepareCaptureProcessing();
154
155 analyzer.Update(*render_delay_buffer->GetRenderBuffer(),
156 absl::optional<size_t>(0));
157 }
158
159 mask.fill(1.f);
160 analyzer.MaskRegionsAroundNarrowBands(&mask);
161 EXPECT_TRUE(std::all_of(mask.begin(), mask.end(),
162 [](float a) { return a == 1.f; }));
163 EXPECT_FALSE(analyzer.PoorSignalExcitation());
164 EXPECT_FALSE(static_cast<bool>(analyzer.NarrowPeakBand()));
165 }
166 }
167
168 // Verify that a sinusoid signal is detected as narrow bands.
TEST(RenderSignalAnalyzer,NarrowBandDetection)169 TEST(RenderSignalAnalyzer, NarrowBandDetection) {
170 for (auto num_channels : {1, 2, 8}) {
171 SCOPED_TRACE(ProduceDebugText(num_channels));
172 RunNarrowBandDetectionTest(num_channels);
173 }
174 }
175 } // namespace webrtc
176