2009-02-15 05:15:39 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-04-17 10:46:57 +00:00
|
|
|
///
|
|
|
|
/// Sampled sound tempo changer/time stretch algorithm. Changes the sound tempo
|
|
|
|
/// while maintaining the original pitch by using a time domain WSOLA-like method
|
2009-02-15 05:15:39 +00:00
|
|
|
/// with several performance-increasing tweaks.
|
|
|
|
///
|
2013-04-17 10:46:57 +00:00
|
|
|
/// Note : MMX/SSE optimized functions reside in separate, platform-specific files
|
2010-03-18 20:04:51 +00:00
|
|
|
/// 'mmx_optimized.cpp' and 'sse_optimized.cpp'
|
2009-02-15 05:15:39 +00:00
|
|
|
///
|
|
|
|
/// Author : Copyright (c) Olli Parviainen
|
|
|
|
/// Author e-mail : oparviai 'at' iki.fi
|
|
|
|
/// SoundTouch WWW: http://www.surina.net/soundtouch
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2017-09-26 23:34:13 +00:00
|
|
|
// Last changed : $Date: 2016-10-20 19:30:11 +0300 (to, 20 loka 2016) $
|
2010-03-18 20:04:51 +00:00
|
|
|
// File revision : $Revision: 4 $
|
2009-02-15 05:15:39 +00:00
|
|
|
//
|
2017-09-26 23:34:13 +00:00
|
|
|
// $Id: TDStretch.h 244 2016-10-20 16:30:11Z oparviai $
|
2009-02-15 05:15:39 +00:00
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// License :
|
|
|
|
//
|
|
|
|
// SoundTouch audio processing library
|
|
|
|
// Copyright (c) Olli Parviainen
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef TDStretch_H
|
|
|
|
#define TDStretch_H
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
#include <stddef.h>
|
2009-02-15 05:15:39 +00:00
|
|
|
#include "STTypes.h"
|
|
|
|
#include "RateTransposer.h"
|
|
|
|
#include "FIFOSamplePipe.h"
|
|
|
|
|
|
|
|
namespace soundtouch
|
|
|
|
{
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
/// Default values for sound processing parameters:
|
2013-04-17 10:46:57 +00:00
|
|
|
/// Notice that the default parameters are tuned for contemporary popular music
|
2010-03-18 20:04:51 +00:00
|
|
|
/// processing. For speech processing applications these parameters suit better:
|
|
|
|
/// #define DEFAULT_SEQUENCE_MS 40
|
|
|
|
/// #define DEFAULT_SEEKWINDOW_MS 15
|
|
|
|
/// #define DEFAULT_OVERLAP_MS 8
|
|
|
|
///
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
/// Default length of a single processing sequence, in milliseconds. This determines to how
|
2009-02-15 05:15:39 +00:00
|
|
|
/// long sequences the original sound is chopped in the time-stretch algorithm.
|
|
|
|
///
|
|
|
|
/// The larger this value is, the lesser sequences are used in processing. In principle
|
|
|
|
/// a bigger value sounds better when slowing down tempo, but worse when increasing tempo
|
|
|
|
/// and vice versa.
|
|
|
|
///
|
|
|
|
/// Increasing this value reduces computational burden & vice versa.
|
2010-03-18 20:04:51 +00:00
|
|
|
//#define DEFAULT_SEQUENCE_MS 40
|
|
|
|
#define DEFAULT_SEQUENCE_MS USE_AUTO_SEQUENCE_LEN
|
|
|
|
|
|
|
|
/// Giving this value for the sequence length sets automatic parameter value
|
|
|
|
/// according to tempo setting (recommended)
|
|
|
|
#define USE_AUTO_SEQUENCE_LEN 0
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
/// Seeking window default length in milliseconds for algorithm that finds the best possible
|
|
|
|
/// overlapping location. This determines from how wide window the algorithm may look for an
|
|
|
|
/// optimal joining location when mixing the sound sequences back together.
|
2010-03-18 20:04:51 +00:00
|
|
|
///
|
|
|
|
/// The bigger this window setting is, the higher the possibility to find a better mixing
|
|
|
|
/// position will become, but at the same time large values may cause a "drifting" artifact
|
|
|
|
/// because consequent sequences will be taken at more uneven intervals.
|
|
|
|
///
|
2013-04-17 10:46:57 +00:00
|
|
|
/// If there's a disturbing artifact that sounds as if a constant frequency was drifting
|
2010-03-18 20:04:51 +00:00
|
|
|
/// around, try reducing this setting.
|
|
|
|
///
|
|
|
|
/// Increasing this value increases computational burden & vice versa.
|
|
|
|
//#define DEFAULT_SEEKWINDOW_MS 15
|
|
|
|
#define DEFAULT_SEEKWINDOW_MS USE_AUTO_SEEKWINDOW_LEN
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
/// Giving this value for the seek window length sets automatic parameter value
|
|
|
|
/// according to tempo setting (recommended)
|
|
|
|
#define USE_AUTO_SEEKWINDOW_LEN 0
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
/// Overlap length in milliseconds. When the chopped sound sequences are mixed back together,
|
|
|
|
/// to form a continuous sound stream, this parameter defines over how long period the two
|
|
|
|
/// consecutive sequences are let to overlap each other.
|
2010-03-18 20:04:51 +00:00
|
|
|
///
|
2013-04-17 10:46:57 +00:00
|
|
|
/// This shouldn't be that critical parameter. If you reduce the DEFAULT_SEQUENCE_MS setting
|
2010-03-18 20:04:51 +00:00
|
|
|
/// by a large amount, you might wish to try a smaller value on this.
|
|
|
|
///
|
|
|
|
/// Increasing this value increases computational burden & vice versa.
|
|
|
|
#define DEFAULT_OVERLAP_MS 8
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// Class that does the time-stretch (tempo change) effect for the processed
|
|
|
|
/// sound.
|
|
|
|
class TDStretch : public FIFOProcessor
|
|
|
|
{
|
|
|
|
protected:
|
2010-03-18 20:04:51 +00:00
|
|
|
int channels;
|
|
|
|
int sampleReq;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
int overlapLength;
|
|
|
|
int seekLength;
|
|
|
|
int seekWindowLength;
|
2015-10-08 00:17:50 +00:00
|
|
|
int overlapDividerBitsNorm;
|
|
|
|
int overlapDividerBitsPure;
|
2010-03-18 20:04:51 +00:00
|
|
|
int slopingDivider;
|
|
|
|
int sampleRate;
|
|
|
|
int sequenceMs;
|
|
|
|
int seekWindowMs;
|
|
|
|
int overlapMs;
|
2015-10-08 00:17:50 +00:00
|
|
|
|
|
|
|
unsigned long maxnorm;
|
|
|
|
float maxnormf;
|
|
|
|
|
|
|
|
double tempo;
|
|
|
|
double nominalSkip;
|
|
|
|
double skipFract;
|
|
|
|
|
|
|
|
bool bQuickSeek;
|
2015-08-22 14:16:27 +00:00
|
|
|
bool bAutoSeqSetting;
|
|
|
|
bool bAutoSeekSetting;
|
2017-09-26 23:34:13 +00:00
|
|
|
bool isBeginning;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2015-10-08 00:17:50 +00:00
|
|
|
SAMPLETYPE *pMidBuffer;
|
|
|
|
SAMPLETYPE *pMidBufferUnaligned;
|
|
|
|
|
|
|
|
FIFOSampleBuffer outputBuffer;
|
|
|
|
FIFOSampleBuffer inputBuffer;
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
void acceptNewOverlapLength(int newOverlapLength);
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
virtual void clearCrossCorrState();
|
2010-03-18 20:04:51 +00:00
|
|
|
void calculateOverlapLength(int overlapMs);
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2015-10-08 00:17:50 +00:00
|
|
|
virtual double calcCrossCorr(const SAMPLETYPE *mixingPos, const SAMPLETYPE *compare, double &norm);
|
|
|
|
virtual double calcCrossCorrAccumulate(const SAMPLETYPE *mixingPos, const SAMPLETYPE *compare, double &norm);
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
virtual int seekBestOverlapPositionFull(const SAMPLETYPE *refPos);
|
|
|
|
virtual int seekBestOverlapPositionQuick(const SAMPLETYPE *refPos);
|
2015-10-08 00:17:50 +00:00
|
|
|
virtual int seekBestOverlapPosition(const SAMPLETYPE *refPos);
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
virtual void overlapStereo(SAMPLETYPE *output, const SAMPLETYPE *input) const;
|
|
|
|
virtual void overlapMono(SAMPLETYPE *output, const SAMPLETYPE *input) const;
|
2015-08-22 14:16:27 +00:00
|
|
|
virtual void overlapMulti(SAMPLETYPE *output, const SAMPLETYPE *input) const;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
void clearMidBuffer();
|
|
|
|
void overlap(SAMPLETYPE *output, const SAMPLETYPE *input, uint ovlPos) const;
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
void calcSeqParameters();
|
2015-10-08 00:17:50 +00:00
|
|
|
void adaptNormalizer();
|
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
/// Changes the tempo of the given sound samples.
|
|
|
|
/// Returns amount of samples returned in the "output" buffer.
|
|
|
|
/// The maximum amount of samples that can be returned at a time is set by
|
|
|
|
/// the 'set_returnBuffer_size' function.
|
|
|
|
void processSamples();
|
2013-04-17 10:46:57 +00:00
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
public:
|
|
|
|
TDStretch();
|
|
|
|
virtual ~TDStretch();
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
/// Operator 'new' is overloaded so that it automatically creates a suitable instance
|
2009-02-15 05:15:39 +00:00
|
|
|
/// depending on if we've a MMX/SSE/etc-capable CPU available or not.
|
2010-03-18 20:04:51 +00:00
|
|
|
static void *operator new(size_t s);
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
/// Use this function instead of "new" operator to create a new instance of this class.
|
2009-02-15 05:15:39 +00:00
|
|
|
/// This function automatically chooses a correct feature set depending on if the CPU
|
|
|
|
/// supports MMX/SSE/etc extensions.
|
|
|
|
static TDStretch *newInstance();
|
2013-04-17 10:46:57 +00:00
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
/// Returns the output buffer object
|
|
|
|
FIFOSamplePipe *getOutput() { return &outputBuffer; };
|
|
|
|
|
|
|
|
/// Returns the input buffer object
|
|
|
|
FIFOSamplePipe *getInput() { return &inputBuffer; };
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
/// Sets new target tempo. Normal tempo = 'SCALE', smaller values represent slower
|
2009-02-15 05:15:39 +00:00
|
|
|
/// tempo, larger faster tempo.
|
2015-10-08 00:17:50 +00:00
|
|
|
void setTempo(double newTempo);
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
/// Returns nonzero if there aren't any samples available for outputting.
|
|
|
|
virtual void clear();
|
|
|
|
|
|
|
|
/// Clears the input buffer
|
|
|
|
void clearInput();
|
|
|
|
|
|
|
|
/// Sets the number of channels, 1 = mono, 2 = stereo
|
2010-03-18 20:04:51 +00:00
|
|
|
void setChannels(int numChannels);
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
/// Enables/disables the quick position seeking algorithm. Zero to disable,
|
2009-02-15 05:15:39 +00:00
|
|
|
/// nonzero to enable
|
2015-08-22 14:16:27 +00:00
|
|
|
void enableQuickSeek(bool enable);
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
/// Returns nonzero if the quick seeking algorithm is enabled.
|
2015-08-22 14:16:27 +00:00
|
|
|
bool isQuickSeekEnabled() const;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
/// Sets routine control parameters. These control are certain time constants
|
|
|
|
/// defining how the sound is stretched to the desired duration.
|
|
|
|
//
|
|
|
|
/// 'sampleRate' = sample rate of the sound
|
|
|
|
/// 'sequenceMS' = one processing sequence length in milliseconds
|
2013-04-17 10:46:57 +00:00
|
|
|
/// 'seekwindowMS' = seeking window length for scanning the best overlapping
|
2009-02-15 05:15:39 +00:00
|
|
|
/// position
|
|
|
|
/// 'overlapMS' = overlapping length
|
2010-03-18 20:04:51 +00:00
|
|
|
void setParameters(int sampleRate, ///< Samplerate of sound being processed (Hz)
|
|
|
|
int sequenceMS = -1, ///< Single processing sequence length (ms)
|
|
|
|
int seekwindowMS = -1, ///< Offset seeking window length (ms)
|
|
|
|
int overlapMS = -1 ///< Sequence overlapping length (ms)
|
2009-02-15 05:15:39 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/// Get routine control parameters, see setParameters() function.
|
|
|
|
/// Any of the parameters to this function can be NULL, in such case corresponding parameter
|
|
|
|
/// value isn't returned.
|
2010-03-18 20:04:51 +00:00
|
|
|
void getParameters(int *pSampleRate, int *pSequenceMs, int *pSeekWindowMs, int *pOverlapMs) const;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
/// Adds 'numsamples' pcs of samples from the 'samples' memory position into
|
|
|
|
/// the input of the object.
|
|
|
|
virtual void putSamples(
|
|
|
|
const SAMPLETYPE *samples, ///< Input sample data
|
|
|
|
uint numSamples ///< Number of samples in 'samples' so that one sample
|
|
|
|
///< contains both channels if stereo
|
|
|
|
);
|
2013-04-17 10:46:57 +00:00
|
|
|
|
|
|
|
/// return nominal input sample requirement for triggering a processing batch
|
|
|
|
int getInputSampleReq() const
|
|
|
|
{
|
|
|
|
return (int)(nominalSkip + 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// return nominal output sample amount when running a processing batch
|
|
|
|
int getOutputBatchSize() const
|
|
|
|
{
|
|
|
|
return seekWindowLength - overlapLength;
|
|
|
|
}
|
2017-09-26 23:34:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// return approximate initial input-output latency
|
|
|
|
int getLatency() const
|
|
|
|
{
|
|
|
|
return sampleReq;
|
|
|
|
}
|
2009-02-15 05:15:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Implementation-specific class declarations:
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
#ifdef SOUNDTOUCH_ALLOW_MMX
|
2010-03-18 20:04:51 +00:00
|
|
|
/// Class that implements MMX optimized routines for 16bit integer samples type.
|
|
|
|
class TDStretchMMX : public TDStretch
|
|
|
|
{
|
|
|
|
protected:
|
2015-10-08 00:17:50 +00:00
|
|
|
double calcCrossCorr(const short *mixingPos, const short *compare, double &norm);
|
|
|
|
double calcCrossCorrAccumulate(const short *mixingPos, const short *compare, double &norm);
|
2010-03-18 20:04:51 +00:00
|
|
|
virtual void overlapStereo(short *output, const short *input) const;
|
|
|
|
virtual void clearCrossCorrState();
|
|
|
|
};
|
2013-04-17 10:46:57 +00:00
|
|
|
#endif /// SOUNDTOUCH_ALLOW_MMX
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
#ifdef SOUNDTOUCH_ALLOW_SSE
|
2009-02-15 05:15:39 +00:00
|
|
|
/// Class that implements SSE optimized routines for floating point samples type.
|
|
|
|
class TDStretchSSE : public TDStretch
|
|
|
|
{
|
|
|
|
protected:
|
2015-10-08 00:17:50 +00:00
|
|
|
double calcCrossCorr(const float *mixingPos, const float *compare, double &norm);
|
|
|
|
double calcCrossCorrAccumulate(const float *mixingPos, const float *compare, double &norm);
|
2009-02-15 05:15:39 +00:00
|
|
|
};
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
#endif /// SOUNDTOUCH_ALLOW_SSE
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
#endif /// TDStretch_H
|