blob: f115cc51d29e67aa98d2709e399477b101a47e42 [file] [log] [blame]
Shunkai Yao6afc8552022-10-26 22:47:20 +00001/*
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
Shunkai Yao6afc8552022-10-26 22:47:20 +000017#include <algorithm>
Mikhail Naganov872d4a62023-03-09 18:19:01 -080018#include <cstddef>
Shunkai Yao6afc8552022-10-26 22:47:20 +000019
Mikhail Naganov872d4a62023-03-09 18:19:01 -080020#define LOG_TAG "AHAL_LoudnessEnhancerSw"
Shunkai Yao6afc8552022-10-26 22:47:20 +000021#include <android-base/logging.h>
22#include <fmq/AidlMessageQueue.h>
23
24#include "LoudnessEnhancerSw.h"
25
Shunkai Yaoc12e0822022-12-12 07:13:58 +000026using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yao6afc8552022-10-26 22:47:20 +000027using aidl::android::hardware::audio::effect::IEffect;
Shunkai Yao812d5b42022-11-16 18:08:50 +000028using aidl::android::hardware::audio::effect::kLoudnessEnhancerSwImplUUID;
Shunkai Yao6afc8552022-10-26 22:47:20 +000029using aidl::android::hardware::audio::effect::LoudnessEnhancerSw;
Shunkai Yao6afc8552022-10-26 22:47:20 +000030using aidl::android::hardware::audio::effect::State;
31using aidl::android::media::audio::common::AudioUuid;
32
33extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
34 std::shared_ptr<IEffect>* instanceSpp) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000035 if (!in_impl_uuid || *in_impl_uuid != kLoudnessEnhancerSwImplUUID) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000036 LOG(ERROR) << __func__ << "uuid not supported";
37 return EX_ILLEGAL_ARGUMENT;
38 }
39 if (instanceSpp) {
40 *instanceSpp = ndk::SharedRefBase::make<LoudnessEnhancerSw>();
41 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
42 return EX_NONE;
43 } else {
44 LOG(ERROR) << __func__ << " invalid input parameter!";
45 return EX_ILLEGAL_ARGUMENT;
46 }
47}
48
Shunkai Yaoc12e0822022-12-12 07:13:58 +000049extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
50 if (!in_impl_uuid || *in_impl_uuid != kLoudnessEnhancerSwImplUUID) {
51 LOG(ERROR) << __func__ << "uuid not supported";
52 return EX_ILLEGAL_ARGUMENT;
Shunkai Yao6afc8552022-10-26 22:47:20 +000053 }
Shunkai Yaoc12e0822022-12-12 07:13:58 +000054 *_aidl_return = LoudnessEnhancerSw::kDescriptor;
Shunkai Yao6afc8552022-10-26 22:47:20 +000055 return EX_NONE;
56}
57
58namespace aidl::android::hardware::audio::effect {
59
Shunkai Yaoc12e0822022-12-12 07:13:58 +000060const std::string LoudnessEnhancerSw::kEffectName = "LoudnessEnhancerSw";
61const Descriptor LoudnessEnhancerSw::kDescriptor = {
62 .common = {.id = {.type = kLoudnessEnhancerTypeUUID,
63 .uuid = kLoudnessEnhancerSwImplUUID,
64 .proxy = std::nullopt},
65 .flags = {.type = Flags::Type::INSERT,
66 .insert = Flags::Insert::FIRST,
67 .volume = Flags::Volume::CTRL},
68 .name = LoudnessEnhancerSw::kEffectName,
69 .implementor = "The Android Open Source Project"}};
70
Shunkai Yao6afc8552022-10-26 22:47:20 +000071ndk::ScopedAStatus LoudnessEnhancerSw::getDescriptor(Descriptor* _aidl_return) {
72 LOG(DEBUG) << __func__ << kDescriptor.toString();
73 *_aidl_return = kDescriptor;
74 return ndk::ScopedAStatus::ok();
75}
76
77ndk::ScopedAStatus LoudnessEnhancerSw::setParameterSpecific(const Parameter::Specific& specific) {
78 RETURN_IF(Parameter::Specific::loudnessEnhancer != specific.getTag(), EX_ILLEGAL_ARGUMENT,
79 "EffectNotSupported");
Shunkai Yao6afc8552022-10-26 22:47:20 +000080 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
81
Shraddha Basantwani68041ca2022-11-04 15:13:32 +053082 auto& leParam = specific.get<Parameter::Specific::loudnessEnhancer>();
83 auto tag = leParam.getTag();
84
85 switch (tag) {
86 case LoudnessEnhancer::gainMb: {
87 RETURN_IF(mContext->setLeGainMb(leParam.get<LoudnessEnhancer::gainMb>()) !=
88 RetCode::SUCCESS,
89 EX_ILLEGAL_ARGUMENT, "setGainMbFailed");
90 return ndk::ScopedAStatus::ok();
91 }
92 default: {
93 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
94 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
95 EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported");
96 }
97 }
Shunkai Yao6afc8552022-10-26 22:47:20 +000098}
99
100ndk::ScopedAStatus LoudnessEnhancerSw::getParameterSpecific(const Parameter::Id& id,
101 Parameter::Specific* specific) {
102 auto tag = id.getTag();
103 RETURN_IF(Parameter::Id::loudnessEnhancerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
Shraddha Basantwani68041ca2022-11-04 15:13:32 +0530104 auto leId = id.get<Parameter::Id::loudnessEnhancerTag>();
105 auto leIdTag = leId.getTag();
106 switch (leIdTag) {
107 case LoudnessEnhancer::Id::commonTag:
108 return getParameterLoudnessEnhancer(leId.get<LoudnessEnhancer::Id::commonTag>(),
109 specific);
110 default:
111 LOG(ERROR) << __func__ << " unsupported tag: " << toString(leIdTag);
112 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
113 EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported");
114 }
115}
116
117ndk::ScopedAStatus LoudnessEnhancerSw::getParameterLoudnessEnhancer(
118 const LoudnessEnhancer::Tag& tag, Parameter::Specific* specific) {
Shraddha Basantwani68041ca2022-11-04 15:13:32 +0530119 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
120
121 LoudnessEnhancer leParam;
122 switch (tag) {
123 case LoudnessEnhancer::gainMb: {
124 leParam.set<LoudnessEnhancer::gainMb>(mContext->getLeGainMb());
125 break;
126 }
127 default: {
128 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
129 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
130 EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported");
131 }
132 }
133
134 specific->set<Parameter::Specific::loudnessEnhancer>(leParam);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000135 return ndk::ScopedAStatus::ok();
136}
137
138std::shared_ptr<EffectContext> LoudnessEnhancerSw::createContext(const Parameter::Common& common) {
139 if (mContext) {
140 LOG(DEBUG) << __func__ << " context already exist";
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530141 } else {
142 mContext = std::make_shared<LoudnessEnhancerSwContext>(1 /* statusFmqDepth */, common);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000143 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530144
145 return mContext;
146}
147
148std::shared_ptr<EffectContext> LoudnessEnhancerSw::getContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000149 return mContext;
150}
151
152RetCode LoudnessEnhancerSw::releaseContext() {
153 if (mContext) {
154 mContext.reset();
155 }
156 return RetCode::SUCCESS;
157}
158
159// Processing method running in EffectWorker thread.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530160IEffect::Status LoudnessEnhancerSw::effectProcessImpl(float* in, float* out, int samples) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000161 // TODO: get data buffer and process.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530162 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
163 for (int i = 0; i < samples; i++) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000164 *out++ = *in++;
165 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530166 return {STATUS_OK, samples, samples};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000167}
168
169} // namespace aidl::android::hardware::audio::effect