OSDN Git Service

Limit maximum equalizer gain.
authorEric Laurent <elaurent@google.com>
Mon, 10 Sep 2012 23:27:35 +0000 (16:27 -0700)
committerEric Laurent <elaurent@google.com>
Tue, 11 Sep 2012 01:21:27 +0000 (18:21 -0700)
Limit maximum gain in all EQ bands according to current volume so that
total gain (current volume + band gain) does not exceed a certain limit.
The gain difference between bands is preserved.

Change-Id: Ice5a9705a0b3353e8778b4c539a29ca9cdf60390

media/libeffects/lvm/wrapper/Bundle/EffectBundle.cpp
media/libeffects/lvm/wrapper/Bundle/EffectBundle.h

index d4be9fa..f3f2d1e 100644 (file)
@@ -299,6 +299,10 @@ extern "C" int EffectCreate(const effect_uuid_t *uuid,
         pContext->pBundledContext->SamplesToExitCountBb     = 0;
         pContext->pBundledContext->SamplesToExitCountEq     = 0;
 
+        for (int i = 0; i < FIVEBAND_NUMBANDS; i++) {
+            pContext->pBundledContext->bandGaindB[i] = EQNB_5BandSoftPresets[i];
+        }
+
         ALOGV("\tEffectCreate - Calling LvmBundle_init");
         ret = LvmBundle_init(pContext);
 
@@ -1194,36 +1198,71 @@ void VirtualizerSetStrength(EffectContext *pContext, uint32_t strength){
     //ALOGV("\tVirtualizerSetStrength Succesfully called LVM_SetControlParameters\n\n");
 }    /* end setStrength */
 
+
 //----------------------------------------------------------------------------
-// EqualizerGetBandLevel()
+// EqualizerLimitBandLevels()
 //----------------------------------------------------------------------------
-// Purpose: Retrieve the gain currently being used for the band passed in
+// Purpose: limit all EQ band gains to a value less than MAX_BAND_GAIN_DB while
+//          preserving the relative band levels.
 //
 // Inputs:
-//  band:       band number
 //  pContext:   effect engine context
 //
 // Outputs:
 //
 //----------------------------------------------------------------------------
-int32_t EqualizerGetBandLevel(EffectContext *pContext, int32_t band){
-
-    int32_t Gain =0;
-    LVM_ControlParams_t     ActiveParams;                           /* Current control Parameters */
-    LVM_ReturnStatus_en     LvmStatus = LVM_SUCCESS;                /* Function call status */
+void EqualizerLimitBandLevels(EffectContext *pContext) {
+    LVM_ControlParams_t     ActiveParams;              /* Current control Parameters */
+    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;     /* Function call status */
     LVM_EQNB_BandDef_t      *BandDef;
-    /* Get the current settings */
-    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance,
-                                         &ActiveParams);
 
-    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "EqualizerGetBandLevel")
+    /* Get the current settings */
+    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
+    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "EqualizerLimitBandLevels")
+    //ALOGV("\tEqualizerLimitBandLevels Succesfully returned from LVM_GetControlParameters\n");
+    //ALOGV("\tEqualizerLimitBandLevels just Got -> %d\n",
+    //          ActiveParams.pEQNB_BandDefinition[band].Gain);
+
+    int gainCorrection = 0;
+    for (int i = 0; i < FIVEBAND_NUMBANDS; i++) {
+        int level = pContext->pBundledContext->bandGaindB[i] + ActiveParams.VC_EffectLevel;
+        if (level > MAX_BAND_GAIN_DB) {
+            int correction = MAX_BAND_GAIN_DB -level;
+            if (correction < gainCorrection) {
+                gainCorrection = correction;
+            }
+        }
+    }
 
+    /* Set local EQ parameters */
     BandDef = ActiveParams.pEQNB_BandDefinition;
-    Gain    = (int32_t)BandDef[band].Gain*100;    // Convert to millibels
+    for (int i=0; i < FIVEBAND_NUMBANDS; i++) {
+        ActiveParams.pEQNB_BandDefinition[i].Gain = pContext->pBundledContext->bandGaindB[i] +
+                gainCorrection;
+    }
+    /* Activate the initial settings */
+    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
+    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "EqualizerLimitBandLevels")
+    //ALOGV("\tEqualizerLimitBandLevels just Set -> %d\n",
+    //          ActiveParams.pEQNB_BandDefinition[band].Gain);
+}
 
