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_EqualizerSw"
21 #include <android-base/logging.h>
22 #include <fmq/AidlMessageQueue.h>
23 #include <system/audio_effects/effect_uuid.h>
24
25 #include "EqualizerSw.h"
26
27 using aidl::android::hardware::audio::effect::Descriptor;
28 using aidl::android::hardware::audio::effect::EqualizerSw;
29 using aidl::android::hardware::audio::effect::getEffectImplUuidEqualizerSw;
30 using aidl::android::hardware::audio::effect::getEffectTypeUuidEqualizer;
31 using aidl::android::hardware::audio::effect::IEffect;
32 using aidl::android::hardware::audio::effect::State;
33 using aidl::android::media::audio::common::AudioUuid;
34
createEffect(const AudioUuid * in_impl_uuid,std::shared_ptr<IEffect> * instanceSpp)35 extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
36 std::shared_ptr<IEffect>* instanceSpp) {
37 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidEqualizerSw()) {
38 LOG(ERROR) << __func__ << "uuid not supported";
39 return EX_ILLEGAL_ARGUMENT;
40 }
41 if (instanceSpp) {
42 *instanceSpp = ndk::SharedRefBase::make<EqualizerSw>();
43 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
44 return EX_NONE;
45 } else {
46 LOG(ERROR) << __func__ << " invalid input parameter!";
47 return EX_ILLEGAL_ARGUMENT;
48 }
49 }
50
queryEffect(const AudioUuid * in_impl_uuid,Descriptor * _aidl_return)51 extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
52 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidEqualizerSw()) {
53 LOG(ERROR) << __func__ << "uuid not supported";
54 return EX_ILLEGAL_ARGUMENT;
55 }
56 *_aidl_return = EqualizerSw::kDesc;
57 return EX_NONE;
58 }
59
60 namespace aidl::android::hardware::audio::effect {
61
62 const std::string EqualizerSw::kEffectName = "EqualizerSw";
63
64 const std::vector<Equalizer::BandFrequency> EqualizerSw::kBandFrequency = {{0, 30000, 120000},
65 {1, 120001, 460000},
66 {2, 460001, 1800000},
67 {3, 1800001, 7000000},
68 {4, 7000001, 20000000}};
69 const std::vector<Equalizer::Preset> EqualizerSw::kPresets = {
70 {0, "Normal"}, {1, "Classical"}, {2, "Dance"}, {3, "Flat"}, {4, "Folk"},
71 {5, "Heavy Metal"}, {6, "Hip Hop"}, {7, "Jazz"}, {8, "Pop"}, {9, "Rock"}};
72
73 /**
74 * Use the same min and max to build a capability represented by Range.
75 */
76 const std::vector<Range::EqualizerRange> EqualizerSw::kRanges = {
77 MAKE_RANGE(Equalizer, preset, 0, EqualizerSw::kPresets.size() - 1),
78 MAKE_RANGE(Equalizer, bandLevels,
79 std::vector<Equalizer::BandLevel>{
80 Equalizer::BandLevel({.index = 0, .levelMb = -15})},
81 std::vector<Equalizer::BandLevel>{Equalizer::BandLevel(
82 {.index = EqualizerSwContext::kMaxBandNumber - 1, .levelMb = 15})}),
83 /* capability definition */
84 MAKE_RANGE(Equalizer, bandFrequencies, EqualizerSw::kBandFrequency,
85 EqualizerSw::kBandFrequency),
86 MAKE_RANGE(Equalizer, presets, EqualizerSw::kPresets, EqualizerSw::kPresets),
87 /* centerFreqMh is get only, set invalid range min > max */
88 MAKE_RANGE(Equalizer, centerFreqMh, std::vector<int>({1}), std::vector<int>({0}))};
89
90 const Capability EqualizerSw::kEqCap = {.range = EqualizerSw::kRanges};
91 const Descriptor EqualizerSw::kDesc = {.common = {.id = {.type = getEffectTypeUuidEqualizer(),
92 .uuid = getEffectImplUuidEqualizerSw()},
93 .flags = {.type = Flags::Type::INSERT,
94 .insert = Flags::Insert::FIRST,
95 .volume = Flags::Volume::CTRL},
96 .name = EqualizerSw::kEffectName,
97 .implementor = "The Android Open Source Project"},
98 .capability = EqualizerSw::kEqCap};
99
getDescriptor(Descriptor * _aidl_return)100 ndk::ScopedAStatus EqualizerSw::getDescriptor(Descriptor* _aidl_return) {
101 LOG(DEBUG) << __func__ << kDesc.toString();
102 *_aidl_return = kDesc;
103 return ndk::ScopedAStatus::ok();
104 }
105
setParameterSpecific(const Parameter::Specific & specific)106 ndk::ScopedAStatus EqualizerSw::setParameterSpecific(const Parameter::Specific& specific) {
107 RETURN_IF(Parameter::Specific::equalizer != specific.getTag(), EX_ILLEGAL_ARGUMENT,
108 "EffectNotSupported");
109 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
110
111 auto& eqParam = specific.get<Parameter::Specific::equalizer>();
112 RETURN_IF(!inRange(eqParam, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange");
113 auto tag = eqParam.getTag();
114 switch (tag) {
115 case Equalizer::preset: {
116 RETURN_IF(mContext->setEqPreset(eqParam.get<Equalizer::preset>()) != RetCode::SUCCESS,
117 EX_ILLEGAL_ARGUMENT, "setBandLevelsFailed");
118 return ndk::ScopedAStatus::ok();
119 }
120 case Equalizer::bandLevels: {
121 RETURN_IF(mContext->setEqBandLevels(eqParam.get<Equalizer::bandLevels>()) !=
122 RetCode::SUCCESS,
123 EX_ILLEGAL_ARGUMENT, "setBandLevelsFailed");
124 return ndk::ScopedAStatus::ok();
125 }
126 default: {
127 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
128 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
129 "EqTagNotSupported");
130 }
131 }
132
133 LOG(ERROR) << __func__ << " unsupported eq param tag: " << toString(tag);
134 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
135 "ParamNotSupported");
136 }
137
getParameterSpecific(const Parameter::Id & id,Parameter::Specific * specific)138 ndk::ScopedAStatus EqualizerSw::getParameterSpecific(const Parameter::Id& id,
139 Parameter::Specific* specific) {
140 auto tag = id.getTag();
141 RETURN_IF(Parameter::Id::equalizerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
142 auto eqId = id.get<Parameter::Id::equalizerTag>();
143 auto eqIdTag = eqId.getTag();
144 switch (eqIdTag) {
145 case Equalizer::Id::commonTag:
146 return getParameterEqualizer(eqId.get<Equalizer::Id::commonTag>(), specific);
147 default:
148 LOG(ERROR) << __func__ << " tag " << toString(eqIdTag) << " not supported";
149 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
150 "EqualizerTagNotSupported");
151 }
152 }
153
getParameterEqualizer(const Equalizer::Tag & tag,Parameter::Specific * specific)154 ndk::ScopedAStatus EqualizerSw::getParameterEqualizer(const Equalizer::Tag& tag,
155 Parameter::Specific* specific) {
156 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
157
158 Equalizer eqParam;
159 switch (tag) {
160 case Equalizer::bandLevels: {
161 eqParam.set<Equalizer::bandLevels>(mContext->getEqBandLevels());
162 break;
163 }
164 case Equalizer::preset: {
165 eqParam.set<Equalizer::preset>(mContext->getEqPreset());
166 break;
167 }
168 case Equalizer::centerFreqMh: {
169 eqParam.set<Equalizer::centerFreqMh>(mContext->getCenterFreqs());
170 break;
171 }
172 case Equalizer::bandFrequencies: {
173 eqParam.set<Equalizer::bandFrequencies>(kBandFrequency);
174 break;
175 }
176 case Equalizer::presets: {
177 eqParam.set<Equalizer::presets>(kPresets);
178 break;
179 }
180 default: {
181 LOG(ERROR) << __func__ << " not handled tag: " << toString(tag);
182 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
183 "unsupportedTag");
184 }
185 }
186
187 specific->set<Parameter::Specific::equalizer>(eqParam);
188 return ndk::ScopedAStatus::ok();
189 }
190
createContext(const Parameter::Common & common)191 std::shared_ptr<EffectContext> EqualizerSw::createContext(const Parameter::Common& common) {
192 if (mContext) {
193 LOG(DEBUG) << __func__ << " context already exist";
194 } else {
195 mContext = std::make_shared<EqualizerSwContext>(1 /* statusFmqDepth */, common);
196 }
197
198 return mContext;
199 }
200
releaseContext()201 RetCode EqualizerSw::releaseContext() {
202 if (mContext) {
203 mContext.reset();
204 }
205 return RetCode::SUCCESS;
206 }
207
208 // Processing method running in EffectWorker thread.
effectProcessImpl(float * in,float * out,int samples)209 IEffect::Status EqualizerSw::effectProcessImpl(float* in, float* out, int samples) {
210 // TODO: get data buffer and process.
211 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
212 for (int i = 0; i < samples; i++) {
213 *out++ = *in++;
214 }
215 return {STATUS_OK, samples, samples};
216 }
217
218 } // namespace aidl::android::hardware::audio::effect
219