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 <stdlib.h>
25 #include "LVEQNB.h"
26 #include "LVEQNB_Private.h"
27 #include <string.h> /* For memset */
28
29 /****************************************************************************************/
30 /* */
31 /* FUNCTION: LVEQNB_Init */
32 /* */
33 /* DESCRIPTION: */
34 /* Create and initialisation function for the N-Band equaliser module. */
35 /* */
36 /* PARAMETERS: */
37 /* phInstance Pointer to instance handle */
38 /* pCapabilities Pointer to the initialisation capabilities */
39 /* pScratch Pointer to bundle scratch buffer */
40 /* */
41 /* RETURNS: */
42 /* LVEQNB_SUCCESS Initialisation succeeded */
43 /* LVEQNB_NULLADDRESS One or more memory has a NULL pointer - malloc failure */
44 /* */
45 /* NOTES: */
46 /* 1. This function must not be interrupted by the LVEQNB_Process function */
47 /* */
48 /****************************************************************************************/
49
LVEQNB_Init(LVEQNB_Handle_t * phInstance,LVEQNB_Capabilities_t * pCapabilities,void * pScratch)50 LVEQNB_ReturnStatus_en LVEQNB_Init(LVEQNB_Handle_t* phInstance,
51 LVEQNB_Capabilities_t* pCapabilities, void* pScratch) {
52 LVEQNB_Instance_t* pInstance;
53
54 *phInstance = new LVEQNB_Instance_t{};
55 pInstance = (LVEQNB_Instance_t*)*phInstance;
56
57 pInstance->Capabilities = *pCapabilities;
58 pInstance->pScratch = pScratch;
59
60 /* Equaliser Biquad Instance */
61 LVM_UINT32 MemSize = pCapabilities->MaxBands * sizeof(*(pInstance->pBandDefinitions));
62 pInstance->pBandDefinitions = (LVEQNB_BandDef_t*)calloc(1, MemSize);
63 if (pInstance->pBandDefinitions == LVM_NULL) {
64 return LVEQNB_NULLADDRESS;
65 }
66 // clear all the bands, setting their gain to 0, otherwise when applying new params,
67 // it will compare against uninitialized values
68 memset(pInstance->pBandDefinitions, 0, MemSize);
69
70 MemSize = (pCapabilities->MaxBands * sizeof(*(pInstance->pBiquadType)));
71 pInstance->pBiquadType = (LVEQNB_BiquadType_en*)calloc(1, MemSize);
72 if (pInstance->pBiquadType == LVM_NULL) {
73 return LVEQNB_NULLADDRESS;
74 }
75
76 pInstance->pFastTemporary = (LVM_FLOAT*)pScratch;
77
78 /*
79 * Update the instance parameters
80 */
81 pInstance->Params.NBands = 0;
82 pInstance->Params.OperatingMode = LVEQNB_BYPASS;
83 pInstance->Params.pBandDefinition = LVM_NULL;
84 pInstance->Params.SampleRate = LVEQNB_FS_8000;
85 pInstance->Params.SourceFormat = LVEQNB_STEREO;
86
87 /*
88 * Initialise the filters
89 */
90 LVEQNB_SetFilters(pInstance, /* Set the filter types */
91 &pInstance->Params);
92
93 /*
94 * Initialise the bypass variables
95 */
96 pInstance->BypassMixer.MixerStream[0].CallbackSet = 0;
97 pInstance->BypassMixer.MixerStream[0].CallbackParam = 0;
98 pInstance->BypassMixer.MixerStream[0].pCallbackHandle = (void*)pInstance;
99 pInstance->BypassMixer.MixerStream[0].pCallBack = LVEQNB_BypassMixerCallBack;
100
101 LVC_Mixer_Init(&pInstance->BypassMixer.MixerStream[0], 0, 0);
102 LVC_Mixer_SetTimeConstant(&pInstance->BypassMixer.MixerStream[0], 0, LVM_FS_8000, 2);
103
104 pInstance->BypassMixer.MixerStream[1].CallbackSet = 1;
105 pInstance->BypassMixer.MixerStream[1].CallbackParam = 0;
106 pInstance->BypassMixer.MixerStream[1].pCallbackHandle = LVM_NULL;
107 pInstance->BypassMixer.MixerStream[1].pCallBack = LVM_NULL;
108 LVC_Mixer_Init(&pInstance->BypassMixer.MixerStream[1], 0, 1.0f);
109 LVC_Mixer_SetTimeConstant(&pInstance->BypassMixer.MixerStream[1], 0, LVM_FS_8000, 2);
110
111 pInstance->bInOperatingModeTransition = LVM_FALSE;
112
113 return (LVEQNB_SUCCESS);
114 }
115 /****************************************************************************************/
116 /* */
117 /* FUNCTION: LVEQNB_DeInit */
118 /* */
119 /* DESCRIPTION: */
120 /* Free the memories created during LVEQNB_Init including instance handle */
121 /* */
122 /* PARAMETERS: */
123 /* phInstance Pointer to instance handle */
124 /* */
125 /* NOTES: */
126 /* 1. This function must not be interrupted by the LVEQNB_Process function */
127 /* */
128 /****************************************************************************************/
129
LVEQNB_DeInit(LVEQNB_Handle_t * phInstance)130 void LVEQNB_DeInit(LVEQNB_Handle_t* phInstance) {
131 LVEQNB_Instance_t* pInstance;
132 if (phInstance == LVM_NULL) {
133 return;
134 }
135 pInstance = (LVEQNB_Instance_t*)*phInstance;
136
137 if (pInstance->pBandDefinitions != LVM_NULL) {
138 free(pInstance->pBandDefinitions);
139 pInstance->pBandDefinitions = LVM_NULL;
140 }
141 if (pInstance->pBiquadType != LVM_NULL) {
142 free(pInstance->pBiquadType);
143 pInstance->pBiquadType = LVM_NULL;
144 }
145 delete pInstance;
146 *phInstance = LVM_NULL;
147 }
148