xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/alignment_mixer_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
21 using ::testing::AllOf;
22 using ::testing::Each;
23 
24 namespace webrtc {
25 namespace {
ProduceDebugText(bool initial_silence,bool huge_activity_threshold,bool prefer_first_two_channels,int num_channels,int strongest_ch)26 std::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 
TEST(AlignmentMixer,GeneralAdaptiveMode)42 TEST(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             Block x(
64                 /*num_bands=*/1, num_channels);
65             if (initial_silence) {
66               std::array<float, kBlockSize> y;
67               for (int frame = 0; frame < 10 * kNumBlocksPerSecond; ++frame) {
68                 am.ProduceOutput(x, y);
69               }
70             }
71 
72             for (int frame = 0; frame < 2 * kNumBlocksPerSecond; ++frame) {
73               const auto channel_value = [&](int frame_index,
74                                              int channel_index) {
75                 return static_cast<float>(frame_index +
76                                           channel_index * kChannelOffset);
77               };
78 
79               for (int ch = 0; ch < num_channels; ++ch) {
80                 float scaling =
81                     ch == strongest_ch ? kStrongestSignalScaling : 1.f;
82                 auto x_ch = x.View(/*band=*/0, ch);
83                 std::fill(x_ch.begin(), x_ch.end(),
84                           channel_value(frame, ch) * scaling);
85               }
86 
87               std::array<float, kBlockSize> y;
88               y.fill(-1.f);
89               am.ProduceOutput(x, y);
90 
91               if (frame > 1 * kNumBlocksPerSecond) {
92                 if (!prefer_first_two_channels || huge_activity_threshold) {
93                   EXPECT_THAT(y,
94                               AllOf(Each(x.View(/*band=*/0, strongest_ch)[0])));
95                 } else {
96                   bool left_or_right_chosen;
97                   for (int ch = 0; ch < 2; ++ch) {
98                     left_or_right_chosen = true;
99                     const auto x_ch = x.View(/*band=*/0, ch);
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 
TEST(AlignmentMixer,DownmixMode)121 TEST(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     Block x(/*num_bands=*/1, num_channels);
128     const auto channel_value = [](int frame_index, int channel_index) {
129       return static_cast<float>(frame_index + channel_index);
130     };
131     for (int frame = 0; frame < 10; ++frame) {
132       for (int ch = 0; ch < num_channels; ++ch) {
133         auto x_ch = x.View(/*band=*/0, 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 
TEST(AlignmentMixer,FixedMode)152 TEST(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     Block x(/*num_band=*/1, num_channels);
159     const auto channel_value = [](int frame_index, int channel_index) {
160       return static_cast<float>(frame_index + channel_index);
161     };
162     for (int frame = 0; frame < 10; ++frame) {
163       for (int ch = 0; ch < num_channels; ++ch) {
164         auto x_ch = x.View(/*band=*/0, 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.View(/*band=*/0, /*channel=*/0)[0])));
172     }
173   }
174 }
175 
176 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
177 
TEST(AlignmentMixerDeathTest,ZeroNumChannels)178 TEST(AlignmentMixerDeathTest, 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 
TEST(AlignmentMixerDeathTest,IncorrectVariant)186 TEST(AlignmentMixerDeathTest, 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
197