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 #define LOG_TAG "VtsHalVirtualizerTest"
18 #include <android-base/logging.h>
19 #include <audio_utils/power.h>
20 #include <system/audio.h>
21
22 #include "EffectHelper.h"
23
24 using namespace android;
25
26 using aidl::android::hardware::audio::common::getChannelCount;
27 using aidl::android::hardware::audio::effect::Descriptor;
28 using aidl::android::hardware::audio::effect::getEffectTypeUuidVirtualizer;
29 using aidl::android::hardware::audio::effect::IEffect;
30 using aidl::android::hardware::audio::effect::IFactory;
31 using aidl::android::hardware::audio::effect::Parameter;
32 using aidl::android::hardware::audio::effect::Virtualizer;
33 using android::hardware::audio::common::testing::detail::TestExecutionTracer;
34
35 class VirtualizerHelper : public EffectHelper {
36 public:
SetUpVirtualizer()37 void SetUpVirtualizer() {
38 ASSERT_NE(nullptr, mFactory);
39 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
40 initFrameCount();
41 Parameter::Specific specific = getDefaultParamSpecific();
42 Parameter::Common common = createParamCommon(
43 0 /* session */, 1 /* ioHandle */, kSamplingFrequency /* iSampleRate */,
44 kSamplingFrequency /* oSampleRate */, mInputFrameCount /* iFrameCount */,
45 mInputFrameCount /* oFrameCount */);
46 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &mOpenEffectReturn, EX_NONE));
47 ASSERT_NE(nullptr, mEffect);
48 }
49
TearDownVirtualizer()50 void TearDownVirtualizer() {
51 ASSERT_NO_FATAL_FAILURE(close(mEffect));
52 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
53 mOpenEffectReturn = IEffect::OpenEffectReturn{};
54 }
55
getDefaultParamSpecific()56 Parameter::Specific getDefaultParamSpecific() {
57 Virtualizer vr = Virtualizer::make<Virtualizer::strengthPm>(0);
58 Parameter::Specific specific =
59 Parameter::Specific::make<Parameter::Specific::virtualizer>(vr);
60 return specific;
61 }
62
createVirtualizerStrengthParam(int param)63 Parameter createVirtualizerStrengthParam(int param) {
64 return Parameter::make<Parameter::specific>(
65 Parameter::Specific::make<Parameter::Specific::virtualizer>(
66 Virtualizer::make<Virtualizer::strengthPm>(param)));
67 }
68
initFrameCount()69 void initFrameCount() {
70 mInputFrameCount = kBufferSize / kChannelCount;
71 mOutputFrameCount = kBufferSize / kChannelCount;
72 }
73
isStrengthValid(int level)74 bool isStrengthValid(int level) {
75 auto vir = Virtualizer::make<Virtualizer::strengthPm>(level);
76 return isParameterValid<Virtualizer, Range::virtualizer>(vir, mDescriptor);
77 }
78
setAndVerifyStrength(int param,binder_exception_t expected)79 void setAndVerifyStrength(int param, binder_exception_t expected) {
80 auto expectedParam = createVirtualizerStrengthParam(param);
81 EXPECT_STATUS(expected, mEffect->setParameter(expectedParam)) << expectedParam.toString();
82
83 if (expected == EX_NONE) {
84 Virtualizer::Id vrlId =
85 Virtualizer::Id::make<Virtualizer::Id::commonTag>(Virtualizer::strengthPm);
86
87 auto id = Parameter::Id::make<Parameter::Id::virtualizerTag>(vrlId);
88 // get parameter
89 Parameter getParam;
90 // if set success, then get should match
91 EXPECT_STATUS(expected, mEffect->getParameter(id, &getParam));
92 EXPECT_EQ(expectedParam, getParam) << "\nexpectedParam:" << expectedParam.toString()
93 << "\ngetParam:" << getParam.toString();
94 }
95 }
96
97 static constexpr int kSamplingFrequency = 44100;
98 static constexpr int kDefaultChannelLayout = AudioChannelLayout::LAYOUT_STEREO;
99 static constexpr int kDurationMilliSec = 720;
100 static constexpr int kBufferSize = kSamplingFrequency * kDurationMilliSec / 1000;
101 int kChannelCount = getChannelCount(
102 AudioChannelLayout::make<AudioChannelLayout::layoutMask>(kDefaultChannelLayout));
103 long mInputFrameCount;
104 long mOutputFrameCount;
105 std::shared_ptr<IFactory> mFactory;
106 std::shared_ptr<IEffect> mEffect;
107 IEffect::OpenEffectReturn mOpenEffectReturn;
108 Descriptor mDescriptor;
109 };
110
111 /**
112 * Here we focus on specific parameter checking, general IEffect interfaces testing performed in
113 * VtsAudioEffectTargetTest.
114 */
115 enum ParamName { PARAM_INSTANCE_NAME, PARAM_STRENGTH };
116 using VirtualizerParamTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int>;
117
118 /*
119 * Testing parameter range, assuming the parameter supported by effect is in this range.
120 * Parameter should be within the valid range defined in the documentation,
121 * for any supported value test expects EX_NONE from IEffect.setParameter(),
122 * otherwise expect EX_ILLEGAL_ARGUMENT.
123 */
124
125 class VirtualizerParamTest : public ::testing::TestWithParam<VirtualizerParamTestParam>,
126 public VirtualizerHelper {
127 public:
VirtualizerParamTest()128 VirtualizerParamTest() : mParamStrength(std::get<PARAM_STRENGTH>(GetParam())) {
129 std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
130 }
SetUp()131 void SetUp() override { ASSERT_NO_FATAL_FAILURE(SetUpVirtualizer()); }
TearDown()132 void TearDown() override { TearDownVirtualizer(); }
133
134 int mParamStrength = 0;
135 };
136
TEST_P(VirtualizerParamTest,SetAndGetStrength)137 TEST_P(VirtualizerParamTest, SetAndGetStrength) {
138 ASSERT_NO_FATAL_FAILURE(setAndVerifyStrength(
139 mParamStrength, isStrengthValid(mParamStrength) ? EX_NONE : EX_ILLEGAL_ARGUMENT));
140 }
141
142 enum ProcessTestParam { PROCESS_INSTANCE_NAME, PROCESS_ZERO_INPUT };
143 using VirtualizerProcessTestParam =
144 std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, bool>;
145
146 class VirtualizerProcessTest : public ::testing::TestWithParam<VirtualizerProcessTestParam>,
147 public VirtualizerHelper {
148 public:
VirtualizerProcessTest()149 VirtualizerProcessTest() : mZeroInput(std::get<PROCESS_ZERO_INPUT>(GetParam())) {
150 std::tie(mFactory, mDescriptor) = std::get<PROCESS_INSTANCE_NAME>(GetParam());
151 }
152
SetUp()153 void SetUp() override {
154 SKIP_TEST_IF_DATA_UNSUPPORTED(mDescriptor.common.flags);
155 ASSERT_NO_FATAL_FAILURE(SetUpVirtualizer());
156 }
157
TearDown()158 void TearDown() override {
159 SKIP_TEST_IF_DATA_UNSUPPORTED(mDescriptor.common.flags);
160 ASSERT_NO_FATAL_FAILURE(TearDownVirtualizer());
161 }
162
generateInput(std::vector<float> & buffer)163 void generateInput(std::vector<float>& buffer) {
164 if (mZeroInput) {
165 std::fill(buffer.begin(), buffer.end(), 0);
166 } else {
167 int frequency = 100;
168 for (size_t i = 0; i < buffer.size(); i++) {
169 buffer[i] = sin(2 * M_PI * frequency * i / kSamplingFrequency);
170 }
171 }
172 }
173
174 static constexpr float kAbsError = 0.00001;
175 bool mZeroInput;
176 };
177
TEST_P(VirtualizerProcessTest,IncreasingStrength)178 TEST_P(VirtualizerProcessTest, IncreasingStrength) {
179 std::vector<float> input(kBufferSize);
180 std::vector<float> output(kBufferSize);
181 std::vector<int> strengths = {250, 500, 750, 1000};
182
183 generateInput(input);
184
185 const float inputRmse =
186 audio_utils_compute_energy_mono(input.data(), AUDIO_FORMAT_PCM_FLOAT, input.size());
187
188 for (int strength : strengths) {
189 // Skipping the further steps for unnsupported Strength values
190 if (!isStrengthValid(strength)) {
191 continue;
192 }
193 setAndVerifyStrength(strength, EX_NONE);
194 ASSERT_NO_FATAL_FAILURE(
195 processAndWriteToOutput(input, output, mEffect, &mOpenEffectReturn));
196
197 const float outputRmse = audio_utils_compute_energy_mono(
198 output.data(), AUDIO_FORMAT_PCM_FLOAT, output.size());
199
200 if (inputRmse != 0) {
201 EXPECT_NE(outputRmse, 0);
202 if (strength != 0) {
203 EXPECT_GT(abs(outputRmse - inputRmse), kAbsError);
204 }
205 } else {
206 EXPECT_NEAR(outputRmse, inputRmse, kAbsError);
207 }
208 }
209 }
210
211 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair;
212 INSTANTIATE_TEST_SUITE_P(
213 VirtualizerTest, VirtualizerParamTest,
214 ::testing::Combine(
215 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
216 IFactory::descriptor, getEffectTypeUuidVirtualizer())),
217 testing::ValuesIn(EffectHelper::getTestValueSet<
218 Virtualizer, int, Range::virtualizer, Virtualizer::strengthPm>(
219 kDescPair, EffectHelper::expandTestValueBasic<int>))),
__anon06a6f7e40102(const testing::TestParamInfo<VirtualizerParamTest::ParamType>& info) 220 [](const testing::TestParamInfo<VirtualizerParamTest::ParamType>& info) {
221 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
222 std::string strength = std::to_string(std::get<PARAM_STRENGTH>(info.param));
223 std::string name = getPrefix(descriptor) + "_strength" + strength;
224 std::replace_if(
225 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
226 return name;
227 });
228
229 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VirtualizerParamTest);
230
231 INSTANTIATE_TEST_SUITE_P(
232 VirtualizerTest, VirtualizerProcessTest,
233 ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
234 IFactory::descriptor, getEffectTypeUuidVirtualizer())),
235 testing::Bool()),
__anon06a6f7e40302(const testing::TestParamInfo<VirtualizerProcessTest::ParamType>& info) 236 [](const testing::TestParamInfo<VirtualizerProcessTest::ParamType>& info) {
237 auto descriptor = std::get<PROCESS_INSTANCE_NAME>(info.param).second;
238 std::string isInputZero = std::to_string(std::get<PROCESS_ZERO_INPUT>(info.param));
239 std::string name = getPrefix(descriptor) + "_isInputZero_" + isInputZero;
240 std::replace_if(
241 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
242 return name;
243 });
244
245 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VirtualizerProcessTest);
246
main(int argc,char ** argv)247 int main(int argc, char** argv) {
248 ::testing::InitGoogleTest(&argc, argv);
249 ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
250 ABinderProcess_setThreadPoolMaxThreadCount(1);
251 ABinderProcess_startThreadPool();
252 return RUN_ALL_TESTS();
253 }
254