blob: fabcfeb54c0e632a46cdede94f5f9e1716b22589 [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 Yaoa4ab38c2022-10-14 01:07:47 +000025#include "effect-impl/EffectUUID.h"
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000026
27namespace aidl::android::hardware::audio::effect {
28
Shunkai Yao6c370642022-11-08 02:42:09 +000029class EqualizerSwContext final : public EffectContext {
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000030 public:
Shunkai Yao6afc8552022-10-26 22:47:20 +000031 EqualizerSwContext(int statusDepth, const Parameter::Common& common)
32 : EffectContext(statusDepth, common) {
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000033 LOG(DEBUG) << __func__;
34 }
35
Shunkai Yao6afc8552022-10-26 22:47:20 +000036 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 Yaocb0fc412022-12-15 20:34:32 +000056 } else {
57 mBandLevels[it.index] = it.levelMb;
Shunkai Yao6afc8552022-10-26 22:47:20 +000058 }
Shunkai Yao6afc8552022-10-26 22:47:20 +000059 }
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 Yao58aaf5b2023-02-06 07:25:09 +000071 std::vector<int> getCenterFreqs() {
72 return {std::begin(kPresetsFrequencies), std::end(kPresetsFrequencies)};
73 }
74
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000075 private:
Shunkai Yao6afc8552022-10-26 22:47:20 +000076 static const int NUM_OF_BANDS = 5;
77 static const int NUM_OF_PRESETS = 10;
78 static const int PRESET_CUSTOM = -1;
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000079 static constexpr std::array<uint16_t, NUM_OF_BANDS> kPresetsFrequencies = {60, 230, 910, 3600,
80 14000};
Shunkai Yao6afc8552022-10-26 22:47:20 +000081 // preset band level
82 int mPreset = PRESET_CUSTOM;
83 int32_t mBandLevels[NUM_OF_BANDS] = {3, 0, 0, 0, 3};
84
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000085 // Add equalizer specific context for processing here
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000086};
87
Shunkai Yao6c370642022-11-08 02:42:09 +000088class EqualizerSw final : public EffectImpl {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000089 public:
Shunkai Yaoc12e0822022-12-12 07:13:58 +000090 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 Yao6afc8552022-10-26 22:47:20 +000096 EqualizerSw() { LOG(DEBUG) << __func__; }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000097 ~EqualizerSw() {
Shunkai Yao812d5b42022-11-16 18:08:50 +000098 cleanUp();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000099 LOG(DEBUG) << __func__;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000100 }
101
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000102 ndk::ScopedAStatus getDescriptor(Descriptor* _aidl_return) override;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000103 ndk::ScopedAStatus setParameterSpecific(const Parameter::Specific& specific) override;
104 ndk::ScopedAStatus getParameterSpecific(const Parameter::Id& id,
105 Parameter::Specific* specific) override;
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530106
Shunkai Yao6afc8552022-10-26 22:47:20 +0000107 std::shared_ptr<EffectContext> createContext(const Parameter::Common& common) override;
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530108 std::shared_ptr<EffectContext> getContext() override;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000109 RetCode releaseContext() override;
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000110
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530111 IEffect::Status effectProcessImpl(float* in, float* out, int samples) override;
112 std::string getEffectName() override { return kEffectName; }
113
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000114 private:
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