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