[Scummvm-cvs-logs] SF.net SVN: scummvm: [27972] scummvm/branches/gsoc2007-mixer/sound

dogmatixman at users.sourceforge.net dogmatixman at users.sourceforge.net
Sun Jul 8 19:16:06 CEST 2007


Revision: 27972
          http://scummvm.svn.sourceforge.net/scummvm/?rev=27972&view=rev
Author:   dogmatixman
Date:     2007-07-08 10:16:05 -0700 (Sun, 08 Jul 2007)

Log Message:
-----------
Cleaned up this code a bit and put it in a class.

Modified Paths:
--------------
    scummvm/branches/gsoc2007-mixer/sound/filter.cpp
    scummvm/branches/gsoc2007-mixer/sound/filter.h

Modified: scummvm/branches/gsoc2007-mixer/sound/filter.cpp
===================================================================
--- scummvm/branches/gsoc2007-mixer/sound/filter.cpp	2007-07-08 17:02:19 UTC (rev 27971)
+++ scummvm/branches/gsoc2007-mixer/sound/filter.cpp	2007-07-08 17:16:05 UTC (rev 27972)
@@ -24,34 +24,15 @@
  */
  
 /* Filter coefficient generation using Kaiser filter design */
-// TODO: Namespace, class, etc.
  
 #include "common/stdafx.h"
 
 #include "sound/filter.h"
 
