blob: 14546a4a9e820a93c748487b92906c15f12c0d2e [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_PresetReverbSw"
Shunkai Yao6afc8552022-10-26 22:47:20 +000021#include <android-base/logging.h>
Sham Rathod73aa2c32022-12-20 17:03:40 +053022#include <android/binder_enums.h>
Shunkai Yao6afc8552022-10-26 22:47:20 +000023#include <fmq/AidlMessageQueue.h>
24
Shunkai Yao812d5b42022-11-16 18:08:50 +000025#include "PresetReverbSw.h"
Shunkai Yao6afc8552022-10-26 22:47:20 +000026
Shunkai Yaoc12e0822022-12-12 07:13:58 +000027using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yao6afc8552022-10-26 22:47:20 +000028using aidl::android::hardware::audio::effect::IEffect;
Shunkai Yao812d5b42022-11-16 18:08:50 +000029using aidl::android::hardware::audio::effect::kPresetReverbSwImplUUID;
30using aidl::android::hardware::audio::effect::PresetReverbSw;
Shunkai Yao6afc8552022-10-26 22:47:20 +000031using aidl::android::hardware::audio::effect::State;
32using aidl::android::media::audio::common::AudioUuid;
33
34extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
35 std::shared_ptr<IEffect>* instanceSpp) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000036 if (!in_impl_uuid || *in_impl_uuid != kPresetReverbSwImplUUID) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000037 LOG(ERROR) << __func__ << "uuid not supported";
38 return EX_ILLEGAL_ARGUMENT;
39 }
40 if (instanceSpp) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000041 *instanceSpp = ndk::SharedRefBase::make<PresetReverbSw>();
Shunkai Yao6afc8552022-10-26 22:47:20 +000042 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
43 return EX_NONE;
44 } else {
45 LOG(ERROR) << __func__ << " invalid input parameter!";
46 return EX_ILLEGAL_ARGUMENT;
47 }
48}
49
Shunkai Yaoc12e0822022-12-12 07:13:58 +000050extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
51 if (!in_impl_uuid || *in_impl_uuid != kPresetReverbSwImplUUID) {
52 LOG(ERROR) << __func__ << "uuid not supported";
53 return EX_ILLEGAL_ARGUMENT;
Shunkai Yao6afc8552022-10-26 22:47:20 +000054 }
Shunkai Yaoc12e0822022-12-12 07:13:58 +000055 *_aidl_return = PresetReverbSw::kDescriptor;
Shunkai Yao6afc8552022-10-26 22:47:20 +000056 return EX_NONE;
57}
58
59namespace aidl::android::hardware::audio::effect {
60
Shunkai Yaoc12e0822022-12-12 07:13:58 +000061const std::string PresetReverbSw::kEffectName = "PresetReverbSw";
Sham Rathod73aa2c32022-12-20 17:03:40 +053062
Shunkai Yao87811022023-02-13 17:40:37 +000063const std::vector<PresetReverb::Presets> PresetReverbSw::kSupportedPresets{
Sham Rathod73aa2c32022-12-20 17:03:40 +053064 ndk::enum_range<PresetReverb::Presets>().begin(),
65 ndk::enum_range<PresetReverb::Presets>().end()};
66
Shunkai Yao87811022023-02-13 17:40:37 +000067const std::vector<Range::PresetReverbRange> PresetReverbSw::kRanges = {
68 MAKE_RANGE(PresetReverb, supportedPresets, PresetReverbSw::kSupportedPresets,
69 PresetReverbSw::kSupportedPresets)};
70
71const Capability PresetReverbSw::kCapability = {
72 .range = Range::make<Range::presetReverb>(PresetReverbSw::kRanges)};
73
Shunkai Yaoc12e0822022-12-12 07:13:58 +000074const Descriptor PresetReverbSw::kDescriptor = {
75 .common = {.id = {.type = kPresetReverbTypeUUID,
76 .uuid = kPresetReverbSwImplUUID,
77 .proxy = std::nullopt},
78 .flags = {.type = Flags::Type::INSERT,
79 .insert = Flags::Insert::FIRST,
80 .volume = Flags::Volume::CTRL},
81 .name = PresetReverbSw::kEffectName,
82 .implementor = "The Android Open Source Project"},
Shunkai Yao87811022023-02-13 17:40:37 +000083 .capability = PresetReverbSw::kCapability};
Shunkai Yaoc12e0822022-12-12 07:13:58 +000084
Shunkai Yao812d5b42022-11-16 18:08:50 +000085ndk::ScopedAStatus PresetReverbSw::getDescriptor(Descriptor* _aidl_return) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000086 LOG(DEBUG) << __func__ << kDescriptor.toString();
87 *_aidl_return = kDescriptor;
88 return ndk::ScopedAStatus::ok();
89}
90
Shunkai Yao812d5b42022-11-16 18:08:50 +000091ndk::ScopedAStatus PresetReverbSw::setParameterSpecific(const Parameter::Specific& specific) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +000092 RETURN_IF(Parameter::Specific::presetReverb != specific.getTag(), EX_ILLEGAL_ARGUMENT,
Shunkai Yao6afc8552022-10-26 22:47:20 +000093 "EffectNotSupported");
Shunkai Yao6afc8552022-10-26 22:47:20 +000094
Sham Rathod73aa2c32022-12-20 17:03:40 +053095 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
96
97 auto& prParam = specific.get<Parameter::Specific::presetReverb>();
Shunkai Yao87811022023-02-13 17:40:37 +000098 RETURN_IF(!inRange(prParam, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange");
Sham Rathod73aa2c32022-12-20 17:03:40 +053099 auto tag = prParam.getTag();
100
101 switch (tag) {
102 case PresetReverb::preset: {
103 RETURN_IF(
104 mContext->setPRPreset(prParam.get<PresetReverb::preset>()) != RetCode::SUCCESS,
105 EX_ILLEGAL_ARGUMENT, "setPresetFailed");
106 return ndk::ScopedAStatus::ok();
107 }
108 default: {
109 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
110 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
111 "PresetReverbTagNotSupported");
112 }
113 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000114}
115
Shunkai Yao812d5b42022-11-16 18:08:50 +0000116ndk::ScopedAStatus PresetReverbSw::getParameterSpecific(const Parameter::Id& id,
117 Parameter::Specific* specific) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000118 auto tag = id.getTag();
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000119 RETURN_IF(Parameter::Id::presetReverbTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
Sham Rathod73aa2c32022-12-20 17:03:40 +0530120 auto prId = id.get<Parameter::Id::presetReverbTag>();
121 auto prIdTag = prId.getTag();
122 switch (prIdTag) {
123 case PresetReverb::Id::commonTag:
124 return getParameterPresetReverb(prId.get<PresetReverb::Id::commonTag>(), specific);
125 default:
126 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
127 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
128 "PresetReverbTagNotSupported");
129 }
130}
131
132ndk::ScopedAStatus PresetReverbSw::getParameterPresetReverb(const PresetReverb::Tag& tag,
133 Parameter::Specific* specific) {
134 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
135 PresetReverb prParam;
136 switch (tag) {
137 case PresetReverb::preset: {
138 prParam.set<PresetReverb::preset>(mContext->getPRPreset());
139 break;
140 }
141 default: {
142 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
143 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
144 "PresetReverbTagNotSupported");
145 }
146 }
147
148 specific->set<Parameter::Specific::presetReverb>(prParam);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000149 return ndk::ScopedAStatus::ok();
150}
151
Shunkai Yao812d5b42022-11-16 18:08:50 +0000152std::shared_ptr<EffectContext> PresetReverbSw::createContext(const Parameter::Common& common) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000153 if (mContext) {
154 LOG(DEBUG) << __func__ << " context already exist";
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530155 } else {
156 mContext = std::make_shared<PresetReverbSwContext>(1 /* statusFmqDepth */, common);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000157 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530158
159 return mContext;
160}
161
162std::shared_ptr<EffectContext> PresetReverbSw::getContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000163 return mContext;
164}
165
Shunkai Yao812d5b42022-11-16 18:08:50 +0000166RetCode PresetReverbSw::releaseContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000167 if (mContext) {
168 mContext.reset();
169 }
170 return RetCode::SUCCESS;
171}
172
173// Processing method running in EffectWorker thread.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530174IEffect::Status PresetReverbSw::effectProcessImpl(float* in, float* out, int samples) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000175 // TODO: get data buffer and process.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530176 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
177 for (int i = 0; i < samples; i++) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000178 *out++ = *in++;
179 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530180 return {STATUS_OK, samples, samples};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000181}
182
183} // namespace aidl::android::hardware::audio::effect