|
1 //////////////////////////////////////////////////////////////////////////////// |
|
2 /// |
|
3 /// Sampled sound tempo changer/time stretch algorithm. Changes the sound tempo |
|
4 /// while maintaining the original pitch by using a time domain WSOLA-like method |
|
5 /// with several performance-increasing tweaks. |
|
6 /// |
|
7 /// Note : MMX/SSE optimized functions reside in separate, platform-specific files |
|
8 /// 'mmx_optimized.cpp' and 'sse_optimized.cpp' |
|
9 /// |
|
10 /// Author : Copyright (c) Olli Parviainen |
|
11 /// Author e-mail : oparviai 'at' iki.fi |
|
12 /// SoundTouch WWW: http://www.surina.net/soundtouch |
|
13 /// |
|
14 //////////////////////////////////////////////////////////////////////////////// |
|
15 // |
|
16 // Last changed : $Date: 2014-04-06 10:57:21 -0500 (Sun, 06 Apr 2014) $ |
|
17 // File revision : $Revision: 4 $ |
|
18 // |
|
19 // $Id: TDStretch.h 195 2014-04-06 15:57:21Z oparviai $ |
|
20 // |
|
21 //////////////////////////////////////////////////////////////////////////////// |
|
22 // |
|
23 // License : |
|
24 // |
|
25 // SoundTouch audio processing library |
|
26 // Copyright (c) Olli Parviainen |
|
27 // |
|
28 // This library is free software; you can redistribute it and/or |
|
29 // modify it under the terms of the GNU Lesser General Public |
|
30 // License as published by the Free Software Foundation; either |
|
31 // version 2.1 of the License, or (at your option) any later version. |
|
32 // |
|
33 // This library is distributed in the hope that it will be useful, |
|
34 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
35 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
36 // Lesser General Public License for more details. |
|
37 // |
|
38 // You should have received a copy of the GNU Lesser General Public |
|
39 // License along with this library; if not, write to the Free Software |
|
40 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
41 // |
|
42 //////////////////////////////////////////////////////////////////////////////// |
|
43 |
|
44 #ifndef TDStretch_H |
|
45 #define TDStretch_H |
|
46 |
|
47 #include <stddef.h> |
|
48 #include "STTypes.h" |
|
49 #include "RateTransposer.h" |
|
50 #include "FIFOSamplePipe.h" |
|
51 |
|
52 namespace soundtouch |
|
53 { |
|
54 |
|
55 /// Default values for sound processing parameters: |
|
56 /// Notice that the default parameters are tuned for contemporary popular music |
|
57 /// processing. For speech processing applications these parameters suit better: |
|
58 /// #define DEFAULT_SEQUENCE_MS 40 |
|
59 /// #define DEFAULT_SEEKWINDOW_MS 15 |
|
60 /// #define DEFAULT_OVERLAP_MS 8 |
|
61 /// |
|
62 |
|
63 /// Default length of a single processing sequence, in milliseconds. This determines to how |
|
64 /// long sequences the original sound is chopped in the time-stretch algorithm. |
|
65 /// |
|
66 /// The larger this value is, the lesser sequences are used in processing. In principle |
|
67 /// a bigger value sounds better when slowing down tempo, but worse when increasing tempo |
|
68 /// and vice versa. |
|
69 /// |
|
70 /// Increasing this value reduces computational burden & vice versa. |
|
71 //#define DEFAULT_SEQUENCE_MS 40 |
|
72 #define DEFAULT_SEQUENCE_MS USE_AUTO_SEQUENCE_LEN |
|
73 |
|
74 /// Giving this value for the sequence length sets automatic parameter value |
|
75 /// according to tempo setting (recommended) |
|
76 #define USE_AUTO_SEQUENCE_LEN 0 |
|
77 |
|
78 /// Seeking window default length in milliseconds for algorithm that finds the best possible |
|
79 /// overlapping location. This determines from how wide window the algorithm may look for an |
|
80 /// optimal joining location when mixing the sound sequences back together. |
|
81 /// |
|
82 /// The bigger this window setting is, the higher the possibility to find a better mixing |
|
83 /// position will become, but at the same time large values may cause a "drifting" artifact |
|
84 /// because consequent sequences will be taken at more uneven intervals. |
|
85 /// |
|
86 /// If there's a disturbing artifact that sounds as if a constant frequency was drifting |
|
87 /// around, try reducing this setting. |
|
88 /// |
|
89 /// Increasing this value increases computational burden & vice versa. |
|
90 //#define DEFAULT_SEEKWINDOW_MS 15 |
|
91 #define DEFAULT_SEEKWINDOW_MS USE_AUTO_SEEKWINDOW_LEN |
|
92 |
|
93 /// Giving this value for the seek window length sets automatic parameter value |
|
94 /// according to tempo setting (recommended) |
|
95 #define USE_AUTO_SEEKWINDOW_LEN 0 |
|
96 |
|
97 /// Overlap length in milliseconds. When the chopped sound sequences are mixed back together, |
|
98 /// to form a continuous sound stream, this parameter defines over how long period the two |
|
99 /// consecutive sequences are let to overlap each other. |
|
100 /// |
|
101 /// This shouldn't be that critical parameter. If you reduce the DEFAULT_SEQUENCE_MS setting |
|
102 /// by a large amount, you might wish to try a smaller value on this. |
|
103 /// |
|
104 /// Increasing this value increases computational burden & vice versa. |
|
105 #define DEFAULT_OVERLAP_MS 8 |
|
106 |
|
107 |
|
108 /// Class that does the time-stretch (tempo change) effect for the processed |
|
109 /// sound. |
|
110 class TDStretch : public FIFOProcessor |
|
111 { |
|
112 protected: |
|
113 int channels; |
|
114 int sampleReq; |
|
115 float tempo; |
|
116 |
|
117 SAMPLETYPE *pMidBuffer; |
|
118 SAMPLETYPE *pMidBufferUnaligned; |
|
119 int overlapLength; |
|
120 int seekLength; |
|
121 int seekWindowLength; |
|
122 int overlapDividerBits; |
|
123 int slopingDivider; |
|
124 float nominalSkip; |
|
125 float skipFract; |
|
126 FIFOSampleBuffer outputBuffer; |
|
127 FIFOSampleBuffer inputBuffer; |
|
128 bool bQuickSeek; |
|
129 |
|
130 int sampleRate; |
|
131 int sequenceMs; |
|
132 int seekWindowMs; |
|
133 int overlapMs; |
|
134 bool bAutoSeqSetting; |
|
135 bool bAutoSeekSetting; |
|
136 |
|
137 void acceptNewOverlapLength(int newOverlapLength); |
|
138 |
|
139 virtual void clearCrossCorrState(); |
|
140 void calculateOverlapLength(int overlapMs); |
|
141 |
|
142 virtual double calcCrossCorr(const SAMPLETYPE *mixingPos, const SAMPLETYPE *compare, double &norm) const; |
|
143 virtual double calcCrossCorrAccumulate(const SAMPLETYPE *mixingPos, const SAMPLETYPE *compare, double &norm) const; |
|
144 |
|
145 virtual int seekBestOverlapPositionFull(const SAMPLETYPE *refPos); |
|
146 virtual int seekBestOverlapPositionQuick(const SAMPLETYPE *refPos); |
|
147 int seekBestOverlapPosition(const SAMPLETYPE *refPos); |
|
148 |
|
149 virtual void overlapStereo(SAMPLETYPE *output, const SAMPLETYPE *input) const; |
|
150 virtual void overlapMono(SAMPLETYPE *output, const SAMPLETYPE *input) const; |
|
151 virtual void overlapMulti(SAMPLETYPE *output, const SAMPLETYPE *input) const; |
|
152 |
|
153 void clearMidBuffer(); |
|
154 void overlap(SAMPLETYPE *output, const SAMPLETYPE *input, uint ovlPos) const; |
|
155 |
|
156 void calcSeqParameters(); |
|
157 |
|
158 /// Changes the tempo of the given sound samples. |
|
159 /// Returns amount of samples returned in the "output" buffer. |
|
160 /// The maximum amount of samples that can be returned at a time is set by |
|
161 /// the 'set_returnBuffer_size' function. |
|
162 void processSamples(); |
|
163 |
|
164 public: |
|
165 TDStretch(); |
|
166 virtual ~TDStretch(); |
|
167 |
|
168 /// Operator 'new' is overloaded so that it automatically creates a suitable instance |
|
169 /// depending on if we've a MMX/SSE/etc-capable CPU available or not. |
|
170 static void *operator new(size_t s); |
|
171 |
|
172 /// Use this function instead of "new" operator to create a new instance of this class. |
|
173 /// This function automatically chooses a correct feature set depending on if the CPU |
|
174 /// supports MMX/SSE/etc extensions. |
|
175 static TDStretch *newInstance(); |
|
176 |
|
177 /// Returns the output buffer object |
|
178 FIFOSamplePipe *getOutput() { return &outputBuffer; }; |
|
179 |
|
180 /// Returns the input buffer object |
|
181 FIFOSamplePipe *getInput() { return &inputBuffer; }; |
|
182 |
|
183 /// Sets new target tempo. Normal tempo = 'SCALE', smaller values represent slower |
|
184 /// tempo, larger faster tempo. |
|
185 void setTempo(float newTempo); |
|
186 |
|
187 /// Returns nonzero if there aren't any samples available for outputting. |
|
188 virtual void clear(); |
|
189 |
|
190 /// Clears the input buffer |
|
191 void clearInput(); |
|
192 |
|
193 /// Sets the number of channels, 1 = mono, 2 = stereo |
|
194 void setChannels(int numChannels); |
|
195 |
|
196 /// Enables/disables the quick position seeking algorithm. Zero to disable, |
|
197 /// nonzero to enable |
|
198 void enableQuickSeek(bool enable); |
|
199 |
|
200 /// Returns nonzero if the quick seeking algorithm is enabled. |
|
201 bool isQuickSeekEnabled() const; |
|
202 |
|
203 /// Sets routine control parameters. These control are certain time constants |
|
204 /// defining how the sound is stretched to the desired duration. |
|
205 // |
|
206 /// 'sampleRate' = sample rate of the sound |
|
207 /// 'sequenceMS' = one processing sequence length in milliseconds |
|
208 /// 'seekwindowMS' = seeking window length for scanning the best overlapping |
|
209 /// position |
|
210 /// 'overlapMS' = overlapping length |
|
211 void setParameters(int sampleRate, ///< Samplerate of sound being processed (Hz) |
|
212 int sequenceMS = -1, ///< Single processing sequence length (ms) |
|
213 int seekwindowMS = -1, ///< Offset seeking window length (ms) |
|
214 int overlapMS = -1 ///< Sequence overlapping length (ms) |
|
215 ); |
|
216 |
|
217 /// Get routine control parameters, see setParameters() function. |
|
218 /// Any of the parameters to this function can be NULL, in such case corresponding parameter |
|
219 /// value isn't returned. |
|
220 void getParameters(int *pSampleRate, int *pSequenceMs, int *pSeekWindowMs, int *pOverlapMs) const; |
|
221 |
|
222 /// Adds 'numsamples' pcs of samples from the 'samples' memory position into |
|
223 /// the input of the object. |
|
224 virtual void putSamples( |
|
225 const SAMPLETYPE *samples, ///< Input sample data |
|
226 uint numSamples ///< Number of samples in 'samples' so that one sample |
|
227 ///< contains both channels if stereo |
|
228 ); |
|
229 |
|
230 /// return nominal input sample requirement for triggering a processing batch |
|
231 int getInputSampleReq() const |
|
232 { |
|
233 return (int)(nominalSkip + 0.5); |
|
234 } |
|
235 |
|
236 /// return nominal output sample amount when running a processing batch |
|
237 int getOutputBatchSize() const |
|
238 { |
|
239 return seekWindowLength - overlapLength; |
|
240 } |
|
241 }; |
|
242 |
|
243 |
|
244 |
|
245 // Implementation-specific class declarations: |
|
246 |
|
247 #ifdef SOUNDTOUCH_ALLOW_MMX |
|
248 /// Class that implements MMX optimized routines for 16bit integer samples type. |
|
249 class TDStretchMMX : public TDStretch |
|
250 { |
|
251 protected: |
|
252 double calcCrossCorr(const short *mixingPos, const short *compare, double &norm) const; |
|
253 double calcCrossCorrAccumulate(const short *mixingPos, const short *compare, double &norm) const; |
|
254 virtual void overlapStereo(short *output, const short *input) const; |
|
255 virtual void clearCrossCorrState(); |
|
256 }; |
|
257 #endif /// SOUNDTOUCH_ALLOW_MMX |
|
258 |
|
259 |
|
260 #ifdef SOUNDTOUCH_ALLOW_SSE |
|
261 /// Class that implements SSE optimized routines for floating point samples type. |
|
262 class TDStretchSSE : public TDStretch |
|
263 { |
|
264 protected: |
|
265 double calcCrossCorr(const float *mixingPos, const float *compare, double &norm) const; |
|
266 double calcCrossCorrAccumulate(const float *mixingPos, const float *compare, double &norm) const; |
|
267 }; |
|
268 |
|
269 #endif /// SOUNDTOUCH_ALLOW_SSE |
|
270 |
|
271 } |
|
272 #endif /// TDStretch_H |