• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
createEffect()20 void EffectTestHelper::createEffect() {
21     int status = AUDIO_EFFECT_LIBRARY_INFO_SYM.create_effect(mUuid, 1, 1, &mEffectHandle);
22     ASSERT_EQ(status, 0) << "create_effect returned an error " << status;
23 }
24 
releaseEffect()25 void EffectTestHelper::releaseEffect() {
26     int status = AUDIO_EFFECT_LIBRARY_INFO_SYM.release_effect(mEffectHandle);
27     ASSERT_EQ(status, 0) << "release_effect returned an error " << status;
28 }
29 
setConfig(bool configReverse)30 void EffectTestHelper::setConfig(bool configReverse) {
31     effect_config_t config{};
32     config.inputCfg.samplingRate = config.outputCfg.samplingRate = mSampleRate;
33     config.inputCfg.channels = config.outputCfg.channels = mChMask;
34     config.inputCfg.format = config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
35 
36     int reply = 0;
37     uint32_t replySize = sizeof(reply);
38 
39     int status = (*mEffectHandle)
40                          ->command(mEffectHandle, EFFECT_CMD_SET_CONFIG, sizeof(effect_config_t),
41                                    &config, &replySize, &reply);
42     ASSERT_EQ(status, 0) << "set_config returned an error " << status;
43     ASSERT_EQ(reply, 0) << "set_config reply non zero " << reply;
44 
45     if (configReverse) {
46         int status = (*mEffectHandle)
47                              ->command(mEffectHandle, EFFECT_CMD_SET_CONFIG_REVERSE,
48                                        sizeof(effect_config_t), &config, &replySize, &reply);
49         ASSERT_EQ(status, 0) << "set_config_reverse returned an error " << status;
50         ASSERT_EQ(reply, 0) << "set_config_reverse reply non zero " << reply;
51     }
52 
53     status = (*mEffectHandle)
54                      ->command(mEffectHandle, EFFECT_CMD_ENABLE, 0, nullptr, &replySize, &reply);
55     ASSERT_EQ(status, 0) << "cmd_enable returned an error " << status;
56     ASSERT_EQ(reply, 0) << "cmd_enable reply non zero " << reply;
57 }
58 
setParam(uint32_t type,uint32_t value)59 void EffectTestHelper::setParam(uint32_t type, uint32_t value) {
60     int reply = 0;
61     uint32_t replySize = sizeof(reply);
62     uint32_t paramData[2] = {type, value};
63     auto effectParam = (effect_param_t*)malloc(sizeof(effect_param_t) + sizeof(paramData));
64     memcpy(&effectParam->data[0], &paramData[0], sizeof(paramData));
65     effectParam->psize = sizeof(paramData[0]);
66     effectParam->vsize = sizeof(paramData[1]);
67     int status = (*mEffectHandle)
68                          ->command(mEffectHandle, EFFECT_CMD_SET_PARAM,
69                                    sizeof(effect_param_t) + sizeof(paramData), effectParam,
70                                    &replySize, &reply);
71     free(effectParam);
72     ASSERT_EQ(status, 0) << "set_param returned an error " << status;
73     ASSERT_EQ(reply, 0) << "set_param reply non zero " << reply;
74 }
75 
process(int16_t * input,int16_t * output,bool setAecEchoDelay)76 void EffectTestHelper::process(int16_t* input, int16_t* output, bool setAecEchoDelay) {
77     audio_buffer_t inBuffer = {.frameCount = mFrameCount, .s16 = input};
78     audio_buffer_t outBuffer = {.frameCount = mFrameCount, .s16 = output};
79     for (size_t i = 0; i < mLoopCount; i++) {
80         if (setAecEchoDelay) ASSERT_NO_FATAL_FAILURE(setParam(AEC_PARAM_ECHO_DELAY, kAECDelay));
81         int status = (*mEffectHandle)->process(mEffectHandle, &inBuffer, &outBuffer);
82         ASSERT_EQ(status, 0) << "process returned an error " << status;
83 
84         inBuffer.s16 += mFrameCount * mChannelCount;
85         outBuffer.s16 += mFrameCount * mChannelCount;
86     }
87 }
88 
process_reverse(int16_t * farInput,int16_t * output)89 void EffectTestHelper::process_reverse(int16_t* farInput, int16_t* output) {
90     audio_buffer_t farInBuffer = {.frameCount = mFrameCount, .s16 = farInput};
91     audio_buffer_t outBuffer = {.frameCount = mFrameCount, .s16 = output};
92     for (size_t i = 0; i < mLoopCount; i++) {
93         int status = (*mEffectHandle)->process_reverse(mEffectHandle, &farInBuffer, &outBuffer);
94         ASSERT_EQ(status, 0) << "process returned an error " << status;
95 
96         farInBuffer.s16 += mFrameCount * mChannelCount;
97         outBuffer.s16 += mFrameCount * mChannelCount;
98     }
99 }
100