[MPlayer-dev-eng] [PATCH][RFC] volnorm target parameter

Joey Parrish joey at nicewarrior.org
Thu Sep 1 23:33:41 CEST 2005


Hello,

Attached is a patch that adds an extra parameter to the volnorm filter.
It allows the "ideal" level as volnorm is concerned to be configured as
a parameter.  I used this to adaptively normalize a sound clip for a
home-brew DVD because:
  1) The wav editor I use can only normalize by measuring the peaks and
     amplifying to achieve a new peak.
  2) The output from volnorm CVS was too loud.

Comments would be appreciated.

--Joey

-- 
"Living in the complex world of the future is somewhat
like having bees live in your head.  But, there they are."
-------------- next part --------------
Adds a target parameter to the volnorm filter.

diff -Nur main.sofar/DOCS/man/en/mplayer.1 main.dev/DOCS/man/en/mplayer.1
--- main.sofar/DOCS/man/en/mplayer.1	2005-08-29 11:58:40.890632000 -0500
+++ main.dev/DOCS/man/en/mplayer.1	2005-08-29 12:10:49.608476800 -0500
@@ -4099,7 +4099,7 @@
 .PD 1
 .
 .TP
-.B volnorm[=method]
+.B volnorm[=method:target]
 Maximizes the volume without distorting the sound.
 .PD 0
 .RSs
@@ -4112,6 +4112,10 @@
 2: Use several samples to smooth the variations via the standard
 weighted mean over past samples.
 .REss
+.IPs <target>
+Sets the target amplitude as a fraction of the maximum for the
+sample type.
+The default is 0.25.
 .RE
 .PD 1
 .
diff -Nur main.sofar/libaf/af_volnorm.c main.dev/libaf/af_volnorm.c
--- main.sofar/libaf/af_volnorm.c	2004-12-27 11:30:13.000000000 -0600
+++ main.dev/libaf/af_volnorm.c	2005-08-29 12:03:16.707236800 -0500
@@ -38,9 +38,6 @@
 #define MUL_INIT 1.0
 #define MUL_MIN 0.1
 #define MUL_MAX 5.0
-// "Ideal" level
-#define MID_S16 (SHRT_MAX * 0.25)
-#define MID_FLOAT (INT_MAX * 0.25)
 
 // Silence level
 // FIXME: should be relative to the level of the samples
@@ -51,6 +48,8 @@
 #define SMOOTH_MUL 0.06
 #define SMOOTH_LASTAVG 0.06
 
+#define DEFAULT_TARGET 0.25
+
 // Data for specific instances of this filter
 typedef struct af_volume_s
 {
@@ -64,6 +63,9 @@
 	float avg; // average level of the sample
 	int len; // sample size (weight)
     } mem[NSAMPLES];
+    // "Ideal" level
+    float mid_s16;
+    float mid_float;
 }af_volnorm_t;
 
 // Initialization and runtime control
@@ -88,11 +90,14 @@
     }
     return af_test_output(af,(af_data_t*)arg);
   case AF_CONTROL_COMMAND_LINE:{
-    int   i;
-    sscanf((char*)arg,"%d", &i);
+    int   i = 0;
+    float target = DEFAULT_TARGET;
+    sscanf((char*)arg,"%d:%f", &i, &target);
     if (i != 1 && i != 2)
 	return AF_ERROR;
     s->method = i-1;
+    s->mid_s16 = ((float)SHRT_MAX) * target;
+    s->mid_float = ((float)INT_MAX) * target;
     return AF_OK;
   }
   }
@@ -128,7 +133,7 @@
   
   if (curavg > SIL_S16)
   {
-    neededmul = MID_S16 / (curavg * s->mul);
+    neededmul = s->mid_s16 / (curavg * s->mul);
     s->mul = (1.0 - SMOOTH_MUL) * s->mul + SMOOTH_MUL * neededmul;
     
     // clamp the mul coefficient
@@ -169,7 +174,7 @@
   
   if (curavg > SIL_FLOAT) // FIXME
   {
-    neededmul = MID_FLOAT / (curavg * s->mul);
+    neededmul = s->mid_float / (curavg * s->mul);
     s->mul = (1.0 - SMOOTH_MUL) * s->mul + SMOOTH_MUL * neededmul;
     
     // clamp the mul coefficient
@@ -215,7 +220,7 @@
     avg /= (float)totallen;
     if (avg >= SIL_S16)
     {
-	s->mul = MID_S16 / avg;
+	s->mul = s->mid_s16 / avg;
 	s->mul = clamp(s->mul, MUL_MIN, MUL_MAX);
     }
   }
@@ -265,7 +270,7 @@
     avg /= (float)totallen;
     if (avg >= SIL_FLOAT)
     {
-	s->mul = MID_FLOAT / avg;
+	s->mul = s->mid_float / avg;
 	s->mul = clamp(s->mul, MUL_MIN, MUL_MAX);
     }
   }
@@ -319,8 +324,10 @@
     return AF_ERROR;
 
   ((af_volnorm_t*)af->setup)->mul = MUL_INIT;
-  ((af_volnorm_t*)af->setup)->lastavg = MID_S16;
+  ((af_volnorm_t*)af->setup)->lastavg = ((float)SHRT_MAX) * DEFAULT_TARGET;
   ((af_volnorm_t*)af->setup)->idx = 0;
+  ((af_volnorm_t*)af->setup)->mid_s16 = ((float)SHRT_MAX) * DEFAULT_TARGET;
+  ((af_volnorm_t*)af->setup)->mid_float = ((float)INT_MAX) * DEFAULT_TARGET;
   for (i = 0; i < NSAMPLES; i++)
   {
      ((af_volnorm_t*)af->setup)->mem[i].len = 0;


More information about the MPlayer-dev-eng mailing list