Updated to soundtouch-1.3 (plus modifications)
[ardour.git] / libs / soundtouch / FIRFilter.h
1 ////////////////////////////////////////////////////////////////////////////////
2 ///
3 /// General FIR digital filter routines with MMX optimization. 
4 ///
5 /// Note : MMX optimized functions reside in a separate, platform-specific file, 
6 /// e.g. 'mmx_win.cpp' or 'mmx_gcc.cpp'
7 ///
8 /// Author        : Copyright (c) Olli Parviainen
9 /// Author e-mail : oparviai @ iki.fi
10 /// SoundTouch WWW: http://www.iki.fi/oparviai/soundtouch
11 ///
12 ////////////////////////////////////////////////////////////////////////////////
13 //
14 // Last changed  : $Date$
15 // File revision : $Revision$
16 //
17 // $Id$
18 //
19 ////////////////////////////////////////////////////////////////////////////////
20 //
21 // License :
22 //
23 //  SoundTouch audio processing library
24 //  Copyright (c) Olli Parviainen
25 //
26 //  This library is free software; you can redistribute it and/or
27 //  modify it under the terms of the GNU Lesser General Public
28 //  License as published by the Free Software Foundation; either
29 //  version 2.1 of the License, or (at your option) any later version.
30 //
31 //  This library is distributed in the hope that it will be useful,
32 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
33 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
34 //  Lesser General Public License for more details.
35 //
36 //  You should have received a copy of the GNU Lesser General Public
37 //  License along with this library; if not, write to the Free Software
38 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
39 //
40 ////////////////////////////////////////////////////////////////////////////////
41
42 #ifndef FIRFilter_H
43 #define FIRFilter_H
44
45 #include "STTypes.h"
46
47 namespace soundtouch
48 {
49
50 class FIRFilter 
51 {
52 protected:
53     // Number of FIR filter taps
54     uint length;    
55     // Number of FIR filter taps divided by 8
56     uint lengthDiv8;
57
58     // Result divider factor in 2^k format
59     uint resultDivFactor;
60
61     // Result divider value.
62     SAMPLETYPE resultDivider;
63
64     // Memory for filter coefficients
65     SAMPLETYPE *filterCoeffs;
66
67     virtual uint evaluateFilterStereo(SAMPLETYPE *dest, 
68                                       const SAMPLETYPE *src, 
69                                       uint numSamples) const;
70     virtual uint evaluateFilterMono(SAMPLETYPE *dest, 
71                                     const SAMPLETYPE *src, 
72                                     uint numSamples) const;
73
74     FIRFilter();
75
76 public:
77     virtual ~FIRFilter();
78
79     static FIRFilter *newInstance();
80
81     /// Applies the filter to the given sequence of samples. 
82     /// Note : The amount of outputted samples is by value of 'filter_length' 
83     /// smaller than the amount of input samples.
84     ///
85     /// \return Number of samples copied to 'dest'.
86     uint evaluate(SAMPLETYPE *dest, 
87                   const SAMPLETYPE *src, 
88                   uint numSamples, 
89                   uint numChannels) const;
90
91     uint getLength() const;
92
93     virtual void setCoefficients(const SAMPLETYPE *coeffs, 
94                                  uint newLength, 
95                                  uint uResultDivFactor);
96 };
97
98
99 // Optional subclasses that implement CPU-specific optimizations:
100
101 #ifdef ALLOW_MMX
102
103     /// Class that implements MMX optimized functions exclusive for 16bit integer samples type.
104     class FIRFilterMMX : public FIRFilter
105     {
106     protected:
107         short *filterCoeffsUnalign;
108         short *filterCoeffsAlign;
109
110         virtual uint evaluateFilterStereo(short *dest, const short *src, uint numSamples) const;
111     public:
112         FIRFilterMMX();
113         ~FIRFilterMMX();
114
115         virtual void setCoefficients(const short *coeffs, uint newLength, uint uResultDivFactor);
116     };
117
118 #endif // ALLOW_MMX
119
120
121 #ifdef ALLOW_3DNOW
122
123     /// Class that implements 3DNow! optimized functions exclusive for floating point samples type.
124     class FIRFilter3DNow : public FIRFilter
125     {
126     protected:
127         float *filterCoeffsUnalign;
128         float *filterCoeffsAlign;
129
130         virtual uint evaluateFilterStereo(float *dest, const float *src, uint numSamples) const;
131     public:
132         FIRFilter3DNow();
133         ~FIRFilter3DNow();
134         virtual void setCoefficients(const float *coeffs, uint newLength, uint uResultDivFactor);
135     };
136
137 #endif  // ALLOW_3DNOW
138
139
140 #ifdef ALLOW_SSE
141     /// Class that implements SSE optimized functions exclusive for floating point samples type.
142     class FIRFilterSSE : public FIRFilter
143     {
144     protected:
145         float *filterCoeffsUnalign;
146         float *filterCoeffsAlign;
147
148         virtual uint evaluateFilterStereo(float *dest, const float *src, uint numSamples) const;
149     public:
150         FIRFilterSSE();
151         ~FIRFilterSSE();
152
153         virtual void setCoefficients(const float *coeffs, uint newLength, uint uResultDivFactor);
154     };
155
156 #endif // ALLOW_SSE
157
158 }
159
160 #endif  // FIRFilter_H