Rivukanta Bhattacharya | b9116da | 2021-03-11 04:32:54 +0530 | [diff] [blame^] | 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 | |
| 40 | static bool isAGCEffect(const effect_uuid_t* uuid) { |
| 41 | return uuid == &kAGCUuid; |
| 42 | } |
| 43 | static bool isAGC2Effect(const effect_uuid_t* uuid) { |
| 44 | return uuid == &kAGC2Uuid; |
| 45 | } |
| 46 | static bool isAECEffect(const effect_uuid_t* uuid) { |
| 47 | return uuid == &kAECUuid; |
| 48 | } |
| 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 |
| 97 | 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 | |
| 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 | ASSERT_NO_FATAL_FAILURE(effect.setParam(AGC2_PARAM_ADAPT_DIGI_LEVEL_ESTIMATOR, |
| 147 | agc2Params->adaptDigiLevelEstimator)); |
| 148 | ASSERT_NO_FATAL_FAILURE(effect.setParam(AGC2_PARAM_ADAPT_DIGI_EXTRA_SATURATION_MARGIN, |
| 149 | agc2Params->extraSaturationMargin)); |
| 150 | } else if (isAECEffect(uuid)) { |
| 151 | const AECParams* aecParams = ¶ms->aecParams; |
| 152 | ASSERT_NO_FATAL_FAILURE(effect.setParam(AEC_PARAM_ECHO_DELAY, aecParams->echoDelay)); |
| 153 | } else if (isNSEffect(uuid)) { |
| 154 | const NSParams* nsParams = ¶ms->nsParams; |
| 155 | ASSERT_NO_FATAL_FAILURE(effect.setParam(NS_PARAM_LEVEL, nsParams->level)); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | typedef std::tuple<int, int, int, int> SingleEffectTestParam; |
| 160 | class SingleEffectTest : public ::testing::TestWithParam<SingleEffectTestParam> { |
| 161 | public: |
| 162 | SingleEffectTest() |
| 163 | : mSampleRate(EffectTestHelper::kSampleRates[std::get<1>(GetParam())]), |
| 164 | mFrameCount(mSampleRate * EffectTestHelper::kTenMilliSecVal), |
| 165 | mLoopCount(EffectTestHelper::kLoopCounts[std::get<2>(GetParam())]), |
| 166 | mTotalFrameCount(mFrameCount * mLoopCount), |
| 167 | mChMask(EffectTestHelper::kChMasks[std::get<0>(GetParam())]), |
| 168 | mChannelCount(audio_channel_count_from_in_mask(mChMask)), |
| 169 | mParamIdx(std::get<3>(GetParam())), |
| 170 | mUuid(kPreProcParams[mParamIdx].uuid){}; |
| 171 | |
| 172 | const size_t mSampleRate; |
| 173 | const size_t mFrameCount; |
| 174 | const size_t mLoopCount; |
| 175 | const size_t mTotalFrameCount; |
| 176 | const size_t mChMask; |
| 177 | const size_t mChannelCount; |
| 178 | const size_t mParamIdx; |
| 179 | const effect_uuid_t* mUuid; |
| 180 | }; |
| 181 | |
| 182 | // Tests applying a single effect |
| 183 | TEST_P(SingleEffectTest, SimpleProcess) { |
| 184 | SCOPED_TRACE(testing::Message() << " chMask: " << mChMask << " sampleRate: " << mSampleRate |
| 185 | << " loopCount: " << mLoopCount << " paramIdx " << mParamIdx); |
| 186 | |
| 187 | EffectTestHelper effect(mUuid, mChMask, mSampleRate, mLoopCount); |
| 188 | |
| 189 | ASSERT_NO_FATAL_FAILURE(effect.createEffect()); |
| 190 | ASSERT_NO_FATAL_FAILURE(effect.setConfig(isAECEffect(mUuid))); |
| 191 | ASSERT_NO_FATAL_FAILURE(setPreProcParams(mUuid, effect, mParamIdx)); |
| 192 | |
| 193 | // Initialize input buffer with deterministic pseudo-random values |
| 194 | std::vector<int16_t> input(mTotalFrameCount * mChannelCount); |
| 195 | std::vector<int16_t> output(mTotalFrameCount * mChannelCount); |
| 196 | std::vector<int16_t> farInput(mTotalFrameCount * mChannelCount); |
| 197 | std::minstd_rand gen(mChMask); |
| 198 | std::uniform_int_distribution<int16_t> dis(INT16_MIN, INT16_MAX); |
| 199 | for (auto& in : input) { |
| 200 | in = dis(gen); |
| 201 | } |
| 202 | if (isAECEffect(mUuid)) { |
| 203 | for (auto& farIn : farInput) { |
| 204 | farIn = dis(gen); |
| 205 | } |
| 206 | } |
| 207 | ASSERT_NO_FATAL_FAILURE(effect.process(input.data(), output.data(), isAECEffect(mUuid))); |
| 208 | if (isAECEffect(mUuid)) { |
| 209 | ASSERT_NO_FATAL_FAILURE(effect.process_reverse(farInput.data(), output.data())); |
| 210 | } |
| 211 | ASSERT_NO_FATAL_FAILURE(effect.releaseEffect()); |
| 212 | } |
| 213 | |
| 214 | INSTANTIATE_TEST_SUITE_P( |
| 215 | PreProcTestAll, SingleEffectTest, |
| 216 | ::testing::Combine(::testing::Range(0, (int)EffectTestHelper::kNumChMasks), |
| 217 | ::testing::Range(0, (int)EffectTestHelper::kNumSampleRates), |
| 218 | ::testing::Range(0, (int)EffectTestHelper::kNumLoopCounts), |
| 219 | ::testing::Range(0, (int)kNumPreProcParams))); |
| 220 | |
| 221 | typedef std::tuple<int, int, int> SingleEffectComparisonTestParam; |
| 222 | class SingleEffectComparisonTest |
| 223 | : public ::testing::TestWithParam<SingleEffectComparisonTestParam> { |
| 224 | public: |
| 225 | SingleEffectComparisonTest() |
| 226 | : mSampleRate(EffectTestHelper::kSampleRates[std::get<0>(GetParam())]), |
| 227 | mFrameCount(mSampleRate * EffectTestHelper::kTenMilliSecVal), |
| 228 | mLoopCount(EffectTestHelper::kLoopCounts[std::get<1>(GetParam())]), |
| 229 | mTotalFrameCount(mFrameCount * mLoopCount), |
| 230 | mParamIdx(std::get<2>(GetParam())), |
| 231 | mUuid(kPreProcParams[mParamIdx].uuid){}; |
| 232 | |
| 233 | const size_t mSampleRate; |
| 234 | const size_t mFrameCount; |
| 235 | const size_t mLoopCount; |
| 236 | const size_t mTotalFrameCount; |
| 237 | const size_t mParamIdx; |
| 238 | const effect_uuid_t* mUuid; |
| 239 | }; |
| 240 | |
| 241 | // Compares first channel in multi-channel output to mono output when same effect is applied |
| 242 | TEST_P(SingleEffectComparisonTest, SimpleProcess) { |
| 243 | SCOPED_TRACE(testing::Message() << " sampleRate: " << mSampleRate |
| 244 | << " loopCount: " << mLoopCount << " paramIdx " << mParamIdx); |
| 245 | |
| 246 | // Initialize mono input buffer with deterministic pseudo-random values |
| 247 | std::vector<int16_t> monoInput(mTotalFrameCount); |
| 248 | std::vector<int16_t> monoFarInput(mTotalFrameCount); |
| 249 | |
| 250 | std::minstd_rand gen(mSampleRate); |
| 251 | std::uniform_int_distribution<int16_t> dis(INT16_MIN, INT16_MAX); |
| 252 | for (auto& in : monoInput) { |
| 253 | in = dis(gen); |
| 254 | } |
| 255 | if (isAECEffect(mUuid)) { |
| 256 | for (auto& farIn : monoFarInput) { |
| 257 | farIn = dis(gen); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // Apply effect on mono channel |
| 262 | EffectTestHelper monoEffect(mUuid, AUDIO_CHANNEL_INDEX_MASK_1, mSampleRate, mLoopCount); |
| 263 | |
| 264 | ASSERT_NO_FATAL_FAILURE(monoEffect.createEffect()); |
| 265 | ASSERT_NO_FATAL_FAILURE(monoEffect.setConfig(isAECEffect(mUuid))); |
| 266 | ASSERT_NO_FATAL_FAILURE(setPreProcParams(mUuid, monoEffect, mParamIdx)); |
| 267 | |
| 268 | std::vector<int16_t> monoOutput(mTotalFrameCount); |
| 269 | ASSERT_NO_FATAL_FAILURE( |
| 270 | monoEffect.process(monoInput.data(), monoOutput.data(), isAECEffect(mUuid))); |
| 271 | if (isAECEffect(mUuid)) { |
| 272 | ASSERT_NO_FATAL_FAILURE(monoEffect.process_reverse(monoFarInput.data(), monoOutput.data())); |
| 273 | } |
| 274 | ASSERT_NO_FATAL_FAILURE(monoEffect.releaseEffect()); |
| 275 | |
| 276 | for (size_t chMask : EffectTestHelper::kChMasks) { |
| 277 | size_t channelCount = audio_channel_count_from_in_mask(chMask); |
| 278 | |
| 279 | EffectTestHelper testEffect(mUuid, chMask, mSampleRate, mLoopCount); |
| 280 | |
| 281 | ASSERT_NO_FATAL_FAILURE(testEffect.createEffect()); |
| 282 | ASSERT_NO_FATAL_FAILURE(testEffect.setConfig(isAECEffect(mUuid))); |
| 283 | ASSERT_NO_FATAL_FAILURE(setPreProcParams(mUuid, testEffect, mParamIdx)); |
| 284 | |
| 285 | std::vector<int16_t> testInput(mTotalFrameCount * channelCount); |
| 286 | std::vector<int16_t> testFarInput(mTotalFrameCount * channelCount); |
| 287 | |
| 288 | // Repeat mono channel data to all the channels |
| 289 | // adjust_channels() zero fills channels > 2, hence can't be used here |
| 290 | for (size_t i = 0; i < mTotalFrameCount; ++i) { |
| 291 | auto* fpInput = &testInput[i * channelCount]; |
| 292 | std::fill(fpInput, fpInput + channelCount, monoInput[i]); |
| 293 | } |
| 294 | if (isAECEffect(mUuid)) { |
| 295 | for (size_t i = 0; i < mTotalFrameCount; ++i) { |
| 296 | auto* fpFarInput = &testFarInput[i * channelCount]; |
| 297 | std::fill(fpFarInput, fpFarInput + channelCount, monoFarInput[i]); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | std::vector<int16_t> testOutput(mTotalFrameCount * channelCount); |
| 302 | ASSERT_NO_FATAL_FAILURE( |
| 303 | testEffect.process(testInput.data(), testOutput.data(), isAECEffect(mUuid))); |
| 304 | if (isAECEffect(mUuid)) { |
| 305 | ASSERT_NO_FATAL_FAILURE( |
| 306 | testEffect.process_reverse(testFarInput.data(), testOutput.data())); |
| 307 | } |
| 308 | ASSERT_NO_FATAL_FAILURE(testEffect.releaseEffect()); |
| 309 | |
| 310 | // Adjust the test output to mono channel |
| 311 | std::vector<int16_t> monoTestOutput(mTotalFrameCount); |
| 312 | adjust_channels(testOutput.data(), channelCount, monoTestOutput.data(), FCC_1, |
| 313 | sizeof(int16_t), mTotalFrameCount * sizeof(int16_t) * channelCount); |
| 314 | |
| 315 | ASSERT_EQ(0, memcmp(monoOutput.data(), monoTestOutput.data(), |
| 316 | mTotalFrameCount * sizeof(int16_t))) |
| 317 | << "Mono channel output does not match with reference output \n"; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | INSTANTIATE_TEST_SUITE_P( |
| 322 | PreProcTestAll, SingleEffectComparisonTest, |
| 323 | ::testing::Combine(::testing::Range(0, (int)EffectTestHelper::kNumSampleRates), |
| 324 | ::testing::Range(0, (int)EffectTestHelper::kNumLoopCounts), |
| 325 | ::testing::Range(0, (int)kNumPreProcParams))); |
| 326 | |
| 327 | int main(int argc, char** argv) { |
| 328 | ::testing::InitGoogleTest(&argc, argv); |
| 329 | int status = RUN_ALL_TESTS(); |
| 330 | ALOGV("Test result = %d", status); |
| 331 | return status; |
| 332 | } |