1 /*
2  * Copyright (C) 2023 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 <android/binder_enums.h>
21 #include <audio_effects/effect_environmentalreverb.h>
22 #include <audio_effects/effect_presetreverb.h>
23 #include <system/audio_effects/effect_uuid.h>
24 
25 #include "effect-impl/EffectTypes.h"
26 // from Reverb/lib
27 #include "LVREV.h"
28 
29 namespace aidl::android::hardware::audio::effect {
30 namespace lvm {
31 
32 constexpr inline int kMaxCallSize = 256;
33 constexpr inline int kMinLevel = -6000;
34 constexpr inline int kMaxT60 = 7000; /* Maximum decay time */
35 constexpr inline int kMaxReverbLevel = 2000;
36 constexpr inline int kMaxFrameSize = 2560;
37 constexpr inline int kCpuLoadARM9E = 470;                      // Expressed in 0.1 MIPS
38 constexpr inline int kMemUsage = (71 + (kMaxFrameSize >> 7));  // Expressed in kB
39 
40 static const std::vector<Range::EnvironmentalReverbRange> kEnvReverbRanges = {
41         MAKE_RANGE(EnvironmentalReverb, roomLevelMb, lvm::kMinLevel, 0),
42         MAKE_RANGE(EnvironmentalReverb, roomHfLevelMb, -4000, 0),
43         MAKE_RANGE(EnvironmentalReverb, decayTimeMs, 0, lvm::kMaxT60),
44         MAKE_RANGE(EnvironmentalReverb, decayHfRatioPm, 100, 2000),
45         MAKE_RANGE(EnvironmentalReverb, levelMb, lvm::kMinLevel, 0),
46         MAKE_RANGE(EnvironmentalReverb, delayMs, 0, 65),
47         MAKE_RANGE(EnvironmentalReverb, diffusionPm, 0, 1000),
48         MAKE_RANGE(EnvironmentalReverb, densityPm, 0, 1000)};
49 static const Capability kEnvReverbCap = {
50         .range = Range::make<Range::environmentalReverb>(kEnvReverbRanges)};
51 
52 // NXP SW auxiliary environmental reverb
53 static const std::string kAuxEnvReverbEffectName = "Auxiliary Environmental Reverb";
54 static const Descriptor kAuxEnvReverbDesc = {
55         .common = {.id = {.type = getEffectTypeUuidEnvReverb(),
56                           .uuid = getEffectImplUuidAuxEnvReverb(),
57                           .proxy = std::nullopt},
58                    .flags = {.type = Flags::Type::AUXILIARY},
59                    .cpuLoad = kCpuLoadARM9E,
60                    .memoryUsage = kMemUsage,
61                    .name = kAuxEnvReverbEffectName,
62                    .implementor = "NXP Software Ltd."},
63         .capability = kEnvReverbCap};
64 
65 // NXP SW insert environmental reverb
66 static const std::string kInsertEnvReverbEffectName = "Insert Environmental Reverb";
67 static const Descriptor kInsertEnvReverbDesc = {
68         .common = {.id = {.type = getEffectTypeUuidEnvReverb(),
69                           .uuid = getEffectImplUuidInsertEnvReverb(),
70                           .proxy = std::nullopt},
71                    .flags = {.type = Flags::Type::INSERT,
72                              .insert = Flags::Insert::FIRST,
73                              .volume = Flags::Volume::CTRL},
74                    .cpuLoad = kCpuLoadARM9E,
75                    .memoryUsage = kMemUsage,
76                    .name = kInsertEnvReverbEffectName,
77                    .implementor = "NXP Software Ltd."},
78         .capability = kEnvReverbCap};
79 
80 static const std::vector<PresetReverb::Presets> kSupportedPresets{
81         ndk::enum_range<PresetReverb::Presets>().begin(),
82         ndk::enum_range<PresetReverb::Presets>().end()};
83 static const std::vector<Range::PresetReverbRange> kPresetReverbRanges = {
84         MAKE_RANGE(PresetReverb, supportedPresets, kSupportedPresets, kSupportedPresets)};
85 static const Capability kPresetReverbCap = {
86         .range = Range::make<Range::presetReverb>(kPresetReverbRanges)};
87 
88 // NXP SW auxiliary preset reverb
89 static const std::string kAuxPresetReverbEffectName = "Auxiliary Preset Reverb";
90 static const Descriptor kAuxPresetReverbDesc = {
91         .common = {.id = {.type = getEffectTypeUuidPresetReverb(),
92                           .uuid = getEffectImplUuidAuxPresetReverb(),
93                           .proxy = std::nullopt},
94                    .flags = {.type = Flags::Type::AUXILIARY},
95                    .cpuLoad = kCpuLoadARM9E,
96                    .memoryUsage = kMemUsage,
97                    .name = kAuxPresetReverbEffectName,
98                    .implementor = "NXP Software Ltd."},
99         .capability = kPresetReverbCap};
100 
101 // NXP SW insert preset reverb
102 static const std::string kInsertPresetReverbEffectName = "Insert Preset Reverb";
103 static const Descriptor kInsertPresetReverbDesc = {
104         .common = {.id = {.type = getEffectTypeUuidPresetReverb(),
105                           .uuid = getEffectImplUuidInsertPresetReverb(),
106                           .proxy = std::nullopt},
107                    .flags = {.type = Flags::Type::INSERT,
108                              .insert = Flags::Insert::FIRST,
109                              .volume = Flags::Volume::CTRL},
110                    .cpuLoad = kCpuLoadARM9E,
111                    .memoryUsage = kMemUsage,
112                    .name = kInsertPresetReverbEffectName,
113                    .implementor = "NXP Software Ltd."},
114         .capability = kPresetReverbCap};
115 
116 enum class ReverbEffectType {
117     AUX_ENV,
118     INSERT_ENV,
119     AUX_PRESET,
120     INSERT_PRESET,
121 };
122 
123 inline std::ostream& operator<<(std::ostream& out, const ReverbEffectType& type) {
124     switch (type) {
125         case ReverbEffectType::AUX_ENV:
126             return out << kAuxEnvReverbEffectName;
127         case ReverbEffectType::INSERT_ENV:
128             return out << kInsertEnvReverbEffectName;
129         case ReverbEffectType::AUX_PRESET:
130             return out << kAuxPresetReverbEffectName;
131         case ReverbEffectType::INSERT_PRESET:
132             return out << kInsertPresetReverbEffectName;
133     }
134     return out << "EnumReverbEffectTypeError";
135 }
136 
137 inline std::ostream& operator<<(std::ostream& out, const LVREV_ReturnStatus_en& status) {
138     switch (status) {
139         case LVREV_SUCCESS:
140             return out << "LVREV_SUCCESS";
141         case LVREV_NULLADDRESS:
142             return out << "LVREV_NULLADDRESS";
143         case LVREV_OUTOFRANGE:
144             return out << "LVREV_OUTOFRANGE";
145         case LVREV_INVALIDNUMSAMPLES:
146             return out << "LVREV_INVALIDNUMSAMPLES";
147         case LVREV_RETURNSTATUS_DUMMY:
148             return out << "LVREV_RETURNSTATUS_DUMMY";
149     }
150     return out << "EnumLvrevRetStatusError";
151 }
152 
153 }  // namespace lvm
154 }  // namespace aidl::android::hardware::audio::effect
155