1 /*
2  * Copyright (C) 2010 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 #ifndef ANDROID_EFFECTBUNDLE_H_
18 #define ANDROID_EFFECTBUNDLE_H_
19 
20 #include <audio_effects/effect_bassboost.h>
21 #include <audio_effects/effect_equalizer.h>
22 #include <audio_effects/effect_virtualizer.h>
23 #include <LVM.h>
24 #include <limits.h>
25 
26 #define FIVEBAND_NUMBANDS          5
27 #define MAX_NUM_BANDS              5
28 #define MAX_CALL_SIZE              256
29 #define LVM_MAX_SESSIONS           32
30 #define LVM_UNUSED_SESSION         INT_MAX
31 #define BASS_BOOST_CUP_LOAD_ARM9E  150    // Expressed in 0.1 MIPS
32 #define VIRTUALIZER_CUP_LOAD_ARM9E 120    // Expressed in 0.1 MIPS
33 #define EQUALIZER_CUP_LOAD_ARM9E   220    // Expressed in 0.1 MIPS
34 #define VOLUME_CUP_LOAD_ARM9E      0      // Expressed in 0.1 MIPS
35 #define BUNDLE_MEM_USAGE           25     // Expressed in kB
36 
37 #ifndef OPENSL_ES_H_
38 static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
39                                             { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
40 const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
41 #endif //OPENSL_ES_H_
42 
43 typedef enum
44 {
45     LVM_BASS_BOOST,
46     LVM_VIRTUALIZER,
47     LVM_EQUALIZER,
48     LVM_VOLUME
49 } lvm_effect_en;
50 
51 // Preset configuration.
52 struct PresetConfig {
53     // Human-readable name.
54     const char * name;
55     // An array of size nBands where each element is a configuration for the
56     // corresponding band.
57     //const BandConfig * bandConfigs;
58 };
59 
60 /* BundledEffectContext : One per session */
61 struct BundledEffectContext{
62     LVM_Handle_t                    hInstance;                /* Instance handle */
63     int                             SessionNo;                /* Current session number */
64     int                             SessionId;                /* Current session id */
65     bool                            bVolumeEnabled;           /* Flag for Volume */
66     bool                            bEqualizerEnabled;        /* Flag for EQ */
67     bool                            bBassEnabled;             /* Flag for Bass */
68     bool                            bBassTempDisabled;        /* Flag for Bass to be re-enabled */
69     bool                            bVirtualizerEnabled;      /* Flag for Virtualizer */
70     bool                            bVirtualizerTempDisabled; /* Flag for effect to be re-enabled */
71     audio_devices_t                 nOutputDevice;            /* Output device for the effect */
72     audio_devices_t                 nVirtualizerForcedDevice; /* Forced device virtualization mode*/
73     int                             NumberEffectsEnabled;     /* Effects in this session */
74     int                             NumberEffectsCalled;      /* Effects called so far */
75     bool                            firstVolume;              /* No smoothing on first Vol change */
76     // Saved parameters for each effect */
77     // Bass Boost
78     int                             BassStrengthSaved;        /* Conversion between Get/Set */
79     // Equalizer
80     int                             CurPreset;                /* Current preset being used */
81     // Virtualzer
82     int                             VirtStrengthSaved;        /* Conversion between Get/Set */
83     // Volume
84     int                             levelSaved;     /* for when mute is set, level must be saved */
85     int                             positionSaved;
86     bool                            bMuteEnabled;   /* Must store as mute = -96dB level */
87     bool                            bStereoPositionEnabled;
88     LVM_Fs_en                       SampleRate;
89     int                             SamplesPerSecond;
90     int                             SamplesToExitCountEq;
91     int                             SamplesToExitCountBb;
92     int                             SamplesToExitCountVirt;
93     effect_buffer_t                 *workBuffer;
94     int                             frameCount;
95     int32_t                         bandGaindB[FIVEBAND_NUMBANDS];
96     int                             volume;
97 #ifdef SUPPORT_MC
98     LVM_INT32                       ChMask;
99 #endif
100 
101     /* Bitmask whether drain is in progress due to disabling the effect.
102        The corresponding bit to an effect is set by 1 << lvm_effect_en. */
103     int                             effectInDrain;
104 
105     /* Bitmask whether process() was called for a particular effect.
106        The corresponding bit to an effect is set by 1 << lvm_effect_en. */
107     int                             effectProcessCalled;
108 };
109 
110 /* SessionContext : One session */
111 struct SessionContext{
112     bool                            bBundledEffectsEnabled;
113     bool                            bVolumeInstantiated;
114     bool                            bEqualizerInstantiated;
115     bool                            bBassInstantiated;
116     bool                            bVirtualizerInstantiated;
117     BundledEffectContext            *pBundledContext;
118 };
119 
120 struct EffectContext{
121     const struct effect_interface_s *itfe;
122     effect_config_t                 config;
123     lvm_effect_en                   EffectType;
124     BundledEffectContext            *pBundledContext;
125 };
126 
127 /* enumerated parameter settings for Volume effect */
128 typedef enum
129 {
130     VOLUME_PARAM_LEVEL,                       // type SLmillibel = typedef SLuint16 (set & get)
131     VOLUME_PARAM_MAXLEVEL,                    // type SLmillibel = typedef SLuint16 (get)
132     VOLUME_PARAM_MUTE,                        // type SLboolean  = typedef SLuint32 (set & get)
133     VOLUME_PARAM_ENABLESTEREOPOSITION,        // type SLboolean  = typedef SLuint32 (set & get)
134     VOLUME_PARAM_STEREOPOSITION,              // type SLpermille = typedef SLuint16 (set & get)
135 } t_volume_params;
136 
137 static const int PRESET_CUSTOM = -1;
138 
139 static const uint32_t bandFreqRange[FIVEBAND_NUMBANDS][2] = {
140                                        {30000, 120000},
141                                        {120001, 460000},
142                                        {460001, 1800000},
143                                        {1800001, 7000000},
144                                        {7000001, 20000000}};
145 
146 //Note: If these frequencies change, please update LimitLevel values accordingly.
147 static const LVM_UINT16  EQNB_5BandPresetsFrequencies[] = {
148                                        60,           /* Frequencies in Hz */
149                                        230,
150                                        910,
151                                        3600,
152                                        14000};
153 
154 static const LVM_UINT16 EQNB_5BandPresetsQFactors[] = {
155                                        96,               /* Q factor multiplied by 100 */
156                                        96,
157                                        96,
158                                        96,
159                                        96};
160 
161 static const LVM_INT16 EQNB_5BandNormalPresets[] = {
162                                        3, 0, 0, 0, 3,       /* Normal Preset */
163                                        8, 5, -3, 5, 6,      /* Classical Preset */
164                                        15, -6, 7, 13, 10,   /* Dance Preset */
165                                        0, 0, 0, 0, 0,       /* Flat Preset */
166                                        6, -2, -2, 6, -3,    /* Folk Preset */
167                                        8, -8, 13, -1, -4,   /* Heavy Metal Preset */
168                                        10, 6, -4, 5, 8,     /* Hip Hop Preset */
169                                        8, 5, -4, 5, 9,      /* Jazz Preset */
170                                       -6, 4, 9, 4, -5,      /* Pop Preset */
171                                        10, 6, -1, 8, 10};   /* Rock Preset */
172 
173 static const LVM_INT16 EQNB_5BandSoftPresets[] = {
174                                         3, 0, 0, 0, 3,      /* Normal Preset */
175                                         5, 3, -2, 4, 4,     /* Classical Preset */
176                                         6, 0, 2, 4, 1,      /* Dance Preset */
177                                         0, 0, 0, 0, 0,      /* Flat Preset */
178                                         3, 0, 0, 2, -1,     /* Folk Preset */
179                                         4, 1, 9, 3, 0,      /* Heavy Metal Preset */
180                                         5, 3, 0, 1, 3,      /* Hip Hop Preset */
181                                         4, 2, -2, 2, 5,     /* Jazz Preset */
182                                        -1, 2, 5, 1, -2,     /* Pop Preset */
183                                         5, 3, -1, 3, 5};    /* Rock Preset */
184 
185 static const PresetConfig gEqualizerPresets[] = {
186                                         {"Normal"},
187                                         {"Classical"},
188                                         {"Dance"},
189                                         {"Flat"},
190                                         {"Folk"},
191                                         {"Heavy Metal"},
192                                         {"Hip Hop"},
193                                         {"Jazz"},
194                                         {"Pop"},
195                                         {"Rock"}};
196 
197 /* The following tables have been computed using the actual levels measured by the output of
198  * white noise or pink noise (IEC268-1) for the EQ and BassBoost Effects. These are estimates of
199  * the actual energy that 'could' be present in the given band.
200  * If the frequency values in EQNB_5BandPresetsFrequencies change, these values might need to be
201  * updated.
202  */
203 
204 static const float LimitLevel_bandEnergyCoefficient[FIVEBAND_NUMBANDS] = {
205         7.56, 9.69, 9.59, 7.37, 2.88};
206 
207 static const float LimitLevel_bandEnergyCrossCoefficient[FIVEBAND_NUMBANDS-1] = {
208         126.0, 115.0, 125.0, 104.0 };
209 
210 static const float LimitLevel_bassBoostEnergyCrossCoefficient[FIVEBAND_NUMBANDS] = {
211         221.21, 208.10, 28.16, 0.0, 0.0 };
212 
213 static const float LimitLevel_bassBoostEnergyCoefficient = 9.00;
214 
215 static const float LimitLevel_virtualizerContribution = 1.9;
216 
217 #endif /*ANDROID_EFFECTBUNDLE_H_*/
218