Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ac1a0e2a authored by Rivukanta Bhattacharya's avatar Rivukanta Bhattacharya
Browse files

Biquad filter integration to LVM : Equaliser changes

Bug: 120944950
Test: lvmtest with lvm/tests/build_and_run_all_unit_tests.sh and snr utility
Change-Id: I28c83bc085135d57701f40654861c7a79c99adc5
parent 0ba11a6b
Loading
Loading
Loading
Loading
+33 −0
Original line number Original line Diff line number Diff line
@@ -21,6 +21,9 @@
/*                                                                                      */
/*                                                                                      */
/****************************************************************************************/
/****************************************************************************************/


#ifdef BIQUAD_OPT
#include <system/audio.h>
#endif
#include "LVEQNB.h"
#include "LVEQNB.h"
#include "LVEQNB_Private.h"
#include "LVEQNB_Private.h"
#include "VectorArithmetic.h"
#include "VectorArithmetic.h"
@@ -179,6 +182,9 @@ void LVEQNB_SetCoefficients(LVEQNB_Instance_t* pInstance) {
    LVM_UINT16 i;                    /* Filter band index */
    LVM_UINT16 i;                    /* Filter band index */
    LVEQNB_BiquadType_en BiquadType; /* Filter biquad type */
    LVEQNB_BiquadType_en BiquadType; /* Filter biquad type */


#ifdef BIQUAD_OPT
    pInstance->gain.resize(pInstance->Params.NBands);
#endif
    /*
    /*
     * Set the coefficients for each band by the init function
     * Set the coefficients for each band by the init function
     */
     */
@@ -198,8 +204,19 @@ void LVEQNB_SetCoefficients(LVEQNB_Instance_t* pInstance) {
                /*
                /*
                 * Set the coefficients
                 * Set the coefficients
                 */
                 */
#ifdef BIQUAD_OPT
                pInstance->gain[i] = Coefficients.G;
                std::array<LVM_FLOAT, android::audio_utils::kBiquadNumCoefs> coefs = {
                        Coefficients.A0, 0.0, -(Coefficients.A0), -(Coefficients.B1),
                        -(Coefficients.B2)};
                pInstance->eqBiquad[i]
                        .setCoefficients<
                                std::array<LVM_FLOAT, android::audio_utils::kBiquadNumCoefs>>(
                                coefs);
#else
                PK_2I_D32F32CssGss_TRC_WRA_01_Init(&pInstance->pEQNB_FilterState_Float[i],
                PK_2I_D32F32CssGss_TRC_WRA_01_Init(&pInstance->pEQNB_FilterState_Float[i],
                                                   &pInstance->pEQNB_Taps_Float[i], &Coefficients);
                                                   &pInstance->pEQNB_Taps_Float[i], &Coefficients);
#endif
                break;
                break;
            }
            }
            default:
            default:
@@ -220,6 +237,11 @@ void LVEQNB_SetCoefficients(LVEQNB_Instance_t* pInstance) {
/*                                                                                  */
/*                                                                                  */
/************************************************************************************/
/************************************************************************************/
void LVEQNB_ClearFilterHistory(LVEQNB_Instance_t* pInstance) {
void LVEQNB_ClearFilterHistory(LVEQNB_Instance_t* pInstance) {
#ifdef BIQUAD_OPT
    for (size_t i = 0; i < pInstance->eqBiquad.size(); i++) {
        pInstance->eqBiquad[i].clear();
    }
#else
    LVM_FLOAT* pTapAddress;
    LVM_FLOAT* pTapAddress;
    LVM_INT16 NumTaps;
    LVM_INT16 NumTaps;


@@ -233,6 +255,7 @@ void LVEQNB_ClearFilterHistory(LVEQNB_Instance_t* pInstance) {
                        pTapAddress, /* Destination */
                        pTapAddress, /* Destination */
                        NumTaps);    /* Number of words */
                        NumTaps);    /* Number of words */
    }
    }
