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 <stdlib.h>
26 #include "LVDBE.h"
27 #include "LVDBE_Private.h"
28 
29 /****************************************************************************************/
30 /*                                                                                      */
31 /* FUNCTION:                 LVDBE_Init                                                 */
32 /*                                                                                      */
33 /* DESCRIPTION:                                                                         */
34 /*    Create and initialisation function for the Bass Enhancement module                */
35 /*                                                                                      */
36 /* PARAMETERS:                                                                          */
37 /*  phInstance               Pointer to instance handle                                 */
38 /*  pCapabilities            Pointer to the initialisation capabilities                 */
39 /*  pScratch                 Pointer to the bundle scratch buffer                       */
40 /*                                                                                      */
41 /* RETURNS:                                                                             */
42 /*  LVDBE_SUCCESS            Initialisation succeeded                                   */
43 /*  LVDBE_NULLADDRESS        One or more memory has a NULL pointer - malloc failure     */
44 /*                                                                                      */
45 /* NOTES:                                                                               */
46 /*  1.    This function must not be interrupted by the LVDBE_Process function           */
47 /*                                                                                      */
48 /****************************************************************************************/
LVDBE_Init(LVDBE_Handle_t * phInstance,LVDBE_Capabilities_t * pCapabilities,void * pScratch)49 LVDBE_ReturnStatus_en LVDBE_Init(LVDBE_Handle_t* phInstance, LVDBE_Capabilities_t* pCapabilities,
50                                  void* pScratch) {
51     LVDBE_Instance_t* pInstance;
52     LVMixer3_1St_FLOAT_st* pMixer_Instance;
53     LVMixer3_2St_FLOAT_st* pBypassMixer_Instance;
54     LVM_FLOAT MixGain;
55 
56     /*
57      * Create the instance handle if not already initialised
58      */
59     if (*phInstance == LVM_NULL) {
60         *phInstance = new LVDBE_Instance_t{};
61     }
62     pInstance = (LVDBE_Instance_t*)*phInstance;
63 
64     /*
65      * Save the memory table in the instance structure
66      */
67     pInstance->Capabilities = *pCapabilities;
68 
69     pInstance->pScratch = pScratch;
70 
71     /*
72      * Set the default instance parameters
73      */
74     pInstance->Params.CentreFrequency = LVDBE_CENTRE_55HZ;
75     pInstance->Params.EffectLevel = 0;
76     pInstance->Params.HeadroomdB = 0;
77     pInstance->Params.HPFSelect = LVDBE_HPF_OFF;
78     pInstance->Params.OperatingMode = LVDBE_OFF;
79     pInstance->Params.SampleRate = LVDBE_FS_8000;
80     pInstance->Params.VolumeControl = LVDBE_VOLUME_OFF;
81     pInstance->Params.VolumedB = 0;
82     pInstance->Params.NrChannels = FCC_2;
83 
84     /*
85      * Create pointer to data and coef memory
86      */
87     pInstance->pData = (LVDBE_Data_FLOAT_t*)calloc(1, sizeof(*(pInstance->pData)));
88     if (pInstance->pData == NULL) {
89         return LVDBE_NULLADDRESS;
90     }
91     /*
92      * Create biquad instance
93      */
94     pInstance->pHPFBiquad.reset(
95             new android::audio_utils::BiquadFilter<LVM_FLOAT>(pInstance->Params.NrChannels));
96     pInstance->pBPFBiquad.reset(new android::audio_utils::BiquadFilter<LVM_FLOAT>(FCC_1));
97 
98     /*
99      * Initialise the filters
100      */
101     LVDBE_SetFilters(pInstance, /* Set the filter taps and coefficients */
102                      &pInstance->Params);
103 
104     /*
105      * Initialise the AGC
106      */
107     LVDBE_SetAGC(pInstance, /* Set the AGC gain */
108                  &pInstance->Params);
109     pInstance->pData->AGCInstance.AGC_Gain = pInstance->pData->AGCInstance.AGC_MaxGain;
110     /* Default to the bass boost setting */
111 
112     // initialize the mixer with some fixes values since otherwise LVDBE_SetVolume ends up
113     // reading uninitialized data
114     pMixer_Instance = &pInstance->pData->BypassVolume;
115     LVC_Mixer_Init(&pMixer_Instance->MixerStream[0], 1.0, 1.0);
116 
117     /*
118      * Initialise the volume
119      */
120     LVDBE_SetVolume(pInstance, /* Set the Volume */
121                     &pInstance->Params);
122 
123     pInstance->pData->AGCInstance.Volume = pInstance->pData->AGCInstance.Target;
124     /* Initialise as the target */
125     MixGain = LVC_Mixer_GetTarget(&pMixer_Instance->MixerStream[0]);
126     LVC_Mixer_Init(&pMixer_Instance->MixerStream[0], MixGain, MixGain);
127 
128     /* Configure the mixer process path */
129     pMixer_Instance->MixerStream[0].CallbackParam = 0;
130     pMixer_Instance->MixerStream[0].pCallbackHandle = LVM_NULL;
131     pMixer_Instance->MixerStream[0].pCallBack = LVM_NULL;
132     pMixer_Instance->MixerStream[0].CallbackSet = 0;
133 
134     /*
135      * Initialise the clicks minimisation BypassMixer
136      */
137 
138     pBypassMixer_Instance = &pInstance->pData->BypassMixer;
139 
140     /*
141      * Setup the mixer gain for the processed path
142      */
143     pBypassMixer_Instance->MixerStream[0].CallbackParam = 0;
144     pBypassMixer_Instance->MixerStream[0].pCallbackHandle = LVM_NULL;
145     pBypassMixer_Instance->MixerStream[0].pCallBack = LVM_NULL;
146     pBypassMixer_Instance->MixerStream[0].CallbackSet = 0;
147 
148     LVC_Mixer_Init(&pBypassMixer_Instance->MixerStream[0], 0, 0);
149     LVC_Mixer_SetTimeConstant(&pBypassMixer_Instance->MixerStream[0], LVDBE_BYPASS_MIXER_TC,
150                               (LVM_Fs_en)pInstance->Params.SampleRate, 2);
151 
152     /*
153      * Setup the mixer gain for the unprocessed path
154      */
155     pBypassMixer_Instance->MixerStream[1].CallbackParam = 0;
156     pBypassMixer_Instance->MixerStream[1].pCallbackHandle = LVM_NULL;
157     pBypassMixer_Instance->MixerStream[1].pCallBack = LVM_NULL;
158     pBypassMixer_Instance->MixerStream[1].CallbackSet = 0;
159     LVC_Mixer_Init(&pBypassMixer_Instance->MixerStream[1], 1.0, 1.0);
160     LVC_Mixer_SetTimeConstant(&pBypassMixer_Instance->MixerStream[1], LVDBE_BYPASS_MIXER_TC,
161                               (LVM_Fs_en)pInstance->Params.SampleRate, 2);
162 
163     return (LVDBE_SUCCESS);
164 }
165 
166 /****************************************************************************************/
167 /*                                                                                      */
168 /* FUNCTION:                 LVDBE_DeInit                                               */
169 /*                                                                                      */
170 /* DESCRIPTION:                                                                         */
171 /*    Free the memories created during LVDBE_Init including instance handle             */
172 /*                                                                                      */
173 /* PARAMETERS:                                                                          */
174 /*  phInstance               Pointer to instance handle                                 */
175 /*                                                                                      */
176 /****************************************************************************************/
LVDBE_DeInit(LVDBE_Handle_t * phInstance)177 void LVDBE_DeInit(LVDBE_Handle_t* phInstance) {
178     LVDBE_Instance_t* pInstance = (LVDBE_Instance_t*)*phInstance;
179     if (pInstance == LVM_NULL) {
180         return;
181     }
182     if (pInstance->pData != LVM_NULL) {
183         free(pInstance->pData);
184         pInstance->pData = LVM_NULL;
185     }
186     delete pInstance;
187     *phInstance = LVM_NULL;
188 }
189