blob: 65a80029cbedb92ad46493f890c644bac30256d7 [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 Yaoa4ab38c2022-10-14 01:07:47 +000071 private:
Shunkai Yao6afc8552022-10-26 22:47:20 +000072 static const int NUM_OF_BANDS = 5;
73 static const int NUM_OF_PRESETS = 10;
74 static const int PRESET_CUSTOM = -1;
75 // preset band level
76 int mPreset = PRESET_CUSTOM;
77 int32_t mBandLevels[NUM_OF_BANDS] = {3, 0, 0, 0, 3};
78
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000079 // Add equalizer specific context for processing here
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000080};
81
Shunkai Yao6c370642022-11-08 02:42:09 +000082class EqualizerSw final : public EffectImpl {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000083 public:
Shunkai Yaoc12e0822022-12-12 07:13:58 +000084 static const std::string kEffectName;
85 static const std::vector<Equalizer::BandFrequency> kBandFrequency;
86 static const std::vector<Equalizer::Preset> kPresets;
87 static const Equalizer::Capability kEqCap;
88 static const Descriptor kDesc;
89
Shunkai Yao6afc8552022-10-26 22:47:20 +000090 EqualizerSw() { LOG(DEBUG) << __func__; }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000091 ~EqualizerSw() {
Shunkai Yao812d5b42022-11-16 18:08:50 +000092 cleanUp();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000093 LOG(DEBUG) << __func__;
Shunkai Yao6afc8552022-10-26 22:47:20 +000094 }
95
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000096 ndk::ScopedAStatus getDescriptor(Descriptor* _aidl_return) override;
Shunkai Yao6afc8552022-10-26 22:47:20 +000097 ndk::ScopedAStatus setParameterSpecific(const Parameter::Specific& specific) override;
98 ndk::ScopedAStatus getParameterSpecific(const Parameter::Id& id,
99 Parameter::Specific* specific) override;
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530100
Shunkai Yao6afc8552022-10-26 22:47:20 +0000101 std::shared_ptr<EffectContext> createContext(const Parameter::Common& common) override;
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530102 std::shared_ptr<EffectContext> getContext() override;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000103 RetCode releaseContext() override;
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000104
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530105 IEffect::Status effectProcessImpl(float* in, float* out, int samples) override;
106 std::string getEffectName() override { return kEffectName; }
107
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000108 private:
Shunkai Yao6afc8552022-10-26 22:47:20 +0000109 ndk::ScopedAStatus getParameterEqualizer(const Equalizer::Tag& tag,
110 Parameter::Specific* specific);
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000111 std::shared_ptr<EqualizerSwContext> mContext;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000112};
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000113
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000114} // namespace aidl::android::hardware::audio::effect