blob: 56af3b5821e7e0278908db9406ca431b960f2cd0 [file] [log] [blame]
Shunkai Yaoea24c1a2022-09-28 17:39:23 +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
17#pragma once
18
19#include <aidl/android/hardware/audio/effect/BnEffect.h>
20#include <fmq/AidlMessageQueue.h>
21#include <cstdlib>
22#include <memory>
23
Shunkai Yao6afc8552022-10-26 22:47:20 +000024#include "effect-impl/EffectImpl.h"
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000025
26namespace aidl::android::hardware::audio::effect {
27
Shunkai Yao6c370642022-11-08 02:42:09 +000028class EqualizerSwContext final : public EffectContext {
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000029 public:
Shunkai Yao6afc8552022-10-26 22:47:20 +000030 EqualizerSwContext(int statusDepth, const Parameter::Common& common)
31 : EffectContext(statusDepth, common) {
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000032 LOG(DEBUG) << __func__;
33 }
34
Shunkai Yao6afc8552022-10-26 22:47:20 +000035 RetCode setEqPreset(const int& presetIdx) {
Shunkai Yao87811022023-02-13 17:40:37 +000036 if (presetIdx < 0 || presetIdx >= kMaxPresetNumber) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000037 return RetCode::ERROR_ILLEGAL_PARAMETER;
38 }
39 mPreset = presetIdx;
40 return RetCode::SUCCESS;
41 }
42 int getEqPreset() { return mPreset; }
43
44 RetCode setEqBandLevels(const std::vector<Equalizer::BandLevel>& bandLevels) {
Shunkai Yao87811022023-02-13 17:40:37 +000045 if (bandLevels.size() > kMaxBandNumber) {
46 LOG(ERROR) << __func__ << " return because size exceed " << kMaxBandNumber;
Shunkai Yao6afc8552022-10-26 22:47:20 +000047 return RetCode::ERROR_ILLEGAL_PARAMETER;
48 }
49 RetCode ret = RetCode::SUCCESS;
50 for (auto& it : bandLevels) {
Shunkai Yao87811022023-02-13 17:40:37 +000051 if (it.index >= kMaxBandNumber || it.index < 0) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000052 LOG(ERROR) << __func__ << " index illegal, skip: " << it.index << " - "
53 << it.levelMb;
54 ret = RetCode::ERROR_ILLEGAL_PARAMETER;
Shunkai Yaocb0fc412022-12-15 20:34:32 +000055 } else {
56 mBandLevels[it.index] = it.levelMb;
Shunkai Yao6afc8552022-10-26 22:47:20 +000057 }
Shunkai Yao6afc8552022-10-26 22:47:20 +000058 }
59 return ret;
60 }
61
62 std::vector<Equalizer::BandLevel> getEqBandLevels() {
63 std::vector<Equalizer::BandLevel> bandLevels;
Shunkai Yao87811022023-02-13 17:40:37 +000064 for (int i = 0; i < kMaxBandNumber; i++) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000065 bandLevels.push_back({i, mBandLevels[i]});
66 }
67 return bandLevels;
68 }
69
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000070 std::vector<int> getCenterFreqs() {
71 return {std::begin(kPresetsFrequencies), std::end(kPresetsFrequencies)};
72 }
Shunkai Yao87811022023-02-13 17:40:37 +000073 static const int kMaxBandNumber = 5;
74 static const int kMaxPresetNumber = 10;
75 static const int kCustomPreset = -1;
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000076
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000077 private:
Shunkai Yao87811022023-02-13 17:40:37 +000078 static constexpr std::array<uint16_t, kMaxBandNumber> kPresetsFrequencies = {60, 230, 910, 3600,
79 14000};
Shunkai Yao6afc8552022-10-26 22:47:20 +000080 // preset band level
Shunkai Yao87811022023-02-13 17:40:37 +000081 int mPreset = kCustomPreset;
82 int32_t mBandLevels[kMaxBandNumber] = {3, 0, 0, 0, 3};
Shunkai Yao6afc8552022-10-26 22:47:20 +000083
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000084 // Add equalizer specific context for processing here
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000085};
86
Shunkai Yao6c370642022-11-08 02:42:09 +000087class EqualizerSw final : public EffectImpl {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000088 public:
Shunkai Yaoc12e0822022-12-12 07:13:58 +000089 static const std::string kEffectName;
Shunkai Yao87811022023-02-13 17:40:37 +000090 static const Capability kEqCap;
Shunkai Yaoc12e0822022-12-12 07:13:58 +000091 static const Descriptor kDesc;
92
Shunkai Yao6afc8552022-10-26 22:47:20 +000093 EqualizerSw() { LOG(DEBUG) << __func__; }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000094 ~EqualizerSw() {
Shunkai Yao812d5b42022-11-16 18:08:50 +000095 cleanUp();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000096 LOG(DEBUG) << __func__;
Shunkai Yao6afc8552022-10-26 22:47:20 +000097 }
98
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000099 ndk::ScopedAStatus getDescriptor(Descriptor* _aidl_return) override;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000100 ndk::ScopedAStatus setParameterSpecific(const Parameter::Specific& specific) override;
101 ndk::ScopedAStatus getParameterSpecific(const Parameter::Id& id,
102 Parameter::Specific* specific) override;
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530103
Shunkai Yao6afc8552022-10-26 22:47:20 +0000104 std::shared_ptr<EffectContext> createContext(const Parameter::Common& common) override;
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530105 std::shared_ptr<EffectContext> getContext() override;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000106 RetCode releaseContext() override;
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000107
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530108 IEffect::Status effectProcessImpl(float* in, float* out, int samples) override;
109 std::string getEffectName() override { return kEffectName; }
110
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000111 private:
Shunkai Yao87811022023-02-13 17:40:37 +0000112 static const std::vector<Equalizer::BandFrequency> kBandFrequency;
113 static const std::vector<Equalizer::Preset> kPresets;
114 static const std::vector<Range::EqualizerRange> kRanges;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000115 ndk::ScopedAStatus getParameterEqualizer(const Equalizer::Tag& tag,
116 Parameter::Specific* specific);
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000117 std::shared_ptr<EqualizerSwContext> mContext;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000118};
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000119
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000120} // namespace aidl::android::hardware::audio::effect