1 /*
2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "EffectTestHelper.h"
18
19 #include <getopt.h>
20 #include <stddef.h>
21 #include <stdint.h>
22 #include <tuple>
23 #include <vector>
24
25 #include <audio_effects/effect_aec.h>
26 #include <audio_effects/effect_agc.h>
27 #include <audio_effects/effect_agc2.h>
28 #include <audio_effects/effect_ns.h>
29 #include <log/log.h>
30
31 constexpr effect_uuid_t kAGCUuid = {
32 0xaa8130e0, 0x66fc, 0x11e0, 0xbad0, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}};
33 constexpr effect_uuid_t kAGC2Uuid = {
34 0x89f38e65, 0xd4d2, 0x4d64, 0xad0e, {0x2b, 0x3e, 0x79, 0x9e, 0xa8, 0x86}};
35 constexpr effect_uuid_t kAECUuid = {
36 0xbb392ec0, 0x8d4d, 0x11e0, 0xa896, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}};
37 constexpr effect_uuid_t kNSUuid = {
38 0xc06c8400, 0x8e06, 0x11e0, 0x9cb6, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}};
39
isAGCEffect(const effect_uuid_t * uuid)40 static bool isAGCEffect(const effect_uuid_t* uuid) {
41 return uuid == &kAGCUuid;
42 }
isAGC2Effect(const effect_uuid_t * uuid)43 static bool isAGC2Effect(const effect_uuid_t* uuid) {
44 return uuid == &kAGC2Uuid;
45 }
isAECEffect(const effect_uuid_t * uuid)46 static bool isAECEffect(const effect_uuid_t* uuid) {
47 return uuid == &kAECUuid;
48 }
isNSEffect(const effect_uuid_t * uuid)49 static bool isNSEffect(const effect_uuid_t* uuid) {
50 return uuid == &kNSUuid;
51 }
52
53 constexpr int kAGCTargetLevels[] = {0, -300, -500, -1000, -3100};
54
55 constexpr int kAGCCompLevels[] = {0, -300, -500, -1000, -9000};
56
57 constexpr size_t kAGC2FixedDigitalGains[] = {0, 3, 10, 20, 49};
58
59 constexpr size_t kAGC2AdaptGigitalLevelEstimators[] = {0, 1};
60
61 constexpr size_t kAGC2ExtraSaturationMargins[] = {0, 3, 10, 20, 100};
62
63 constexpr size_t kAECEchoDelays[] = {0, 250, 500};
64
65 constexpr size_t kNSLevels[] = {0, 1, 2, 3};
66
67 struct AGCParams {
68 int targetLevel;
69 int compLevel;
70 };
71
72 struct AGC2Params {
73 size_t fixedDigitalGain;
74 size_t adaptDigiLevelEstimator;
75 size_t extraSaturationMargin;
76 };
77
78 struct AECParams {
79 size_t echoDelay;
80 };
81
82 struct NSParams {
83 size_t level;
84 };
85
86 struct PreProcParams {
87 const effect_uuid_t* uuid;
88 union {
89 AGCParams agcParams;
90 AGC2Params agc2Params;
91 AECParams aecParams;
92 NSParams nsParams;
93 };
94 };
95
96 // Create a list of pre-processing parameters to be used for testing
__anon09ffe9c10202null97 static const std::vector<PreProcParams> kPreProcParams = [] {
98 std::vector<PreProcParams> result;
99
100 for (const auto targetLevel : kAGCTargetLevels) {
101 for (const auto compLevel : kAGCCompLevels) {
102 AGCParams agcParams = {.targetLevel = targetLevel, .compLevel = compLevel};
103 PreProcParams params = {.uuid = &kAGCUuid, .agcParams = agcParams};
104 result.push_back(params);
105 }
106 }
107
108 for (const auto fixedDigitalGain : kAGC2FixedDigitalGains) {
109 for (const auto adaptDigiLevelEstimator : kAGC2AdaptGigitalLevelEstimators) {
110 for (const auto extraSaturationMargin : kAGC2ExtraSaturationMargins) {
111 AGC2Params agc2Params = {.fixedDigitalGain = fixedDigitalGain,
112 .adaptDigiLevelEstimator = adaptDigiLevelEstimator,
113 .extraSaturationMargin = extraSaturationMargin};
114 PreProcParams params = {.uuid = &kAGC2Uuid, .agc2Params = agc2Params};
115 result.push_back(params);
116 }
117 }
118 }
119
120 for (const auto echoDelay : kAECEchoDelays) {
121 AECParams aecParams = {.echoDelay = echoDelay};
122 PreProcParams params = {.uuid = &kAECUuid, .aecParams = aecParams};
123 result.push_back(params);
124 }
125
126 for (const auto level : kNSLevels) {
127 NSParams nsParams = {.level = level};
128 PreProcParams params = {.uuid = &kNSUuid, .nsParams = nsParams};
129 result.push_back(params);
130 }
131 return result;
132 }();
133
134 static const size_t kNumPreProcParams = std::size(kPreProcParams);
135
setPreProcParams(const effect_uuid_t * uuid,EffectTestHelper & effect,size_t paramIdx)136 void setPreProcParams(const effect_uuid_t* uuid, EffectTestHelper& effect, size_t paramIdx) {
137 const PreProcParams* params = &kPreProcParams[paramIdx];
138 if (isAGCEffect(uuid)) {
139 const AGCParams* agcParams = ¶ms->agcParams;
140 ASSERT_NO_FATAL_FAILURE(effect.setParam(AGC_PARAM_TARGET_LEVEL, agcParams->targetLevel));
141 ASSERT_NO_FATAL_FAILURE(effect.setParam(AGC_PARAM_COMP_GAIN, agcParams->compLevel));
142 } else if (isAGC2Effect(uuid)) {
143 const AGC2Params* agc2Params = ¶ms->agc2Params;
144 ASSERT_NO_FATAL_FAILURE(
145 effect.setParam(AGC2_PARAM_FIXED_DIGITAL_GAIN, agc2Params->fixedDigitalGain));
146 } else if (isAECEffect(uuid)) {
147 const AECParams* aecParams = ¶ms->aecParams;
148 ASSERT_NO_FATAL_FAILURE(effect.setParam(AEC_PARAM_ECHO_DELAY, aecParams->echoDelay));
149 } else if (isNSEffect(uuid)) {
150 const NSParams* nsParams = ¶ms->nsParams;
151 ASSERT_NO_FATAL_FAILURE(effect.setParam(NS_PARAM_LEVEL, nsParams->level));
152 }
153 }
154
155 typedef std::tuple<int, int, int, int> SingleEffectTestParam;
156 class SingleEffectTest : public ::testing::TestWithParam<SingleEffectTestParam> {
157 public:
SingleEffectTest()158 SingleEffectTest()
159 : mSampleRate(EffectTestHelper::kSampleRates[std::get<1>(GetParam())]),
160 mFrameCount(mSampleRate * EffectTestHelper::kTenMilliSecVal),
161 mLoopCount(EffectTestHelper::kLoopCounts[std::get<2>(GetParam())]),
162 mTotalFrameCount(mFrameCount * mLoopCount),
163 mChMask(EffectTestHelper::kChMasks[std::get<0>(GetParam())]),
164 mChannelCount(audio_channel_count_from_in_mask(mChMask)),
165 mParamIdx(std::get<3>(GetParam())),
166 mUuid(kPreProcParams[mParamIdx].uuid){};
167
168 const size_t mSampleRate;
169 const size_t mFrameCount;
170 const size_t mLoopCount;
171 const size_t mTotalFrameCount;
172 const size_t mChMask;
173 const size_t mChannelCount;
174 const size_t mParamIdx;
175 const effect_uuid_t* mUuid;
176 };
177
178 // Tests applying a single effect
TEST_P(SingleEffectTest,SimpleProcess)179 TEST_P(SingleEffectTest, SimpleProcess) {
180 SCOPED_TRACE(testing::Message() << " chMask: " << mChMask << " sampleRate: " << mSampleRate
181 << " loopCount: " << mLoopCount << " paramIdx " << mParamIdx);
182
183 EffectTestHelper effect(mUuid, mChMask, mSampleRate, mLoopCount);
184
185 ASSERT_NO_FATAL_FAILURE(effect.createEffect());
186 ASSERT_NO_FATAL_FAILURE(effect.setConfig(isAECEffect(mUuid)));
187 ASSERT_NO_FATAL_FAILURE(setPreProcParams(mUuid, effect, mParamIdx));
188
189 // Initialize input buffer with deterministic pseudo-random values
190 std::vector<int16_t> input(mTotalFrameCount * mChannelCount);
191 std::vector<int16_t> output(mTotalFrameCount * mChannelCount);
192 std::vector<int16_t> farInput(mTotalFrameCount * mChannelCount);
193 std::minstd_rand gen(mChMask);
194 std::uniform_int_distribution<int16_t> dis(INT16_MIN, INT16_MAX);
195 for (auto& in : input) {
196 in = dis(gen);
197 }
198 if (isAECEffect(mUuid)) {
199 for (auto& farIn : farInput) {
200 farIn = dis(gen);
201 }
202 }
203 ASSERT_NO_FATAL_FAILURE(effect.process(input.data(), output.data(), isAECEffect(mUuid)));
204 if (isAECEffect(mUuid)) {
205 ASSERT_NO_FATAL_FAILURE(effect.process_reverse(farInput.data(), output.data()));
206 }
207 ASSERT_NO_FATAL_FAILURE(effect.releaseEffect());
208 }
209
210 INSTANTIATE_TEST_SUITE_P(
211 PreProcTestAll, SingleEffectTest,
212 ::testing::Combine(::testing::Range(0, (int)EffectTestHelper::kNumChMasks),
213 ::testing::Range(0, (int)EffectTestHelper::kNumSampleRates),
214 ::testing::Range(0, (int)EffectTestHelper::kNumLoopCounts),
215 ::testing::Range(0, (int)kNumPreProcParams)));
216
217 typedef std::tuple<int, int, int> SingleEffectComparisonTestParam;
218 class SingleEffectComparisonTest
219 : public ::testing::TestWithParam<SingleEffectComparisonTestParam> {
220 public:
SingleEffectComparisonTest()221 SingleEffectComparisonTest()
222 : mSampleRate(EffectTestHelper::kSampleRates[std::get<0>(GetParam())]),
223 mFrameCount(mSampleRate * EffectTestHelper::kTenMilliSecVal),
224 mLoopCount(EffectTestHelper::kLoopCounts[std::get<1>(GetParam())]),
225 mTotalFrameCount(mFrameCount * mLoopCount),
226 mParamIdx(std::get<2>(GetParam())),
227 mUuid(kPreProcParams[mParamIdx].uuid){};
228
229 const size_t mSampleRate;
230 const size_t mFrameCount;
231 const size_t mLoopCount;
232 const size_t mTotalFrameCount;
233 const size_t mParamIdx;
234 const effect_uuid_t* mUuid;
235 };
236
237 // Compares first channel in multi-channel output to mono output when same effect is applied
TEST_P(SingleEffectComparisonTest,SimpleProcess)238 TEST_P(SingleEffectComparisonTest, SimpleProcess) {
239 SCOPED_TRACE(testing::Message() << " sampleRate: " << mSampleRate
240 << " loopCount: " << mLoopCount << " paramIdx " << mParamIdx);
241
242 // Initialize mono input buffer with deterministic pseudo-random values
243 std::vector<int16_t> monoInput(mTotalFrameCount);
244 std::vector<int16_t> monoFarInput(mTotalFrameCount);
245
246 std::minstd_rand gen(mSampleRate);
247 std::uniform_int_distribution<int16_t> dis(INT16_MIN, INT16_MAX);
248 for (auto& in : monoInput) {
249 in = dis(gen);
250 }
251 if (isAECEffect(mUuid)) {
252 for (auto& farIn : monoFarInput) {
253 farIn = dis(gen);
254 }
255 }
256
257 // Apply effect on mono channel
258 EffectTestHelper monoEffect(mUuid, AUDIO_CHANNEL_INDEX_MASK_1, mSampleRate, mLoopCount);
259
260 ASSERT_NO_FATAL_FAILURE(monoEffect.createEffect());
261 ASSERT_NO_FATAL_FAILURE(monoEffect.setConfig(isAECEffect(mUuid)));
262 ASSERT_NO_FATAL_FAILURE(setPreProcParams(mUuid, monoEffect, mParamIdx));
263
264 std::vector<int16_t> monoOutput(mTotalFrameCount);
265 ASSERT_NO_FATAL_FAILURE(
266 monoEffect.process(monoInput.data(), monoOutput.data(), isAECEffect(mUuid)));
267 if (isAECEffect(mUuid)) {
268 ASSERT_NO_FATAL_FAILURE(monoEffect.process_reverse(monoFarInput.data(), monoOutput.data()));
269 }
270 ASSERT_NO_FATAL_FAILURE(monoEffect.releaseEffect());
271
272 for (size_t chMask : EffectTestHelper::kChMasks) {
273 size_t channelCount = audio_channel_count_from_in_mask(chMask);
274
275 EffectTestHelper testEffect(mUuid, chMask, mSampleRate, mLoopCount);
276
277 ASSERT_NO_FATAL_FAILURE(testEffect.createEffect());
278 ASSERT_NO_FATAL_FAILURE(testEffect.setConfig(isAECEffect(mUuid)));
279 ASSERT_NO_FATAL_FAILURE(setPreProcParams(mUuid, testEffect, mParamIdx));
280
281 std::vector<int16_t> testInput(mTotalFrameCount * channelCount);
282 std::vector<int16_t> testFarInput(mTotalFrameCount * channelCount);
283
284 // Repeat mono channel data to all the channels
285 // adjust_channels() zero fills channels > 2, hence can't be used here
286 for (size_t i = 0; i < mTotalFrameCount; ++i) {
287 auto* fpInput = &testInput[i * channelCount];
288 std::fill(fpInput, fpInput + channelCount, monoInput[i]);
289 }
290 if (isAECEffect(mUuid)) {
291 for (size_t i = 0; i < mTotalFrameCount; ++i) {
292 auto* fpFarInput = &testFarInput[i * channelCount];
293 std::fill(fpFarInput, fpFarInput + channelCount, monoFarInput[i]);
294 }
295 }
296
297 std::vector<int16_t> testOutput(mTotalFrameCount * channelCount);
298 ASSERT_NO_FATAL_FAILURE(
299 testEffect.process(testInput.data(), testOutput.data(), isAECEffect(mUuid)));
300 if (isAECEffect(mUuid)) {
301 ASSERT_NO_FATAL_FAILURE(
302 testEffect.process_reverse(testFarInput.data(), testOutput.data()));
303 }
304 ASSERT_NO_FATAL_FAILURE(testEffect.releaseEffect());
305
306 // Adjust the test output to mono channel
307 std::vector<int16_t> monoTestOutput(mTotalFrameCount);
308 adjust_channels(testOutput.data(), channelCount, monoTestOutput.data(), FCC_1,
309 sizeof(int16_t), mTotalFrameCount * sizeof(int16_t) * channelCount);
310
311 ASSERT_EQ(0, memcmp(monoOutput.data(), monoTestOutput.data(),
312 mTotalFrameCount * sizeof(int16_t)))
313 << "Mono channel output does not match with reference output \n";
314 }
315 }
316
317 INSTANTIATE_TEST_SUITE_P(
318 PreProcTestAll, SingleEffectComparisonTest,
319 ::testing::Combine(::testing::Range(0, (int)EffectTestHelper::kNumSampleRates),
320 ::testing::Range(0, (int)EffectTestHelper::kNumLoopCounts),
321 ::testing::Range(0, (int)kNumPreProcParams)));
322
main(int argc,char ** argv)323 int main(int argc, char** argv) {
324 ::testing::InitGoogleTest(&argc, argv);
325 int status = RUN_ALL_TESTS();
326 ALOGV("Test result = %d", status);
327 return status;
328 }
329