• 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 #include <stdlib.h>
24 
25 #include "LVM_Private.h"
26 #include "LVM_Tables.h"
27 #include "VectorArithmetic.h"
28 
29 /****************************************************************************************/
30 /*                                                                                      */
31 /* FUNCTION:                LVM_GetInstanceHandle                                       */
32 /*                                                                                      */
33 /* DESCRIPTION:                                                                         */
34 /*  This function is used to create a bundle instance.                                  */
35 /*  All parameters are set to their default, inactive state.                            */
36 /*                                                                                      */
37 /* PARAMETERS:                                                                          */
38 /*  phInstance              Pointer to the instance handle                              */
39 /*  pInstParams             Pointer to the instance parameters                          */
40 /*                                                                                      */
41 /* RETURNS:                                                                             */
42 /*  LVM_SUCCESS             Initialisation succeeded                                    */
43 /*  LVM_NULLADDRESS         One or more memory has a NULL pointer                       */
44 /*  LVM_OUTOFRANGE          When any of the Instance parameters are out of range        */
45 /*                                                                                      */
46 /* NOTES:                                                                               */
47 /*  1. This function must not be interrupted by the LVM_Process function                */
48 /*                                                                                      */
49 /****************************************************************************************/
LVM_GetInstanceHandle(LVM_Handle_t * phInstance,LVM_InstParams_t * pInstParams)50 LVM_ReturnStatus_en LVM_GetInstanceHandle(LVM_Handle_t* phInstance, LVM_InstParams_t* pInstParams) {
51     LVM_ReturnStatus_en Status = LVM_SUCCESS;
52     LVM_Instance_t* pInstance;
53     LVM_INT16 i;
54     LVM_UINT16 InternalBlockSize;
55     LVM_INT32 BundleScratchSize;
56 
57     /*
58      * Check valid points have been given
59      */
60     if ((phInstance == LVM_NULL) || (pInstParams == LVM_NULL)) {
61         return (LVM_NULLADDRESS);
62     }
63 
64     /*
65      * Check the instance parameters
66      */
67     if ((pInstParams->BufferMode != LVM_MANAGED_BUFFERS) &&
68         (pInstParams->BufferMode != LVM_UNMANAGED_BUFFERS)) {
69         return (LVM_OUTOFRANGE);
70     }
71 
72     if (pInstParams->EQNB_NumBands > 32) {
73         return (LVM_OUTOFRANGE);
74     }
75 
76     if (pInstParams->BufferMode == LVM_MANAGED_BUFFERS) {
77         if ((pInstParams->MaxBlockSize < LVM_MIN_MAXBLOCKSIZE) ||
78             (pInstParams->MaxBlockSize > LVM_MANAGED_MAX_MAXBLOCKSIZE)) {
79             return (LVM_OUTOFRANGE);
80         }
81     } else {
82         if ((pInstParams->MaxBlockSize < LVM_MIN_MAXBLOCKSIZE) ||
83             (pInstParams->MaxBlockSize > LVM_UNMANAGED_MAX_MAXBLOCKSIZE)) {
84             return (LVM_OUTOFRANGE);
85         }
86     }
87 
88     if (pInstParams->PSA_Included > LVM_PSA_ON) {
89         return (LVM_OUTOFRANGE);
90     }
91 
92     /*
93      * Create the instance handle
94      */
95     *phInstance = new LVM_Instance_t{};
96     pInstance = (LVM_Instance_t*)*phInstance;
97 
98     pInstance->InstParams = *pInstParams;
99 
100     /*
101      * Create the bundle scratch memory and initialse the buffer management
102      */
103     InternalBlockSize = (LVM_UINT16)(
104             (pInstParams->MaxBlockSize) &
105             MIN_INTERNAL_BLOCKMASK); /* Force to a multiple of MIN_INTERNAL_BLOCKSIZE */
106     if (InternalBlockSize < MIN_INTERNAL_BLOCKSIZE) {
107         InternalBlockSize = MIN_INTERNAL_BLOCKSIZE;
108     }
109 
110     /* Maximum Internal Black Size should not be more than MAX_INTERNAL_BLOCKSIZE*/
111     if (InternalBlockSize > MAX_INTERNAL_BLOCKSIZE) {
112         InternalBlockSize = MAX_INTERNAL_BLOCKSIZE;
113     }
114     pInstance->InternalBlockSize = (LVM_INT16)InternalBlockSize;
115 
116     /*
117      * Common settings for managed and unmanaged buffers
118      */
119     pInstance->SamplesToProcess = 0; /* No samples left to process */
120     BundleScratchSize =
121             (LVM_INT32)(3 * LVM_MAX_CHANNELS * (MIN_INTERNAL_BLOCKSIZE + InternalBlockSize) *
122                         sizeof(LVM_FLOAT));
123     pInstance->pScratch = calloc(1, BundleScratchSize);
124     if (pInstance->pScratch == LVM_NULL) {
125         return LVM_NULLADDRESS;
126     }
127 
128     if (pInstParams->BufferMode == LVM_MANAGED_BUFFERS) {
129         /*
130          * Managed buffers required
131          */
132         pInstance->pBufferManagement =
133                 (LVM_Buffer_t*)calloc(1, sizeof(*(pInstance->pBufferManagement)));
134         if (pInstance->pBufferManagement == LVM_NULL) {
135             return LVM_NULLADDRESS;
136         }
137 
138         pInstance->pBufferManagement->pScratch = (LVM_FLOAT*)pInstance->pScratch;
139 
140         memset(pInstance->pBufferManagement->InDelayBuffer, 0,
141                 LVM_MAX_CHANNELS * MIN_INTERNAL_BLOCKSIZE *
142                 sizeof(pInstance->pBufferManagement->InDelayBuffer[0]));
143         pInstance->pBufferManagement->InDelaySamples =
144                 MIN_INTERNAL_BLOCKSIZE;                    /* Set the number of delay samples */
145         pInstance->pBufferManagement->OutDelaySamples = 0; /* No samples in the output buffer */
146         pInstance->pBufferManagement->BufferState =
147                 LVM_FIRSTCALL; /* Set the state ready for the first call */
148     }
149 
150     /*
151      * Set default parameters
152      */
153     pInstance->Params.OperatingMode = LVM_MODE_OFF;
154     pInstance->Params.SampleRate = LVM_FS_8000;
155     pInstance->Params.SourceFormat = LVM_MONO;
156     pInstance->Params.SpeakerType = LVM_HEADPHONES;
157     pInstance->Params.VC_EffectLevel = 0;
158     pInstance->Params.VC_Balance = 0;
159 
160     /*
161      * Set callback
162      */
163     pInstance->CallBack = LVM_AlgoCallBack;
164 
165     /*
166      * DC removal filter
167      */
168     DC_Mc_D16_TRC_WRA_01_Init(&pInstance->DC_RemovalInstance);
169 
170     /*
171      * Treble Enhancement
172      */
173     pInstance->Params.TE_OperatingMode = LVM_TE_OFF;
174     pInstance->Params.TE_EffectLevel = 0;
175     pInstance->TE_Active = LVM_FALSE;
176 
177     /*
178      * Set the volume control and initialise Current to Target
179      */
180     pInstance->VC_Volume.MixerStream[0].CallbackParam = 0;
181     pInstance->VC_Volume.MixerStream[0].CallbackSet = 0;
182     pInstance->VC_Volume.MixerStream[0].pCallbackHandle = pInstance;
183     pInstance->VC_Volume.MixerStream[0].pCallBack = LVM_VCCallBack;
184 
185     /* In managed buffering, start with low signal level as delay in buffer management causes a
186      * click*/
187     if (pInstParams->BufferMode == LVM_MANAGED_BUFFERS) {
188         LVC_Mixer_Init(&pInstance->VC_Volume.MixerStream[0], 0, 0);
189     } else {
190         LVC_Mixer_Init(&pInstance->VC_Volume.MixerStream[0], LVM_MAXFLOAT, LVM_MAXFLOAT);
191     }
192 
193     LVC_Mixer_SetTimeConstant(&pInstance->VC_Volume.MixerStream[0], 0, LVM_FS_8000, 2);
194 
195     pInstance->VC_VolumedB = 0;
196     pInstance->VC_AVLFixedVolume = 0;
197     pInstance->VC_Active = LVM_FALSE;
198 
199     pInstance->VC_BalanceMix.MixerStream[0].CallbackParam = 0;
200     pInstance->VC_BalanceMix.MixerStream[0].CallbackSet = 0;
201     pInstance->VC_BalanceMix.MixerStream[0].pCallbackHandle = pInstance;
202     pInstance->VC_BalanceMix.MixerStream[0].pCallBack = LVM_VCCallBack;
203     LVC_Mixer_Init(&pInstance->VC_BalanceMix.MixerStream[0], LVM_MAXFLOAT, LVM_MAXFLOAT);
204     LVC_Mixer_VarSlope_SetTimeConstant(&pInstance->VC_BalanceMix.MixerStream[0], LVM_VC_MIXER_TIME,
205                                        LVM_FS_8000, 2);
206 
207     pInstance->VC_BalanceMix.MixerStream[1].CallbackParam = 0;
208     pInstance->VC_BalanceMix.MixerStream[1].CallbackSet = 0;
209     pInstance->VC_BalanceMix.MixerStream[1].pCallbackHandle = pInstance;
210     pInstance->VC_BalanceMix.MixerStream[1].pCallBack = LVM_VCCallBack;
211     LVC_Mixer_Init(&pInstance->VC_BalanceMix.MixerStream[1], LVM_MAXFLOAT, LVM_MAXFLOAT);
212     LVC_Mixer_VarSlope_SetTimeConstant(&pInstance->VC_BalanceMix.MixerStream[1], LVM_VC_MIXER_TIME,
213                                        LVM_FS_8000, 2);
214 
215     /*
216      * Create the default EQNB pre-gain and pointer to the band definitions
217      */
218     pInstance->pEQNB_BandDefs = (LVM_EQNB_BandDef_t*)calloc(pInstParams->EQNB_NumBands,
219                                                             sizeof(*(pInstance->pEQNB_BandDefs)));
220     if (pInstance->pEQNB_BandDefs == LVM_NULL) {
221         return LVM_NULLADDRESS;
222     }
223     pInstance->pEQNB_UserDefs = (LVM_EQNB_BandDef_t*)calloc(pInstParams->EQNB_NumBands,
224                                                             sizeof(*(pInstance->pEQNB_UserDefs)));
225     if (pInstance->pEQNB_UserDefs == LVM_NULL) {
226         return LVM_NULLADDRESS;
227     }
228 
229     /*
230      * Initialise the Concert Sound module
231      */
232     {
233         LVCS_Handle_t hCSInstance;           /* Instance handle */
234         LVCS_Capabilities_t CS_Capabilities; /* Initial capabilities */
235         LVCS_ReturnStatus_en LVCS_Status;    /* Function call status */
236 
237         /*
238          * Set default parameters
239          */
240         pInstance->Params.VirtualizerReverbLevel = 100;
241         pInstance->Params.VirtualizerType = LVM_CONCERTSOUND;
242         pInstance->Params.VirtualizerOperatingMode = LVM_MODE_OFF;
243         pInstance->CS_Active = LVM_FALSE;
244 
245         /*
246          * Set the initialisation capabilities
247          */
248         CS_Capabilities.MaxBlockSize = (LVM_UINT16)InternalBlockSize;
249         CS_Capabilities.CallBack = pInstance->CallBack;
250         CS_Capabilities.pBundleInstance = (void*)pInstance;
251 
252         /*
253          * Initialise the Concert Sound instance and save the instance handle
254          */
255         hCSInstance = LVM_NULL;               /* Set to NULL to return handle */
256         LVCS_Status = LVCS_Init(&hCSInstance, /* Create and initiailse */
257                                 &CS_Capabilities, pInstance->pScratch);
258         if (LVCS_Status != LVCS_SUCCESS) return ((LVM_ReturnStatus_en)LVCS_Status);
259         pInstance->hCSInstance = hCSInstance; /* Save the instance handle */
260     }
261 
262     /*
263      * Initialise the Bass Enhancement module
264      */
265     {
266         LVDBE_Handle_t hDBEInstance;           /* Instance handle */
267         LVDBE_Capabilities_t DBE_Capabilities; /* Initial capabilities */
268         LVDBE_ReturnStatus_en LVDBE_Status;    /* Function call status */
269 
270         /*
271          * Set the initialisation parameters
272          */
273         pInstance->Params.BE_OperatingMode = LVM_BE_OFF;
274         pInstance->Params.BE_CentreFreq = LVM_BE_CENTRE_55Hz;
275         pInstance->Params.BE_EffectLevel = 0;
276         pInstance->Params.BE_HPF = LVM_BE_HPF_OFF;
277 
278         pInstance->DBE_Active = LVM_FALSE;
279 
280         /*
281          * Set the initialisation capabilities
282          */
283         DBE_Capabilities.SampleRate = LVDBE_CAP_FS_8000 | LVDBE_CAP_FS_11025 | LVDBE_CAP_FS_12000 |
284                                       LVDBE_CAP_FS_16000 | LVDBE_CAP_FS_22050 | LVDBE_CAP_FS_24000 |
285                                       LVDBE_CAP_FS_32000 | LVDBE_CAP_FS_44100 | LVDBE_CAP_FS_48000 |
286                                       LVDBE_CAP_FS_88200 | LVDBE_CAP_FS_96000 |
287                                       LVDBE_CAP_FS_176400 | LVDBE_CAP_FS_192000;
288 
289         DBE_Capabilities.CentreFrequency = LVDBE_CAP_CENTRE_55Hz | LVDBE_CAP_CENTRE_55Hz |
290                                            LVDBE_CAP_CENTRE_66Hz | LVDBE_CAP_CENTRE_78Hz |
291                                            LVDBE_CAP_CENTRE_90Hz;
292         DBE_Capabilities.MaxBlockSize = (LVM_UINT16)InternalBlockSize;
293 
294         /*
295          * Initialise the Dynamic Bass Enhancement instance and save the instance handle
296          */
297         hDBEInstance = LVM_NULL;                 /* Set to NULL to return handle */
298         LVDBE_Status = LVDBE_Init(&hDBEInstance, /* Create and initiailse */
299                                   &DBE_Capabilities, pInstance->pScratch);
300         if (LVDBE_Status != LVDBE_SUCCESS) return ((LVM_ReturnStatus_en)LVDBE_Status);
301         pInstance->hDBEInstance = hDBEInstance; /* Save the instance handle */
302     }
303 
304     /*
305      * Initialise the N-Band Equaliser module
306      */
307     {
308         LVEQNB_Handle_t hEQNBInstance;           /* Instance handle */
309         LVEQNB_Capabilities_t EQNB_Capabilities; /* Initial capabilities */
310         LVEQNB_ReturnStatus_en LVEQNB_Status;    /* Function call status */
311 
312         /*
313          * Set the initialisation parameters
314          */
315         pInstance->Params.EQNB_OperatingMode = LVM_EQNB_OFF;
316         pInstance->Params.EQNB_NBands = 0;
317         pInstance->Params.pEQNB_BandDefinition = LVM_NULL;
318         pInstance->EQNB_Active = LVM_FALSE;
319 
320         /*
321          * Set the initialisation capabilities
322          */
323         EQNB_Capabilities.SampleRate =
324                 LVEQNB_CAP_FS_8000 | LVEQNB_CAP_FS_11025 | LVEQNB_CAP_FS_12000 |
325                 LVEQNB_CAP_FS_16000 | LVEQNB_CAP_FS_22050 | LVEQNB_CAP_FS_24000 |
326                 LVEQNB_CAP_FS_32000 | LVEQNB_CAP_FS_44100 | LVEQNB_CAP_FS_48000 |
327                 LVEQNB_CAP_FS_88200 | LVEQNB_CAP_FS_96000 | LVEQNB_CAP_FS_176400 |
328                 LVEQNB_CAP_FS_192000;
329 
330         EQNB_Capabilities.MaxBlockSize = (LVM_UINT16)InternalBlockSize;
331         EQNB_Capabilities.MaxBands = pInstParams->EQNB_NumBands;
332         EQNB_Capabilities.SourceFormat = LVEQNB_CAP_STEREO | LVEQNB_CAP_MONOINSTEREO;
333         EQNB_Capabilities.CallBack = pInstance->CallBack;
334         EQNB_Capabilities.pBundleInstance = (void*)pInstance;
335 
336         /*
337          * Initialise the Dynamic Bass Enhancement instance and save the instance handle
338          */
339         hEQNBInstance = LVM_NULL;                   /* Set to NULL to return handle */
340         LVEQNB_Status = LVEQNB_Init(&hEQNBInstance, /* Create and initiailse */
341                                     &EQNB_Capabilities, pInstance->pScratch);
342         if (LVEQNB_Status != LVEQNB_SUCCESS) return ((LVM_ReturnStatus_en)LVEQNB_Status);
343         pInstance->hEQNBInstance = hEQNBInstance; /* Save the instance handle */
344     }
345 
346     /*
347      * Headroom management memory allocation
348      */
349     {
350         pInstance->pHeadroom_BandDefs = (LVM_HeadroomBandDef_t*)calloc(
351                 LVM_HEADROOM_MAX_NBANDS, sizeof(*(pInstance->pHeadroom_BandDefs)));
352         if (pInstance->pHeadroom_BandDefs == LVM_NULL) {
353             return LVM_NULLADDRESS;
354         }
355         pInstance->pHeadroom_UserDefs = (LVM_HeadroomBandDef_t*)calloc(
356                 LVM_HEADROOM_MAX_NBANDS, sizeof(*(pInstance->pHeadroom_UserDefs)));
357         if (pInstance->pHeadroom_UserDefs == LVM_NULL) {
358             return LVM_NULLADDRESS;
359         }
360 
361         /* Headroom management parameters initialisation */
362         pInstance->NewHeadroomParams.NHeadroomBands = 2;
363         pInstance->NewHeadroomParams.pHeadroomDefinition = pInstance->pHeadroom_BandDefs;
364         pInstance->NewHeadroomParams.pHeadroomDefinition[0].Limit_Low = 20;
365         pInstance->NewHeadroomParams.pHeadroomDefinition[0].Limit_High = 4999;
366         pInstance->NewHeadroomParams.pHeadroomDefinition[0].Headroom_Offset = 3;
367         pInstance->NewHeadroomParams.pHeadroomDefinition[1].Limit_Low = 5000;
368         pInstance->NewHeadroomParams.pHeadroomDefinition[1].Limit_High = 24000;
369         pInstance->NewHeadroomParams.pHeadroomDefinition[1].Headroom_Offset = 4;
370         pInstance->NewHeadroomParams.Headroom_OperatingMode = LVM_HEADROOM_ON;
371 
372         pInstance->Headroom = 0;
373     }
374 
375     /*
376      * Initialise the PSA module
377      */
378     {
379         pLVPSA_Handle_t hPSAInstance = LVM_NULL; /* Instance handle */
380         LVPSA_RETURN PSA_Status;                 /* Function call status */
381         LVPSA_FilterParam_t FiltersParams[9];
382 
383         if (pInstParams->PSA_Included == LVM_PSA_ON) {
384             pInstance->PSA_InitParams.SpectralDataBufferDuration = (LVM_UINT16)500;
385             pInstance->PSA_InitParams.MaxInputBlockSize = (LVM_UINT16)2048;
386             pInstance->PSA_InitParams.nBands = (LVM_UINT16)9;
387             pInstance->PSA_InitParams.pFiltersParams = &FiltersParams[0];
388             for (i = 0; i < pInstance->PSA_InitParams.nBands; i++) {
389                 FiltersParams[i].CenterFrequency = (LVM_UINT16)1000;
390                 FiltersParams[i].QFactor = (LVM_UINT16)100;
391                 FiltersParams[i].PostGain = (LVM_INT16)0;
392             }
393 
394             /*Initialise PSA instance and save the instance handle*/
395             pInstance->PSA_ControlParams.Fs = LVM_FS_48000;
396             pInstance->PSA_ControlParams.LevelDetectionSpeed = LVPSA_SPEED_MEDIUM;
397             pInstance->pPSAInput = (LVM_FLOAT*)calloc(MAX_INTERNAL_BLOCKSIZE, sizeof(LVM_FLOAT));
398             if (pInstance->pPSAInput == LVM_NULL) {
399                 return LVM_NULLADDRESS;
400             }
401             PSA_Status = LVPSA_Init(&hPSAInstance, &pInstance->PSA_InitParams,
402                                     &pInstance->PSA_ControlParams, pInstance->pScratch);
403 
404             if (PSA_Status != LVPSA_OK) {
405                 return ((LVM_ReturnStatus_en)LVM_ALGORITHMPSA);
406             }
407 
408             pInstance->hPSAInstance = hPSAInstance; /* Save the instance handle */
409             pInstance->PSA_GainOffset = 0;
410         } else {
411             pInstance->hPSAInstance = LVM_NULL;
412         }
413 
414         /*
415          * Set the initialisation parameters.
416          */
417         pInstance->Params.PSA_PeakDecayRate = LVM_PSA_SPEED_MEDIUM;
418         pInstance->Params.PSA_Enable = LVM_PSA_OFF;
419     }
420 
421     /*
422      * Copy the initial parameters to the new parameters for correct readback of
423      * the settings.
424      */
425     pInstance->NewParams = pInstance->Params;
426 
427     /*
428      * Create configuration number
429      */
430     pInstance->ConfigurationNumber = 0x00000000;
431     pInstance->ConfigurationNumber += LVM_CS_MASK;
432     pInstance->ConfigurationNumber += LVM_EQNB_MASK;
433     pInstance->ConfigurationNumber += LVM_DBE_MASK;
434     pInstance->ConfigurationNumber += LVM_VC_MASK;
435     pInstance->ConfigurationNumber += LVM_PSA_MASK;
436 
437     if (((pInstance->ConfigurationNumber & LVM_CS_MASK) != 0) ||
438         ((pInstance->ConfigurationNumber & LVM_DBE_MASK) != 0) ||
439         ((pInstance->ConfigurationNumber & LVM_EQNB_MASK) != 0) ||
440         ((pInstance->ConfigurationNumber & LVM_TE_MASK) != 0) ||
441         ((pInstance->ConfigurationNumber & LVM_VC_MASK) != 0)) {
442         pInstance->BlickSizeMultiple = 4;
443     } else {
444         pInstance->BlickSizeMultiple = 1;
445     }
446 
447     return (Status);
448 }
449 /****************************************************************************************/
450 /*                                                                                      */
451 /* FUNCTION:                LVM_DelInstanceHandle                                       */
452 /*                                                                                      */
453 /* DESCRIPTION:                                                                         */
454 /*  This function is used to create a bundle instance. It returns the created instance  */
455 /*  handle through phInstance. All parameters are set to their default, inactive state. */
456 /*                                                                                      */
457 /* PARAMETERS:                                                                          */
458 /*  phInstance              Pointer to the instance handle                              */
459 /*                                                                                      */
460 /* NOTES:                                                                               */
461 /*  1. This function must not be interrupted by the LVM_Process function                */
462 /*                                                                                      */
463 /****************************************************************************************/
LVM_DelInstanceHandle(LVM_Handle_t * phInstance)464 void LVM_DelInstanceHandle(LVM_Handle_t* phInstance) {
465     LVM_Instance_t* pInstance = (LVM_Instance_t*)*phInstance;
466 
467     if (pInstance->pScratch != LVM_NULL) {
468         free(pInstance->pScratch);
469         pInstance->pScratch = LVM_NULL;
470     }
471 
472     if (pInstance->InstParams.BufferMode == LVM_MANAGED_BUFFERS) {
473         /*
474          * Managed buffers required
475          */
476         if (pInstance->pBufferManagement != LVM_NULL) {
477             free(pInstance->pBufferManagement);
478             pInstance->pBufferManagement = LVM_NULL;
479         }
480     }
481 
482     /*
483      * Treble Enhancement
484      */
485 
486     /*
487      * Free the default EQNB pre-gain and pointer to the band definitions
488      */
489     if (pInstance->pEQNB_BandDefs != LVM_NULL) {
490         free(pInstance->pEQNB_BandDefs);
491         pInstance->pEQNB_BandDefs = LVM_NULL;
492     }
493     if (pInstance->pEQNB_UserDefs != LVM_NULL) {
494         free(pInstance->pEQNB_UserDefs);
495         pInstance->pEQNB_UserDefs = LVM_NULL;
496     }
497 
498     /*
499      * De-initialise the Concert Sound module
500      */
501     if (pInstance->hCSInstance != LVM_NULL) {
502         LVCS_DeInit(&pInstance->hCSInstance);
503     }
504 
505     /*
506      * De-initialise the Bass Enhancement module
507      */
508     if (pInstance->hDBEInstance != LVM_NULL) {
509         LVDBE_DeInit(&pInstance->hDBEInstance);
510     }
511 
512     /*
513      * De-initialise the N-Band Equaliser module
514      */
515     if (pInstance->hEQNBInstance != LVM_NULL) {
516         LVEQNB_DeInit(&pInstance->hEQNBInstance);
517     }
518 
519     /*
520      * Free Headroom management memory.
521      */
522     if (pInstance->pHeadroom_BandDefs != LVM_NULL) {
523         free(pInstance->pHeadroom_BandDefs);
524         pInstance->pHeadroom_BandDefs = LVM_NULL;
525     }
526     if (pInstance->pHeadroom_UserDefs != LVM_NULL) {
527         free(pInstance->pHeadroom_UserDefs);
528         pInstance->pHeadroom_UserDefs = LVM_NULL;
529     }
530 
531     /*
532      * De-initialise the PSA module
533      */
534     if (pInstance->hPSAInstance != LVM_NULL) {
535         LVPSA_DeInit(&pInstance->hPSAInstance);
536     }
537     if (pInstance->pPSAInput != LVM_NULL) {
538         free(pInstance->pPSAInput);
539         pInstance->pPSAInput = LVM_NULL;
540     }
541 
542     delete pInstance;
543     return;
544 }
545 
546 /****************************************************************************************/
547 /*                                                                                      */
548 /* FUNCTION:                LVM_ClearAudioBuffers                                       */
549 /*                                                                                      */
550 /* DESCRIPTION:                                                                         */
551 /*  This function is used to clear the internal audio buffers of the bundle.            */
552 /*                                                                                      */
553 /* PARAMETERS:                                                                          */
554 /*  hInstance               Instance handle                                             */
555 /*                                                                                      */
556 /* RETURNS:                                                                             */
557 /*  LVM_SUCCESS             Initialisation succeeded                                    */
558 /*  LVM_NULLADDRESS         Instance or scratch memory has a NULL pointer               */
559 /*                                                                                      */
560 /* NOTES:                                                                               */
561 /*  1. This function must not be interrupted by the LVM_Process function                */
562 /*                                                                                      */
563 /****************************************************************************************/
564 
LVM_ClearAudioBuffers(LVM_Handle_t hInstance)565 LVM_ReturnStatus_en LVM_ClearAudioBuffers(LVM_Handle_t hInstance) {
566     LVM_InstParams_t InstParams;                            /* Instance parameters */
567     LVM_ControlParams_t Params;                             /* Control Parameters */
568     LVM_Instance_t* pInstance = (LVM_Instance_t*)hInstance; /* Pointer to Instance */
569     LVM_HeadroomParams_t HeadroomParams;
570 
571     if (hInstance == LVM_NULL) {
572         return LVM_NULLADDRESS;
573     }
574 
575     /* Save the control parameters */ /* coverity[unchecked_value] */ /* Do not check return value
576                                                                          internal function calls */
577     LVM_GetControlParameters(hInstance, &Params);
578 
579     /*Save the headroom parameters*/
580     LVM_GetHeadroomParams(hInstance, &HeadroomParams);
581 
582     /*  Save the instance parameters */
583     InstParams = pInstance->InstParams;
584 
585     /*  Call  LVM_GetInstanceHandle to re-initialise the bundle */
586     /* Restore control parameters */ /* coverity[unchecked_value] */ /* Do not check return value
587                                                                         internal function calls */
588     LVM_SetControlParameters(hInstance, &Params);
589 
590     /*Restore the headroom parameters*/
591     LVM_SetHeadroomParams(hInstance, &HeadroomParams);
592 
593     /* DC removal filter */
594     DC_Mc_D16_TRC_WRA_01_Init(&pInstance->DC_RemovalInstance);
595 
596     return LVM_SUCCESS;
597 }
598