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 
process(float * input,float * output)53 void EffectTestHelper::process(float* input, float* output) {
54     audio_buffer_t inBuffer = {.frameCount = mFrameCount, .f32 = input};
55     audio_buffer_t outBuffer = {.frameCount = mFrameCount, .f32 = output};
56     for (size_t i = 0; i < mLoopCount; i++) {
57         int status = (*mEffectHandle)->process(mEffectHandle, &inBuffer, &outBuffer);
58         ASSERT_EQ(status, 0) << "process returned an error " << status;
59 
60         inBuffer.f32 += mFrameCount * mInChannelCount;
61         outBuffer.f32 += mFrameCount * mOutChannelCount;
62     }
63 }
64 }  // namespace android
65