2 * Copyright (C) 2004-2010 NXP Software
3 * Copyright (C) 2010 The Android Open Source Project
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 /************************************************************************************/
22 /************************************************************************************/
25 #include "LVCS_Private.h"
26 #include "LVCS_ReverbGenerator.h"
27 #include "LVC_Mixer.h"
28 #include "VectorArithmetic.h"
30 #include "LVCS_Tables.h"
32 /************************************************************************************/
34 /* FUNCTION: LVCS_ReverbGeneratorInit */
37 /* Initialises the reverb module. The delay buffer size is configured for the */
38 /* sample rate and the speaker type. */
40 /* The routine may also be called for re-initialisation, i.e. when one of the */
41 /* control parameters has changed. In this case the delay and filters are only */
42 /* re-initialised if one of the following two conditions is met: */
43 /* - the sample rate has changed */
44 /* - the speaker type changes to/from the mobile speaker */
48 /* hInstance Instance Handle */
49 /* pParams Pointer to the inialisation parameters */
52 /* LVCS_Success Always succeeds */
55 /* 1. In the delay settings 'Samples' is the number of samples to the end of the */
57 /* 2. The numerator coefficients of the filter are negated to cause an inversion. */
59 /************************************************************************************/
61 LVCS_ReturnStatus_en LVCS_ReverbGeneratorInit(LVCS_Handle_t hInstance,
62 LVCS_Params_t *pParams)
67 LVCS_Instance_t *pInstance = (LVCS_Instance_t *)hInstance;
68 LVCS_ReverbGenerator_t *pConfig = (LVCS_ReverbGenerator_t *)&pInstance->Reverberation;
69 LVCS_Data_t *pData = (LVCS_Data_t *)pInstance->MemoryTable.Region[LVCS_MEMREGION_PERSISTENT_FAST_DATA].pBaseAddress;
70 LVCS_Coefficient_t *pCoefficients = (LVCS_Coefficient_t *)pInstance->MemoryTable.Region[LVCS_MEMREGION_PERSISTENT_FAST_COEF].pBaseAddress;
71 BQ_C16_Coefs_t Coeffs;
72 const BiquadA012B12CoefsSP_t *pReverbCoefTable;
75 * Initialise the delay and filters if:
76 * - the sample rate has changed
77 * - the speaker type has changed to or from the mobile speaker
79 if(pInstance->Params.SampleRate != pParams->SampleRate ) /* Sample rate change test */
85 Delay = (LVM_UINT16)LVCS_StereoDelayCS[(LVM_UINT16)pParams->SampleRate];
88 pConfig->DelaySize = (LVM_INT16)(2 * Delay);
89 pConfig->DelayOffset = 0;
90 LoadConst_16(0, /* Value */
91 (LVM_INT16 *)&pConfig->StereoSamples[0], /* Destination */
92 (LVM_UINT16)(sizeof(pConfig->StereoSamples)/sizeof(LVM_INT16))); /* Number of words */
97 Offset = (LVM_UINT16)pParams->SampleRate;
98 pReverbCoefTable = (BiquadA012B12CoefsSP_t*)&LVCS_ReverbCoefTable[0];
100 /* Convert incoming coefficients to the required format/ordering */
101 Coeffs.A0 = (LVM_INT16)pReverbCoefTable[Offset].A0;
102 Coeffs.A1 = (LVM_INT16)pReverbCoefTable[Offset].A1;
103 Coeffs.A2 = (LVM_INT16)pReverbCoefTable[Offset].A2;
104 Coeffs.B1 = (LVM_INT16)-pReverbCoefTable[Offset].B1;
105 Coeffs.B2 = (LVM_INT16)-pReverbCoefTable[Offset].B2;
107 LoadConst_16(0, /* Value */
108 (void *)&pData->ReverbBiquadTaps, /* Destination Cast to void: no dereferencing in function*/
109 (LVM_UINT16)(sizeof(pData->ReverbBiquadTaps)/sizeof(LVM_INT16))); /* Number of words */
111 BQ_2I_D16F16Css_TRC_WRA_01_Init(&pCoefficients->ReverbBiquadInstance,
112 &pData->ReverbBiquadTaps,
116 switch(pReverbCoefTable[Offset].Scale)
119 pConfig->pBiquadCallBack = BQ_2I_D16F16C14_TRC_WRA_01;
122 pConfig->pBiquadCallBack = BQ_2I_D16F16C15_TRC_WRA_01;
130 pConfig->ProcGain = (LVM_UINT16)(HEADPHONEGAINPROC);
131 pConfig->UnprocGain = (LVM_UINT16)(HEADPHONEGAINUNPROC);
134 if(pInstance->Params.ReverbLevel != pParams->ReverbLevel)
136 LVM_INT32 ReverbPercentage=83886; // 1 Percent Reverb i.e 1/100 in Q 23 format
137 ReverbPercentage*=pParams->ReverbLevel; // Actual Reverb Level in Q 23 format
138 pConfig->ReverbLevel=(LVM_INT16)(ReverbPercentage>>8); // Reverb Level in Q 15 format
141 return(LVCS_SUCCESS);
144 /************************************************************************************/
146 /* FUNCTION: LVCS_Reverb */
149 /* Create reverb using the block of input samples based on the following block */
151 /* ________ ________ */
153 /* _____ _______ | |----------->| | ______ ___ */
154 /* | | | | | Stereo | | L & R | | | | | */
155 /* -->| LPF |-->| Delay |-->| to | ____ | to |-->| Gain |-->| + |--> */
156 /* | |_____| |_______| | L & R | | | | Stereo | |______| |___| */
157 /* | | |-->| -1 |-->| | | */
158 /* | |________| |____| |________| | */
160 /* |-----------------------------------------------------------------------| */
162 /* The input buffer is broken in to sub-blocks of the size of the delay or less. */
163 /* This allows the delay buffer to be treated as a circular buffer but processed */
164 /* as a linear buffer. */
168 /* hInstance Instance Handle */
169 /* pInData Pointer to the input buffer */
170 /* pOutData Pointer to the output buffer */
171 /* NumSamples Number of samples to process */
174 /* LVCS_Success Always succeeds */
177 /* 1. Process in blocks of samples the size of the delay where possible, if not */
178 /* the number of samples left over */
179 /* 2. The Gain is combined with the LPF and incorporated in to the coefficients */
181 /************************************************************************************/
183 LVCS_ReturnStatus_en LVCS_ReverbGenerator(LVCS_Handle_t hInstance,
184 const LVM_INT16 *pInData,
186 LVM_UINT16 NumSamples)
189 LVCS_Instance_t *pInstance = (LVCS_Instance_t *)hInstance;
190 LVCS_ReverbGenerator_t *pConfig = (LVCS_ReverbGenerator_t *)&pInstance->Reverberation;
191 LVCS_Coefficient_t *pCoefficients = (LVCS_Coefficient_t *)pInstance->MemoryTable.Region[LVCS_MEMREGION_PERSISTENT_FAST_COEF].pBaseAddress;
192 LVM_INT16 *pScratch = (LVM_INT16 *)pInstance->MemoryTable.Region[LVCS_MEMREGION_TEMPORARY_FAST].pBaseAddress;
196 * Copy the data to the output in outplace processing
198 if (pInData != pOutData)
201 * Reverb not required so just copy the data
203 Copy_16((LVM_INT16 *)pInData, /* Source */
204 (LVM_INT16 *)pOutData, /* Destination */
205 (LVM_INT16)(2*NumSamples)); /* Left and right */
210 * Check if the reverb is required
212 if (((pInstance->Params.SpeakerType == LVCS_HEADPHONE) || /* Disable when CS4MS in stereo mode */
213 (pInstance->Params.SpeakerType == LVCS_EX_HEADPHONES) ||
214 (pInstance->Params.SourceFormat != LVCS_STEREO)) &&
215 ((pInstance->Params.OperatingMode & LVCS_REVERBSWITCH) !=0)) /* For validation testing */
217 /********************************************************************************/
219 /* Copy the input data to scratch memory and filter it */
221 /********************************************************************************/
224 * Copy the input data to the scratch memory
226 Copy_16((LVM_INT16 *)pInData, /* Source */
227 (LVM_INT16 *)pScratch, /* Destination */
228 (LVM_INT16)(2*NumSamples)); /* Left and right */
234 (pConfig->pBiquadCallBack)((Biquad_Instance_t*)&pCoefficients->ReverbBiquadInstance,
235 (LVM_INT16 *)pScratch,
236 (LVM_INT16 *)pScratch,
237 (LVM_INT16)NumSamples);
239 Mult3s_16x16( (LVM_INT16 *)pScratch,
240 pConfig->ReverbLevel,
241 (LVM_INT16 *)pScratch,
242 (LVM_INT16)(2*NumSamples));
246 * Apply the delay mix
248 DelayMix_16x16((LVM_INT16 *)pScratch,
249 &pConfig->StereoSamples[0],
252 &pConfig->DelayOffset,
253 (LVM_INT16)NumSamples);
258 return(LVCS_SUCCESS);