#endif
}
}
/****************************************************************************************/
/****************************************************************************************/
/*                                                                                      */
/*                                                                                      */
@@ -310,6 +333,16 @@ LVEQNB_ReturnStatus_en LVEQNB_Control(LVEQNB_Handle_t hInstance, LVEQNB_Params_t
            (OperatingModeSave == LVEQNB_ON && pInstance->bInOperatingModeTransition &&
            (OperatingModeSave == LVEQNB_ON && pInstance->bInOperatingModeTransition &&
             LVC_Mixer_GetTarget(&pInstance->BypassMixer.MixerStream[0]) == 0);
             LVC_Mixer_GetTarget(&pInstance->BypassMixer.MixerStream[0]) == 0);


#ifdef BIQUAD_OPT
    /*
     * Create biquad instance
     */
    pInstance->eqBiquad.resize(
            pParams->NBands, android::audio_utils::BiquadFilter<LVM_FLOAT>(
                                     (FCC_1 == pParams->NrChannels) ? FCC_2 : pParams->NrChannels));
    LVEQNB_ClearFilterHistory(pInstance);
#endif

    if (bChange || modeChange) {
    if (bChange || modeChange) {
        /*
        /*
         * If the sample rate has changed clear the history
         * If the sample rate has changed clear the history
+8 −1
Original line number Original line Diff line number Diff line
@@ -62,6 +62,9 @@ LVEQNB_ReturnStatus_en LVEQNB_Init(LVEQNB_Handle_t* phInstance,
    pInstance->pScratch = pScratch;
    pInstance->pScratch = pScratch;


    /* Equaliser Biquad Instance */
    /* Equaliser Biquad Instance */
#ifdef BIQUAD_OPT
    LVM_UINT32 MemSize = pCapabilities->MaxBands * sizeof(*(pInstance->pBandDefinitions));
#else
    LVM_UINT32 MemSize = pCapabilities->MaxBands * sizeof(*(pInstance->pEQNB_FilterState_Float));
    LVM_UINT32 MemSize = pCapabilities->MaxBands * sizeof(*(pInstance->pEQNB_FilterState_Float));
    pInstance->pEQNB_FilterState_Float = (Biquad_FLOAT_Instance_t*)calloc(1, MemSize);
    pInstance->pEQNB_FilterState_Float = (Biquad_FLOAT_Instance_t*)calloc(1, MemSize);
    if (pInstance->pEQNB_FilterState_Float == LVM_NULL) {
    if (pInstance->pEQNB_FilterState_Float == LVM_NULL) {
@@ -75,6 +78,7 @@ LVEQNB_ReturnStatus_en LVEQNB_Init(LVEQNB_Handle_t* phInstance,
    }
    }


    MemSize = (pCapabilities->MaxBands * sizeof(*(pInstance->pBandDefinitions)));
    MemSize = (pCapabilities->MaxBands * sizeof(*(pInstance->pBandDefinitions)));
#endif
    pInstance->pBandDefinitions = (LVEQNB_BandDef_t*)calloc(1, MemSize);
    pInstance->pBandDefinitions = (LVEQNB_BandDef_t*)calloc(1, MemSize);
    if (pInstance->pBandDefinitions == LVM_NULL) {
    if (pInstance->pBandDefinitions == LVM_NULL) {
        return LVEQNB_NULLADDRESS;
        return LVEQNB_NULLADDRESS;
@@ -105,10 +109,11 @@ LVEQNB_ReturnStatus_en LVEQNB_Init(LVEQNB_Handle_t* phInstance,
     */
     */
    LVEQNB_SetFilters(pInstance, /* Set the filter types */
    LVEQNB_SetFilters(pInstance, /* Set the filter types */
                      &pInstance->Params);
                      &pInstance->Params);

#ifndef BIQUAD_OPT
    LVEQNB_SetCoefficients(pInstance); /* Set the filter coefficients */
    LVEQNB_SetCoefficients(pInstance); /* Set the filter coefficients */


    LVEQNB_ClearFilterHistory(pInstance); /* Clear the filter history */
    LVEQNB_ClearFilterHistory(pInstance); /* Clear the filter history */
#endif


    /*
    /*
     * Initialise the bypass variables
     * Initialise the bypass variables
@@ -154,6 +159,7 @@ void LVEQNB_DeInit(LVEQNB_Handle_t* phInstance) {
    }
    }
    pInstance = (LVEQNB_Instance_t*)*phInstance;
    pInstance = (LVEQNB_Instance_t*)*phInstance;


#ifndef BIQUAD_OPT
    /* Equaliser Biquad Instance */
    /* Equaliser Biquad Instance */
    if (pInstance->pEQNB_FilterState_Float != LVM_NULL) {
    if (pInstance->pEQNB_FilterState_Float != LVM_NULL) {
        free(pInstance->pEQNB_FilterState_Float);
        free(pInstance->pEQNB_FilterState_Float);
@@ -163,6 +169,7 @@ void LVEQNB_DeInit(LVEQNB_Handle_t* phInstance) {
        free(pInstance->pEQNB_Taps_Float);
        free(pInstance->pEQNB_Taps_Float);
        pInstance->pEQNB_Taps_Float = LVM_NULL;
        pInstance->pEQNB_Taps_Float = LVM_NULL;
    }
    }
#endif
    if (pInstance->pBandDefinitions != LVM_NULL) {
    if (pInstance->pBandDefinitions != LVM_NULL) {
        free(pInstance->pBandDefinitions);
        free(pInstance->pBandDefinitions);
        pInstance->pBandDefinitions = LVM_NULL;
        pInstance->pBandDefinitions = LVM_NULL;
+9 −0
Original line number Original line Diff line number Diff line
@@ -24,6 +24,9 @@
/*                                                                                      */
/*                                                                                      */
/****************************************************************************************/
/****************************************************************************************/


#ifdef BIQUAD_OPT
#include <audio_utils/BiquadFilter.h>
#endif
#include "LVEQNB.h" /* Calling or Application layer definitions */
#include "LVEQNB.h" /* Calling or Application layer definitions */
#include "BIQUAD.h"
#include "BIQUAD.h"
#include "LVC_Mixer.h"
#include "LVC_Mixer.h"
@@ -69,8 +72,14 @@ typedef struct {
    /* Aligned memory pointers */
    /* Aligned memory pointers */
    LVM_FLOAT* pFastTemporary; /* Fast temporary data base address */
    LVM_FLOAT* pFastTemporary; /* Fast temporary data base address */


#ifdef BIQUAD_OPT
    std::vector<android::audio_utils::BiquadFilter<LVM_FLOAT>>
            eqBiquad;            /* Biquad filter instances */
    std::vector<LVM_FLOAT> gain; /* Gain values for all bands*/
#else
    Biquad_2I_Order2_FLOAT_Taps_t* pEQNB_Taps_Float;  /* Equaliser Taps */
    Biquad_2I_Order2_FLOAT_Taps_t* pEQNB_Taps_Float;  /* Equaliser Taps */
    Biquad_FLOAT_Instance_t* pEQNB_FilterState_Float; /* State for each filter band */
    Biquad_FLOAT_Instance_t* pEQNB_FilterState_Float; /* State for each filter band */
#endif


    /* Filter definitions and call back */
    /* Filter definitions and call back */
    LVM_UINT16 NBands;                  /* Number of bands */
    LVM_UINT16 NBands;                  /* Number of bands */
+11 −0
Original line number Original line Diff line number Diff line
@@ -104,19 +104,30 @@ LVEQNB_ReturnStatus_en LVEQNB_Process(
                 * Check if band is non-zero dB gain
                 * Check if band is non-zero dB gain
                 */
                 */
                if (pInstance->pBandDefinitions[i].Gain != 0) {
                if (pInstance->pBandDefinitions[i].Gain != 0) {
#ifndef BIQUAD_OPT
                    /*
                    /*
                     * Get the address of the biquad instance
                     * Get the address of the biquad instance
                     */
                     */
                    Biquad_FLOAT_Instance_t* pBiquad = &pInstance->pEQNB_FilterState_Float[i];
                    Biquad_FLOAT_Instance_t* pBiquad = &pInstance->pEQNB_FilterState_Float[i];
#endif


                    /*
                    /*
                     * Select single or double precision as required
                     * Select single or double precision as required
                     */
                     */
                    switch (pInstance->pBiquadType[i]) {
                    switch (pInstance->pBiquadType[i]) {
                        case LVEQNB_SinglePrecision_Float: {
                        case LVEQNB_SinglePrecision_Float: {
#ifdef BIQUAD_OPT
                            LVM_FLOAT* pTemp = pScratch + NrSamples;
                            pInstance->eqBiquad[i].process(pTemp, pScratch, NrFrames);
                            const auto gain = pInstance->gain[i];
                            for (unsigned j = 0; j < NrSamples; ++j) {
                                pScratch[j] += pTemp[j] * gain;
                            }
#else
                            PK_Mc_D32F32C14G11_TRC_WRA_01(pBiquad, pScratch, pScratch,
                            PK_Mc_D32F32C14G11_TRC_WRA_01(pBiquad, pScratch, pScratch,
                                                          (LVM_INT16)NrFrames,
                                                          (LVM_INT16)NrFrames,
                                                          (LVM_INT16)NrChannels);
                                                          (LVM_INT16)NrChannels);
#endif
                            break;
                            break;
                        }
                        }
                        default:
                        default: