changeset 17812:1ade5769c97b

added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
author reynaldo
date Sat, 11 Mar 2006 21:16:59 +0000
parents 6fc398692249
children f34f61843448
files libaf/af_equalizer.c
diffstat 1 files changed, 23 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/libaf/af_equalizer.c	Sat Mar 11 19:47:05 2006 +0000
+++ b/libaf/af_equalizer.c	Sat Mar 11 21:16:59 2006 +0000
@@ -58,6 +58,7 @@
   float   g[AF_NCH][KM];      	// Gain factor for each channel and band
   int     K; 		   	// Number of used eq bands
   int     channels;        	// Number of channels
+  float   gain_factor;     // applied at output to avoid clipping
 } af_equalizer_t;
 
 // 2nd order Band-pass Filter design
@@ -79,9 +80,11 @@
 
   switch(cmd){
   case AF_CONTROL_REINIT:{
-    int k =0;
+    int k =0, i =0;
     float F[KM] = CF;
     
+    s->gain_factor=0.0;
+
     // Sanity check
     if(!arg) return AF_ERROR;
     
@@ -105,7 +108,25 @@
 
     // Calculate how much this plugin adds to the overall time delay
     af->delay += 2000.0/((float)af->data->rate);
+    
+    // Calculate gain factor to prevent clipping at output
+    for(k=0;k<AF_NCH;k++)
+    {
+        for(i=0;i<KM;i++)
+        {
+            if(s->gain_factor < s->g[k][i]) s->gain_factor=s->g[k][i];
+        }
+    }
 
+    s->gain_factor=log10(s->gain_factor + 1.0) * 20.0;
+	 
+    if(s->gain_factor > 0.0)
+    {
+        s->gain_factor=0.1+(s->gain_factor/12.0);
+    }else{
+        s->gain_factor=1;
+    }
+	
     return af_test_output(af,arg);
   }
   case AF_CONTROL_COMMAND_LINE:{
@@ -190,7 +211,7 @@
 	wq[0] = w;
       }
       // Calculate output 
-      *out=yt;
+      *out=yt*s->gain_factor;
       out+=nch;
     }
   }