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 extern audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM;
19
20 namespace android {
21
createEffect()22 void EffectTestHelper::createEffect() {
23 int status = AUDIO_EFFECT_LIBRARY_INFO_SYM.create_effect(mUuid, 1, 1, &mEffectHandle);
24 ASSERT_EQ(status, 0) << "create_effect returned an error " << status;
25 }
26
releaseEffect()27 void EffectTestHelper::releaseEffect() {
28 int status = AUDIO_EFFECT_LIBRARY_INFO_SYM.release_effect(mEffectHandle);
29 ASSERT_EQ(status, 0) << "release_effect returned an error " << status;
30 }
31
setConfig()32 void EffectTestHelper::setConfig() {
33 effect_config_t config{};
34 config.inputCfg.samplingRate = config.outputCfg.samplingRate = mSampleRate;
35 config.inputCfg.channels = mInChMask;
36 config.outputCfg.channels = mOutChMask;
37 config.inputCfg.format = config.outputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
38
39 int reply = 0;
40 uint32_t replySize = sizeof(reply);
41 int status = (*mEffectHandle)
42 ->command(mEffectHandle, EFFECT_CMD_SET_CONFIG, sizeof(effect_config_t),
43 &config, &replySize, &reply);
44 ASSERT_EQ(status, 0) << "set_config returned an error " << status;
45 ASSERT_EQ(reply, 0) << "set_config reply non zero " << reply;
46
47 status = (*mEffectHandle)
48 ->command(mEffectHandle, EFFECT_CMD_ENABLE, 0, nullptr, &replySize, &reply);
49 ASSERT_EQ(status, 0) << "cmd_enable returned an error " << status;
50 ASSERT_EQ(reply, 0) << "cmd_enable reply non zero " << reply;
51 }
52
setParam(uint32_t type,uint32_t value)53 void EffectTestHelper::setParam(uint32_t type, uint32_t value) {
54 int reply = 0;
55 uint32_t replySize = sizeof(reply);
56 uint32_t paramData[2] = {type, value};
57 auto effectParam = new effect_param_t[sizeof(effect_param_t) + sizeof(paramData)];
58 memcpy(&effectParam->data[0], ¶mData[0], sizeof(paramData));
59 effectParam->psize = sizeof(paramData[0]);
60 effectParam->vsize = sizeof(paramData[1]);
61 int status = (*mEffectHandle)
62 ->command(mEffectHandle, EFFECT_CMD_SET_PARAM,
63 sizeof(effect_param_t) + sizeof(paramData), effectParam,
64 &replySize, &reply);
65 delete[] effectParam;
66 ASSERT_EQ(status, 0) << "set_param returned an error " << status;
67 ASSERT_EQ(reply, 0) << "set_param reply non zero " << reply;
68 }
69
process(float * input,float * output)70 void EffectTestHelper::process(float* input, float* output) {
71 audio_buffer_t inBuffer = {.frameCount = mFrameCount, .f32 = input};
72 audio_buffer_t outBuffer = {.frameCount = mFrameCount, .f32 = output};
73 for (size_t i = 0; i < mLoopCount; i++) {
74 int status = (*mEffectHandle)->process(mEffectHandle, &inBuffer, &outBuffer);
75 ASSERT_EQ(status, 0) << "process returned an error " << status;
76
77 inBuffer.f32 += mFrameCount * mInChannelCount;
78 outBuffer.f32 += mFrameCount * mOutChannelCount;
79 }
80 }
81 } // namespace android
82