[Mplayer-cvslog] CVS: main/libao2 audio_plugin.h,1.9,1.10 pl_volume.c,1.1,1.2

Anders Johansson anders at mplayer.dev.hu
Wed Mar 13 13:32:44 CET 2002


Update of /cvsroot/mplayer/main/libao2
In directory mplayer:/var/tmp.root/cvs-serv638/libao2

Modified Files:
	audio_plugin.h pl_volume.c 
Log Message:
Added soft clipping for software volume control

Index: audio_plugin.h
===================================================================
RCS file: /cvsroot/mplayer/main/libao2/audio_plugin.h,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- audio_plugin.h	5 Mar 2002 09:17:36 -0000	1.9
+++ audio_plugin.h	13 Mar 2002 12:32:42 -0000	1.10
@@ -37,6 +37,7 @@
   int pl_resample_fout;	// Output frequency from resampling
   int pl_volume_volume; // Initial volume setting
   float pl_extrastereo_mul; // Stereo enhancer multiplier
+  int pl_volume_softclip;   // Enable soft clipping
 } ao_plugin_cfg_t;
 
 extern ao_plugin_cfg_t ao_plugin_cfg;
@@ -47,8 +48,9 @@
  AFMT_S16_LE, \
  0, \
  48000, \
- 255, \
- 2.5 \
+ 101, \
+ 2.5, \
+ 0 \
 };
 
 // This block should not be available in the pl_xxxx files

Index: pl_volume.c
===================================================================
RCS file: /cvsroot/mplayer/main/libao2/pl_volume.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- pl_volume.c	25 Feb 2002 13:31:26 -0000	1.1
+++ pl_volume.c	13 Mar 2002 12:32:42 -0000	1.2
@@ -4,6 +4,14 @@
 
 #define PLUGIN
 
+// Some limits
+#define MIN_S16 -32650
+#define MAX_S16  32650
+#define MIN_U8   0
+#define MAX_U8   255
+#define MIN_S8  -128
+#define MAX_S8   127
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -87,7 +95,7 @@
   of wether this plugin is in use or not. */
   pl_volume.inuse=1;
   // Tell the world what we are up to
-  printf("[pl_volume] Software volume control in use.\n");
+  printf("[pl_volume] Software volume control in use%s.\n",ao_plugin_cfg.pl_volume_softclip?", soft clipping enabled":"");
   return 1;
 }
 
@@ -100,24 +108,54 @@
 static void reset(){
 }
 
+#define SIGN(x) (x>0?1:-1) 
 // processes 'ao_plugin_data.len' bytes of 'data'
 // called for every block of data
 static int play(){
   register int i=0;
+  register int vol=pl_volume.volume; // Logarithmic control sounds more natural
+  vol=(vol*vol*vol)>>12;
   // Change the volume.
   switch(pl_volume.format){
   case(AFMT_U8):{
     register uint8_t* data=(uint8_t*)ao_plugin_data.data;
-    for(i=0;i<ao_plugin_data.len;i++){
-      data[i]=(((data[i]-128) * pl_volume.volume) >> 8) + 128;
-    }
-    break;
+    register int x;
+      for(i=0;i<ao_plugin_data.len;i++){
+        x=((data[i]-128) * vol) >> 8; 
+        if(x>MAX_S8)
+	  data[i]=MAX_U8;
+	  else if(x<MIN_S8)
+	  data[i]=MIN_U8;
+        else{
+	  if(ao_plugin_cfg.pl_volume_softclip)
+	    data[i] = ((3*x - ((x*x*x) >> 14)) >> 1) + 128;
+	  else
+	    data[i] = x + 128;
+	}
+      }
+      break;
   }
   case(AFMT_S16_LE):{
     register int len=ao_plugin_data.len>>1;
     register int16_t* data=(int16_t*)ao_plugin_data.data;
-    for(i=0;i<len;i++)
-      data[i]=(data[i]* pl_volume.volume)>>8;
+    register int x;
+    for(i=0;i<len;i++){
+      x=(data[i] * vol) >> 8;
+      if(x>MAX_S16)
+	data[i]=MAX_S16;
+      else if(x<MIN_S16)
+	data[i]=MIN_S16;
+      else{
+	if(ao_plugin_cfg.pl_volume_softclip){
+	  int64_t t=x*x;
+	  t=(t*x) >> 30;
+	  data[i] = (3*x - (int)t) >> 1;
+	  //data[i] = 2*x - SIGN(x)*((x*x)>>15);
+	}
+	else
+	  data[i] = x;
+      }
+    }
     break;
   }
   default: 
@@ -126,8 +164,3 @@
   return 1;
 
 }
-
-
-
-
-




More information about the MPlayer-cvslog mailing list