1 /*
2  * Copyright (C) 2004-2010 NXP Software
3  * Copyright (C) 2010 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef __LVREV_PRIVATE_H__
19 #define __LVREV_PRIVATE_H__
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 
26 /****************************************************************************************/
27 /*                                                                                      */
28 /*  Includes                                                                            */
29 /*                                                                                      */
30 /****************************************************************************************/
31 #include "LVREV.h"
32 #include "LVREV_Tables.h"
33 #include "BIQUAD.h"
34 #include "Filter.h"
35 #include "VectorArithmetic.h"
36 #include "Mixer.h"
37 #include "LVM_Macros.h"
38 
39 
40 /****************************************************************************************/
41 /*                                                                                      */
42 /*  Defines                                                                             */
43 /*                                                                                      */
44 /****************************************************************************************/
45 #ifndef BUILD_FLOAT
46 /* General */
47 #define ONE_OVER_SQRT_TWO               23170           /* 1/sqrt(2) * 2^15 */
48 #define LVREV_B_8_on_1000            17179869           /* 0.8 * 2^31 */
49 #define LVREV_HEADROOM                   8192           /* -12dB * 2^15 */
50 #define LVREV_2_9_INQ29           1583769190L           /* 2.9 in Q29 format */
51 #define LVREV_MIN3DB                   0x5A82           /* -3dB in Q15 format */
52 #else
53 /* General */
54 #define ONE_OVER_SQRT_TWO            0.707107f           /* 1/sqrt(2) * 2^15 */
55 #define LVREV_B_8_on_1000               0.008f           /* 0.8 * 2^31 */
56 #define LVREV_HEADROOM                   0.25f           /* -12dB * 2^15 */
57 #define LVREV_2_9_INQ29                   2.9f           /* 2.9 in Q29 format */
58 #define LVREV_MIN3DB                0.7079457f           /* -3dB in Q15 format */
59 #endif
60 
61 /* Intenal constants */
62 #define LVREV_LP_Poly_Order                 4
63 #define LVREV_LP_Poly_Shift                 5
64 
65 #ifndef BUILD_FLOAT
66 #define LVREV_T_3_Power_0_on_4          32768
67 #define LVREV_T_3_Power_1_on_4          43125
68 #define LVREV_T_3_Power_2_on_4          56755
69 #define LVREV_T_3_Power_3_on_4          74694
70 #define LVREV_T60_SCALE                306774           /*(32767/7000)<<16 */
71 #define LVREV_T_3_Power_minus0_on_4     32767           /* 3^(-0/4) * 2^15 */
72 #define LVREV_T_3_Power_minus1_on_4     24898           /* 3^(-1/4) * 2^15 */
73 #define LVREV_T_3_Power_minus2_on_4     18919           /* 3^(-2/4) * 2^15 */
74 #define LVREV_T_3_Power_minus3_on_4     14375           /* 3^(-3/4) * 2^15 */
75 #else/*BUILD_FLOAT*/
76 #define LVREV_T60_SCALE                0.000142f           /*(1/7000) */
77 
78 #define LVREV_T_3_Power_0_on_4              1.0f
79 #define LVREV_T_3_Power_1_on_4         1.316074f
80 #define LVREV_T_3_Power_2_on_4         1.732051f
81 #define LVREV_T_3_Power_3_on_4         2.279507f
82 #define LVREV_T_3_Power_minus0_on_4         1.0f        /* 3^(-0/4) * 2^15 */
83 #define LVREV_T_3_Power_minus1_on_4    0.759836f        /* 3^(-1/4) * 2^15 */
84 #define LVREV_T_3_Power_minus2_on_4    0.577350f        /* 3^(-2/4) * 2^15 */
85 #define LVREV_T_3_Power_minus3_on_4    0.438691f        /* 3^(-3/4) * 2^15 */
86 #endif
87 
88 #ifndef HIGHER_FS
89 #define LVREV_MAX_T3_DELAY                2527           /* ((48000 * 120 * LVREV_T_3_Power_minus3_on_4) >> 15) / 1000 */
90 #define LVREV_MAX_T2_DELAY                3326           /* ((48000 * 120 * LVREV_T_3_Power_minus2_on_4) >> 15) / 1000 */
91 #define LVREV_MAX_T1_DELAY                4377           /* ((48000 * 120 * LVREV_T_3_Power_minus1_on_4) >> 15) / 1000 */
92 #define LVREV_MAX_T0_DELAY                5760           /* ((48000 * 120 * LVREV_T_3_Power_minus0_on_4) >> 15) / 1000 */
93 #define LVREV_MAX_AP3_DELAY               1685           /* ((48000 * 120 * LVREV_T_3_Power_minus3_on_4) >> 15) / 1500 */
94 #define LVREV_MAX_AP2_DELAY               2218           /* ((48000 * 120 * LVREV_T_3_Power_minus2_on_4) >> 15) / 1500 */
95 #define LVREV_MAX_AP1_DELAY               2918           /* ((48000 * 120 * LVREV_T_3_Power_minus1_on_4) >> 15) / 1500 */
96 #define LVREV_MAX_AP0_DELAY               3840           /* ((48000 * 120 * LVREV_T_3_Power_minus0_on_4) >> 15) / 1500 */
97 #else
98     /* ((192000 * 120 * LVREV_T_3_Power_minus3_on_4) >> 15) / 1000 */
99 #define LVREV_MAX_T3_DELAY               10108
100     /* ((192000 * 120 * LVREV_T_3_Power_minus2_on_4) >> 15) / 1000 */
101 #define LVREV_MAX_T2_DELAY               13304
102     /* ((192000 * 120 * LVREV_T_3_Power_minus1_on_4) >> 15) / 1000 */
103 #define LVREV_MAX_T1_DELAY               17508
104     /* ((192000 * 120 * LVREV_T_3_Power_minus0_on_4) >> 15) / 1000 */
105 #define LVREV_MAX_T0_DELAY               23040
106     /* ((192000 * 120 * LVREV_T_3_Power_minus3_on_4) >> 15) / 1500 */
107 #define LVREV_MAX_AP3_DELAY               6740
108     /* ((192000 * 120 * LVREV_T_3_Power_minus2_on_4) >> 15) / 1500 */
109 #define LVREV_MAX_AP2_DELAY               8872
110     /* ((192000 * 120 * LVREV_T_3_Power_minus1_on_4) >> 15) / 1500 */
111 #define LVREV_MAX_AP1_DELAY              11672
112     /* ((192000 * 120 * LVREV_T_3_Power_minus0_on_4) >> 15) / 1500 */
113 #define LVREV_MAX_AP0_DELAY              15360
114 #endif
115 
116 #define LVREV_BYPASSMIXER_TC             1000           /* Bypass mixer time constant*/
117 #define LVREV_ALLPASS_TC                 1000           /* All-pass filter time constant */
118 #define LVREV_ALLPASS_TAP_TC             10000           /* All-pass filter dely tap change */
119 #define LVREV_FEEDBACKMIXER_TC            100           /* Feedback mixer time constant*/
120 #define LVREV_OUTPUTGAIN_SHIFT              5           /* Bits shift for output gain correction */
121 
122 /* Parameter limits */
123 #ifndef HIGHER_FS
124 #define LVREV_NUM_FS                        9           /* Number of supported sample rates */
125 #else
126 #define LVREV_NUM_FS                       11           /* Number of supported sample rates */
127 #endif
128 
129 #define LVREV_MAXBLKSIZE_LIMIT             64           /* Maximum block size low limit */
130 #define LVREV_MAX_LEVEL                   100           /* Maximum level, 100% */
131 #define LVREV_MIN_LPF_CORNER               50           /* Low pass filter limits */
132 #define LVREV_MAX_LPF_CORNER            23999
133 #define LVREV_MIN_HPF_CORNER               20           /* High pass filrer limits */
134 #define LVREV_MAX_HPF_CORNER             1000
135 #define LVREV_MAX_T60                    7000           /* Maximum T60 time in ms */
136 #define LVREV_MAX_DENSITY                 100           /* Maximum density, 100% */
137 #define LVREV_MAX_DAMPING                 100           /* Maximum damping, 100% */
138 #define LVREV_MAX_ROOMSIZE                100           /* Maximum room size, 100% */
139 
140 
141 
142 /****************************************************************************************/
143 /*                                                                                      */
144 /*  Structures                                                                          */
145 /*                                                                                      */
146 /****************************************************************************************/
147 #ifndef BUILD_FLOAT
148 /* Fast data structure */
149 typedef struct
150 {
151 
152     Biquad_1I_Order1_Taps_t HPTaps;                     /* High pass filter taps */
153     Biquad_1I_Order1_Taps_t LPTaps;                     /* Low pass filter taps */
154     Biquad_1I_Order1_Taps_t RevLPTaps[4];               /* Reverb low pass filters taps */
155 
156 } LVREV_FastData_st;
157 
158 
159 /* Fast coefficient structure */
160 typedef struct
161 {
162 
163     Biquad_Instance_t       HPCoefs;                    /* High pass filter coefficients */
164     Biquad_Instance_t       LPCoefs;                    /* Low pass filter coefficients */
165     Biquad_Instance_t       RevLPCoefs[4];              /* Reverb low pass filters coefficients */
166 
167 } LVREV_FastCoef_st;
168 
169 
170 /* Instance parameter structure */
171 typedef struct
172 {
173     /* General */
174     LVREV_InstanceParams_st InstanceParams;             /* Initialisation time instance parameters */
175     LVREV_MemoryTable_st    MemoryTable;                /* Memory table */
176     LVREV_ControlParams_st  CurrentParams;              /* Parameters being used */
177     LVREV_ControlParams_st  NewParams;                  /* New parameters from the calling application */
178     LVM_CHAR                bControlPending;            /* Flag to indicate new parameters are available */
179     LVM_CHAR                bFirstControl;              /* Flag to indicate that the control function is called for the first time */
180     LVM_CHAR                bDisableReverb;             /* Flag to indicate that the mix level is 0% and the reverb can be disabled */
181     LVM_INT32               RoomSizeInms;               /* Room size in msec */
182     LVM_INT32               MaxBlkLen;                  /* Maximum block size for internal processing */
183 
184     /* Aligned memory pointers */
185     LVREV_FastData_st       *pFastData;                 /* Fast data memory base address */
186     LVREV_FastCoef_st       *pFastCoef;                 /* Fast coefficient memory base address */
187     LVM_INT32               *pScratchDelayLine[4];      /* Delay line scratch memory */
188     LVM_INT32               *pScratch;                  /* Multi ussge scratch */
189     LVM_INT32               *pInputSave;                /* Reverb block input save for dry/wet mixing*/
190 
191     /* Feedback matrix */
192     Mix_1St_Cll_t           FeedbackMixer[4];           /* Mixer for Pop and Click Supression caused by feedback Gain */
193 
194     /* All-Pass Filter */
195     LVM_INT32               T[4];                       /* Maximum delay size of buffer */
196     LVM_INT32               *pDelay_T[4];               /* Pointer to delay buffers */
197     LVM_INT32               Delay_AP[4];                /* Offset to AP delay buffer start */
198     LVM_INT16               AB_Selection;               /* Smooth from tap A to B when 1 otherwise B to A */
199     LVM_INT32               A_DelaySize[4];             /* A delay length in samples */
200     LVM_INT32               B_DelaySize[4];             /* B delay length in samples */
201     LVM_INT32               *pOffsetA[4];               /* Offset for the A delay tap */
202     LVM_INT32               *pOffsetB[4];               /* Offset for the B delay tap */
203     Mix_2St_Cll_t           Mixer_APTaps[4];            /* Smoothed AP delay mixer */
204     Mix_1St_Cll_t           Mixer_SGFeedback[4];        /* Smoothed SAfeedback gain */
205     Mix_1St_Cll_t           Mixer_SGFeedforward[4];     /* Smoothed AP feedforward gain */
206 
207     /* Output gain */
208     Mix_2St_Cll_t           BypassMixer;                /* Dry/wet mixer */
209     LVM_INT16               Gain;                       /* Gain applied to output to maintain average signal power */
210     Mix_1St_Cll_t           GainMixer;                  /* Gain smoothing */
211 
212 } LVREV_Instance_st;
213 
214 #else /* BUILD_FLOAT */
215 
216 /* Fast data structure */
217 typedef struct
218 {
219     Biquad_1I_Order1_FLOAT_Taps_t HPTaps;                     /* High pass filter taps */
220     Biquad_1I_Order1_FLOAT_Taps_t LPTaps;                     /* Low pass filter taps */
221     Biquad_1I_Order1_FLOAT_Taps_t RevLPTaps[4];               /* Reverb low pass filters taps */
222 
223 } LVREV_FastData_st;
224 
225 
226 /* Fast coefficient structure */
227 typedef struct
228 {
229 
230     Biquad_FLOAT_Instance_t       HPCoefs;              /* High pass filter coefficients */
231     Biquad_FLOAT_Instance_t       LPCoefs;              /* Low pass filter coefficients */
232     Biquad_FLOAT_Instance_t       RevLPCoefs[4];        /* Reverb low pass filters coefficients */
233 
234 } LVREV_FastCoef_st;
235 typedef struct
236 {
237     /* General */
238     LVREV_InstanceParams_st InstanceParams;           /* Initialisation time instance parameters */
239     LVREV_MemoryTable_st    MemoryTable;              /* Memory table */
240     LVREV_ControlParams_st  CurrentParams;            /* Parameters being used */
241     LVREV_ControlParams_st  NewParams;                /* New parameters from the \
242                                                          calling application */
243     LVM_CHAR                bControlPending;          /* Flag to indicate new parameters \
244                                                          are available */
245     LVM_CHAR                bFirstControl;            /* Flag to indicate that the control \
246                                                          function is called for the first time */
247     LVM_CHAR                bDisableReverb;           /* Flag to indicate that the mix level is
248                                                          0% and the reverb can be disabled */
249     LVM_INT32               RoomSizeInms;             /* Room size in msec */
250     LVM_INT32               MaxBlkLen;                /* Maximum block size for internal
251                                                          processing */
252 
253     /* Aligned memory pointers */
254     LVREV_FastData_st       *pFastData;               /* Fast data memory base address */
255     LVREV_FastCoef_st       *pFastCoef;               /* Fast coefficient memory base address */
256     LVM_FLOAT               *pScratchDelayLine[4];    /* Delay line scratch memory */
257     LVM_FLOAT               *pScratch;                /* Multi ussge scratch */
258     LVM_FLOAT               *pInputSave;              /* Reverb block input save for dry/wet
259                                                          mixing*/
260 
261     /* Feedback matrix */
262     Mix_1St_Cll_FLOAT_t     FeedbackMixer[4];         /* Mixer for Pop and Click Supression \
263                                                          caused by feedback Gain */
264 
265 
266     /* All-Pass Filter */
267     LVM_INT32               T[4];                     /* Maximum delay size of buffer */
268     LVM_FLOAT               *pDelay_T[4];             /* Pointer to delay buffers */
269     LVM_INT32               Delay_AP[4];              /* Offset to AP delay buffer start */
270     LVM_INT16               AB_Selection;             /* Smooth from tap A to B when 1 \
271                                                          otherwise B to A */
272     LVM_INT32               A_DelaySize[4];           /* A delay length in samples */
273     LVM_INT32               B_DelaySize[4];           /* B delay length in samples */
274     LVM_FLOAT               *pOffsetA[4];             /* Offset for the A delay tap */
275     LVM_FLOAT               *pOffsetB[4];             /* Offset for the B delay tap */
276     Mix_2St_Cll_FLOAT_t     Mixer_APTaps[4];          /* Smoothed AP delay mixer */
277     Mix_1St_Cll_FLOAT_t     Mixer_SGFeedback[4];      /* Smoothed SAfeedback gain */
278     Mix_1St_Cll_FLOAT_t     Mixer_SGFeedforward[4];   /* Smoothed AP feedforward gain */
279 
280     /* Output gain */
281     Mix_2St_Cll_FLOAT_t     BypassMixer;              /* Dry/wet mixer */
282     LVM_FLOAT               Gain;                     /* Gain applied to output to maintain
283                                                          average signal power */
284     Mix_1St_Cll_FLOAT_t     GainMixer;                /* Gain smoothing */
285 
286 } LVREV_Instance_st;
287 
288 #endif
289 /****************************************************************************************/
290 /*                                                                                      */
291 /*  Function prototypes                                                                 */
292 /*                                                                                      */
293 /****************************************************************************************/
294 
295 LVREV_ReturnStatus_en   LVREV_ApplyNewSettings(LVREV_Instance_st     *pPrivate);
296 #ifdef BUILD_FLOAT
297 void                    ReverbBlock(LVM_FLOAT           *pInput,
298                                     LVM_FLOAT           *pOutput,
299                                     LVREV_Instance_st   *pPrivate,
300                                     LVM_UINT16          NumSamples);
301 #else
302 void                    ReverbBlock(LVM_INT32           *pInput,
303                                     LVM_INT32           *pOutput,
304                                     LVREV_Instance_st   *pPrivate,
305                                     LVM_UINT16          NumSamples);
306 #endif
307 LVM_INT32               BypassMixer_Callback(void       *pCallbackData,
308                                              void       *pGeneralPurpose,
309                                              LVM_INT16  GeneralPurpose );
310 
311 
312 #ifdef __cplusplus
313 }
314 #endif
315 
316 #endif  /** __LVREV_PRIVATE_H__ **/
317 
318 /* End of file */
319