Subject: pkg/25521: pkgsrc/audio/normalize applies wrong volume adjustments
To: None <gnats-bugs@gnats.NetBSD.org>
From: Matthias Pfaller <leo@dachau.marco.de>
List: pkgsrc-bugs
Date: 05/10/2004 12:52:54
>Number:         25521
>Category:       pkg
>Synopsis:       pkgsrc/audio/normalize applies wrong volume adjustments
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    pkg-manager
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon May 10 10:54:00 UTC 2004
>Closed-Date:
>Last-Modified:
>Originator:     Matthias Pfaller
>Release:        NetBSD 1.6ZK
>Organization:
Matthias Pfaller                            Software Entwicklung
marco Systemanalyse und Entwicklung GmbH    Tel   +49 8131 5161-41
Hans-B=F6ckler-Str. 2, D 85221 Dachau         Fax   +49 8131 5161-66
http://www.marco.de/                        Email leo@dachau.marco.de
>Environment:
	<The following information is extracted from your kernel. Please>
	<append output of "ldd", "ident" where relevant (multiple lines).>
System: NetBSD joghurt 1.6ZK NetBSD 1.6ZK (SIN) #1: Sat Mar 27 16:17:47 CET=
 2004 leo@sin:/usr/src/sys/arch/i386/compile/SIN i386
Architecture: i386
Machine: i386
>Description:
	When trying to normalize a bunch of mp3-files, normalize will
	(sometimes) apply volumeadjustments of about ~-300db...
>How-To-Repeat:
	Use normalize on a couple of mp3 files.
>Fix:
One part of the structure (maxpow) isn't initialized. Initialize it to 0.
While we are at it, compute the 100 element sum only once and from there on
only subtract to be deleted elements and add new elements.

--- mpegvolume.c	2002-08-26 08:32:26.000000000 +0200
+++ /tmp/mpegvolume.c	2004-05-10 12:41:34.000000000 +0200
@@ -77,6 +77,7 @@
   int buflen;  /* elements allocated to buffer */
   int start;   /* index of first element in buffer */
   int n;       /* num of elements in buffer */
+  long long sum;
 } datasmooth_t;
=20
 struct decode_struct {
@@ -136,14 +137,18 @@
     ds->sums[c] =3D 0;
     end =3D ((ds->powsmooth[c].start + ds->powsmooth[c].n)
 	   % ds->powsmooth[c].buflen);
-    ds->powsmooth[c].buf[end] =3D pow;
     if (ds->powsmooth[c].n =3D=3D ds->powsmooth[c].buflen) {
       ds->powsmooth[c].start =3D ((ds->powsmooth[c].start + 1)
 				% ds->powsmooth[c].buflen);
-      pow =3D get_smoothed_data(&ds->powsmooth[c]);
+      ds->powsmooth[c].sum -=3D ds->powsmooth[c].buf[end];
+      ds->powsmooth[c].sum +=3D ds->powsmooth[c].buf[end] =3D pow;
+      pow =3D ds->powsmooth[c].sum;
+//      pow =3D get_smoothed_data(&ds->powsmooth[c]);
       if (pow > ds->maxpow)
 	ds->maxpow =3D pow;
     } else {
+      ds->powsmooth[c].buf[end] =3D pow;
+      ds->powsmooth[c].sum +=3D pow;
       ds->powsmooth[c].n++;
     }
   }
@@ -312,12 +317,14 @@
      */
     ds->si->bits_per_sample =3D 16;
     ds->sums[0] =3D ds->sums[1] =3D 0;
+    ds->maxpow =3D 0;
=20
     /* set up smoothing window buffer */
     for (c =3D 0; c < 2; c++) {
       ds->powsmooth[c].buflen =3D 100; /* use a 100-element (1 second) win=
dow */
       ds->powsmooth[c].buf =3D (double *)xmalloc(ds->powsmooth[c].buflen *=
 sizeof(double));
       ds->powsmooth[c].start =3D ds->powsmooth[c].n =3D 0;
+      ds->powsmooth[c].sum =3D 0;
     }
   }
=20

>Release-Note:
>Audit-Trail:
>Unformatted:
 	<Please check that the above is correct for the bug being reported,>
 	<and append source date of snapshot, if applicable (one line).>