PageRenderTime 64ms CodeModel.GetById 30ms app.highlight 14ms RepoModel.GetById 16ms app.codeStats 1ms

/external/pysoundtouch14/libsoundtouch/TDStretch.h

http://echo-nest-remix.googlecode.com/
C++ Header | 268 lines | 106 code | 46 blank | 116 comment | 0 complexity | 4f7543f7e78304c534c0a57827d1afe9 MD5 | raw file
  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 optimized functions reside in a separate, platform-specific file, 
  8/// e.g. 'mmx_win.cpp' or 'mmx_gcc.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-01-25 15:43:54 +0200 (Sun, 25 Jan 2009) $
 17// File revision : $Revision: 4 $
 18//
 19// $Id: TDStretch.h 49 2009-01-25 13:43:54Z 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 "STTypes.h"

 48#include "RateTransposer.h"

 49#include "FIFOSamplePipe.h"

 50
 51namespace soundtouch
 52{
 53
 54// Default values for sound processing parameters:
 55
 56/// Default length of a single processing sequence, in milliseconds. This determines to how 
 57/// long sequences the original sound is chopped in the time-stretch algorithm.
 58///
 59/// The larger this value is, the lesser sequences are used in processing. In principle
 60/// a bigger value sounds better when slowing down tempo, but worse when increasing tempo
 61/// and vice versa.
 62///
 63/// Increasing this value reduces computational burden & vice versa.
 64//#define DEFAULT_SEQUENCE_MS         130
 65#define DEFAULT_SEQUENCE_MS         USE_AUTO_SEQUENCE_LEN

 66
 67/// Giving this value for the sequence length sets automatic parameter value
 68/// according to tempo setting (recommended)
 69#define USE_AUTO_SEQUENCE_LEN       0

 70
 71/// Seeking window default length in milliseconds for algorithm that finds the best possible 
 72/// overlapping location. This determines from how wide window the algorithm may look for an 
 73/// optimal joining location when mixing the sound sequences back together. 
 74///
 75/// The bigger this window setting is, the higher the possibility to find a better mixing
 76/// position will become, but at the same time large values may cause a "drifting" artifact
 77/// because consequent sequences will be taken at more uneven intervals.
 78///
 79/// If there's a disturbing artifact that sounds as if a constant frequency was drifting 
 80/// around, try reducing this setting.
 81///
 82/// Increasing this value increases computational burden & vice versa.
 83//#define DEFAULT_SEEKWINDOW_MS       25
 84#define DEFAULT_SEEKWINDOW_MS       USE_AUTO_SEEKWINDOW_LEN

 85
 86/// Giving this value for the seek window length sets automatic parameter value
 87/// according to tempo setting (recommended)
 88#define USE_AUTO_SEEKWINDOW_LEN     0

 89
 90/// Overlap length in milliseconds. When the chopped sound sequences are mixed back together, 
 91/// to form a continuous sound stream, this parameter defines over how long period the two 
 92/// consecutive sequences are let to overlap each other. 
 93///
 94/// This shouldn't be that critical parameter. If you reduce the DEFAULT_SEQUENCE_MS setting 
 95/// by a large amount, you might wish to try a smaller value on this.
 96///
 97/// Increasing this value increases computational burden & vice versa.
 98#define DEFAULT_OVERLAP_MS      8

 99
100
101/// Class that does the time-stretch (tempo change) effect for the processed
102/// sound.
103class TDStretch : public FIFOProcessor
104{
105protected:
106    int channels;
107    int sampleReq;
108    float tempo;
109
110    SAMPLETYPE *pMidBuffer;
111    SAMPLETYPE *pRefMidBuffer;
112    SAMPLETYPE *pRefMidBufferUnaligned;
113    int overlapLength;
114    int seekLength;
115    int seekWindowLength;
116    int maxOffset;
117    int overlapDividerBits;
118    int slopingDivider;
119    float nominalSkip;
120    float skipFract;
121    FIFOSampleBuffer outputBuffer;
122    FIFOSampleBuffer inputBuffer;
123    BOOL bQuickSeek;
124    BOOL bMidBufferDirty;
125
126    int sampleRate;
127    int sequenceMs;
128    int seekWindowMs;
129    int overlapMs;
130    BOOL bAutoSeqSetting;
131    BOOL bAutoSeekSetting;
132
133    void acceptNewOverlapLength(int newOverlapLength);
134
135    virtual void clearCrossCorrState();
136    void calculateOverlapLength(int overlapMs);
137
138    virtual LONG_SAMPLETYPE calcCrossCorrStereo(const SAMPLETYPE *mixingPos, const SAMPLETYPE *compare) const;
139    virtual LONG_SAMPLETYPE calcCrossCorrMono(const SAMPLETYPE *mixingPos, const SAMPLETYPE *compare) const;
140
141    virtual int seekBestOverlapPositionStereo(const SAMPLETYPE *refPos);
142    virtual int seekBestOverlapPositionStereoQuick(const SAMPLETYPE *refPos);
143    virtual int seekBestOverlapPositionMono(const SAMPLETYPE *refPos);
144    virtual int seekBestOverlapPositionMonoQuick(const SAMPLETYPE *refPos);
145    int seekBestOverlapPosition(const SAMPLETYPE *refPos);
146
147    virtual void overlapStereo(SAMPLETYPE *output, const SAMPLETYPE *input) const;
148    virtual void overlapMono(SAMPLETYPE *output, const SAMPLETYPE *input) const;
149
150    void clearMidBuffer();
151    void overlap(SAMPLETYPE *output, const SAMPLETYPE *input, uint ovlPos) const;
152
153    void precalcCorrReferenceMono();
154    void precalcCorrReferenceStereo();
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    
164public:
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
231
232
233// Implementation-specific class declarations:
234
235#ifdef ALLOW_MMX

236    /// Class that implements MMX optimized routines for 16bit integer samples type.
237    class TDStretchMMX : public TDStretch
238    {
239    protected:
240        long calcCrossCorrStereo(const short *mixingPos, const short *compare) const;
241        virtual void overlapStereo(short *output, const short *input) const;
242        virtual void clearCrossCorrState();
243    };
244#endif /// ALLOW_MMX
245
246
247#ifdef ALLOW_3DNOW

248    /// Class that implements 3DNow! optimized routines for floating point samples type.
249    class TDStretch3DNow : public TDStretch
250    {
251    protected:
252        double calcCrossCorrStereo(const float *mixingPos, const float *compare) const;
253    };
254#endif /// ALLOW_3DNOW
255
256
257#ifdef ALLOW_SSE

258    /// Class that implements SSE optimized routines for floating point samples type.
259    class TDStretchSSE : public TDStretch
260    {
261    protected:
262        double calcCrossCorrStereo(const float *mixingPos, const float *compare) const;
263    };
264
265#endif /// ALLOW_SSE
266
267}
268#endif  /// TDStretch_H