-    //ALOGV("\tEqualizerGetBandLevel -> %d\n", Gain );
-    //ALOGV("\tEqualizerGetBandLevel Succesfully returned from LVM_GetControlParameters\n");
-    return Gain;
+
+//----------------------------------------------------------------------------
+// EqualizerGetBandLevel()
+//----------------------------------------------------------------------------
+// Purpose: Retrieve the gain currently being used for the band passed in
+//
+// Inputs:
+//  band:       band number
+//  pContext:   effect engine context
+//
+// Outputs:
+//
+//----------------------------------------------------------------------------
+int32_t EqualizerGetBandLevel(EffectContext *pContext, int32_t band){
+    //ALOGV("\tEqualizerGetBandLevel -> %d\n", pContext->pBundledContext->bandGaindB[band] );
+    return pContext->pBundledContext->bandGaindB[band] * 100;
 }
 
 //----------------------------------------------------------------------------
@@ -1248,30 +1287,12 @@ void EqualizerSetBandLevel(EffectContext *pContext, int band, short Gain){
         gainRounded = (int)((Gain-50)/100);
     }
     //ALOGV("\tEqualizerSetBandLevel(%d)->(%d)", Gain, gainRounded);
-
-
-    LVM_ControlParams_t     ActiveParams;              /* Current control Parameters */
-    LVM_ReturnStatus_en     LvmStatus=LVM_SUCCESS;     /* Function call status */
-    LVM_EQNB_BandDef_t      *BandDef;
-
-    /* Get the current settings */
-    LvmStatus = LVM_GetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
-    LVM_ERROR_CHECK(LvmStatus, "LVM_GetControlParameters", "EqualizerSetBandLevel")
-    //ALOGV("\tEqualizerSetBandLevel Succesfully returned from LVM_GetControlParameters\n");
-    //ALOGV("\tEqualizerSetBandLevel just Got -> %d\n",ActiveParams.pEQNB_BandDefinition[band].Gain);
-
-    /* Set local EQ parameters */
-    BandDef = ActiveParams.pEQNB_BandDefinition;
-    ActiveParams.pEQNB_BandDefinition[band].Gain = gainRounded;
-
-    /* Activate the initial settings */
-    LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
-    LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "EqualizerSetBandLevel")
-    //ALOGV("\tEqualizerSetBandLevel just Set -> %d\n",ActiveParams.pEQNB_BandDefinition[band].Gain);
-
+    pContext->pBundledContext->bandGaindB[band] = gainRounded;
     pContext->pBundledContext->CurPreset = PRESET_CUSTOM;
-    return;
+
+    EqualizerLimitBandLevels(pContext);
 }
+
 //----------------------------------------------------------------------------
 // EqualizerGetCentreFrequency()
 //----------------------------------------------------------------------------
@@ -1410,13 +1431,15 @@ void EqualizerSetPreset(EffectContext *pContext, int preset){
     {
         ActiveParams.pEQNB_BandDefinition[i].Frequency = EQNB_5BandPresetsFrequencies[i];
         ActiveParams.pEQNB_BandDefinition[i].QFactor   = EQNB_5BandPresetsQFactors[i];
-        ActiveParams.pEQNB_BandDefinition[i].Gain
-        = EQNB_5BandSoftPresets[i + preset * FIVEBAND_NUMBANDS];
+        pContext->pBundledContext->bandGaindB[i] =
+                EQNB_5BandSoftPresets[i + preset * FIVEBAND_NUMBANDS];
     }
     /* Activate the new settings */
     LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, &ActiveParams);
     LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "EqualizerSetPreset")
 
+    EqualizerLimitBandLevels(pContext);
+
     //ALOGV("\tEqualizerSetPreset Succesfully called LVM_SetControlParameters\n");
     return;
 }
@@ -1494,6 +1517,9 @@ int VolumeSetVolumeLevel(EffectContext *pContext, int16_t level){
         ALOGV("\tLVM_VOLUME: Disabling Smoothing for first volume change to remove spikes/clicks");
         pContext->pBundledContext->firstVolume = LVM_FALSE;
     }
+
+    EqualizerLimitBandLevels(pContext);
+
     return 0;
 }    /* end VolumeSetVolumeLevel */
 
index 5634ca1..9c58ecd 100644 (file)
@@ -38,6 +38,7 @@ extern "C" {
 #define VOLUME_CUP_LOAD_ARM9E      0      // Expressed in 0.1 MIPS
 #define BUNDLE_MEM_USAGE           25     // Expressed in kB
 //#define LVM_PCM
+#define MAX_BAND_GAIN_DB           4
 
 #ifndef OPENSL_ES_H_
 static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
@@ -95,6 +96,7 @@ struct BundledEffectContext{
     int                             SamplesToExitCountVirt;
     LVM_INT16                       *workBuffer;
     int                             frameCount;
+    int32_t                         bandGaindB[FIVEBAND_NUMBANDS];
     #ifdef LVM_PCM
     FILE                            *PcmInPtr;
     FILE                            *PcmOutPtr;