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: 2009-05-17 14:35:13 +0300 (Sun, 17 May 2009) $ |
17 |
// File revision : $Revision: 4 $ |
18 |
// |
19 |
// $Id: TDStretch.h 71 2009-05-17 11:35:13Z 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 *pRefMidBuffer; |
119 |
SAMPLETYPE *pRefMidBufferUnaligned; |
120 |
int overlapLength; |
121 |
int seekLength; |
122 |
int seekWindowLength; |
123 |
int overlapDividerBits; |
124 |
int slopingDivider; |
125 |
float nominalSkip; |
126 |
float skipFract; |
127 |
FIFOSampleBuffer outputBuffer; |
128 |
FIFOSampleBuffer inputBuffer; |
129 |
BOOL bQuickSeek; |
130 |
// int outDebt; |
131 |
// BOOL bMidBufferDirty; |
132 |
|
133 |
int sampleRate; |
134 |
int sequenceMs; |
135 |
int seekWindowMs; |
136 |
int overlapMs; |
137 |
BOOL bAutoSeqSetting; |
138 |
BOOL bAutoSeekSetting; |
139 |
|
140 |
void acceptNewOverlapLength(int newOverlapLength); |
141 |
|
142 |
virtual void clearCrossCorrState(); |
143 |
void calculateOverlapLength(int overlapMs); |
144 |
|
145 |
virtual LONG_SAMPLETYPE calcCrossCorrStereo(const SAMPLETYPE *mixingPos, const SAMPLETYPE *compare) const; |
146 |
virtual LONG_SAMPLETYPE calcCrossCorrMono(const SAMPLETYPE *mixingPos, const SAMPLETYPE *compare) const; |
147 |
|
148 |
virtual int seekBestOverlapPositionStereo(const SAMPLETYPE *refPos); |
149 |
virtual int seekBestOverlapPositionStereoQuick(const SAMPLETYPE *refPos); |
150 |
virtual int seekBestOverlapPositionMono(const SAMPLETYPE *refPos); |
151 |
virtual int seekBestOverlapPositionMonoQuick(const SAMPLETYPE *refPos); |
152 |
int seekBestOverlapPosition(const SAMPLETYPE *refPos); |
153 |
|
154 |
virtual void overlapStereo(SAMPLETYPE *output, const SAMPLETYPE *input) const; |
155 |
virtual void overlapMono(SAMPLETYPE *output, const SAMPLETYPE *input) const; |
156 |
|
157 |
void clearMidBuffer(); |
158 |
void overlap(SAMPLETYPE *output, const SAMPLETYPE *input, uint ovlPos) const; |
159 |
|
160 |
void precalcCorrReferenceMono(); |
161 |
void precalcCorrReferenceStereo(); |
162 |
|
163 |
void calcSeqParameters(); |
164 |
|
165 |
/// Changes the tempo of the given sound samples. |
166 |
/// Returns amount of samples returned in the "output" buffer. |
167 |
/// The maximum amount of samples that can be returned at a time is set by |
168 |
/// the 'set_returnBuffer_size' function. |
169 |
void processSamples(); |
170 |
|
171 |
public: |
172 |
TDStretch(); |
173 |
virtual ~TDStretch(); |
174 |
|
175 |
/// Operator 'new' is overloaded so that it automatically creates a suitable instance |
176 |
/// depending on if we've a MMX/SSE/etc-capable CPU available or not. |
177 |
static void *operator new(size_t s); |
178 |
|
179 |
/// Use this function instead of "new" operator to create a new instance of this class. |
180 |
/// This function automatically chooses a correct feature set depending on if the CPU |
181 |
/// supports MMX/SSE/etc extensions. |
182 |
static TDStretch *newInstance(); |
183 |
|
184 |
/// Returns the output buffer object |
185 |
FIFOSamplePipe *getOutput() { return &outputBuffer; }; |
186 |
|
187 |
/// Returns the input buffer object |
188 |
FIFOSamplePipe *getInput() { return &inputBuffer; }; |
189 |
|
190 |
/// Sets new target tempo. Normal tempo = 'SCALE', smaller values represent slower |
191 |
/// tempo, larger faster tempo. |
192 |
void setTempo(float newTempo); |
193 |
|
194 |
/// Returns nonzero if there aren't any samples available for outputting. |
195 |
virtual void clear(); |
196 |
|
197 |
/// Clears the input buffer |
198 |
void clearInput(); |
199 |
|
200 |
/// Sets the number of channels, 1 = mono, 2 = stereo |
201 |
void setChannels(int numChannels); |
202 |
|
203 |
/// Enables/disables the quick position seeking algorithm. Zero to disable, |
204 |
/// nonzero to enable |
205 |
void enableQuickSeek(BOOL enable); |
206 |
|
207 |
/// Returns nonzero if the quick seeking algorithm is enabled. |
208 |
BOOL isQuickSeekEnabled() const; |
209 |
|
210 |
/// Sets routine control parameters. These control are certain time constants |
211 |
/// defining how the sound is stretched to the desired duration. |
212 |
// |
213 |
/// 'sampleRate' = sample rate of the sound |
214 |
/// 'sequenceMS' = one processing sequence length in milliseconds |
215 |
/// 'seekwindowMS' = seeking window length for scanning the best overlapping |
216 |
/// position |
217 |
/// 'overlapMS' = overlapping length |
218 |
void setParameters(int sampleRate, ///< Samplerate of sound being processed (Hz) |
219 |
int sequenceMS = -1, ///< Single processing sequence length (ms) |
220 |
int seekwindowMS = -1, ///< Offset seeking window length (ms) |
221 |
int overlapMS = -1 ///< Sequence overlapping length (ms) |
222 |
); |
223 |
|
224 |
/// Get routine control parameters, see setParameters() function. |
225 |
/// Any of the parameters to this function can be NULL, in such case corresponding parameter |
226 |
/// value isn't returned. |
227 |
void getParameters(int *pSampleRate, int *pSequenceMs, int *pSeekWindowMs, int *pOverlapMs) const; |
228 |
|
229 |
/// Adds 'numsamples' pcs of samples from the 'samples' memory position into |
230 |
/// the input of the object. |
231 |
virtual void putSamples( |
232 |
const SAMPLETYPE *samples, ///< Input sample data |
233 |
uint numSamples ///< Number of samples in 'samples' so that one sample |
234 |
///< contains both channels if stereo |
235 |
); |
236 |
}; |
237 |
|
238 |
|
239 |
|
240 |
// Implementation-specific class declarations: |
241 |
|
242 |
#ifdef ALLOW_MMX |
243 |
/// Class that implements MMX optimized routines for 16bit integer samples type. |
244 |
class TDStretchMMX : public TDStretch |
245 |
{ |
246 |
protected: |
247 |
long calcCrossCorrStereo(const short *mixingPos, const short *compare) const; |
248 |
virtual void overlapStereo(short *output, const short *input) const; |
249 |
virtual void clearCrossCorrState(); |
250 |
}; |
251 |
#endif /// ALLOW_MMX |
252 |
|
253 |
|
254 |
#ifdef ALLOW_3DNOW |
255 |
/// Class that implements 3DNow! optimized routines for floating point samples type. |
256 |
class TDStretch3DNow : public TDStretch |
257 |
{ |
258 |
protected: |
259 |
double calcCrossCorrStereo(const float *mixingPos, const float *compare) const; |
260 |
}; |
261 |
#endif /// ALLOW_3DNOW |
262 |
|
263 |
|
264 |
#ifdef ALLOW_SSE |
265 |
/// Class that implements SSE optimized routines for floating point samples type. |
266 |
class TDStretchSSE : public TDStretch |
267 |
{ |
268 |
protected: |
269 |
double calcCrossCorrStereo(const float *mixingPos, const float *compare) const; |
270 |
}; |
271 |
|
272 |
#endif /// ALLOW_SSE |
273 |
|
274 |
} |
275 |
#endif /// TDStretch_H |