blob: aa4587a4c0b6ee6bdb0cb100e853c742495ec9da [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;
56 }
57 mBandLevels[it.index] = it.levelMb;
58 }
59 return ret;
60 }
61
62 std::vector<Equalizer::BandLevel> getEqBandLevels() {
63 std::vector<Equalizer::BandLevel> bandLevels;
64 for (int i = 0; i < NUM_OF_BANDS; i++) {
65 bandLevels.push_back({i, mBandLevels[i]});
66 }
67 return bandLevels;
68 }
69
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000070 private:
Shunkai Yao6afc8552022-10-26 22:47:20 +000071 static const int NUM_OF_BANDS = 5;
72 static const int NUM_OF_PRESETS = 10;
73 static const int PRESET_CUSTOM = -1;
74 // preset band level
75 int mPreset = PRESET_CUSTOM;
76 int32_t mBandLevels[NUM_OF_BANDS] = {3, 0, 0, 0, 3};
77
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000078 // Add equalizer specific context for processing here
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000079};
80
Shunkai Yao6c370642022-11-08 02:42:09 +000081class EqualizerSw final : public EffectImpl {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000082 public:
Shunkai Yao6afc8552022-10-26 22:47:20 +000083 EqualizerSw() { LOG(DEBUG) << __func__; }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000084 ~EqualizerSw() {
Shunkai Yao812d5b42022-11-16 18:08:50 +000085 cleanUp();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000086 LOG(DEBUG) << __func__;
Shunkai Yao6afc8552022-10-26 22:47:20 +000087 }
88
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000089 ndk::ScopedAStatus getDescriptor(Descriptor* _aidl_return) override;
Shunkai Yao6afc8552022-10-26 22:47:20 +000090 ndk::ScopedAStatus setParameterSpecific(const Parameter::Specific& specific) override;
91 ndk::ScopedAStatus getParameterSpecific(const Parameter::Id& id,
92 Parameter::Specific* specific) override;
93 IEffect::Status effectProcessImpl(float* in, float* out, int process) override;
94 std::shared_ptr<EffectContext> createContext(const Parameter::Common& common) override;
95 RetCode releaseContext() override;
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000096
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000097 private:
Shunkai Yao6afc8552022-10-26 22:47:20 +000098 std::shared_ptr<EqualizerSwContext> mContext;
99 /* capabilities */
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000100 const std::vector<Equalizer::BandFrequency> mBandFrequency = {{0, 30000, 120000},
101 {1, 120001, 460000},
102 {2, 460001, 1800000},
103 {3, 1800001, 7000000},
104 {4, 7000001, 20000000}};
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000105 // presets supported by the device
106 const std::vector<Equalizer::Preset> mPresets = {
107 {0, "Normal"}, {1, "Classical"}, {2, "Dance"}, {3, "Flat"}, {4, "Folk"},
108 {5, "Heavy Metal"}, {6, "Hip Hop"}, {7, "Jazz"}, {8, "Pop"}, {9, "Rock"}};
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000109
Shunkai Yao6afc8552022-10-26 22:47:20 +0000110 const Equalizer::Capability kEqCap = {.bandFrequencies = mBandFrequency, .presets = mPresets};
111 // Effect descriptor.
Shunkai Yao812d5b42022-11-16 18:08:50 +0000112 const Descriptor kDesc = {.common = {.id = {.type = kEqualizerTypeUUID,
113 .uuid = kEqualizerSwImplUUID,
114 .proxy = kEqualizerProxyUUID},
Shunkai Yao6afc8552022-10-26 22:47:20 +0000115 .flags = {.type = Flags::Type::INSERT,
116 .insert = Flags::Insert::FIRST,
117 .volume = Flags::Volume::CTRL},
Shunkai Yao812d5b42022-11-16 18:08:50 +0000118 .name = "EqualizerSw",
119 .implementor = "The Android Open Source Project"},
Shunkai Yao6afc8552022-10-26 22:47:20 +0000120 .capability = Capability::make<Capability::equalizer>(kEqCap)};
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000121
Shunkai Yao6afc8552022-10-26 22:47:20 +0000122 ndk::ScopedAStatus getParameterEqualizer(const Equalizer::Tag& tag,
123 Parameter::Specific* specific);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000124};
125} // namespace aidl::android::hardware::audio::effect