blob: 832e4ea88457740d7c4ac72b5f94a2caf5c4b803 [file] [log] [blame]
Per Ã…hgren6a05bb12019-12-03 11:24:59 +01001/*
2 * Copyright (c) 2019 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/alignment_mixer.h"
12
13#include <string>
14
15#include "api/array_view.h"
16#include "modules/audio_processing/aec3/aec3_common.h"
17#include "rtc_base/strings/string_builder.h"
18#include "test/gmock.h"
19#include "test/gtest.h"
20
21using ::testing::AllOf;
22using ::testing::Each;
23
24namespace webrtc {
25namespace {
26std::string ProduceDebugText(bool initial_silence,
27 bool huge_activity_threshold,
28 bool prefer_first_two_channels,
29 int num_channels,
30 int strongest_ch) {
31 rtc::StringBuilder ss;
32 ss << ", Initial silence: " << initial_silence;
33 ss << ", Huge activity threshold: " << huge_activity_threshold;
34 ss << ", Prefer first two channels: " << prefer_first_two_channels;
35 ss << ", Number of channels: " << num_channels;
36 ss << ", Strongest channel: " << strongest_ch;
37 return ss.Release();
38}
39
40} // namespace
41
42TEST(AlignmentMixer, GeneralAdaptiveMode) {
43 constexpr int kChannelOffset = 100;
44 constexpr int kMaxChannelsToTest = 8;
45 constexpr float kStrongestSignalScaling =
46 kMaxChannelsToTest * kChannelOffset * 100;
47
48 for (bool initial_silence : {false, true}) {
49 for (bool huge_activity_threshold : {false, true}) {
50 for (bool prefer_first_two_channels : {false, true}) {
51 for (int num_channels = 2; num_channels < 8; ++num_channels) {
52 for (int strongest_ch = 0; strongest_ch < num_channels;
53 ++strongest_ch) {
54 SCOPED_TRACE(ProduceDebugText(
55 initial_silence, huge_activity_threshold,
56 prefer_first_two_channels, num_channels, strongest_ch));
57 const float excitation_limit =
58 huge_activity_threshold ? 1000000000.f : 0.001f;
59 AlignmentMixer am(num_channels, /*downmix*/ false,
60 /*adaptive_selection*/ true, excitation_limit,
61 prefer_first_two_channels);
62
63 std::vector<std::vector<float>> x(
64 num_channels, std::vector<float>(kBlockSize, 0.f));
65 if (initial_silence) {
66 for (int ch = 0; ch < num_channels; ++ch) {
67 std::fill(x[ch].begin(), x[ch].end(), 0.f);
68 }
69 std::array<float, kBlockSize> y;
70 for (int frame = 0; frame < 10 * kNumBlocksPerSecond; ++frame) {
71 am.ProduceOutput(x, y);
72 }
73 }
74
75 for (int frame = 0; frame < 2 * kNumBlocksPerSecond; ++frame) {
76 const auto channel_value = [&](int frame_index,
77 int channel_index) {
78 return static_cast<float>(frame_index +
79 channel_index * kChannelOffset);
80 };
81
82 for (int ch = 0; ch < num_channels; ++ch) {
83 float scaling =
84 ch == strongest_ch ? kStrongestSignalScaling : 1.f;
85 std::fill(x[ch].begin(), x[ch].end(),
86 channel_value(frame, ch) * scaling);
87 }
88
89 std::array<float, kBlockSize> y;
90 y.fill(-1.f);
91 am.ProduceOutput(x, y);
92
93 if (frame > 1 * kNumBlocksPerSecond) {
94 if (!prefer_first_two_channels || huge_activity_threshold) {
95 EXPECT_THAT(y, AllOf(Each(x[strongest_ch][0])));
96 } else {
97 bool left_or_right_chosen;
98 for (int ch = 0; ch < 2; ++ch) {
99 left_or_right_chosen = true;
100 for (size_t k = 0; k < kBlockSize; ++k) {
101 if (y[k] != x[ch][k]) {
102 left_or_right_chosen = false;
103 break;
104 }
105 }
106 if (left_or_right_chosen) {
107 break;
108 }
109 }
110 EXPECT_TRUE(left_or_right_chosen);
111 }
112 }
113 }
114 }
115 }
116 }
117 }
118 }
119}
120
121TEST(AlignmentMixer, DownmixMode) {
122 for (int num_channels = 1; num_channels < 8; ++num_channels) {
123 AlignmentMixer am(num_channels, /*downmix*/ true,
124 /*adaptive_selection*/ false, /*excitation_limit*/ 1.f,
125 /*prefer_first_two_channels*/ false);
126
127 std::vector<std::vector<float>> x(num_channels,
128 std::vector<float>(kBlockSize, 0.f));
129 const auto channel_value = [](int frame_index, int channel_index) {
130 return static_cast<float>(frame_index + channel_index);
131 };
132 for (int frame = 0; frame < 10; ++frame) {
133 for (int ch = 0; ch < num_channels; ++ch) {
134 std::fill(x[ch].begin(), x[ch].end(), channel_value(frame, ch));
135 }
136
137 std::array<float, kBlockSize> y;
138 y.fill(-1.f);
139 am.ProduceOutput(x, y);
140
141 float expected_mixed_value = 0.f;
142 for (int ch = 0; ch < num_channels; ++ch) {
143 expected_mixed_value += channel_value(frame, ch);
144 }
145 expected_mixed_value *= 1.f / num_channels;
146
147 EXPECT_THAT(y, AllOf(Each(expected_mixed_value)));
148 }
149 }
150}
151
152TEST(AlignmentMixer, FixedMode) {
153 for (int num_channels = 1; num_channels < 8; ++num_channels) {
154 AlignmentMixer am(num_channels, /*downmix*/ false,
155 /*adaptive_selection*/ false, /*excitation_limit*/ 1.f,
156 /*prefer_first_two_channels*/ false);
157
158 std::vector<std::vector<float>> x(num_channels,
159 std::vector<float>(kBlockSize, 0.f));
160 const auto channel_value = [](int frame_index, int channel_index) {
161 return static_cast<float>(frame_index + channel_index);
162 };
163 for (int frame = 0; frame < 10; ++frame) {
164 for (int ch = 0; ch < num_channels; ++ch) {
165 std::fill(x[ch].begin(), x[ch].end(), channel_value(frame, ch));
166 }
167
168 std::array<float, kBlockSize> y;
169 y.fill(-1.f);
170 am.ProduceOutput(x, y);
171 EXPECT_THAT(y, AllOf(Each(x[0][0])));
172 }
173 }
174}
175
176#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
177
178TEST(AlignmentMixer, ZeroNumChannels) {
179 EXPECT_DEATH(
180 AlignmentMixer(/*num_channels*/ 0, /*downmix*/ false,
181 /*adaptive_selection*/ false, /*excitation_limit*/ 1.f,
182 /*prefer_first_two_channels*/ false);
183 , "");
184}
185
186TEST(AlignmentMixer, IncorrectVariant) {
187 EXPECT_DEATH(
188 AlignmentMixer(/*num_channels*/ 1, /*downmix*/ true,
189 /*adaptive_selection*/ true, /*excitation_limit*/ 1.f,
190 /*prefer_first_two_channels*/ false);
191 , "");
192}
193
194#endif
195
196} // namespace webrtc