blob: f8987c718b307e450fc357ef36f81eedebedd873 [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) {
Shunkai Yao87811022023-02-13 17:40:37 +000037 if (presetIdx < 0 || presetIdx >= kMaxPresetNumber) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000038 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) {
Shunkai Yao87811022023-02-13 17:40:37 +000046 if (bandLevels.size() > kMaxBandNumber) {
47 LOG(ERROR) << __func__ << " return because size exceed " << kMaxBandNumber;
Shunkai Yao6afc8552022-10-26 22:47:20 +000048 return RetCode::ERROR_ILLEGAL_PARAMETER;
49 }
50 RetCode ret = RetCode::SUCCESS;
51 for (auto& it : bandLevels) {
Shunkai Yao87811022023-02-13 17:40:37 +000052 if (it.index >= kMaxBandNumber || it.index < 0) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000053 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;
Shunkai Yao87811022023-02-13 17:40:37 +000065 for (int i = 0; i < kMaxBandNumber; i++) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000066 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 }
Shunkai Yao87811022023-02-13 17:40:37 +000074 static const int kMaxBandNumber = 5;
75 static const int kMaxPresetNumber = 10;
76 static const int kCustomPreset = -1;
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000077
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000078 private:
Shunkai Yao87811022023-02-13 17:40:37 +000079 static constexpr std::array<uint16_t, kMaxBandNumber> kPresetsFrequencies = {60, 230, 910, 3600,
80 14000};
Shunkai Yao6afc8552022-10-26 22:47:20 +000081 // preset band level
Shunkai Yao87811022023-02-13 17:40:37 +000082 int mPreset = kCustomPreset;
83 int32_t mBandLevels[kMaxBandNumber] = {3, 0, 0, 0, 3};
Shunkai Yao6afc8552022-10-26 22:47:20 +000084
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;
Shunkai Yao87811022023-02-13 17:40:37 +000091 static const Capability kEqCap;
Shunkai Yaoc12e0822022-12-12 07:13:58 +000092 static const Descriptor kDesc;
93
Shunkai Yao6afc8552022-10-26 22:47:20 +000094 EqualizerSw() { LOG(DEBUG) << __func__; }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000095 ~EqualizerSw() {
Shunkai Yao812d5b42022-11-16 18:08:50 +000096 cleanUp();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000097 LOG(DEBUG) << __func__;
Shunkai Yao6afc8552022-10-26 22:47:20 +000098 }
99
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000100 ndk::ScopedAStatus getDescriptor(Descriptor* _aidl_return) override;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000101 ndk::ScopedAStatus setParameterSpecific(const Parameter::Specific& specific) override;
102 ndk::ScopedAStatus getParameterSpecific(const Parameter::Id& id,
103 Parameter::Specific* specific) override;
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530104
Shunkai Yao6afc8552022-10-26 22:47:20 +0000105 std::shared_ptr<EffectContext> createContext(const Parameter::Common& common) override;
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530106 std::shared_ptr<EffectContext> getContext() override;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000107 RetCode releaseContext() override;
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000108
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530109 IEffect::Status effectProcessImpl(float* in, float* out, int samples) override;
110 std::string getEffectName() override { return kEffectName; }
111
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000112 private:
Shunkai Yao87811022023-02-13 17:40:37 +0000113 static const std::vector<Equalizer::BandFrequency> kBandFrequency;
114 static const std::vector<Equalizer::Preset> kPresets;
115 static const std::vector<Range::EqualizerRange> kRanges;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000116 ndk::ScopedAStatus getParameterEqualizer(const Equalizer::Tag& tag,
117 Parameter::Specific* specific);
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000118 std::shared_ptr<EqualizerSwContext> mContext;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000119};
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000120
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000121} // namespace aidl::android::hardware::audio::effect