shithub: choc

Download patch

ref: 8c084734707060e05476f74edb069d76e05b5a06
parent: 8764a4a9cd040906651180771d7c65d334dc1774
author: Simon Howard <[email protected]>
date: Sat Feb 23 17:51:17 EST 2008

Perform a low-pass filter of converted sounds to filter out
high-frequency noise from the upscaling process.

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 1088

--- a/src/i_sdlsound.c
+++ b/src/i_sdlsound.c
@@ -46,6 +46,7 @@
 
 #include "doomdef.h"
 
+#define LOW_PASS_FILTER
 #define NUM_CHANNELS 16
 
 static boolean sound_initialised = false;
@@ -255,6 +256,33 @@
 
             expanded[i * 2] = expanded[i * 2 + 1] = sample;
         }
+
+#ifdef LOW_PASS_FILTER
+        // Perform a low-pass filter on the upscaled sound to filter
+        // out high-frequency noise from the conversion process.
+
+        {
+            float rc, dt, alpha;
+
+            // Low-pass filter for cutoff frequency f:
+            //
+            // For sampling rate r, dt = 1 / r
+            // rc = 1 / 2*pi*f
+            // alpha = dt / (rc + dt)
+
+            // Filter to the half sample rate of the original sound effect
+            // (maximum frequency, by nyquist)
+
+            dt = 1.0 / mixer_freq;
+            rc = 1.0 / (3.14 * samplerate);
+            alpha = dt / (rc + dt);
+
+            for (i=1; i<expanded_length; ++i) 
+            {
+                expanded[i] = alpha * expanded[i] + (1 - alpha) * expanded[i-1];
+            }
+        }
+#endif /* #ifdef LOW_PASS_FILTER */
     }
 }