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 #pragma once
18 
19 #include <aidl/android/hardware/audio/effect/BnEffect.h>
20 #include <fmq/AidlMessageQueue.h>
21 #include <cstdlib>
22 #include <memory>
23 
24 #include "effect-impl/EffectImpl.h"
25 
26 namespace aidl::android::hardware::audio::effect {
27 
28 class EqualizerSwContext final : public EffectContext {
29   public:
EqualizerSwContext(int statusDepth,const Parameter::Common & common)30     EqualizerSwContext(int statusDepth, const Parameter::Common& common)
31         : EffectContext(statusDepth, common) {
32         LOG(DEBUG) << __func__;
33     }
34 
setEqPreset(const int & presetIdx)35     RetCode setEqPreset(const int& presetIdx) {
36         if (presetIdx < 0 || presetIdx >= kMaxPresetNumber) {
37             return RetCode::ERROR_ILLEGAL_PARAMETER;
38         }
39         mPreset = presetIdx;
40         return RetCode::SUCCESS;
41     }
getEqPreset()42     int getEqPreset() { return mPreset; }
43 
setEqBandLevels(const std::vector<Equalizer::BandLevel> & bandLevels)44     RetCode setEqBandLevels(const std::vector<Equalizer::BandLevel>& bandLevels) {
45         if (bandLevels.size() > kMaxBandNumber) {
46             LOG(ERROR) << __func__ << " return because size exceed " << kMaxBandNumber;
47             return RetCode::ERROR_ILLEGAL_PARAMETER;
48         }
49         RetCode ret = RetCode::SUCCESS;
50         for (auto& it : bandLevels) {
51             if (it.index >= kMaxBandNumber || it.index < 0) {
52                 LOG(ERROR) << __func__ << " index illegal, skip: " << it.index << " - "
53                            << it.levelMb;
54                 ret = RetCode::ERROR_ILLEGAL_PARAMETER;
55             } else {
56                 mBandLevels[it.index] = it.levelMb;
57             }
58         }
59         return ret;
60     }
61 
getEqBandLevels()62     std::vector<Equalizer::BandLevel> getEqBandLevels() {
63         std::vector<Equalizer::BandLevel> bandLevels;
64         for (int i = 0; i < kMaxBandNumber; i++) {
65             bandLevels.push_back({i, mBandLevels[i]});
66         }
67         return bandLevels;
68     }
69 
getCenterFreqs()70     std::vector<int> getCenterFreqs() {
71         return {std::begin(kPresetsFrequencies), std::end(kPresetsFrequencies)};
72     }
73     static const int kMaxBandNumber = 5;
74     static const int kMaxPresetNumber = 10;
75     static const int kCustomPreset = -1;
76 
77   private:
78     static constexpr std::array<uint16_t, kMaxBandNumber> kPresetsFrequencies = {60, 230, 910, 3600,
79                                                                                  14000};
80     // preset band level
81     int mPreset = kCustomPreset;
82     int32_t mBandLevels[kMaxBandNumber] = {3, 0, 0, 0, 3};
83 
84     // Add equalizer specific context for processing here
85 };
86 
87 class EqualizerSw final : public EffectImpl {
88   public:
89     static const std::string kEffectName;
90     static const Capability kEqCap;
91     static const Descriptor kDesc;
92 
EqualizerSw()93     EqualizerSw() { LOG(DEBUG) << __func__; }
~EqualizerSw()94     ~EqualizerSw() {
95         cleanUp();
96         LOG(DEBUG) << __func__;
97     }
98 
99     ndk::ScopedAStatus getDescriptor(Descriptor* _aidl_return) override;
100     ndk::ScopedAStatus setParameterSpecific(const Parameter::Specific& specific)
101             REQUIRES(mImplMutex) override;
102     ndk::ScopedAStatus getParameterSpecific(const Parameter::Id& id, Parameter::Specific* specific)
103             REQUIRES(mImplMutex) override;
104 
105     std::shared_ptr<EffectContext> createContext(const Parameter::Common& common)
106             REQUIRES(mImplMutex) override;
107     RetCode releaseContext() REQUIRES(mImplMutex) override;
108 
109     IEffect::Status effectProcessImpl(float* in, float* out, int samples)
110             REQUIRES(mImplMutex) override;
getEffectName()111     std::string getEffectName() override { return kEffectName; }
112 
113   private:
114     static const std::vector<Equalizer::BandFrequency> kBandFrequency;
115     static const std::vector<Equalizer::Preset> kPresets;
116     static const std::vector<Range::EqualizerRange> kRanges;
117     ndk::ScopedAStatus getParameterEqualizer(const Equalizer::Tag& tag,
118                                              Parameter::Specific* specific) REQUIRES(mImplMutex);
119     std::shared_ptr<EqualizerSwContext> mContext;
120 };
121 
122 }  // namespace aidl::android::hardware::audio::effect
123