00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00041
00042 #include <memory.h>
00043 #include <assert.h>
00044 #include <math.h>
00045 #include <stdlib.h>
00046 #include <stdexcept>
00047 #include "FIRFilter.h"
00048 #include "cpu_detect.h"
00049
00050 using namespace soundtouch;
00051
00052
00053
00054
00055
00056
00057
00058 FIRFilter::FIRFilter()
00059 {
00060 resultDivFactor = 0;
00061 length = 0;
00062 lengthDiv8 = 0;
00063 filterCoeffs = NULL;
00064 }
00065
00066
00067 FIRFilter::~FIRFilter()
00068 {
00069 delete[] filterCoeffs;
00070 }
00071
00072
00073 uint FIRFilter::evaluateFilterStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples) const
00074 {
00075 uint i, j, end;
00076 LONG_SAMPLETYPE suml, sumr;
00077 #ifdef FLOAT_SAMPLES
00078
00079
00080 double dScaler = 1.0 / (double)resultDivider;
00081 #endif
00082
00083 assert(length != 0);
00084
00085 end = 2 * (numSamples - length);
00086
00087 for (j = 0; j < end; j += 2)
00088 {
00089 const SAMPLETYPE *ptr;
00090
00091 suml = sumr = 0;
00092 ptr = src + j;
00093
00094 for (i = 0; i < length; i += 4)
00095 {
00096
00097 suml += ptr[2 * i + 0] * filterCoeffs[i + 0] +
00098 ptr[2 * i + 2] * filterCoeffs[i + 1] +
00099 ptr[2 * i + 4] * filterCoeffs[i + 2] +
00100 ptr[2 * i + 6] * filterCoeffs[i + 3];
00101 sumr += ptr[2 * i + 1] * filterCoeffs[i + 0] +
00102 ptr[2 * i + 3] * filterCoeffs[i + 1] +
00103 ptr[2 * i + 5] * filterCoeffs[i + 2] +
00104 ptr[2 * i + 7] * filterCoeffs[i + 3];
00105 }
00106
00107 #ifdef INTEGER_SAMPLES
00108 suml >>= resultDivFactor;
00109 sumr >>= resultDivFactor;
00110
00111 suml = (suml < -32768) ? -32768 : (suml > 32767) ? 32767 : suml;
00112
00113 sumr = (sumr < -32768) ? -32768 : (sumr > 32767) ? 32767 : sumr;
00114 #else
00115 suml *= dScaler;
00116 sumr *= dScaler;
00117 #endif // INTEGER_SAMPLES
00118 dest[j] = (SAMPLETYPE)suml;
00119 dest[j + 1] = (SAMPLETYPE)sumr;
00120 }
00121 return numSamples - length;
00122 }
00123
00124
00125
00126
00127
00128 uint FIRFilter::evaluateFilterMono(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples) const
00129 {
00130 uint i, j, end;
00131 LONG_SAMPLETYPE sum;
00132 #ifdef FLOAT_SAMPLES
00133
00134
00135 double dScaler = 1.0 / (double)resultDivider;
00136 #endif
00137
00138
00139 assert(length != 0);
00140
00141 end = numSamples - length;
00142 for (j = 0; j < end; j ++)
00143 {
00144 sum = 0;
00145 for (i = 0; i < length; i += 4)
00146 {
00147
00148 sum += src[i + 0] * filterCoeffs[i + 0] +
00149 src[i + 1] * filterCoeffs[i + 1] +
00150 src[i + 2] * filterCoeffs[i + 2] +
00151 src[i + 3] * filterCoeffs[i + 3];
00152 }
00153 #ifdef INTEGER_SAMPLES
00154 sum >>= resultDivFactor;
00155
00156 sum = (sum < -32768) ? -32768 : (sum > 32767) ? 32767 : sum;
00157 #else
00158 sum *= dScaler;
00159 #endif // INTEGER_SAMPLES
00160 dest[j] = (SAMPLETYPE)sum;
00161 src ++;
00162 }
00163 return end;
00164 }
00165
00166
00167
00168
00169
00170 void FIRFilter::setCoefficients(const SAMPLETYPE *coeffs, uint newLength, uint uResultDivFactor)
00171 {
00172 assert(newLength > 0);
00173 if (newLength % 8) throw std::runtime_error("FIR filter length not divisible by 8");
00174
00175 lengthDiv8 = newLength / 8;
00176 length = lengthDiv8 * 8;
00177 assert(length == newLength);
00178
00179 resultDivFactor = uResultDivFactor;
00180 resultDivider = (SAMPLETYPE)pow(2, resultDivFactor);
00181
00182 delete[] filterCoeffs;
00183 filterCoeffs = new SAMPLETYPE[length];
00184 memcpy(filterCoeffs, coeffs, length * sizeof(SAMPLETYPE));
00185 }
00186
00187
00188 uint FIRFilter::getLength() const
00189 {
00190 return length;
00191 }
00192
00193
00194
00195
00196
00197
00198
00199 uint FIRFilter::evaluate(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples, uint numChannels) const
00200 {
00201 assert(numChannels == 1 || numChannels == 2);
00202
00203 assert(length > 0);
00204 assert(lengthDiv8 * 8 == length);
00205 if (numSamples < length) return 0;
00206 assert(resultDivFactor >= 0);
00207 if (numChannels == 2)
00208 {
00209 return evaluateFilterStereo(dest, src, numSamples);
00210 } else {
00211 return evaluateFilterMono(dest, src, numSamples);
00212 }
00213 }
00214
00215
00216
00217
00218
00219 void * FIRFilter::operator new(size_t s)
00220 {
00221
00222 throw std::runtime_error("Don't use 'new FIRFilter', use 'newInstance' member instead!");
00223 return NULL;
00224 }
00225
00226
00227 FIRFilter * FIRFilter::newInstance()
00228 {
00229 uint uExtensions;
00230
00231 uExtensions = detectCPUextensions();
00232
00233
00234
00235 #ifdef ALLOW_MMX
00236
00237 if (uExtensions & MM_MMX)
00238 {
00239 return ::new FIRFilterMMX;
00240 }
00241 else
00242 #endif // ALLOW_MMX
00243
00244 #ifdef ALLOW_SSE
00245 if (uExtensions & MM_SSE)
00246 {
00247
00248 return ::new FIRFilterSSE;
00249 }
00250 else
00251 #endif // ALLOW_SSE
00252
00253 #ifdef ALLOW_3DNOW
00254 if (uExtensions & MM_3DNOW)
00255 {
00256
00257 return ::new FIRFilter3DNow;
00258 }
00259 else
00260 #endif // ALLOW_3DNOW
00261
00262 {
00263
00264 return ::new FIRFilter;
00265 }
00266 }