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

dogmatixman at users.sourceforge.net dogmatixman at users.sourceforge.net
Sun Sep 2 13:18:36 CEST 2007


Revision: 28816
          http://scummvm.svn.sourceforge.net/scummvm/?rev=28816&view=rev
Author:   dogmatixman
Date:     2007-09-02 04:18:36 -0700 (Sun, 02 Sep 2007)

Log Message:
-----------
Added caching of filters, since it seems that in practice only a few different ones are actually needed.

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

Modified: scummvm/branches/gsoc2007-mixer/sound/filter.cpp
===================================================================
--- scummvm/branches/gsoc2007-mixer/sound/filter.cpp	2007-09-02 09:52:06 UTC (rev 28815)
+++ scummvm/branches/gsoc2007-mixer/sound/filter.cpp	2007-09-02 11:18:36 UTC (rev 28816)
@@ -31,6 +31,8 @@
 
 namespace Audio {
 
+Common::Array<FIRFilter *> FIRFilter::filters;
+
 static double bessel_i0(double x);
 
 /* 
@@ -190,4 +192,24 @@
 	LPDesign(_coeffs, _length, _passbandEdge, _stopbandEdge, _samplingFreq);
 }
 
+FIRFilter *FIRFilter::getFilter(double passbandEdge, double stopbandEdge,
+					double dBPassbandRipple, double dBStopbandAtten,
+					uint32 samplingFreq, uint16 upFactor) {
+	uint i;
+	
+	for (i = 0; i < filters.size(); i++) {
+		if (filters[i]->_samplingFreq == samplingFreq
+			&& filters[i]->_upFactor == upFactor) {
+			return filters[i];
+		}
+	}
+	
+	FIRFilter *filter = new FIRFilter(passbandEdge, stopbandEdge, 
+				dBPassbandRipple, dBStopbandAtten, samplingFreq, upFactor);
+	
+	filters.push_back(filter);
+	
+	return filters[i];
+}
+
 } // End of namespace Audio

Modified: scummvm/branches/gsoc2007-mixer/sound/filter.h
===================================================================
--- scummvm/branches/gsoc2007-mixer/sound/filter.h	2007-09-02 09:52:06 UTC (rev 28815)
+++ scummvm/branches/gsoc2007-mixer/sound/filter.h	2007-09-02 11:18:36 UTC (rev 28816)
@@ -27,6 +27,7 @@
 #define SOUND_FILTER_H
 
 #include "common/scummsys.h"
+#include "common/array.h"
 
 namespace Audio {
 
@@ -43,10 +44,16 @@
 	double _ripple;
 	uint32 _length;
 	double *_coeffs;
+	
+	static Common::Array<FIRFilter *> filters;
 
 public:
-/* Generates lowpass filter coefficients using the parameters provided */
-	FIRFilter(
+
+	/*
+	 * Returns a pointer to an existing FIRFilter if possible; otherwise
+	 * generates a new one
+	 */
+	static FIRFilter *getFilter(
 			double passbandEdge,
 			double stopbandEdge,
 			double dBPassbandRipple,
@@ -54,13 +61,23 @@
 			uint32 samplingFreq,
 			uint16 upFactor);
 	
-	~FIRFilter() { free(_coeffs); }
-	
 	uint32 getLength() { return _length; }
 	
 	double *getCoeffs() { return _coeffs; }
 
 private:
+
+	/* Generates lowpass filter coefficients using the parameters provided */
+	FIRFilter(
+			double passbandEdge,
+			double stopbandEdge,
+			double dBPassbandRipple,
+			double dBStopbandAtten,
+			uint32 samplingFreq,
+			uint16 upFactor);
+	
+	~FIRFilter() { free(_coeffs); }
+	
 	double equiripple(
 			double dBPassbandRipple,
 			double dBStopbandAtten);

Modified: scummvm/branches/gsoc2007-mixer/sound/rate.cpp
===================================================================
--- scummvm/branches/gsoc2007-mixer/sound/rate.cpp	2007-09-02 09:52:06 UTC (rev 28815)
+++ scummvm/branches/gsoc2007-mixer/sound/rate.cpp	2007-09-02 11:18:36 UTC (rev 28816)
@@ -284,6 +284,7 @@
 class FilteringRateConverter : public RateConverter {
 protected:
 	FIRFilter *filt;
+	double *coeffs;
 	
 	/* Circular buffers for inputs which are currently in the filter */
 	st_sample_t *inBuf;
@@ -330,7 +331,6 @@
 public:
 	FilteringRateConverter(st_rate_t inrate, st_rate_t outrate);
 	~FilteringRateConverter() {
-		delete filt;
 		free(inBuf);
 		delete rand;
 	}
@@ -368,9 +368,11 @@
 	// TODO: Make it so that this filter data can be reused by other
 	//       converters. 
 	/* Generate the filter coefficients */
-	filt = new FIRFilter(lowpassBW * stopband / 2.0, stopband / 2.0,
+	filt = FIRFilter::getFilter(lowpassBW * stopband / 2.0, stopband / 2.0,
 						-40, 80, (uint32)inrate * upFactor, upFactor);
 	
+	coeffs = filt->getCoeffs();
+	
 	uint32 len = filt->getLength();
 	
 	subLen = (len + (upFactor - 1)) / upFactor;
@@ -389,8 +391,8 @@
 		 * softer than those of other resamplers using otherwise equal
 		 * options.
 		 */
-		//gain[i % upFactor] += fabs((filt->getCoeffs())[i]);
-		gain[i % upFactor] += (filt->getCoeffs())[i];
+		//gain[i % upFactor] += fabs(coeffs[i]);
+		gain[i % upFactor] += coeffs[i];
 	}
 	
 	/* Find the maximum of these subfilter gains */
@@ -455,10 +457,10 @@
 		 */
 		for (i = 0; i < subLen; i++) {
 			accum0 += (double)inBuf[(inPos + numChan * i) % subLen]
-						* (filt->getCoeffs())[currBank + i*upFactor];
+						* coeffs[currBank + i*upFactor];
 			if (stereo) {
 				accum1 += (double)inBuf[(inPos + numChan * i + 1) % subLen]
-						* (filt->getCoeffs())[currBank + i*upFactor];
+						* coeffs[currBank + i*upFactor];
 			}
 		}
 		


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