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 /************************************************************************************/
19 /*                                                                                  */
20 /*  Includes                                                                        */
21 /*                                                                                  */
22 /************************************************************************************/
23 
24 #include "LVCS.h"
25 #include "LVCS_Private.h"
26 #include "LVCS_Equaliser.h"
27 #include "BIQUAD.h"
28 #include "VectorArithmetic.h"
29 #include "LVCS_Tables.h"
30 
31 /************************************************************************************/
32 /*                                                                                  */
33 /* FUNCTION:                LVCS_EqualiserInit                                      */
34 /*                                                                                  */
35 /* DESCRIPTION:                                                                     */
36 /*  Initialises the equaliser module                                                */
37 /*                                                                                  */
38 /*  The function selects the coefficients for the filters and clears the data       */
39 /*  history. It is also used for re-initialisation when one of the system control   */
40 /*  parameters changes but will only change the coefficients and clear the history  */
41 /*  if the sample rate or speaker type has changed.                                 */
42 /*                                                                                  */
43 /*  To avoid excessive testing during the sample processing the biquad type is      */
44 /*  set as a callback function in the init routine.                                 */
45 /*                                                                                  */
46 /* PARAMETERS:                                                                      */
47 /*  hInstance               Instance Handle                                         */
48 /*  pParams                 Initialisation parameters                               */
49 /*                                                                                  */
50 /* RETURNS:                                                                         */
51 /*  LVCS_Success            Always succeeds                                         */
52 /*                                                                                  */
53 /* NOTES:                                                                           */
54 /*                                                                                  */
55 /************************************************************************************/
LVCS_EqualiserInit(LVCS_Handle_t hInstance,LVCS_Params_t * pParams)56 LVCS_ReturnStatus_en LVCS_EqualiserInit(LVCS_Handle_t       hInstance,
57                                         LVCS_Params_t       *pParams)
58 {
59 
60     LVM_UINT16          Offset;
61     LVCS_Instance_t     *pInstance = (LVCS_Instance_t  *)hInstance;
62     LVCS_Equaliser_t    *pConfig   = (LVCS_Equaliser_t *)&pInstance->Equaliser;
63     LVCS_Data_t         *pData;
64     LVCS_Coefficient_t  *pCoefficients;
65     BQ_FLOAT_Coefs_t      Coeffs;
66     const BiquadA012B12CoefsSP_t *pEqualiserCoefTable;
67 
68     pData = (LVCS_Data_t *) \
69                 pInstance->MemoryTable.Region[LVCS_MEMREGION_PERSISTENT_FAST_DATA].pBaseAddress;
70 
71     pCoefficients = (LVCS_Coefficient_t *) \
72                 pInstance->MemoryTable.Region[LVCS_MEMREGION_PERSISTENT_FAST_COEF].pBaseAddress;
73     /*
74      * If the sample rate changes re-initialise the filters
75      */
76     if ((pInstance->Params.SampleRate != pParams->SampleRate) ||
77         (pInstance->Params.SpeakerType != pParams->SpeakerType))
78     {
79         /*
80          * Setup the filter coefficients and clear the history
81          */
82         Offset = (LVM_UINT16)(pParams->SampleRate + (pParams->SpeakerType * (1 + LVM_FS_48000)));
83         pEqualiserCoefTable = (BiquadA012B12CoefsSP_t*)&LVCS_EqualiserCoefTable[0];
84 
85         /* Left and right filters */
86         /* Convert incoming coefficients to the required format/ordering */
87         Coeffs.A0 = (LVM_FLOAT) pEqualiserCoefTable[Offset].A0;
88         Coeffs.A1 = (LVM_FLOAT) pEqualiserCoefTable[Offset].A1;
89         Coeffs.A2 = (LVM_FLOAT) pEqualiserCoefTable[Offset].A2;
90         Coeffs.B1 = (LVM_FLOAT)-pEqualiserCoefTable[Offset].B1;
91         Coeffs.B2 = (LVM_FLOAT)-pEqualiserCoefTable[Offset].B2;
92 
93         LoadConst_Float((LVM_INT16)0,                                         /* Value */
94                         (LVM_FLOAT *)&pData->EqualiserBiquadTaps, /* Destination */
95                         /* Number of words */
96                         (LVM_UINT16)(sizeof(pData->EqualiserBiquadTaps) / sizeof(LVM_FLOAT)));
97 
98         BQ_2I_D16F32Css_TRC_WRA_01_Init(&pCoefficients->EqualiserBiquadInstance,
99                                         &pData->EqualiserBiquadTaps,
100                                         &Coeffs);
101 
102         /* Callbacks */
103         switch(pEqualiserCoefTable[Offset].Scale)
104         {
105             case 13:
106                 pConfig->pBiquadCallBack  = BQ_2I_D16F32C13_TRC_WRA_01;
107                 break;
108             case 14:
109                 pConfig->pBiquadCallBack  = BQ_2I_D16F32C14_TRC_WRA_01;
110                 break;
111             case 15:
112                 pConfig->pBiquadCallBack  = BQ_2I_D16F32C15_TRC_WRA_01;
113                 break;
114         }
115     }
116 
117     return(LVCS_SUCCESS);
118 }
119 /************************************************************************************/
120 /*                                                                                  */
121 /* FUNCTION:                LVCS_Equaliser                                          */
122 /*                                                                                  */
123 /* DESCRIPTION:                                                                     */
124 /*  Apply the equaliser filter.                                                     */
125 /*                                                                                  */
126 /* PARAMETERS:                                                                      */
127 /*  hInstance               Instance Handle                                         */
128 /*  pInputOutput            Pointer to the input/output buffer                      */
129 /*  NumSamples              The number of samples to process                        */
130 /*                                                                                  */
131 /* RETURNS:                                                                         */
132 /*  LVCS_Success            Always succeeds                                         */
133 /*                                                                                  */
134 /* NOTES:                                                                           */
135 /*  1.  Always processes in place.                                                  */
136 /*                                                                                  */
137 /************************************************************************************/
LVCS_Equaliser(LVCS_Handle_t hInstance,LVM_FLOAT * pInputOutput,LVM_UINT16 NumSamples)138 LVCS_ReturnStatus_en LVCS_Equaliser(LVCS_Handle_t       hInstance,
139                                     LVM_FLOAT           *pInputOutput,
140                                     LVM_UINT16          NumSamples)
141 {
142 
143     LVCS_Instance_t     *pInstance = (LVCS_Instance_t  *)hInstance;
144     LVCS_Equaliser_t    *pConfig   = (LVCS_Equaliser_t  *)&pInstance->Equaliser;
145     LVCS_Coefficient_t  *pCoefficients;
146 
147     pCoefficients = (LVCS_Coefficient_t *) \
148                   pInstance->MemoryTable.Region[LVCS_MEMREGION_PERSISTENT_FAST_COEF].pBaseAddress;
149 
150     /*
151      * Check if the equaliser is required
152      */
153     if ((pInstance->Params.OperatingMode & LVCS_EQUALISERSWITCH) != 0)
154     {
155         /* Apply filter to the left and right channels */
156         (pConfig->pBiquadCallBack)((Biquad_FLOAT_Instance_t*) \
157                                         &pCoefficients->EqualiserBiquadInstance,
158                                         (LVM_FLOAT *)pInputOutput,
159                                         (LVM_FLOAT *)pInputOutput,
160                                         (LVM_INT16)NumSamples);
161     }
162 
163     return(LVCS_SUCCESS);
164 }
165