Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 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 <cstddef> |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 18 | #define LOG_TAG "AHAL_PresetReverbSw" |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 19 | #include <Utils.h> |
| 20 | #include <algorithm> |
| 21 | #include <unordered_set> |
| 22 | |
| 23 | #include <android-base/logging.h> |
Sham Rathod | 73aa2c3 | 2022-12-20 17:03:40 +0530 | [diff] [blame] | 24 | #include <android/binder_enums.h> |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 25 | #include <fmq/AidlMessageQueue.h> |
| 26 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 27 | #include "PresetReverbSw.h" |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 28 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 29 | using aidl::android::hardware::audio::effect::Descriptor; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 30 | using aidl::android::hardware::audio::effect::IEffect; |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 31 | using aidl::android::hardware::audio::effect::kPresetReverbSwImplUUID; |
| 32 | using aidl::android::hardware::audio::effect::PresetReverbSw; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 33 | using aidl::android::hardware::audio::effect::State; |
| 34 | using aidl::android::media::audio::common::AudioUuid; |
| 35 | |
| 36 | extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid, |
| 37 | std::shared_ptr<IEffect>* instanceSpp) { |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 38 | if (!in_impl_uuid || *in_impl_uuid != kPresetReverbSwImplUUID) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 39 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 40 | return EX_ILLEGAL_ARGUMENT; |
| 41 | } |
| 42 | if (instanceSpp) { |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 43 | *instanceSpp = ndk::SharedRefBase::make<PresetReverbSw>(); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 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 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 52 | extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) { |
| 53 | if (!in_impl_uuid || *in_impl_uuid != kPresetReverbSwImplUUID) { |
| 54 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 55 | return EX_ILLEGAL_ARGUMENT; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 56 | } |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 57 | *_aidl_return = PresetReverbSw::kDescriptor; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 58 | return EX_NONE; |
| 59 | } |
| 60 | |
| 61 | namespace aidl::android::hardware::audio::effect { |
| 62 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 63 | const std::string PresetReverbSw::kEffectName = "PresetReverbSw"; |
Sham Rathod | 73aa2c3 | 2022-12-20 17:03:40 +0530 | [diff] [blame] | 64 | |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame^] | 65 | const std::vector<PresetReverb::Presets> PresetReverbSw::kSupportedPresets{ |
Sham Rathod | 73aa2c3 | 2022-12-20 17:03:40 +0530 | [diff] [blame] | 66 | ndk::enum_range<PresetReverb::Presets>().begin(), |
| 67 | ndk::enum_range<PresetReverb::Presets>().end()}; |
| 68 | |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame^] | 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 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 76 | const Descriptor PresetReverbSw::kDescriptor = { |
| 77 | .common = {.id = {.type = kPresetReverbTypeUUID, |
| 78 | .uuid = kPresetReverbSwImplUUID, |
| 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"}, |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame^] | 85 | .capability = PresetReverbSw::kCapability}; |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 86 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 87 | ndk::ScopedAStatus PresetReverbSw::getDescriptor(Descriptor* _aidl_return) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 88 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 89 | *_aidl_return = kDescriptor; |
| 90 | return ndk::ScopedAStatus::ok(); |
| 91 | } |
| 92 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 93 | ndk::ScopedAStatus PresetReverbSw::setParameterSpecific(const Parameter::Specific& specific) { |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 94 | RETURN_IF(Parameter::Specific::presetReverb != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 95 | "EffectNotSupported"); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 96 | |
Sham Rathod | 73aa2c3 | 2022-12-20 17:03:40 +0530 | [diff] [blame] | 97 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 98 | |
| 99 | auto& prParam = specific.get<Parameter::Specific::presetReverb>(); |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame^] | 100 | RETURN_IF(!inRange(prParam, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange"); |
Sham Rathod | 73aa2c3 | 2022-12-20 17:03:40 +0530 | [diff] [blame] | 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 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 118 | ndk::ScopedAStatus PresetReverbSw::getParameterSpecific(const Parameter::Id& id, |
| 119 | Parameter::Specific* specific) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 120 | auto tag = id.getTag(); |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 121 | RETURN_IF(Parameter::Id::presetReverbTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
Sham Rathod | 73aa2c3 | 2022-12-20 17:03:40 +0530 | [diff] [blame] | 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 | |
| 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); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 151 | return ndk::ScopedAStatus::ok(); |
| 152 | } |
| 153 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 154 | std::shared_ptr<EffectContext> PresetReverbSw::createContext(const Parameter::Common& common) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 155 | if (mContext) { |
| 156 | LOG(DEBUG) << __func__ << " context already exist"; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 157 | } else { |
| 158 | mContext = std::make_shared<PresetReverbSwContext>(1 /* statusFmqDepth */, common); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 159 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 160 | |
| 161 | return mContext; |
| 162 | } |
| 163 | |
| 164 | std::shared_ptr<EffectContext> PresetReverbSw::getContext() { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 165 | return mContext; |
| 166 | } |
| 167 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 168 | RetCode PresetReverbSw::releaseContext() { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 169 | if (mContext) { |
| 170 | mContext.reset(); |
| 171 | } |
| 172 | return RetCode::SUCCESS; |
| 173 | } |
| 174 | |
| 175 | // Processing method running in EffectWorker thread. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 176 | IEffect::Status PresetReverbSw::effectProcessImpl(float* in, float* out, int samples) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 177 | // TODO: get data buffer and process. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 178 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples; |
| 179 | for (int i = 0; i < samples; i++) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 180 | *out++ = *in++; |
| 181 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 182 | return {STATUS_OK, samples, samples}; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | } // namespace aidl::android::hardware::audio::effect |