|
1 //////////////////////////////////////////////////////////////////////////////// |
|
2 /// |
|
3 /// Sample rate transposer. Changes sample rate by using linear interpolation |
|
4 /// together with anti-alias filtering (first order interpolation with anti- |
|
5 /// alias filtering should be quite adequate for this application). |
|
6 /// |
|
7 /// Use either of the derived classes of 'RateTransposerInteger' or |
|
8 /// 'RateTransposerFloat' for corresponding integer/floating point tranposing |
|
9 /// algorithm implementation. |
|
10 /// |
|
11 /// Author : Copyright (c) Olli Parviainen |
|
12 /// Author e-mail : oparviai 'at' iki.fi |
|
13 /// SoundTouch WWW: http://www.surina.net/soundtouch |
|
14 /// |
|
15 //////////////////////////////////////////////////////////////////////////////// |
|
16 // |
|
17 // Last changed : $Date: 2014-04-06 10:57:21 -0500 (Sun, 06 Apr 2014) $ |
|
18 // File revision : $Revision: 4 $ |
|
19 // |
|
20 // $Id: RateTransposer.h 195 2014-04-06 15:57:21Z oparviai $ |
|
21 // |
|
22 //////////////////////////////////////////////////////////////////////////////// |
|
23 // |
|
24 // License : |
|
25 // |
|
26 // SoundTouch audio processing library |
|
27 // Copyright (c) Olli Parviainen |
|
28 // |
|
29 // This library is free software; you can redistribute it and/or |
|
30 // modify it under the terms of the GNU Lesser General Public |
|
31 // License as published by the Free Software Foundation; either |
|
32 // version 2.1 of the License, or (at your option) any later version. |
|
33 // |
|
34 // This library is distributed in the hope that it will be useful, |
|
35 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
36 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
37 // Lesser General Public License for more details. |
|
38 // |
|
39 // You should have received a copy of the GNU Lesser General Public |
|
40 // License along with this library; if not, write to the Free Software |
|
41 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
42 // |
|
43 //////////////////////////////////////////////////////////////////////////////// |
|
44 |
|
45 #ifndef RateTransposer_H |
|
46 #define RateTransposer_H |
|
47 |
|
48 #include <stddef.h> |
|
49 #include "AAFilter.h" |
|
50 #include "FIFOSamplePipe.h" |
|
51 #include "FIFOSampleBuffer.h" |
|
52 |
|
53 #include "STTypes.h" |
|
54 |
|
55 namespace soundtouch |
|
56 { |
|
57 |
|
58 /// Abstract base class for transposer implementations (linear, advanced vs integer, float etc) |
|
59 class TransposerBase |
|
60 { |
|
61 public: |
|
62 enum ALGORITHM { |
|
63 LINEAR = 0, |
|
64 CUBIC, |
|
65 SHANNON |
|
66 }; |
|
67 |
|
68 protected: |
|
69 virtual void resetRegisters() = 0; |
|
70 |
|
71 virtual int transposeMono(SAMPLETYPE *dest, |
|
72 const SAMPLETYPE *src, |
|
73 int &srcSamples) = 0; |
|
74 virtual int transposeStereo(SAMPLETYPE *dest, |
|
75 const SAMPLETYPE *src, |
|
76 int &srcSamples) = 0; |
|
77 virtual int transposeMulti(SAMPLETYPE *dest, |
|
78 const SAMPLETYPE *src, |
|
79 int &srcSamples) = 0; |
|
80 |
|
81 static ALGORITHM algorithm; |
|
82 |
|
83 public: |
|
84 float rate; |
|
85 int numChannels; |
|
86 |
|
87 TransposerBase(); |
|
88 virtual ~TransposerBase(); |
|
89 |
|
90 virtual int transpose(FIFOSampleBuffer &dest, FIFOSampleBuffer &src); |
|
91 virtual void setRate(float newRate); |
|
92 virtual void setChannels(int channels); |
|
93 |
|
94 // static factory function |
|
95 static TransposerBase *newInstance(); |
|
96 |
|
97 // static function to set interpolation algorithm |
|
98 static void setAlgorithm(ALGORITHM a); |
|
99 }; |
|
100 |
|
101 |
|
102 /// A common linear samplerate transposer class. |
|
103 /// |
|
104 class RateTransposer : public FIFOProcessor |
|
105 { |
|
106 protected: |
|
107 /// Anti-alias filter object |
|
108 AAFilter *pAAFilter; |
|
109 TransposerBase *pTransposer; |
|
110 |
|
111 /// Buffer for collecting samples to feed the anti-alias filter between |
|
112 /// two batches |
|
113 FIFOSampleBuffer inputBuffer; |
|
114 |
|
115 /// Buffer for keeping samples between transposing & anti-alias filter |
|
116 FIFOSampleBuffer midBuffer; |
|
117 |
|
118 /// Output sample buffer |
|
119 FIFOSampleBuffer outputBuffer; |
|
120 |
|
121 bool bUseAAFilter; |
|
122 |
|
123 |
|
124 /// Transposes sample rate by applying anti-alias filter to prevent folding. |
|
125 /// Returns amount of samples returned in the "dest" buffer. |
|
126 /// The maximum amount of samples that can be returned at a time is set by |
|
127 /// the 'set_returnBuffer_size' function. |
|
128 void processSamples(const SAMPLETYPE *src, |
|
129 uint numSamples); |
|
130 |
|
131 public: |
|
132 RateTransposer(); |
|
133 virtual ~RateTransposer(); |
|
134 |
|
135 /// Operator 'new' is overloaded so that it automatically creates a suitable instance |
|
136 /// depending on if we're to use integer or floating point arithmetics. |
|
137 // static void *operator new(size_t s); |
|
138 |
|
139 /// Use this function instead of "new" operator to create a new instance of this class. |
|
140 /// This function automatically chooses a correct implementation, depending on if |
|
141 /// integer ot floating point arithmetics are to be used. |
|
142 // static RateTransposer *newInstance(); |
|
143 |
|
144 /// Returns the output buffer object |
|
145 FIFOSamplePipe *getOutput() { return &outputBuffer; }; |
|
146 |
|
147 /// Returns the store buffer object |
|
148 // FIFOSamplePipe *getStore() { return &storeBuffer; }; |
|
149 |
|
150 /// Return anti-alias filter object |
|
151 AAFilter *getAAFilter(); |
|
152 |
|
153 /// Enables/disables the anti-alias filter. Zero to disable, nonzero to enable |
|
154 void enableAAFilter(bool newMode); |
|
155 |
|
156 /// Returns nonzero if anti-alias filter is enabled. |
|
157 bool isAAFilterEnabled() const; |
|
158 |
|
159 /// Sets new target rate. Normal rate = 1.0, smaller values represent slower |
|
160 /// rate, larger faster rates. |
|
161 virtual void setRate(float newRate); |
|
162 |
|
163 /// Sets the number of channels, 1 = mono, 2 = stereo |
|
164 void setChannels(int channels); |
|
165 |
|
166 /// Adds 'numSamples' pcs of samples from the 'samples' memory position into |
|
167 /// the input of the object. |
|
168 void putSamples(const SAMPLETYPE *samples, uint numSamples); |
|
169 |
|
170 /// Clears all the samples in the object |
|
171 void clear(); |
|
172 |
|
173 /// Returns nonzero if there aren't any samples available for outputting. |
|
174 int isEmpty() const; |
|
175 }; |
|
176 |
|
177 } |
|
178 |
|
179 #endif |