1 /*
2 * Copyright (C) 2022 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 <limits.h>
18
19 #define LOG_TAG "VtsHalBassBoostTest"
20 #include <aidl/Vintf.h>
21 #include <android-base/logging.h>
22 #include "EffectHelper.h"
23 #include "pffft.hpp"
24
25 using namespace android;
26
27 using aidl::android::hardware::audio::common::getChannelCount;
28 using aidl::android::hardware::audio::effect::BassBoost;
29 using aidl::android::hardware::audio::effect::Capability;
30 using aidl::android::hardware::audio::effect::Descriptor;
31 using aidl::android::hardware::audio::effect::getEffectTypeUuidBassBoost;
32 using aidl::android::hardware::audio::effect::IEffect;
33 using aidl::android::hardware::audio::effect::IFactory;
34 using aidl::android::hardware::audio::effect::Parameter;
35 using aidl::android::hardware::audio::effect::Range;
36 using android::hardware::audio::common::testing::detail::TestExecutionTracer;
37
38 // minimal HAL interface version to run bassboost data path test
39 constexpr int32_t kMinDataTestHalVersion = 2;
40 static const std::vector<int32_t> kLayouts = {AudioChannelLayout::LAYOUT_STEREO,
41 AudioChannelLayout::LAYOUT_MONO};
42 /*
43 * Testing parameter range, assuming the parameter supported by effect is in this range.
44 * Parameter should be within the valid range defined in the documentation,
45 * for any supported value test expects EX_NONE from IEffect.setParameter(),
46 * otherwise expect EX_ILLEGAL_ARGUMENT.
47 */
48
49 class BassBoostEffectHelper : public EffectHelper {
50 public:
SetUpBassBoost(int32_t layout=AudioChannelLayout::LAYOUT_STEREO)51 void SetUpBassBoost(int32_t layout = AudioChannelLayout::LAYOUT_STEREO) {
52 ASSERT_NE(nullptr, mFactory);
53 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
54 setFrameCounts(layout);
55
56 AudioChannelLayout channelLayout =
57 AudioChannelLayout::make<AudioChannelLayout::layoutMask>(layout);
58
59 Parameter::Specific specific = getDefaultParamSpecific();
60 Parameter::Common common = createParamCommon(
61 0 /* session */, 1 /* ioHandle */, kSamplingFrequency /* iSampleRate */,
62 kSamplingFrequency /* oSampleRate */, mInputFrameCount /* iFrameCount */,
63 mOutputFrameCount /* oFrameCount */, channelLayout, channelLayout);
64 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &mOpenEffectReturn, EX_NONE));
65 ASSERT_NE(nullptr, mEffect);
66 }
67
TearDownBassBoost()68 void TearDownBassBoost() {
69 ASSERT_NO_FATAL_FAILURE(close(mEffect));
70 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
71 mOpenEffectReturn = IEffect::OpenEffectReturn{};
72 }
73
getDefaultParamSpecific()74 Parameter::Specific getDefaultParamSpecific() {
75 BassBoost bb = BassBoost::make<BassBoost::strengthPm>(0);
76 Parameter::Specific specific =
77 Parameter::Specific::make<Parameter::Specific::bassBoost>(bb);
78 return specific;
79 }
80
setFrameCounts(int32_t inputBufferLayout)81 void setFrameCounts(int32_t inputBufferLayout) {
82 int channelCount = getChannelCount(
83 AudioChannelLayout::make<AudioChannelLayout::layoutMask>(inputBufferLayout));
84 mInputFrameCount = kInputSize / channelCount;
85 mOutputFrameCount = kInputSize / channelCount;
86 }
87
createBassBoostParam(int strength)88 Parameter createBassBoostParam(int strength) {
89 return Parameter::make<Parameter::specific>(
90 Parameter::Specific::make<Parameter::Specific::bassBoost>(
91 BassBoost::make<BassBoost::strengthPm>(strength)));
92 }
93
isStrengthValid(int strength)94 bool isStrengthValid(int strength) {
95 auto bb = BassBoost::make<BassBoost::strengthPm>(strength);
96 return isParameterValid<BassBoost, Range::bassBoost>(bb, mDescriptor);
97 }
98
setAndVerifyParameters(int strength,binder_exception_t expected)99 void setAndVerifyParameters(int strength, binder_exception_t expected) {
100 auto expectedParam = createBassBoostParam(strength);
101 EXPECT_STATUS(expected, mEffect->setParameter(expectedParam)) << expectedParam.toString();
102
103 if (expected == EX_NONE) {
104 auto bbId = BassBoost::Id::make<BassBoost::Id::commonTag>(
105 BassBoost::Tag(BassBoost::strengthPm));
106 auto id = Parameter::Id::make<Parameter::Id::bassBoostTag>(bbId);
107 // get parameter
108 Parameter getParam;
109 // if set success, then get should match
110 EXPECT_STATUS(expected, mEffect->getParameter(id, &getParam));
111 EXPECT_EQ(expectedParam, getParam) << "\nexpectedParam:" << expectedParam.toString()
112 << "\ngetParam:" << getParam.toString();
113 }
114 }
115
116 static constexpr int kSamplingFrequency = 44100;
117 static constexpr int kDurationMilliSec = 720;
118 static constexpr int kInputSize = kSamplingFrequency * kDurationMilliSec / 1000;
119 long mInputFrameCount, mOutputFrameCount;
120 std::shared_ptr<IFactory> mFactory;
121 Descriptor mDescriptor;
122 std::shared_ptr<IEffect> mEffect;
123 IEffect::OpenEffectReturn mOpenEffectReturn;
124 };
125
126 /**
127 * Here we focus on specific parameter checking, general IEffect interfaces testing performed in
128 * VtsAudioEffectTargetTest.
129 */
130 enum ParamName { PARAM_INSTANCE_NAME, PARAM_STRENGTH };
131 using BassBoostParamTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int>;
132
133 class BassBoostParamTest : public ::testing::TestWithParam<BassBoostParamTestParam>,
134 public BassBoostEffectHelper {
135 public:
BassBoostParamTest()136 BassBoostParamTest() : mParamStrength(std::get<PARAM_STRENGTH>(GetParam())) {
137 std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
138 }
139
SetUp()140 void SetUp() override { ASSERT_NO_FATAL_FAILURE(SetUpBassBoost()); }
TearDown()141 void TearDown() override { TearDownBassBoost(); }
142
143 int mParamStrength = 0;
144 };
145
TEST_P(BassBoostParamTest,SetAndGetStrength)146 TEST_P(BassBoostParamTest, SetAndGetStrength) {
147 if (isStrengthValid(mParamStrength)) {
148 ASSERT_NO_FATAL_FAILURE(setAndVerifyParameters(mParamStrength, EX_NONE));
149 } else {
150 ASSERT_NO_FATAL_FAILURE(setAndVerifyParameters(mParamStrength, EX_ILLEGAL_ARGUMENT));
151 }
152 }
153
154 enum DataParamName { DATA_INSTANCE_NAME, DATA_LAYOUT };
155
156 using BassBoostDataTestParam =
157 std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int32_t>;
158
159 class BassBoostDataTest : public ::testing::TestWithParam<BassBoostDataTestParam>,
160 public BassBoostEffectHelper {
161 public:
BassBoostDataTest()162 BassBoostDataTest() : mChannelLayout(std::get<DATA_LAYOUT>(GetParam())) {
163 std::tie(mFactory, mDescriptor) = std::get<DATA_INSTANCE_NAME>(GetParam());
164 mStrengthValues = getTestValueSet<BassBoost, int, Range::bassBoost, BassBoost::strengthPm>(
165 {std::get<DATA_INSTANCE_NAME>(GetParam())}, expandTestValueBasic<int>);
166 }
167
SetUp()168 void SetUp() override {
169 SKIP_TEST_IF_DATA_UNSUPPORTED(mDescriptor.common.flags);
170 ASSERT_NO_FATAL_FAILURE(SetUpBassBoost(mChannelLayout));
171 if (int32_t version;
172 mEffect->getInterfaceVersion(&version).isOk() && version < kMinDataTestHalVersion) {
173 GTEST_SKIP() << "Skipping the data test for version: " << version << "\n";
174 }
175 }
176
TearDown()177 void TearDown() override {
178 SKIP_TEST_IF_DATA_UNSUPPORTED(mDescriptor.common.flags);
179 TearDownBassBoost();
180 }
181
182 // Find FFT bin indices for testFrequencies and get bin center frequencies
roundToFreqCenteredToFftBin(std::vector<int> & testFrequencies,std::vector<int> & binOffsets)183 void roundToFreqCenteredToFftBin(std::vector<int>& testFrequencies,
184 std::vector<int>& binOffsets) {
185 for (size_t i = 0; i < testFrequencies.size(); i++) {
186 binOffsets[i] = std::round(testFrequencies[i] / kBinWidth);
187 testFrequencies[i] = std::round(binOffsets[i] * kBinWidth);
188 }
189 }
190
191 // Generate multitone input between -1 to +1 using testFrequencies
generateMultiTone(const std::vector<int> & testFrequencies,std::vector<float> & input)192 void generateMultiTone(const std::vector<int>& testFrequencies, std::vector<float>& input) {
193 for (auto i = 0; i < kInputSize; i++) {
194 input[i] = 0;
195
196 for (size_t j = 0; j < testFrequencies.size(); j++) {
197 input[i] += sin(2 * M_PI * testFrequencies[j] * i / kSamplingFrequency);
198 }
199 input[i] /= testFrequencies.size();
200 }
201 }
202
203 // Use FFT transform to convert the buffer to frequency domain
204 // Compute its magnitude at binOffsets
calculateMagnitude(const std::vector<float> & buffer,const std::vector<int> & binOffsets)205 std::vector<float> calculateMagnitude(const std::vector<float>& buffer,
206 const std::vector<int>& binOffsets) {
207 std::vector<float> fftInput(kNPointFFT);
208 PFFFT_Setup* inputHandle = pffft_new_setup(kNPointFFT, PFFFT_REAL);
209 pffft_transform_ordered(inputHandle, buffer.data(), fftInput.data(), nullptr,
210 PFFFT_FORWARD);
211 pffft_destroy_setup(inputHandle);
212 std::vector<float> bufferMag(binOffsets.size());
213 for (size_t i = 0; i < binOffsets.size(); i++) {
214 size_t k = binOffsets[i];
215 bufferMag[i] = sqrt((fftInput[k * 2] * fftInput[k * 2]) +
216 (fftInput[k * 2 + 1] * fftInput[k * 2 + 1]));
217 }
218
219 return bufferMag;
220 }
221
222 // Calculate gain difference between low frequency and high frequency magnitude
calculateGainDiff(const std::vector<float> & inputMag,const std::vector<float> & outputMag)223 float calculateGainDiff(const std::vector<float>& inputMag,
224 const std::vector<float>& outputMag) {
225 std::vector<float> gains(inputMag.size());
226
227 for (size_t i = 0; i < inputMag.size(); i++) {
228 gains[i] = 20 * log10(outputMag[i] / inputMag[i]);
229 }
230
231 return gains[0] - gains[1];
232 }
233
234 static constexpr int kNPointFFT = 16384;
235 static constexpr float kBinWidth = (float)kSamplingFrequency / kNPointFFT;
236 std::set<int> mStrengthValues;
237 int32_t mChannelLayout;
238 };
239
TEST_P(BassBoostDataTest,IncreasingStrength)240 TEST_P(BassBoostDataTest, IncreasingStrength) {
241 // Frequencies to generate multitone input
242 std::vector<int> testFrequencies = {100, 1000};
243
244 // FFT bin indices for testFrequencies
245 std::vector<int> binOffsets(testFrequencies.size());
246
247 std::vector<float> input(kInputSize);
248 std::vector<float> baseOutput(kInputSize);
249
250 std::vector<float> inputMag(testFrequencies.size());
251 float prevGain = -100;
252
253 roundToFreqCenteredToFftBin(testFrequencies, binOffsets);
254
255 generateMultiTone(testFrequencies, input);
256
257 inputMag = calculateMagnitude(input, binOffsets);
258
259 if (isStrengthValid(0)) {
260 ASSERT_NO_FATAL_FAILURE(setAndVerifyParameters(0, EX_NONE));
261 } else {
262 GTEST_SKIP() << "Strength not supported, skipping the test\n";
263 }
264
265 ASSERT_NO_FATAL_FAILURE(
266 processAndWriteToOutput(input, baseOutput, mEffect, &mOpenEffectReturn));
267
268 std::vector<float> baseMag(testFrequencies.size());
269 baseMag = calculateMagnitude(baseOutput, binOffsets);
270 float baseDiff = calculateGainDiff(inputMag, baseMag);
271
272 for (int strength : mStrengthValues) {
273 // Skipping the further steps for invalid strength values
274 if (!isStrengthValid(strength)) {
275 continue;
276 }
277
278 ASSERT_NO_FATAL_FAILURE(setAndVerifyParameters(strength, EX_NONE));
279
280 std::vector<float> output(kInputSize);
281 std::vector<float> outputMag(testFrequencies.size());
282
283 ASSERT_NO_FATAL_FAILURE(
284 processAndWriteToOutput(input, output, mEffect, &mOpenEffectReturn));
285
286 outputMag = calculateMagnitude(output, binOffsets);
287 float diff = calculateGainDiff(inputMag, outputMag);
288
289 ASSERT_GT(diff, prevGain);
290 ASSERT_GT(diff, baseDiff);
291 prevGain = diff;
292 }
293 }
294
295 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair;
296 INSTANTIATE_TEST_SUITE_P(
297 BassBoostTest, BassBoostParamTest,
298 ::testing::Combine(
299 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
300 IFactory::descriptor, getEffectTypeUuidBassBoost())),
301 testing::ValuesIn(EffectHelper::getTestValueSet<BassBoost, int, Range::bassBoost,
302 BassBoost::strengthPm>(
303 kDescPair, EffectHelper::expandTestValueBasic<int>))),
__anone722a2130102(const testing::TestParamInfo<BassBoostParamTest::ParamType>& info) 304 [](const testing::TestParamInfo<BassBoostParamTest::ParamType>& info) {
305 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
306 std::string strength = std::to_string(std::get<PARAM_STRENGTH>(info.param));
307 std::string name = getPrefix(descriptor) + "_strength_" + strength;
308 std::replace_if(
309 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
310 return name;
311 });
312
313 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BassBoostParamTest);
314
315 INSTANTIATE_TEST_SUITE_P(
316 BassBoostTest, BassBoostDataTest,
317 ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
318 IFactory::descriptor, getEffectTypeUuidBassBoost())),
319 testing::ValuesIn(kLayouts)),
__anone722a2130302(const testing::TestParamInfo<BassBoostDataTest::ParamType>& info) 320 [](const testing::TestParamInfo<BassBoostDataTest::ParamType>& info) {
321 auto descriptor = std::get<DATA_INSTANCE_NAME>(info.param).second;
322 std::string layout = std::to_string(std::get<DATA_LAYOUT>(info.param));
323 std::string name = getPrefix(descriptor) + "_layout_" + layout;
324 std::replace_if(
325 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
326 return name;
327 });
328
329 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BassBoostDataTest);
330
main(int argc,char ** argv)331 int main(int argc, char** argv) {
332 ::testing::InitGoogleTest(&argc, argv);
333 ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
334 ABinderProcess_setThreadPoolMaxThreadCount(1);
335 ABinderProcess_startThreadPool();
336 return RUN_ALL_TESTS();
337 }
338