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 
19 namespace android {
20 
createEffect()21 void EffectTestHelper::createEffect() {
22     int status = AUDIO_EFFECT_LIBRARY_INFO_SYM.create_effect(mUuid, 1, 1, &mEffectHandle);
23     ASSERT_EQ(status, 0) << "create_effect returned an error " << status;
24 }
25 
releaseEffect()26 void EffectTestHelper::releaseEffect() {
27     int status = AUDIO_EFFECT_LIBRARY_INFO_SYM.release_effect(mEffectHandle);
28     ASSERT_EQ(status, 0) << "release_effect returned an error " << status;
29 }
30 
setConfig()31 void EffectTestHelper::setConfig() {
32     effect_config_t config{};
33     config.inputCfg.samplingRate = config.outputCfg.samplingRate = mSampleRate;
34     config.inputCfg.channels = mInChMask;
35     config.outputCfg.channels = mOutChMask;
36     config.inputCfg.format = config.outputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
37 
38     int reply = 0;
39     uint32_t replySize = sizeof(reply);
40     int status = (*mEffectHandle)
41                          ->command(mEffectHandle, EFFECT_CMD_SET_CONFIG, sizeof(effect_config_t),
42                                    &config, &replySize, &reply);
43     ASSERT_EQ(status, 0) << "set_config returned an error " << status;
44     ASSERT_EQ(reply, 0) << "set_config reply non zero " << reply;
45 
46     status = (*mEffectHandle)
47                      ->command(mEffectHandle, EFFECT_CMD_ENABLE, 0, nullptr, &replySize, &reply);
48     ASSERT_EQ(status, 0) << "cmd_enable returned an error " << status;
49     ASSERT_EQ(reply, 0) << "cmd_enable reply non zero " << reply;
50 }
51 
process(float * input,float * output)52 void EffectTestHelper::process(float* input, float* output) {
53     audio_buffer_t inBuffer = {.frameCount = mFrameCount, .f32 = input};
54     audio_buffer_t outBuffer = {.frameCount = mFrameCount, .f32 = output};
55     for (size_t i = 0; i < mLoopCount; i++) {
56         int status = (*mEffectHandle)->process(mEffectHandle, &inBuffer, &outBuffer);
57         ASSERT_EQ(status, 0) << "process returned an error " << status;
58 
59         inBuffer.f32 += mFrameCount * mInChannelCount;
60         outBuffer.f32 += mFrameCount * mOutChannelCount;
61     }
62 }
63 }  // namespace android
64