fix bug in find_next_region() when starting frame matches region start
[ardour.git] / libs / soundtouch / RateTransposer.h
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 @ iki.fi
13 /// SoundTouch WWW: http://www.iki.fi/oparviai/soundtouch
14 ///
15 ////////////////////////////////////////////////////////////////////////////////
16 //
17 // Last changed  : $Date$
18 // File revision : $Revision$
19 //
20 // $Id$
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 "AAFilter.h"
49 #include "FIFOSamplePipe.h"
50 #include "FIFOSampleBuffer.h"
51
52 #include "STTypes.h"
53
54 namespace soundtouch
55 {
56
57 /// A common linear samplerate transposer class.
58 ///
59 /// Note: Use function "RateTransposer::newInstance()" to create a new class 
60 /// instance instead of the "new" operator; that function automatically 
61 /// chooses a correct implementation depending on if integer or floating 
62 /// arithmetics are to be used.
63 class RateTransposer : public FIFOProcessor
64 {
65 protected:
66     /// Anti-alias filter object
67     AAFilter *pAAFilter;
68
69     float fRate;
70
71     uint uChannels;
72
73     /// Buffer for collecting samples to feed the anti-alias filter between
74     /// two batches
75     FIFOSampleBuffer storeBuffer;
76
77     /// Buffer for keeping samples between transposing & anti-alias filter
78     FIFOSampleBuffer tempBuffer;
79
80     /// Output sample buffer
81     FIFOSampleBuffer outputBuffer;
82
83     BOOL bUseAAFilter;
84
85     void init();
86
87     virtual void resetRegisters() = 0;
88
89     virtual uint transposeStereo(SAMPLETYPE *dest, 
90                          const SAMPLETYPE *src, 
91                          uint numSamples) = 0;
92     virtual uint transposeMono(SAMPLETYPE *dest, 
93                        const SAMPLETYPE *src, 
94                        uint numSamples) = 0;
95     uint transpose(SAMPLETYPE *dest, 
96                    const SAMPLETYPE *src, 
97                    uint numSamples);
98
99     void flushStoreBuffer();
100
101     void downsample(const SAMPLETYPE *src, 
102                     uint numSamples);
103     void upsample(const SAMPLETYPE *src, 
104                  uint numSamples);
105
106     /// Transposes sample rate by applying anti-alias filter to prevent folding. 
107     /// Returns amount of samples returned in the "dest" buffer.
108     /// The maximum amount of samples that can be returned at a time is set by
109     /// the 'set_returnBuffer_size' function.
110     void processSamples(const SAMPLETYPE *src, 
111                         uint numSamples);
112
113     RateTransposer();
114
115 public:
116     virtual ~RateTransposer();
117
118     /// Use this function instead of "new" operator to create a new instance of this class. 
119     /// This function automatically chooses a correct implementation, depending on if 
120     /// integer ot floating point arithmetics are to be used.
121     static RateTransposer *newInstance();
122
123     /// Returns the output buffer object
124     FIFOSamplePipe *getOutput() { return &outputBuffer; };
125
126     /// Returns the store buffer object
127     FIFOSamplePipe *getStore() { return &storeBuffer; };
128
129     /// Return anti-alias filter object
130     AAFilter *getAAFilter() const;
131
132     /// Enables/disables the anti-alias filter. Zero to disable, nonzero to enable
133     void enableAAFilter(BOOL newMode);
134
135     /// Returns nonzero if anti-alias filter is enabled.
136     BOOL isAAFilterEnabled() const;
137
138     /// Sets new target rate. Normal rate = 1.0, smaller values represent slower 
139     /// rate, larger faster rates.
140     virtual void setRate(float newRate);
141
142     /// Sets the number of channels, 1 = mono, 2 = stereo
143     void setChannels(uint channels);
144
145     /// Adds 'numSamples' pcs of samples from the 'samples' memory position into
146     /// the input of the object.
147     void putSamples(const SAMPLETYPE *samples, uint numSamples);
148
149     /// Clears all the samples in the object
150     void clear();
151
152     /// Returns nonzero if there aren't any samples available for outputting.
153     uint isEmpty();
154 };
155
156 }
157
158 #endif