1
////////////////////////////////////////////////////////////////////////////////
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.
7
/// Note : MMX/SSE optimized functions reside in separate, platform-specific files
8
/// 'mmx_optimized.cpp' and 'sse_optimized.cpp'
10
/// Author : Copyright (c) Olli Parviainen
11
/// Author e-mail : oparviai 'at' iki.fi
12
/// SoundTouch WWW: http://www.surina.net/soundtouch
14
////////////////////////////////////////////////////////////////////////////////
16
// Last changed : $Date: 2011-02-13 21:13:57 +0200 (Sun, 13 Feb 2011) $
17
// File revision : $Revision: 4 $
19
// $Id: TDStretch.h 104 2011-02-13 19:13:57Z oparviai $
21
////////////////////////////////////////////////////////////////////////////////
25
// SoundTouch audio processing library
26
// Copyright (c) Olli Parviainen
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.
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.
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
42
////////////////////////////////////////////////////////////////////////////////
49
#include "RateTransposer.h"
50
#include "FIFOSamplePipe.h"
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
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.
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
70
/// Increasing this value reduces computational burden & vice versa.
71
//#define DEFAULT_SEQUENCE_MS 40
72
#define DEFAULT_SEQUENCE_MS USE_AUTO_SEQUENCE_LEN
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
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.
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.
86
/// If there's a disturbing artifact that sounds as if a constant frequency was drifting
87
/// around, try reducing this setting.
89
/// Increasing this value increases computational burden & vice versa.
90
//#define DEFAULT_SEEKWINDOW_MS 15
91
#define DEFAULT_SEEKWINDOW_MS USE_AUTO_SEEKWINDOW_LEN
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
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.
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.
104
/// Increasing this value increases computational burden & vice versa.
105
#define DEFAULT_OVERLAP_MS 8
108
/// Class that does the time-stretch (tempo change) effect for the processed
110
class TDStretch : public FIFOProcessor
117
SAMPLETYPE *pMidBuffer;
118
SAMPLETYPE *pRefMidBuffer;
119
SAMPLETYPE *pRefMidBufferUnaligned;
122
int seekWindowLength;
123
int overlapDividerBits;
127
FIFOSampleBuffer outputBuffer;
128
FIFOSampleBuffer inputBuffer;
131
// BOOL bMidBufferDirty;
137
BOOL bAutoSeqSetting;
138
BOOL bAutoSeekSetting;
140
void acceptNewOverlapLength(int newOverlapLength);
142
virtual void clearCrossCorrState();
143
void calculateOverlapLength(int overlapMs);
145
virtual LONG_SAMPLETYPE calcCrossCorrStereo(const SAMPLETYPE *mixingPos, const SAMPLETYPE *compare) const;
146
virtual LONG_SAMPLETYPE calcCrossCorrMono(const SAMPLETYPE *mixingPos, const SAMPLETYPE *compare) const;
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);
154
virtual void overlapStereo(SAMPLETYPE *output, const SAMPLETYPE *input) const;
155
virtual void overlapMono(SAMPLETYPE *output, const SAMPLETYPE *input) const;
157
void clearMidBuffer();
158
void overlap(SAMPLETYPE *output, const SAMPLETYPE *input, uint ovlPos) const;
160
void precalcCorrReferenceMono();
161
void precalcCorrReferenceStereo();
163
void calcSeqParameters();
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();
173
virtual ~TDStretch();
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);
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();
184
/// Returns the output buffer object
185
FIFOSamplePipe *getOutput() { return &outputBuffer; };
187
/// Returns the input buffer object
188
FIFOSamplePipe *getInput() { return &inputBuffer; };
190
/// Sets new target tempo. Normal tempo = 'SCALE', smaller values represent slower
191
/// tempo, larger faster tempo.
192
void setTempo(float newTempo);
194
/// Returns nonzero if there aren't any samples available for outputting.
195
virtual void clear();
197
/// Clears the input buffer
200
/// Sets the number of channels, 1 = mono, 2 = stereo
201
void setChannels(int numChannels);
203
/// Enables/disables the quick position seeking algorithm. Zero to disable,
204
/// nonzero to enable
205
void enableQuickSeek(BOOL enable);
207
/// Returns nonzero if the quick seeking algorithm is enabled.
208
BOOL isQuickSeekEnabled() const;
210
/// Sets routine control parameters. These control are certain time constants
211
/// defining how the sound is stretched to the desired duration.
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
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)
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;
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
237
/// return nominal input sample requirement for triggering a processing batch
238
int getInputSampleReq() const
240
return (int)(nominalSkip + 0.5);
243
/// return nominal output sample amount when running a processing batch
244
int getOutputBatchSize() const
246
return seekWindowLength - overlapLength;
252
// Implementation-specific class declarations:
254
#ifdef SOUNDTOUCH_ALLOW_MMX
255
/// Class that implements MMX optimized routines for 16bit integer samples type.
256
class TDStretchMMX : public TDStretch
259
long calcCrossCorrStereo(const short *mixingPos, const short *compare) const;
260
virtual void overlapStereo(short *output, const short *input) const;
261
virtual void clearCrossCorrState();
263
#endif /// SOUNDTOUCH_ALLOW_MMX
266
#ifdef SOUNDTOUCH_ALLOW_SSE
267
/// Class that implements SSE optimized routines for floating point samples type.
268
class TDStretchSSE : public TDStretch
271
double calcCrossCorrStereo(const float *mixingPos, const float *compare) const;
274
#endif /// SOUNDTOUCH_ALLOW_SSE
277
#endif /// TDStretch_H