• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <system/audio.h>
25 #include "LVCS.h"
26 #include "LVCS_Private.h"
27 #include "LVCS_Equaliser.h"
28 #include "BIQUAD.h"
29 #include "VectorArithmetic.h"
30 #include "LVCS_Tables.h"
31 
32 /************************************************************************************/
33 /*                                                                                  */
34 /* FUNCTION:                LVCS_EqualiserInit                                      */
35 /*                                                                                  */
36 /* DESCRIPTION:                                                                     */
37 /*  Initialises the equaliser module                                                */
38 /*                                                                                  */
39 /*  The function selects the coefficients for the filters and clears the data       */
40 /*  history. It is also used for re-initialisation when one of the system control   */
41 /*  parameters changes but will only change the coefficients and clear the history  */
42 /*  if the sample rate or speaker type has changed.                                 */
43 /*                                                                                  */
44 /*  To avoid excessive testing during the sample processing the biquad type is      */
45 /*  set as a callback function in the init routine.                                 */
46 /*                                                                                  */
47 /* PARAMETERS:                                                                      */
48 /*  hInstance               Instance Handle                                         */
49 /*  pParams                 Initialisation parameters                               */
50 /*                                                                                  */
51 /* RETURNS:                                                                         */
52 /*  LVCS_Success            Always succeeds                                         */
53 /*                                                                                  */
54 /* NOTES:                                                                           */
55 /*                                                                                  */
56 /************************************************************************************/
LVCS_EqualiserInit(LVCS_Handle_t hInstance,LVCS_Params_t * pParams)57 LVCS_ReturnStatus_en LVCS_EqualiserInit(LVCS_Handle_t hInstance, LVCS_Params_t* pParams) {
58     LVM_UINT16 Offset;
59     LVCS_Instance_t* pInstance = (LVCS_Instance_t*)hInstance;
60     const BiquadA012B12CoefsSP_t* pEqualiserCoefTable;
61 
62     /*
63      * If the sample rate changes re-initialise the filters
64      */
65     if ((pInstance->Params.SampleRate != pParams->SampleRate) ||
66         (pInstance->Params.SpeakerType != pParams->SpeakerType)) {
67         /*
68          * Setup the filter coefficients and clear the history
69          */
70         Offset = (LVM_UINT16)(pParams->SampleRate + (pParams->SpeakerType * (1 + LVM_FS_48000)));
71         pEqualiserCoefTable = (BiquadA012B12CoefsSP_t*)&LVCS_EqualiserCoefTable[0];
72 
73         std::array<LVM_FLOAT, android::audio_utils::kBiquadNumCoefs> coefs = {
74                 pEqualiserCoefTable[Offset].A0, pEqualiserCoefTable[Offset].A1,
75                 pEqualiserCoefTable[Offset].A2, pEqualiserCoefTable[Offset].B1,
76                 pEqualiserCoefTable[Offset].B2};
77         pInstance->pEqBiquad.reset(new android::audio_utils::BiquadFilter<LVM_FLOAT>(
78                 (pParams->NrChannels == FCC_1) ? FCC_1 : FCC_2, coefs));
79     }
80 
81     return (LVCS_SUCCESS);
82 }
83 /************************************************************************************/
84 /*                                                                                  */
85 /* FUNCTION:                LVCS_Equaliser                                          */
86 /*                                                                                  */
87 /* DESCRIPTION:                                                                     */
88 /*  Apply the equaliser filter.                                                     */
89 /*                                                                                  */
90 /* PARAMETERS:                                                                      */
91 /*  hInstance               Instance Handle                                         */
92 /*  pInputOutput            Pointer to the input/output buffer                      */
93 /*  NumSamples              The number of samples to process                        */
94 /*                                                                                  */
95 /* RETURNS:                                                                         */
96 /*  LVCS_Success            Always succeeds                                         */
97 /*                                                                                  */
98 /* NOTES:                                                                           */
99 /*  1.  Always processes in place.                                                  */
100 /*                                                                                  */
101 /************************************************************************************/
LVCS_Equaliser(LVCS_Handle_t hInstance,LVM_FLOAT * pInputOutput,LVM_UINT16 NumSamples)102 LVCS_ReturnStatus_en LVCS_Equaliser(LVCS_Handle_t hInstance, LVM_FLOAT* pInputOutput,
103                                     LVM_UINT16 NumSamples) {
104     LVCS_Instance_t* pInstance = (LVCS_Instance_t*)hInstance;
105 
106     /*
107      * Check if the equaliser is required
108      */
109     if ((pInstance->Params.OperatingMode & LVCS_EQUALISERSWITCH) != 0) {
110         /* Apply filter to the left and right channels */
111         pInstance->pEqBiquad->process(pInputOutput, pInputOutput, NumSamples);
112     }
113 
114     return (LVCS_SUCCESS);
115 }
116