blob: 1c1489deb5d4118c210ebfb075840dc57a7ec33b [file] [log] [blame]
Sham Rathod1b6c1f02022-11-22 17:39:22 +05301/*
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
Mikhail Naganov872d4a62023-03-09 18:19:01 -080017#define LOG_TAG "VtsHalVolumeTest"
18#include <android-base/logging.h>
19
Sham Rathod1b6c1f02022-11-22 17:39:22 +053020#include "EffectHelper.h"
21
22using namespace android;
23
Sneha Patil93e4eb52024-02-13 14:09:48 +053024using aidl::android::hardware::audio::common::getChannelCount;
Sham Rathod1b6c1f02022-11-22 17:39:22 +053025using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000026using aidl::android::hardware::audio::effect::getEffectTypeUuidVolume;
Sham Rathod1b6c1f02022-11-22 17:39:22 +053027using aidl::android::hardware::audio::effect::IEffect;
28using aidl::android::hardware::audio::effect::IFactory;
Sham Rathod1b6c1f02022-11-22 17:39:22 +053029using aidl::android::hardware::audio::effect::Parameter;
30using aidl::android::hardware::audio::effect::Volume;
Jaideep Sharma74498412023-09-13 15:25:25 +053031using android::hardware::audio::common::testing::detail::TestExecutionTracer;
Sham Rathod1b6c1f02022-11-22 17:39:22 +053032
Sneha Patil93e4eb52024-02-13 14:09:48 +053033class VolumeControlHelper : public EffectHelper {
34 public:
35 void SetUpVolumeControl() {
36 ASSERT_NE(nullptr, mFactory);
37 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
38 initFrameCount();
39 Parameter::Specific specific = getDefaultParamSpecific();
40 Parameter::Common common = EffectHelper::createParamCommon(
41 0 /* session */, 1 /* ioHandle */, kSamplingFrequency /* iSampleRate */,
42 kSamplingFrequency /* oSampleRate */, mInputFrameCount /* iFrameCount */,
43 mInputFrameCount /* oFrameCount */);
44 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &mOpenEffectReturn, EX_NONE));
45 ASSERT_NE(nullptr, mEffect);
46 }
47
48 void TearDownVolumeControl() {
49 ASSERT_NO_FATAL_FAILURE(close(mEffect));
50 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
51 mOpenEffectReturn = IEffect::OpenEffectReturn{};
52 }
53
54 Parameter::Specific getDefaultParamSpecific() {
55 Volume vol = Volume::make<Volume::levelDb>(kMinLevel);
56 Parameter::Specific specific = Parameter::Specific::make<Parameter::Specific::volume>(vol);
57 return specific;
58 }
59
60 Parameter createVolumeParam(int param, Volume::Tag volTag) {
61 return Parameter::make<Parameter::specific>(
62 Parameter::Specific::make<Parameter::Specific::volume>(
63 (volTag == Volume::mute) ? Volume::make<Volume::mute>(param)
64 : Volume::make<Volume::levelDb>(param)));
65 }
66
67 void initFrameCount() {
68 int channelCount = getChannelCount(
69 AudioChannelLayout::make<AudioChannelLayout::layoutMask>(kDefaultChannelLayout));
70 mInputFrameCount = kBufferSize / channelCount;
71 mOutputFrameCount = kBufferSize / channelCount;
72 }
73
74 bool isLevelValid(int level) {
75 auto vol = Volume::make<Volume::levelDb>(level);
76 return isParameterValid<Volume, Range::volume>(vol, mDescriptor);
77 }
78
79 void setAndVerifyParameters(Volume::Tag volTag, int param, binder_exception_t expected) {
80 auto expectedParam = createVolumeParam(param, volTag);
81 EXPECT_STATUS(expected, mEffect->setParameter(expectedParam)) << expectedParam.toString();
82
83 if (expected == EX_NONE) {
84 Volume::Id volId = Volume::Id::make<Volume::Id::commonTag>(volTag);
85
86 auto id = Parameter::Id::make<Parameter::Id::volumeTag>(volId);
87 // get parameter
88 Parameter getParam;
89 // if set success, then get should match
90 EXPECT_STATUS(expected, mEffect->getParameter(id, &getParam));
91 EXPECT_EQ(expectedParam, getParam) << "\nexpectedParam:" << expectedParam.toString()
92 << "\ngetParam:" << getParam.toString();
93 }
94 }
95
96 static constexpr int kSamplingFrequency = 44100;
97 static constexpr int kDurationMilliSec = 2000;
98 static constexpr int kBufferSize = kSamplingFrequency * kDurationMilliSec / 1000;
99 static constexpr int kMinLevel = -96;
100 static constexpr int kDefaultChannelLayout = AudioChannelLayout::LAYOUT_STEREO;
101 long mInputFrameCount, mOutputFrameCount;
102 std::shared_ptr<IFactory> mFactory;
103 std::shared_ptr<IEffect> mEffect;
104 IEffect::OpenEffectReturn mOpenEffectReturn;
105 Descriptor mDescriptor;
106};
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530107/**
108 * Here we focus on specific parameter checking, general IEffect interfaces testing performed in
109 * VtsAudioEffectTargetTest.
110 */
Sham Rathod85793d82022-12-22 19:09:10 +0530111enum ParamName { PARAM_INSTANCE_NAME, PARAM_LEVEL, PARAM_MUTE };
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530112using VolumeParamTestParam =
113 std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int, bool>;
114
Sneha Patil93e4eb52024-02-13 14:09:48 +0530115class VolumeParamTest : public ::testing::TestWithParam<VolumeParamTestParam>,
116 public VolumeControlHelper {
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530117 public:
118 VolumeParamTest()
Sham Rathod85793d82022-12-22 19:09:10 +0530119 : mParamLevel(std::get<PARAM_LEVEL>(GetParam())),
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530120 mParamMute(std::get<PARAM_MUTE>(GetParam())) {
121 std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
122 }
123
Sneha Patil93e4eb52024-02-13 14:09:48 +0530124 void SetUp() override { ASSERT_NO_FATAL_FAILURE(SetUpVolumeControl()); }
125 void TearDown() override { TearDownVolumeControl(); }
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530126
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530127 int mParamLevel = 0;
128 bool mParamMute = false;
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530129};
130
Sneha Patil93e4eb52024-02-13 14:09:48 +0530131TEST_P(VolumeParamTest, SetAndGetParams) {
132 ASSERT_NO_FATAL_FAILURE(
133 setAndVerifyParameters(Volume::levelDb, mParamLevel,
134 isLevelValid(mParamLevel) ? EX_NONE : EX_ILLEGAL_ARGUMENT));
135 ASSERT_NO_FATAL_FAILURE(setAndVerifyParameters(Volume::mute, mParamMute, EX_NONE));
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530136}
137
Sneha Patil93e4eb52024-02-13 14:09:48 +0530138using VolumeDataTestParam = std::pair<std::shared_ptr<IFactory>, Descriptor>;
139
140class VolumeDataTest : public ::testing::TestWithParam<VolumeDataTestParam>,
141 public VolumeControlHelper {
142 public:
143 VolumeDataTest() {
144 std::tie(mFactory, mDescriptor) = GetParam();
145 mInput.resize(kBufferSize);
146 mInputMag.resize(mTestFrequencies.size());
147 mBinOffsets.resize(mTestFrequencies.size());
148 roundToFreqCenteredToFftBin(mTestFrequencies, mBinOffsets, kBinWidth);
149 generateMultiTone(mTestFrequencies, mInput, kSamplingFrequency);
150 mInputMag = calculateMagnitude(mInput, mBinOffsets, kNPointFFT);
151 }
152
153 std::vector<int> calculatePercentageDiff(const std::vector<float>& outputMag) {
154 std::vector<int> percentages(mTestFrequencies.size());
155
156 for (size_t i = 0; i < mInputMag.size(); i++) {
157 float diff = mInputMag[i] - outputMag[i];
158 percentages[i] = std::round(diff / mInputMag[i] * 100);
159 }
160 return percentages;
161 }
162
163 // Convert Decibel value to Percentage
164 int percentageDb(float level) { return std::round((1 - (pow(10, level / 20))) * 100); }
165
Shunkai Yaof0cb5ec2024-03-14 01:54:58 +0000166 void SetUp() override {
167 SKIP_TEST_IF_DATA_UNSUPPORTED(mDescriptor.common.flags);
168 ASSERT_NO_FATAL_FAILURE(SetUpVolumeControl());
169 }
170 void TearDown() override {
171 SKIP_TEST_IF_DATA_UNSUPPORTED(mDescriptor.common.flags);
172 TearDownVolumeControl();
173 }
Sneha Patil93e4eb52024-02-13 14:09:48 +0530174
175 static constexpr int kMaxAudioSample = 1;
176 static constexpr int kTransitionDuration = 300;
177 static constexpr int kNPointFFT = 32768;
178 static constexpr float kBinWidth = (float)kSamplingFrequency / kNPointFFT;
179 static constexpr size_t offset = kSamplingFrequency * kTransitionDuration / 1000;
180 static constexpr float kBaseLevel = 0;
181 std::vector<int> mTestFrequencies = {100, 1000};
182 std::vector<float> mInput;
183 std::vector<float> mInputMag;
184 std::vector<int> mBinOffsets;
185};
186
187TEST_P(VolumeDataTest, ApplyLevelMuteUnmute) {
188 std::vector<float> output(kBufferSize);
189 std::vector<int> diffs(mTestFrequencies.size());
190 std::vector<float> outputMag(mTestFrequencies.size());
191
192 if (!isLevelValid(kBaseLevel)) {
193 GTEST_SKIP() << "Volume Level not supported, skipping the test\n";
194 }
195
196 // Apply Volume Level
197
198 ASSERT_NO_FATAL_FAILURE(setAndVerifyParameters(Volume::levelDb, kBaseLevel, EX_NONE));
199 ASSERT_NO_FATAL_FAILURE(processAndWriteToOutput(mInput, output, mEffect, &mOpenEffectReturn));
200
201 outputMag = calculateMagnitude(output, mBinOffsets, kNPointFFT);
202 diffs = calculatePercentageDiff(outputMag);
203
204 for (size_t i = 0; i < diffs.size(); i++) {
205 ASSERT_EQ(diffs[i], percentageDb(kBaseLevel));
206 }
207
208 // Apply Mute
209
210 ASSERT_NO_FATAL_FAILURE(setAndVerifyParameters(Volume::mute, true /*mute*/, EX_NONE));
211 ASSERT_NO_FATAL_FAILURE(processAndWriteToOutput(mInput, output, mEffect, &mOpenEffectReturn));
212
213 std::vector<float> subOutputMute(output.begin() + offset, output.end());
214 outputMag = calculateMagnitude(subOutputMute, mBinOffsets, kNPointFFT);
215 diffs = calculatePercentageDiff(outputMag);
216
217 for (size_t i = 0; i < diffs.size(); i++) {
218 ASSERT_EQ(diffs[i], percentageDb(kMinLevel /*Mute*/));
219 }
220
221 // Verifying Fade out
222 outputMag = calculateMagnitude(output, mBinOffsets, kNPointFFT);
223 diffs = calculatePercentageDiff(outputMag);
224
225 for (size_t i = 0; i < diffs.size(); i++) {
226 ASSERT_LT(diffs[i], percentageDb(kMinLevel /*Mute*/));
227 }
228
229 // Apply Unmute
230
231 ASSERT_NO_FATAL_FAILURE(setAndVerifyParameters(Volume::mute, false /*unmute*/, EX_NONE));
232 ASSERT_NO_FATAL_FAILURE(processAndWriteToOutput(mInput, output, mEffect, &mOpenEffectReturn));
233
234 std::vector<float> subOutputUnmute(output.begin() + offset, output.end());
235
236 outputMag = calculateMagnitude(subOutputUnmute, mBinOffsets, kNPointFFT);
237 diffs = calculatePercentageDiff(outputMag);
238
239 for (size_t i = 0; i < diffs.size(); i++) {
240 ASSERT_EQ(diffs[i], percentageDb(kBaseLevel));
241 }
242
243 // Verifying Fade in
244 outputMag = calculateMagnitude(output, mBinOffsets, kNPointFFT);
245 diffs = calculatePercentageDiff(outputMag);
246
247 for (size_t i = 0; i < diffs.size(); i++) {
248 ASSERT_GT(diffs[i], percentageDb(kBaseLevel));
249 }
250}
251
252TEST_P(VolumeDataTest, DecreasingLevels) {
253 std::vector<int> decreasingLevels = {-24, -48, -96};
254 std::vector<float> baseOutput(kBufferSize);
255 std::vector<int> baseDiffs(mTestFrequencies.size());
256 std::vector<float> outputMag(mTestFrequencies.size());
257
258 if (!isLevelValid(kBaseLevel)) {
259 GTEST_SKIP() << "Volume Level not supported, skipping the test\n";
260 }
261
262 ASSERT_NO_FATAL_FAILURE(setAndVerifyParameters(Volume::levelDb, kBaseLevel, EX_NONE));
263 ASSERT_NO_FATAL_FAILURE(
264 processAndWriteToOutput(mInput, baseOutput, mEffect, &mOpenEffectReturn));
265
266 outputMag = calculateMagnitude(baseOutput, mBinOffsets, kNPointFFT);
267 baseDiffs = calculatePercentageDiff(outputMag);
268
269 for (int level : decreasingLevels) {
270 std::vector<float> output(kBufferSize);
271 std::vector<int> diffs(mTestFrequencies.size());
272
273 // Skipping the further steps for unnsupported level values
274 if (!isLevelValid(level)) {
275 continue;
276 }
277 ASSERT_NO_FATAL_FAILURE(setAndVerifyParameters(Volume::levelDb, level, EX_NONE));
278 ASSERT_NO_FATAL_FAILURE(
279 processAndWriteToOutput(mInput, output, mEffect, &mOpenEffectReturn));
280
281 outputMag = calculateMagnitude(output, mBinOffsets, kNPointFFT);
282 diffs = calculatePercentageDiff(outputMag);
283
284 // Decrease in volume level results in greater magnitude difference
285 for (size_t i = 0; i < diffs.size(); i++) {
286 ASSERT_GT(diffs[i], baseDiffs[i]);
287 }
288
289 baseDiffs = diffs;
290 }
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530291}
292
Shunkai Yao0a0c45e2023-02-13 17:41:11 +0000293std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair;
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530294INSTANTIATE_TEST_SUITE_P(
295 VolumeTest, VolumeParamTest,
Sham Rathod85793d82022-12-22 19:09:10 +0530296 ::testing::Combine(
Shunkai Yao0a0c45e2023-02-13 17:41:11 +0000297 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000298 IFactory::descriptor, getEffectTypeUuidVolume())),
Shunkai Yao0a0c45e2023-02-13 17:41:11 +0000299 testing::ValuesIn(
300 EffectHelper::getTestValueSet<Volume, int, Range::volume, Volume::levelDb>(
301 kDescPair, EffectHelper::expandTestValueBasic<int>)),
Sham Rathod85793d82022-12-22 19:09:10 +0530302 testing::Bool() /* mute */),
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530303 [](const testing::TestParamInfo<VolumeParamTest::ParamType>& info) {
304 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
Sham Rathod85793d82022-12-22 19:09:10 +0530305 std::string level = std::to_string(std::get<PARAM_LEVEL>(info.param));
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530306 std::string mute = std::to_string(std::get<PARAM_MUTE>(info.param));
Shunkai Yao9e60e632023-06-23 04:34:50 +0000307 std::string name = getPrefix(descriptor) + "_level" + level + "_mute" + mute;
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530308 std::replace_if(
309 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
310 return name;
311 });
312
313GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VolumeParamTest);
314
Sneha Patil93e4eb52024-02-13 14:09:48 +0530315INSTANTIATE_TEST_SUITE_P(VolumeTest, VolumeDataTest,
316 testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
317 IFactory::descriptor, getEffectTypeUuidVolume())),
318 [](const testing::TestParamInfo<VolumeDataTest::ParamType>& info) {
319 auto descriptor = info.param;
320 std::string name = getPrefix(descriptor.second);
321 std::replace_if(
322 name.begin(), name.end(),
323 [](const char c) { return !std::isalnum(c); }, '_');
324 return name;
325 });
326
327GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VolumeDataTest);
328
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530329int main(int argc, char** argv) {
330 ::testing::InitGoogleTest(&argc, argv);
Jaideep Sharma74498412023-09-13 15:25:25 +0530331 ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530332 ABinderProcess_setThreadPoolMaxThreadCount(1);
333 ABinderProcess_startThreadPool();
334 return RUN_ALL_TESTS();
335}