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