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