xref: /aosp_15_r20/hardware/interfaces/audio/aidl/default/presetReverb/PresetReverbSw.cpp (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
1 /*
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 
17 #include <algorithm>
18 #include <cstddef>
19 
20 #define LOG_TAG "AHAL_PresetReverbSw"
21 #include <android-base/logging.h>
22 #include <android/binder_enums.h>
23 #include <fmq/AidlMessageQueue.h>
24 #include <system/audio_effects/effect_uuid.h>
25 
26 #include "PresetReverbSw.h"
27 
28 using aidl::android::hardware::audio::effect::Descriptor;
29 using aidl::android::hardware::audio::effect::getEffectImplUuidPresetReverbSw;
30 using aidl::android::hardware::audio::effect::getEffectTypeUuidPresetReverb;
31 using aidl::android::hardware::audio::effect::IEffect;
32 using aidl::android::hardware::audio::effect::PresetReverbSw;
33 using aidl::android::hardware::audio::effect::State;
34 using aidl::android::media::audio::common::AudioUuid;
35 
createEffect(const AudioUuid * in_impl_uuid,std::shared_ptr<IEffect> * instanceSpp)36 extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
37                                            std::shared_ptr<IEffect>* instanceSpp) {
38     if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidPresetReverbSw()) {
39         LOG(ERROR) << __func__ << "uuid not supported";
40         return EX_ILLEGAL_ARGUMENT;
41     }
42     if (instanceSpp) {
43         *instanceSpp = ndk::SharedRefBase::make<PresetReverbSw>();
44         LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
45         return EX_NONE;
46     } else {
47         LOG(ERROR) << __func__ << " invalid input parameter!";
48         return EX_ILLEGAL_ARGUMENT;
49     }
50 }
51 
queryEffect(const AudioUuid * in_impl_uuid,Descriptor * _aidl_return)52 extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
53     if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidPresetReverbSw()) {
54         LOG(ERROR) << __func__ << "uuid not supported";
55         return EX_ILLEGAL_ARGUMENT;
56     }
57     *_aidl_return = PresetReverbSw::kDescriptor;
58     return EX_NONE;
59 }
60 
61 namespace aidl::android::hardware::audio::effect {
62 
63 const std::string PresetReverbSw::kEffectName = "PresetReverbSw";
64 
65 const std::vector<PresetReverb::Presets> PresetReverbSw::kSupportedPresets{
66         ndk::enum_range<PresetReverb::Presets>().begin(),
67         ndk::enum_range<PresetReverb::Presets>().end()};
68 
69 const std::vector<Range::PresetReverbRange> PresetReverbSw::kRanges = {
70         MAKE_RANGE(PresetReverb, supportedPresets, PresetReverbSw::kSupportedPresets,
71                    PresetReverbSw::kSupportedPresets)};
72 
73 const Capability PresetReverbSw::kCapability = {
74         .range = Range::make<Range::presetReverb>(PresetReverbSw::kRanges)};
75 
76 const Descriptor PresetReverbSw::kDescriptor = {
77         .common = {.id = {.type = getEffectTypeUuidPresetReverb(),
78                           .uuid = getEffectImplUuidPresetReverbSw(),
79                           .proxy = std::nullopt},
80                    .flags = {.type = Flags::Type::INSERT,
81                              .insert = Flags::Insert::FIRST,
82                              .volume = Flags::Volume::CTRL},
83                    .name = PresetReverbSw::kEffectName,
84                    .implementor = "The Android Open Source Project"},
85         .capability = PresetReverbSw::kCapability};
86 
getDescriptor(Descriptor * _aidl_return)87 ndk::ScopedAStatus PresetReverbSw::getDescriptor(Descriptor* _aidl_return) {
88     LOG(DEBUG) << __func__ << kDescriptor.toString();
89     *_aidl_return = kDescriptor;
90     return ndk::ScopedAStatus::ok();
91 }
92 
setParameterSpecific(const Parameter::Specific & specific)93 ndk::ScopedAStatus PresetReverbSw::setParameterSpecific(const Parameter::Specific& specific) {
94     RETURN_IF(Parameter::Specific::presetReverb != specific.getTag(), EX_ILLEGAL_ARGUMENT,
95               "EffectNotSupported");
96 
97     RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
98 
99     auto& prParam = specific.get<Parameter::Specific::presetReverb>();
100     RETURN_IF(!inRange(prParam, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange");
101     auto tag = prParam.getTag();
102 
103     switch (tag) {
104         case PresetReverb::preset: {
105             RETURN_IF(
106                     mContext->setPRPreset(prParam.get<PresetReverb::preset>()) != RetCode::SUCCESS,
107                     EX_ILLEGAL_ARGUMENT, "setPresetFailed");
108             return ndk::ScopedAStatus::ok();
109         }
110         default: {
111             LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
112             return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
113                                                                     "PresetReverbTagNotSupported");
114         }
115     }
116 }
117 
getParameterSpecific(const Parameter::Id & id,Parameter::Specific * specific)118 ndk::ScopedAStatus PresetReverbSw::getParameterSpecific(const Parameter::Id& id,
119                                                         Parameter::Specific* specific) {
120     auto tag = id.getTag();
121     RETURN_IF(Parameter::Id::presetReverbTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
122     auto prId = id.get<Parameter::Id::presetReverbTag>();
123     auto prIdTag = prId.getTag();
124     switch (prIdTag) {
125         case PresetReverb::Id::commonTag:
126             return getParameterPresetReverb(prId.get<PresetReverb::Id::commonTag>(), specific);
127         default:
128             LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
129             return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
130                                                                     "PresetReverbTagNotSupported");
131     }
132 }
133 
getParameterPresetReverb(const PresetReverb::Tag & tag,Parameter::Specific * specific)134 ndk::ScopedAStatus PresetReverbSw::getParameterPresetReverb(const PresetReverb::Tag& tag,
135                                                             Parameter::Specific* specific) {
136     RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
137     PresetReverb prParam;
138     switch (tag) {
139         case PresetReverb::preset: {
140             prParam.set<PresetReverb::preset>(mContext->getPRPreset());
141             break;
142         }
143         default: {
144             LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
145             return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
146                                                                     "PresetReverbTagNotSupported");
147         }
148     }
149 
150     specific->set<Parameter::Specific::presetReverb>(prParam);
151     return ndk::ScopedAStatus::ok();
152 }
153 
createContext(const Parameter::Common & common)154 std::shared_ptr<EffectContext> PresetReverbSw::createContext(const Parameter::Common& common) {
155     if (mContext) {
156         LOG(DEBUG) << __func__ << " context already exist";
157     } else {
158         mContext = std::make_shared<PresetReverbSwContext>(1 /* statusFmqDepth */, common);
159     }
160 
161     return mContext;
162 }
163 
releaseContext()164 RetCode PresetReverbSw::releaseContext() {
165     if (mContext) {
166         mContext.reset();
167     }
168     return RetCode::SUCCESS;
169 }
170 
171 // Processing method running in EffectWorker thread.
effectProcessImpl(float * in,float * out,int samples)172 IEffect::Status PresetReverbSw::effectProcessImpl(float* in, float* out, int samples) {
173     // TODO: get data buffer and process.
174     LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
175     for (int i = 0; i < samples; i++) {
176         *out++ = *in++;
177     }
178     return {STATUS_OK, samples, samples};
179 }
180 
181 }  // namespace aidl::android::hardware::audio::effect
182