+namespace Audio {
+
 static double bessel_i0(double x);
 
-static double equiripple(
-			double dBPassbandRipple,
-			double dBStopbandAtten);
-
-static uint16 windowLength(
-			double ripple,
-			double transitionBW,
-			double samplingFreq);
-
-static void windowDesign(double *coeffs, uint16 length, double ripple);
-
-static double sinc(double arg);
-
-static void LPDesign(
-		double *coeffs,
-		uint16 length,
-		double passbandEdge,
-		double stopbandEdge,
-		double samplingFreq);
- 
 /* 
  * Modified Bessel function of the first type and zeroth order.  This is
  * calculated using an iterative expansion of I_0(x) = sum from n=0:infinity
@@ -60,7 +41,7 @@
  *                |---------previous term----------| |---new factor--|
  *                |------------------current term--------------------|
  */
-static double bessel_i0(double x) {
+static double bessel_i0(double x) { 
 	uint16 n;
 	
 	/* Base case for n == 0 */
@@ -89,7 +70,7 @@
 }
 
 /* Returns the tolerable ripple in an equiripple filter */
-static inline double equiripple(
+inline double FIRFilter::equiripple(
 			double dBPassbandRipple,
 			double dBStopbandAtten) {
 	return fmin(dBPassbandRipple, -dBStopbandAtten);
@@ -99,12 +80,14 @@
  * Estimated window length using the Kaiser design formula.
  * Assumption: window length <= 65535 taps.
  */
-static inline uint16 windowLength(
+inline uint16 FIRFilter::windowLength(
 					double ripple,
 					double transitionBW,
-					double samplingFreq) {
+					uint16 samplingFreq) {
 	double length = (samplingFreq * (-ripple - 7.95)) / (14.36 * transitionBW);
 	
+	assert(length > 0);
+	
 	return (uint16)(length + 1);
 }
 
@@ -112,7 +95,7 @@
  * Derives the coefficients for a Kaiser window of the given properties, using
  * standard Kaiser design formulae.
  */
-static void windowDesign(double *coeffs, uint16 length, double ripple) {
+void FIRFilter::windowDesign(double *coeffs, uint16 length, double ripple) {
 	uint16 i;
 	
 	double alpha = -ripple;
@@ -139,7 +122,7 @@
 	}
 }
 
-static inline double sinc(double arg) {
+inline double FIRFilter::sinc(double arg) {
 	if (arg == 0) {
 		/* continuous extension of sin(arg) / arg */
 		return 1;
@@ -152,12 +135,12 @@
  * Generates the filter coefficients for a windowed lowpass filter by applying
  * the provided window to the ideal lowpass (sinc) filter
  */
-static void LPDesign(
+void FIRFilter::LPDesign(
 		double *coeffs,
 		uint16 length,
 		double passbandEdge,
 		double stopbandEdge,
-		double samplingFreq) {
+		uint16 samplingFreq) {
 	uint16 i;
 	
 	for (i = 0; i <= (length - 1) / 2; i++) {
@@ -177,24 +160,26 @@
 }
 
 /* Generates lowpass filter coefficients using the parameters provided */
-// TODO: Write a nice interface for this :)
-void lowPassCoeffs(
-		double passbandEdge,
-		double stopbandEdge,
-		double dBPassbandRipple,
-		double dBStopbandAtten,
-		double samplingFreq) {
+FIRFilter::FIRFilter(double passbandEdge, double stopbandEdge,
+					double dBPassbandRipple, double dBStopbandAtten,
+					uint16 samplingFreq)
+		: _passbandEdge(passbandEdge), _stopbandEdge(stopbandEdge),
+		  _dBPassbandRipple(dBPassbandRipple),
+		  _dBStopbandAtten(dBStopbandAtten),
+		  _samplingFreq(samplingFreq), _ripple(0), _length(0), _coeffs(0) {
 	/* Find the amount of ripple that the filter will produce */
-	double ripple = equiripple(dBPassbandRipple, dBStopbandAtten);
+	_ripple = equiripple(_dBPassbandRipple, _dBStopbandAtten);
 	
 	/* Find the number of coefficients in the window */
-	uint16 length = 
-		windowLength(ripple, stopbandEdge - passbandEdge, samplingFreq);
+	_length = 
+		windowLength(_ripple, _stopbandEdge - _passbandEdge, _samplingFreq);
 	
 	/* Calculate the window coefficients */
-	double *coeffs = (double *)malloc(length * sizeof(double));
-	windowDesign(coeffs, length, ripple);
+	_coeffs = (double *)malloc(_length * sizeof(double));
+	windowDesign(_coeffs, _length, _ripple);
 	
 	/* Generate the coefficients of a low pass filter using this window */
-	LPDesign(coeffs, length, passbandEdge, stopbandEdge, samplingFreq);
+	LPDesign(_coeffs, _length, _passbandEdge, _stopbandEdge, _samplingFreq);
 }
+
+} // End of namespace Audio

Modified: scummvm/branches/gsoc2007-mixer/sound/filter.h
===================================================================
--- scummvm/branches/gsoc2007-mixer/sound/filter.h	2007-07-08 17:02:19 UTC (rev 27971)
+++ scummvm/branches/gsoc2007-mixer/sound/filter.h	2007-07-08 17:16:05 UTC (rev 27972)
@@ -28,12 +28,57 @@
 
 #include "common/scummsys.h"
 
+namespace Audio {
+
+class FIRFilter {
+
+private:
+	double _passbandEdge;
+	double _stopbandEdge;
+	double _dBPassbandRipple;
+	double _dBStopbandAtten;
+	uint16 _samplingFreq;
+	
+	double _ripple;
+	uint16 _length;
+	double *_coeffs;
+
+public:
 /* Generates lowpass filter coefficients using the parameters provided */
-void lowPassCoeffs(
-		double passbandEdge,
-		double stopbandEdge,
-		double dBPassbandRipple,
-		double dBStopbandAtten,
-		double samplingFreq);
+	FIRFilter(
+			double passbandEdge,
+			double stopbandEdge,
+			double dBPassbandRipple,
+			double dBStopbandAtten,
+			uint16 samplingFreq);
+	
+	uint16 getLength() { return _length; }
+	
+	double *getCoeffs() { return _coeffs; }
 
+private:
+	double equiripple(
+			double dBPassbandRipple,
+			double dBStopbandAtten);
+	
+	uint16 windowLength(
+			double ripple,
+			double transitionBW,
+			uint16 samplingFreq);
+	
+	void windowDesign(double *coeffs, uint16 length, double ripple);
+	
+	double sinc(double arg);
+	
+	void LPDesign(
+			double *coeffs,
+			uint16 length,
+			double passbandEdge,
+			double stopbandEdge,
+			uint16 samplingFreq);
+
+};
+
+} // End of namespace Audio
+
 #endif


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list