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
|
2009-02-15 05:15:39 +00:00
|
|
|
/// method with several performance-increasing tweaks.
|
|
|
|
///
|
2017-09-26 23:34:13 +00:00
|
|
|
/// Notes : MMX optimized functions reside in a separate, platform-specific
|
|
|
|
/// file, e.g. 'mmx_win.cpp' or 'mmx_gcc.cpp'.
|
|
|
|
///
|
|
|
|
/// This source file contains OpenMP optimizations that allow speeding up the
|
|
|
|
/// corss-correlation algorithm by executing it in several threads / CPU cores
|
|
|
|
/// in parallel. See the following article link for more detailed discussion
|
|
|
|
/// about SoundTouch OpenMP optimizations:
|
|
|
|
/// http://www.softwarecoven.com/parallel-computing-in-embedded-mobile-devices
|
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: 2017-04-07 22:01:22 +0300 (pe, 07 huhti 2017) $
|
2010-03-18 20:04:51 +00:00
|
|
|
// File revision : $Revision: 1.12 $
|
2009-02-15 05:15:39 +00:00
|
|
|
//
|
2017-09-26 23:34:13 +00:00
|
|
|
// $Id: TDStretch.cpp 249 2017-04-07 19:01:22Z 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
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <assert.h>
|
2010-03-18 20:04:51 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <float.h>
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
#include "STTypes.h"
|
|
|
|
#include "cpu_detect.h"
|
|
|
|
#include "TDStretch.h"
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
using namespace soundtouch;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
#define max(x, y) (((x) > (y)) ? (x) : (y))
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* Constant definitions
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
// Table for the hierarchical mixing position seeking algorithm
|
2015-10-08 00:17:50 +00:00
|
|
|
const short _scanOffsets[5][24]={
|
2010-03-18 20:04:51 +00:00
|
|
|
{ 124, 186, 248, 310, 372, 434, 496, 558, 620, 682, 744, 806,
|
|
|
|
868, 930, 992, 1054, 1116, 1178, 1240, 1302, 1364, 1426, 1488, 0},
|
2009-02-15 05:15:39 +00:00
|
|
|
{-100, -75, -50, -25, 25, 50, 75, 100, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
{ -20, -15, -10, -5, 5, 10, 15, 20, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
{ -4, -3, -2, -1, 1, 2, 3, 4, 0, 0, 0, 0,
|
2010-03-18 20:04:51 +00:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
{ 121, 114, 97, 114, 98, 105, 108, 32, 104, 99, 117, 111,
|
|
|
|
116, 100, 110, 117, 111, 115, 0, 0, 0, 0, 0, 0}};
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* Implementation of the class 'TDStretch'
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
TDStretch::TDStretch() : FIFOProcessor(&outputBuffer)
|
|
|
|
{
|
2015-08-22 14:16:27 +00:00
|
|
|
bQuickSeek = false;
|
2009-02-15 05:15:39 +00:00
|
|
|
channels = 2;
|
|
|
|
|
|
|
|
pMidBuffer = NULL;
|
2013-04-17 10:46:57 +00:00
|
|
|
pMidBufferUnaligned = NULL;
|
2009-02-15 05:15:39 +00:00
|
|
|
overlapLength = 0;
|
|
|
|
|
2015-08-22 14:16:27 +00:00
|
|
|
bAutoSeqSetting = true;
|
|
|
|
bAutoSeekSetting = true;
|
2010-03-18 20:04:51 +00:00
|
|
|
|
2015-10-08 00:17:50 +00:00
|
|
|
maxnorm = 0;
|
|
|
|
maxnormf = 1e8;
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
skipFract = 0;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
tempo = 1.0f;
|
2013-04-17 10:46:57 +00:00
|
|
|
setParameters(44100, DEFAULT_SEQUENCE_MS, DEFAULT_SEEKWINDOW_MS, DEFAULT_OVERLAP_MS);
|
2009-02-15 05:15:39 +00:00
|
|
|
setTempo(1.0f);
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
clear();
|
|
|
|
}
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TDStretch::~TDStretch()
|
|
|
|
{
|
2013-04-17 10:46:57 +00:00
|
|
|
delete[] pMidBufferUnaligned;
|
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 (default = 82 ms)
|
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 (default = 28 ms)
|
|
|
|
// 'overlapMS' = overlapping length (default = 12 ms)
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
void TDStretch::setParameters(int aSampleRate, int aSequenceMS,
|
2010-03-18 20:04:51 +00:00
|
|
|
int aSeekWindowMS, int aOverlapMS)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
// accept only positive parameter values - if zero or negative, use old values instead
|
|
|
|
if (aSampleRate > 0) this->sampleRate = aSampleRate;
|
|
|
|
if (aOverlapMS > 0) this->overlapMs = aOverlapMS;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
if (aSequenceMS > 0)
|
|
|
|
{
|
|
|
|
this->sequenceMs = aSequenceMS;
|
2015-08-22 14:16:27 +00:00
|
|
|
bAutoSeqSetting = false;
|
2013-04-17 10:46:57 +00:00
|
|
|
}
|
2010-03-18 20:04:51 +00:00
|
|
|
else if (aSequenceMS == 0)
|
|
|
|
{
|
|
|
|
// if zero, use automatic setting
|
2015-08-22 14:16:27 +00:00
|
|
|
bAutoSeqSetting = true;
|
2010-03-18 20:04:51 +00:00
|
|
|
}
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
if (aSeekWindowMS > 0)
|
2010-03-18 20:04:51 +00:00
|
|
|
{
|
|
|
|
this->seekWindowMs = aSeekWindowMS;
|
2015-08-22 14:16:27 +00:00
|
|
|
bAutoSeekSetting = false;
|
2013-04-17 10:46:57 +00:00
|
|
|
}
|
|
|
|
else if (aSeekWindowMS == 0)
|
2010-03-18 20:04:51 +00:00
|
|
|
{
|
|
|
|
// if zero, use automatic setting
|
2015-08-22 14:16:27 +00:00
|
|
|
bAutoSeekSetting = true;
|
2010-03-18 20:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
calcSeqParameters();
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
calculateOverlapLength(overlapMs);
|
|
|
|
|
|
|
|
// set tempo to recalculate 'sampleReq'
|
|
|
|
setTempo(tempo);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// 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 TDStretch::getParameters(int *pSampleRate, int *pSequenceMs, int *pSeekWindowMs, int *pOverlapMs) const
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
if (pSampleRate)
|
|
|
|
{
|
|
|
|
*pSampleRate = sampleRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pSequenceMs)
|
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
*pSequenceMs = (bAutoSeqSetting) ? (USE_AUTO_SEQUENCE_LEN) : sequenceMs;
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pSeekWindowMs)
|
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
*pSeekWindowMs = (bAutoSeekSetting) ? (USE_AUTO_SEEKWINDOW_LEN) : seekWindowMs;
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pOverlapMs)
|
|
|
|
{
|
|
|
|
*pOverlapMs = overlapMs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
// Overlaps samples in 'midBuffer' with the samples in 'pInput'
|
|
|
|
void TDStretch::overlapMono(SAMPLETYPE *pOutput, const SAMPLETYPE *pInput) const
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2013-04-17 10:46:57 +00:00
|
|
|
int i;
|
|
|
|
SAMPLETYPE m1, m2;
|
|
|
|
|
|
|
|
m1 = (SAMPLETYPE)0;
|
|
|
|
m2 = (SAMPLETYPE)overlapLength;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
for (i = 0; i < overlapLength ; i ++)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2013-04-17 10:46:57 +00:00
|
|
|
pOutput[i] = (pInput[i] * m1 + pMidBuffer[i] * m2 ) / overlapLength;
|
|
|
|
m1 += 1;
|
|
|
|
m2 -= 1;
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TDStretch::clearMidBuffer()
|
|
|
|
{
|
2015-08-22 14:16:27 +00:00
|
|
|
memset(pMidBuffer, 0, channels * sizeof(SAMPLETYPE) * overlapLength);
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TDStretch::clearInput()
|
|
|
|
{
|
|
|
|
inputBuffer.clear();
|
|
|
|
clearMidBuffer();
|
2017-09-26 23:34:13 +00:00
|
|
|
isBeginning = true;
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Clears the sample buffers
|
|
|
|
void TDStretch::clear()
|
|
|
|
{
|
|
|
|
outputBuffer.clear();
|
2010-03-18 20:04:51 +00:00
|
|
|
clearInput();
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Enables/disables the quick position seeking algorithm. Zero to disable, nonzero
|
|
|
|
// to enable
|
2015-08-22 14:16:27 +00:00
|
|
|
void TDStretch::enableQuickSeek(bool enable)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
bQuickSeek = 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 TDStretch::isQuickSeekEnabled() const
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
return bQuickSeek;
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Seeks for the optimal overlap-mixing position.
|
2010-03-18 20:04:51 +00:00
|
|
|
int TDStretch::seekBestOverlapPosition(const SAMPLETYPE *refPos)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2013-04-17 10:46:57 +00:00
|
|
|
if (bQuickSeek)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2013-04-17 10:46:57 +00:00
|
|
|
return seekBestOverlapPositionQuick(refPos);
|
2015-10-08 00:17:50 +00:00
|
|
|
}
|
2013-04-17 10:46:57 +00:00
|
|
|
else
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2013-04-17 10:46:57 +00:00
|
|
|
return seekBestOverlapPositionFull(refPos);
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
// Overlaps samples in 'midBuffer' with the samples in 'pInputBuffer' at position
|
2009-02-15 05:15:39 +00:00
|
|
|
// of 'ovlPos'.
|
2010-03-18 20:04:51 +00:00
|
|
|
inline void TDStretch::overlap(SAMPLETYPE *pOutput, const SAMPLETYPE *pInput, uint ovlPos) const
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2015-08-22 14:16:27 +00:00
|
|
|
#ifndef USE_MULTICH_ALWAYS
|
|
|
|
if (channels == 1)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
// mono sound.
|
2010-03-18 20:04:51 +00:00
|
|
|
overlapMono(pOutput, pInput + ovlPos);
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
2015-08-22 14:16:27 +00:00
|
|
|
else if (channels == 2)
|
|
|
|
{
|
|
|
|
// stereo sound
|
|
|
|
overlapStereo(pOutput, pInput + 2 * ovlPos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif // USE_MULTICH_ALWAYS
|
|
|
|
{
|
|
|
|
assert(channels > 0);
|
|
|
|
overlapMulti(pOutput, pInput + channels * ovlPos);
|
|
|
|
}
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Seeks for the optimal overlap-mixing position. The 'stereo' version of the
|
|
|
|
// routine
|
|
|
|
//
|
|
|
|
// The best position is determined as the position where the two overlapped
|
|
|
|
// sample sequences are 'most alike', in terms of the highest cross-correlation
|
|
|
|
// value over the overlapping period
|
2013-04-17 10:46:57 +00:00
|
|
|
int TDStretch::seekBestOverlapPositionFull(const SAMPLETYPE *refPos)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
int bestOffs;
|
2015-08-22 14:16:27 +00:00
|
|
|
double bestCorr;
|
2010-03-18 20:04:51 +00:00
|
|
|
int i;
|
2015-08-22 14:16:27 +00:00
|
|
|
double norm;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2017-09-26 23:34:13 +00:00
|
|
|
bestCorr = -FLT_MAX;
|
2009-02-15 05:15:39 +00:00
|
|
|
bestOffs = 0;
|
|
|
|
|
|
|
|
// Scans for the best correlation value by testing each possible position
|
|
|
|
// over the permitted range.
|
2015-08-22 14:16:27 +00:00
|
|
|
bestCorr = calcCrossCorr(refPos, pMidBuffer, norm);
|
2017-09-26 23:34:13 +00:00
|
|
|
bestCorr = (bestCorr + 0.1) * 0.75;
|
2015-08-22 14:16:27 +00:00
|
|
|
|
|
|
|
#pragma omp parallel for
|
|
|
|
for (i = 1; i < seekLength; i ++)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2015-08-22 14:16:27 +00:00
|
|
|
double corr;
|
|
|
|
// Calculates correlation value for the mixing position corresponding to 'i'
|
|
|
|
#ifdef _OPENMP
|
|
|
|
// in parallel OpenMP mode, can't use norm accumulator version as parallel executor won't
|
|
|
|
// iterate the loop in sequential order
|
|
|
|
corr = calcCrossCorr(refPos + channels * i, pMidBuffer, norm);
|
|
|
|
#else
|
|
|
|
// In non-parallel version call "calcCrossCorrAccumulate" that is otherwise same
|
|
|
|
// as "calcCrossCorr", but saves time by reusing & updating previously stored
|
|
|
|
// "norm" value
|
|
|
|
corr = calcCrossCorrAccumulate(refPos + channels * i, pMidBuffer, norm);
|
|
|
|
#endif
|
2010-03-18 20:04:51 +00:00
|
|
|
// heuristic rule to slightly favour values close to mid of the range
|
|
|
|
double tmp = (double)(2 * i - seekLength) / (double)seekLength;
|
|
|
|
corr = ((corr + 0.1) * (1.0 - 0.25 * tmp * tmp));
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
// Checks for the highest correlation value
|
2013-04-17 10:46:57 +00:00
|
|
|
if (corr > bestCorr)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2015-08-22 14:16:27 +00:00
|
|
|
// For optimal performance, enter critical section only in case that best value found.
|
|
|
|
// in such case repeat 'if' condition as it's possible that parallel execution may have
|
|
|
|
// updated the bestCorr value in the mean time
|
|
|
|
#pragma omp critical
|
|
|
|
if (corr > bestCorr)
|
|
|
|
{
|
|
|
|
bestCorr = corr;
|
|
|
|
bestOffs = i;
|
|
|
|
}
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-08 00:17:50 +00:00
|
|
|
|
|
|
|
#ifdef SOUNDTOUCH_INTEGER_SAMPLES
|
|
|
|
adaptNormalizer();
|
|
|
|
#endif
|
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
// clear cross correlation routine state if necessary (is so e.g. in MMX routines).
|
|
|
|
clearCrossCorrState();
|
|
|
|
|
|
|
|
return bestOffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-08 00:17:50 +00:00
|
|
|
// Quick seek algorithm for improved runtime-performance: First roughly scans through the
|
|
|
|
// correlation area, and then scan surroundings of two best preliminary correlation candidates
|
|
|
|
// with improved precision
|
2009-02-15 05:15:39 +00:00
|
|
|
//
|
2015-10-08 00:17:50 +00:00
|
|
|
// Based on testing:
|
|
|
|
// - This algorithm gives on average 99% as good match as the full algorith
|
|
|
|
// - this quick seek algorithm finds the best match on ~90% of cases
|
|
|
|
// - on those 10% of cases when this algorithm doesn't find best match,
|
|
|
|
// it still finds on average ~90% match vs. the best possible match
|
|
|
|
int TDStretch::seekBestOverlapPositionQuick(const SAMPLETYPE *refPos)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2015-10-08 00:17:50 +00:00
|
|
|
#define _MIN(a, b) (((a) < (b)) ? (a) : (b))
|
|
|
|
#define SCANSTEP 16
|
|
|
|
#define SCANWIND 8
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
int bestOffs;
|
2015-10-08 00:17:50 +00:00
|
|
|
int i;
|
|
|
|
int bestOffs2;
|
|
|
|
float bestCorr, corr;
|
|
|
|
float bestCorr2;
|
|
|
|
double norm;
|
|
|
|
|
|
|
|
// note: 'float' types used in this function in case that the platform would need to use software-fp
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2017-09-26 23:34:13 +00:00
|
|
|
bestCorr =
|
|
|
|
bestCorr2 = -FLT_MAX;
|
|
|
|
bestOffs =
|
|
|
|
bestOffs2 = SCANWIND;
|
2015-10-08 00:17:50 +00:00
|
|
|
|
|
|
|
// Scans for the best correlation value by testing each possible position
|
|
|
|
// over the permitted range. Look for two best matches on the first pass to
|
|
|
|
// increase possibility of ideal match.
|
|
|
|
//
|
|
|
|
// Begin from "SCANSTEP" instead of SCANWIND to make the calculation
|
|
|
|
// catch the 'middlepoint' of seekLength vector as that's the a-priori
|
|
|
|
// expected best match position
|
2009-02-15 05:15:39 +00:00
|
|
|
//
|
2015-10-08 00:17:50 +00:00
|
|
|
// Roughly:
|
|
|
|
// - 15% of cases find best result directly on the first round,
|
|
|
|
// - 75% cases find better match on 2nd round around the best match from 1st round
|
|
|
|
// - 10% cases find better match on 2nd round around the 2nd-best-match from 1st round
|
|
|
|
for (i = SCANSTEP; i < seekLength - SCANWIND - 1; i += SCANSTEP)
|
|
|
|
{
|
|
|
|
// Calculates correlation value for the mixing position corresponding
|
|
|
|
// to 'i'
|
|
|
|
corr = (float)calcCrossCorr(refPos + channels*i, pMidBuffer, norm);
|
|
|
|
// heuristic rule to slightly favour values close to mid of the seek range
|
|
|
|
float tmp = (float)(2 * i - seekLength - 1) / (float)seekLength;
|
|
|
|
corr = ((corr + 0.1f) * (1.0f - 0.25f * tmp * tmp));
|
|
|
|
|
|
|
|
// Checks for the highest correlation value
|
|
|
|
if (corr > bestCorr)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2015-10-08 00:17:50 +00:00
|
|
|
// found new best match. keep the previous best as 2nd best match
|
|
|
|
bestCorr2 = bestCorr;
|
|
|
|
bestOffs2 = bestOffs;
|
|
|
|
bestCorr = corr;
|
|
|
|
bestOffs = i;
|
|
|
|
}
|
|
|
|
else if (corr > bestCorr2)
|
|
|
|
{
|
|
|
|
// not new best, but still new 2nd best match
|
|
|
|
bestCorr2 = corr;
|
|
|
|
bestOffs2 = i;
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-08 00:17:50 +00:00
|
|
|
|
|
|
|
// Scans surroundings of the found best match with small stepping
|
|
|
|
int end = _MIN(bestOffs + SCANWIND + 1, seekLength);
|
|
|
|
for (i = bestOffs - SCANWIND; i < end; i++)
|
|
|
|
{
|
|
|
|
if (i == bestOffs) continue; // this offset already calculated, thus skip
|
|
|
|
|
|
|
|
// Calculates correlation value for the mixing position corresponding
|
|
|
|
// to 'i'
|
|
|
|
corr = (float)calcCrossCorr(refPos + channels*i, pMidBuffer, norm);
|
|
|
|
// heuristic rule to slightly favour values close to mid of the range
|
|
|
|
float tmp = (float)(2 * i - seekLength - 1) / (float)seekLength;
|
|
|
|
corr = ((corr + 0.1f) * (1.0f - 0.25f * tmp * tmp));
|
|
|
|
|
|
|
|
// Checks for the highest correlation value
|
|
|
|
if (corr > bestCorr)
|
|
|
|
{
|
|
|
|
bestCorr = corr;
|
|
|
|
bestOffs = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scans surroundings of the 2nd best match with small stepping
|
|
|
|
end = _MIN(bestOffs2 + SCANWIND + 1, seekLength);
|
|
|
|
for (i = bestOffs2 - SCANWIND; i < end; i++)
|
|
|
|
{
|
|
|
|
if (i == bestOffs2) continue; // this offset already calculated, thus skip
|
|
|
|
|
|
|
|
// Calculates correlation value for the mixing position corresponding
|
|
|
|
// to 'i'
|
|
|
|
corr = (float)calcCrossCorr(refPos + channels*i, pMidBuffer, norm);
|
|
|
|
// heuristic rule to slightly favour values close to mid of the range
|
|
|
|
float tmp = (float)(2 * i - seekLength - 1) / (float)seekLength;
|
|
|
|
corr = ((corr + 0.1f) * (1.0f - 0.25f * tmp * tmp));
|
|
|
|
|
|
|
|
// Checks for the highest correlation value
|
|
|
|
if (corr > bestCorr)
|
|
|
|
{
|
|
|
|
bestCorr = corr;
|
|
|
|
bestOffs = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
// clear cross correlation routine state if necessary (is so e.g. in MMX routines).
|
|
|
|
clearCrossCorrState();
|
|
|
|
|
2015-10-08 00:17:50 +00:00
|
|
|
#ifdef SOUNDTOUCH_INTEGER_SAMPLES
|
|
|
|
adaptNormalizer();
|
|
|
|
#endif
|
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
return bestOffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-10-08 00:17:50 +00:00
|
|
|
|
|
|
|
/// For integer algorithm: adapt normalization factor divider with music so that
|
|
|
|
/// it'll not be pessimistically restrictive that can degrade quality on quieter sections
|
|
|
|
/// yet won't cause integer overflows either
|
|
|
|
void TDStretch::adaptNormalizer()
|
|
|
|
{
|
|
|
|
// Do not adapt normalizer over too silent sequences to avoid averaging filter depleting to
|
|
|
|
// too low values during pauses in music
|
|
|
|
if ((maxnorm > 1000) || (maxnormf > 40000000))
|
|
|
|
{
|
|
|
|
//norm averaging filter
|
|
|
|
maxnormf = 0.9f * maxnormf + 0.1f * (float)maxnorm;
|
|
|
|
|
|
|
|
if ((maxnorm > 800000000) && (overlapDividerBitsNorm < 16))
|
|
|
|
{
|
|
|
|
// large values, so increase divider
|
|
|
|
overlapDividerBitsNorm++;
|
|
|
|
if (maxnorm > 1600000000) overlapDividerBitsNorm++; // extra large value => extra increase
|
|
|
|
}
|
|
|
|
else if ((maxnormf < 1000000) && (overlapDividerBitsNorm > 0))
|
|
|
|
{
|
|
|
|
// extra small values, decrease divider
|
|
|
|
overlapDividerBitsNorm--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
maxnorm = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
/// clear cross correlation routine state if necessary
|
2009-02-15 05:15:39 +00:00
|
|
|
void TDStretch::clearCrossCorrState()
|
|
|
|
{
|
|
|
|
// default implementation is empty.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
/// Calculates processing sequence length according to tempo setting
|
|
|
|
void TDStretch::calcSeqParameters()
|
|
|
|
{
|
|
|
|
// Adjust tempo param according to tempo, so that variating processing sequence length is used
|
|
|
|
// at varius tempo settings, between the given low...top limits
|
|
|
|
#define AUTOSEQ_TEMPO_LOW 0.5 // auto setting low tempo range (-50%)
|
|
|
|
#define AUTOSEQ_TEMPO_TOP 2.0 // auto setting top tempo range (+100%)
|
|
|
|
|
|
|
|
// sequence-ms setting values at above low & top tempo
|
2017-09-26 23:34:13 +00:00
|
|
|
#define AUTOSEQ_AT_MIN 90.0
|
|
|
|
#define AUTOSEQ_AT_MAX 40.0
|
2010-03-18 20:04:51 +00:00
|
|
|
#define AUTOSEQ_K ((AUTOSEQ_AT_MAX - AUTOSEQ_AT_MIN) / (AUTOSEQ_TEMPO_TOP - AUTOSEQ_TEMPO_LOW))
|
|
|
|
#define AUTOSEQ_C (AUTOSEQ_AT_MIN - (AUTOSEQ_K) * (AUTOSEQ_TEMPO_LOW))
|
|
|
|
|
2015-10-08 00:17:50 +00:00
|
|
|
// seek-window-ms setting values at above low & top tempoq
|
2017-09-26 23:34:13 +00:00
|
|
|
#define AUTOSEEK_AT_MIN 20.0
|
2010-03-18 20:04:51 +00:00
|
|
|
#define AUTOSEEK_AT_MAX 15.0
|
|
|
|
#define AUTOSEEK_K ((AUTOSEEK_AT_MAX - AUTOSEEK_AT_MIN) / (AUTOSEQ_TEMPO_TOP - AUTOSEQ_TEMPO_LOW))
|
|
|
|
#define AUTOSEEK_C (AUTOSEEK_AT_MIN - (AUTOSEEK_K) * (AUTOSEQ_TEMPO_LOW))
|
|
|
|
|
|
|
|
#define CHECK_LIMITS(x, mi, ma) (((x) < (mi)) ? (mi) : (((x) > (ma)) ? (ma) : (x)))
|
|
|
|
|
|
|
|
double seq, seek;
|
2013-04-17 10:46:57 +00:00
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
if (bAutoSeqSetting)
|
|
|
|
{
|
|
|
|
seq = AUTOSEQ_C + AUTOSEQ_K * tempo;
|
|
|
|
seq = CHECK_LIMITS(seq, AUTOSEQ_AT_MAX, AUTOSEQ_AT_MIN);
|
|
|
|
sequenceMs = (int)(seq + 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bAutoSeekSetting)
|
|
|
|
{
|
|
|
|
seek = AUTOSEEK_C + AUTOSEEK_K * tempo;
|
|
|
|
seek = CHECK_LIMITS(seek, AUTOSEEK_AT_MAX, AUTOSEEK_AT_MIN);
|
|
|
|
seekWindowMs = (int)(seek + 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update seek window lengths
|
|
|
|
seekWindowLength = (sampleRate * sequenceMs) / 1000;
|
2013-04-17 10:46:57 +00:00
|
|
|
if (seekWindowLength < 2 * overlapLength)
|
2010-03-18 20:04:51 +00:00
|
|
|
{
|
|
|
|
seekWindowLength = 2 * overlapLength;
|
|
|
|
}
|
|
|
|
seekLength = (sampleRate * seekWindowMs) / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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 TDStretch::setTempo(double newTempo)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
int intskip;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
tempo = newTempo;
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
// Calculate new sequence duration
|
|
|
|
calcSeqParameters();
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
// Calculate ideal skip length (according to tempo value)
|
2009-02-15 05:15:39 +00:00
|
|
|
nominalSkip = tempo * (seekWindowLength - overlapLength);
|
2015-10-08 00:17:50 +00:00
|
|
|
intskip = (int)(nominalSkip + 0.5);
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
// Calculate how many samples are needed in the 'inputBuffer' to
|
2009-02-15 05:15:39 +00:00
|
|
|
// process another batch of samples
|
2010-03-18 20:04:51 +00:00
|
|
|
//sampleReq = max(intskip + overlapLength, seekWindowLength) + seekLength / 2;
|
|
|
|
sampleReq = max(intskip + overlapLength, seekWindowLength) + seekLength;
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Sets the number of channels, 1 = mono, 2 = stereo
|
2010-03-18 20:04:51 +00:00
|
|
|
void TDStretch::setChannels(int numChannels)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
assert(numChannels > 0);
|
2009-02-15 05:15:39 +00:00
|
|
|
if (channels == numChannels) return;
|
2015-08-22 14:16:27 +00:00
|
|
|
// assert(numChannels == 1 || numChannels == 2);
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
channels = numChannels;
|
|
|
|
inputBuffer.setChannels(channels);
|
|
|
|
outputBuffer.setChannels(channels);
|
2015-08-22 14:16:27 +00:00
|
|
|
|
|
|
|
// re-init overlap/buffer
|
|
|
|
overlapLength=0;
|
|
|
|
setParameters(sampleRate);
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// nominal tempo, no need for processing, just pass the samples through
|
|
|
|
// to outputBuffer
|
2010-03-18 20:04:51 +00:00
|
|
|
/*
|
2009-02-15 05:15:39 +00:00
|
|
|
void TDStretch::processNominalTempo()
|
|
|
|
{
|
|
|
|
assert(tempo == 1.0f);
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
if (bMidBufferDirty)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
// If there are samples in pMidBuffer waiting for overlapping,
|
2013-04-17 10:46:57 +00:00
|
|
|
// do a single sliding overlapping with them in order to prevent a
|
2009-02-15 05:15:39 +00:00
|
|
|
// clicking distortion in the output sound
|
2013-04-17 10:46:57 +00:00
|
|
|
if (inputBuffer.numSamples() < overlapLength)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
// wait until we've got overlapLength input samples
|
|
|
|
return;
|
|
|
|
}
|
2013-04-17 10:46:57 +00:00
|
|
|
// Mix the samples in the beginning of 'inputBuffer' with the
|
|
|
|
// samples in 'midBuffer' using sliding overlapping
|
2009-02-15 05:15:39 +00:00
|
|
|
overlap(outputBuffer.ptrEnd(overlapLength), inputBuffer.ptrBegin(), 0);
|
|
|
|
outputBuffer.putSamples(overlapLength);
|
|
|
|
inputBuffer.receiveSamples(overlapLength);
|
|
|
|
clearMidBuffer();
|
|
|
|
// now we've caught the nominal sample flow and may switch to
|
|
|
|
// bypass mode
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simply bypass samples from input to output
|
|
|
|
outputBuffer.moveSamples(inputBuffer);
|
|
|
|
}
|
2010-03-18 20:04:51 +00:00
|
|
|
*/
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Processes as many processing frames of the samples 'inputBuffer', store
|
|
|
|
// the result into 'outputBuffer'
|
|
|
|
void TDStretch::processSamples()
|
|
|
|
{
|
2017-09-26 23:34:13 +00:00
|
|
|
int ovlSkip;
|
|
|
|
int offset = 0;
|
2009-02-15 05:15:39 +00:00
|
|
|
int temp;
|
|
|
|
|
|
|
|
/* Removed this small optimization - can introduce a click to sound when tempo setting
|
|
|
|
crosses the nominal value
|
2013-04-17 10:46:57 +00:00
|
|
|
if (tempo == 1.0f)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
// tempo not changed from the original, so bypass the processing
|
|
|
|
processNominalTempo();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Process samples as long as there are enough samples in 'inputBuffer'
|
|
|
|
// to form a processing frame.
|
2013-04-17 10:46:57 +00:00
|
|
|
while ((int)inputBuffer.numSamples() >= sampleReq)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2017-09-26 23:34:13 +00:00
|
|
|
if (isBeginning == false)
|
|
|
|
{
|
|
|
|
// apart from the very beginning of the track,
|
|
|
|
// scan for the best overlapping position & do overlap-add
|
|
|
|
offset = seekBestOverlapPosition(inputBuffer.ptrBegin());
|
|
|
|
|
|
|
|
// Mix the samples in the 'inputBuffer' at position of 'offset' with the
|
|
|
|
// samples in 'midBuffer' using sliding overlapping
|
|
|
|
// ... first partially overlap with the end of the previous sequence
|
|
|
|
// (that's in 'midBuffer')
|
|
|
|
overlap(outputBuffer.ptrEnd((uint)overlapLength), inputBuffer.ptrBegin(), (uint)offset);
|
|
|
|
outputBuffer.putSamples((uint)overlapLength);
|
|
|
|
offset += overlapLength;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Adjust processing offset at beginning of track by not perform initial overlapping
|
|
|
|
// and compensating that in the 'input buffer skip' calculation
|
|
|
|
isBeginning = false;
|
|
|
|
int skip = (int)(tempo * overlapLength + 0.5);
|
|
|
|
|
|
|
|
#ifdef SOUNDTOUCH_ALLOW_NONEXACT_SIMD_OPTIMIZATION
|
|
|
|
#ifdef SOUNDTOUCH_ALLOW_SSE
|
|
|
|
// if SSE mode, round the skip amount to value corresponding to aligned memory address
|
|
|
|
if (channels == 1)
|
|
|
|
{
|
|
|
|
skip &= -4;
|
|
|
|
}
|
|
|
|
else if (channels == 2)
|
|
|
|
{
|
|
|
|
skip &= -2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
skipFract -= skip;
|
|
|
|
assert(nominalSkip >= -skipFract);
|
|
|
|
}
|
2010-03-18 20:04:51 +00:00
|
|
|
|
|
|
|
// ... then copy sequence samples from 'inputBuffer' to output:
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
// crosscheck that we don't have buffer overflow...
|
2017-09-26 23:34:13 +00:00
|
|
|
if ((int)inputBuffer.numSamples() < (offset + seekWindowLength - overlapLength))
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
continue; // just in case, shouldn't really happen
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 23:34:13 +00:00
|
|
|
// length of sequence
|
|
|
|
temp = (seekWindowLength - 2 * overlapLength);
|
|
|
|
outputBuffer.putSamples(inputBuffer.ptrBegin() + channels * offset, (uint)temp);
|
2010-03-18 20:04:51 +00:00
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
// Copies the end of the current sequence from 'inputBuffer' to
|
|
|
|
// 'midBuffer' for being mixed with the beginning of the next
|
2009-02-15 05:15:39 +00:00
|
|
|
// processing sequence and so on
|
2017-09-26 23:34:13 +00:00
|
|
|
assert((offset + temp + overlapLength) <= (int)inputBuffer.numSamples());
|
|
|
|
memcpy(pMidBuffer, inputBuffer.ptrBegin() + channels * (offset + temp),
|
2009-02-15 05:15:39 +00:00
|
|
|
channels * sizeof(SAMPLETYPE) * overlapLength);
|
|
|
|
|
|
|
|
// Remove the processed samples from the input buffer. Update
|
|
|
|
// the difference between integer & nominal skip step to 'skipFract'
|
|
|
|
// in order to prevent the error from accumulating over time.
|
|
|
|
skipFract += nominalSkip; // real skip size
|
|
|
|
ovlSkip = (int)skipFract; // rounded to integer skip
|
|
|
|
skipFract -= ovlSkip; // maintain the fraction part, i.e. real vs. integer skip
|
2010-03-18 20:04:51 +00:00
|
|
|
inputBuffer.receiveSamples((uint)ovlSkip);
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Adds 'numsamples' pcs of samples from the 'samples' memory position into
|
|
|
|
// the input of the object.
|
2010-03-18 20:04:51 +00:00
|
|
|
void TDStretch::putSamples(const SAMPLETYPE *samples, uint nSamples)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
// Add the samples into the input buffer
|
2010-03-18 20:04:51 +00:00
|
|
|
inputBuffer.putSamples(samples, nSamples);
|
2009-02-15 05:15:39 +00:00
|
|
|
// Process the samples in input buffer
|
|
|
|
processSamples();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Set new overlap length parameter & reallocate RefMidBuffer if necessary.
|
2010-03-18 20:04:51 +00:00
|
|
|
void TDStretch::acceptNewOverlapLength(int newOverlapLength)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
int prevOvl;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
assert(newOverlapLength >= 0);
|
2009-02-15 05:15:39 +00:00
|
|
|
prevOvl = overlapLength;
|
|
|
|
overlapLength = newOverlapLength;
|
|
|
|
|
|
|
|
if (overlapLength > prevOvl)
|
|
|
|
{
|
2013-04-17 10:46:57 +00:00
|
|
|
delete[] pMidBufferUnaligned;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2015-08-22 14:16:27 +00:00
|
|
|
pMidBufferUnaligned = new SAMPLETYPE[overlapLength * channels + 16 / sizeof(SAMPLETYPE)];
|
2013-04-17 10:46:57 +00:00
|
|
|
// ensure that 'pMidBuffer' is aligned to 16 byte boundary for efficiency
|
|
|
|
pMidBuffer = (SAMPLETYPE *)SOUNDTOUCH_ALIGN_POINTER_16(pMidBufferUnaligned);
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
clearMidBuffer();
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
void * TDStretch::operator new(size_t s)
|
|
|
|
{
|
|
|
|
// Notice! don't use "new TDStretch" directly, use "newInstance" to create a new instance instead!
|
2013-04-17 10:46:57 +00:00
|
|
|
ST_THROW_RT_ERROR("Error in TDStretch::new: Don't use 'new TDStretch' directly, use 'newInstance' member instead!");
|
|
|
|
return newInstance();
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TDStretch * TDStretch::newInstance()
|
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
uint uExtensions;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
uExtensions = detectCPUextensions();
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
// Check if MMX/SSE instruction set extensions supported by CPU
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
#ifdef SOUNDTOUCH_ALLOW_MMX
|
2009-02-15 05:15:39 +00:00
|
|
|
// MMX routines available only with integer sample types
|
|
|
|
if (uExtensions & SUPPORT_MMX)
|
|
|
|
{
|
|
|
|
return ::new TDStretchMMX;
|
|
|
|
}
|
|
|
|
else
|
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
|
|
|
if (uExtensions & SUPPORT_SSE)
|
|
|
|
{
|
|
|
|
// SSE support
|
|
|
|
return ::new TDStretchSSE;
|
|
|
|
}
|
|
|
|
else
|
2013-04-17 10:46:57 +00:00
|
|
|
#endif // SOUNDTOUCH_ALLOW_SSE
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// ISA optimizations not supported, use plain C version
|
|
|
|
return ::new TDStretch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Integer arithmetics specific algorithm implementations.
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
#ifdef SOUNDTOUCH_INTEGER_SAMPLES
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
// Overlaps samples in 'midBuffer' with the samples in 'input'. The 'Stereo'
|
2009-02-15 05:15:39 +00:00
|
|
|
// version of the routine.
|
2010-03-18 20:04:51 +00:00
|
|
|
void TDStretch::overlapStereo(short *poutput, const short *input) const
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
short temp;
|
2010-03-18 20:04:51 +00:00
|
|
|
int cnt2;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
for (i = 0; i < overlapLength ; i ++)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
temp = (short)(overlapLength - i);
|
|
|
|
cnt2 = 2 * i;
|
2010-03-18 20:04:51 +00:00
|
|
|
poutput[cnt2] = (input[cnt2] * i + pMidBuffer[cnt2] * temp ) / overlapLength;
|
|
|
|
poutput[cnt2 + 1] = (input[cnt2 + 1] * i + pMidBuffer[cnt2 + 1] * temp ) / overlapLength;
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-22 14:16:27 +00:00
|
|
|
|
|
|
|
// Overlaps samples in 'midBuffer' with the samples in 'input'. The 'Multi'
|
|
|
|
// version of the routine.
|
|
|
|
void TDStretch::overlapMulti(SAMPLETYPE *poutput, const SAMPLETYPE *input) const
|
|
|
|
{
|
|
|
|
SAMPLETYPE m1=(SAMPLETYPE)0;
|
|
|
|
SAMPLETYPE m2;
|
|
|
|
int i=0;
|
|
|
|
|
|
|
|
for (m2 = (SAMPLETYPE)overlapLength; m2; m2 --)
|
|
|
|
{
|
|
|
|
for (int c = 0; c < channels; c ++)
|
|
|
|
{
|
|
|
|
poutput[i] = (input[i] * m1 + pMidBuffer[i] * m2) / overlapLength;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
m1++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
// Calculates the x having the closest 2^x value for the given value
|
|
|
|
static int _getClosest2Power(double value)
|
|
|
|
{
|
|
|
|
return (int)(log(value) / log(2.0) + 0.5);
|
|
|
|
}
|
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
/// Calculates overlap period length in samples.
|
|
|
|
/// Integer version rounds overlap length to closest power of 2
|
|
|
|
/// for a divide scaling operation.
|
2010-03-18 20:04:51 +00:00
|
|
|
void TDStretch::calculateOverlapLength(int aoverlapMs)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
int newOvl;
|
|
|
|
|
|
|
|
assert(aoverlapMs >= 0);
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
// calculate overlap length so that it's power of 2 - thus it's easy to do
|
2013-04-17 10:46:57 +00:00
|
|
|
// integer division by right-shifting. Term "-1" at end is to account for
|
|
|
|
// the extra most significatnt bit left unused in result by signed multiplication
|
2015-10-08 00:17:50 +00:00
|
|
|
overlapDividerBitsPure = _getClosest2Power((sampleRate * aoverlapMs) / 1000.0) - 1;
|
|
|
|
if (overlapDividerBitsPure > 9) overlapDividerBitsPure = 9;
|
|
|
|
if (overlapDividerBitsPure < 3) overlapDividerBitsPure = 3;
|
|
|
|
newOvl = (int)pow(2.0, (int)overlapDividerBitsPure + 1); // +1 => account for -1 above
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
acceptNewOverlapLength(newOvl);
|
|
|
|
|
2015-10-08 00:17:50 +00:00
|
|
|
overlapDividerBitsNorm = overlapDividerBitsPure;
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
// calculate sloping divider so that crosscorrelation operation won't
|
|
|
|
// overflow 32-bit register. Max. sum of the crosscorrelation sum without
|
2009-02-15 05:15:39 +00:00
|
|
|
// divider would be 2^30*(N^3-N)/3, where N = overlap length
|
|
|
|
slopingDivider = (newOvl * newOvl - 1) / 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-08 00:17:50 +00:00
|
|
|
double TDStretch::calcCrossCorr(const short *mixingPos, const short *compare, double &norm)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
long corr;
|
2015-10-08 00:17:50 +00:00
|
|
|
unsigned long lnorm;
|
2010-03-18 20:04:51 +00:00
|
|
|
int i;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2015-08-22 14:16:27 +00:00
|
|
|
corr = lnorm = 0;
|
2013-04-17 10:46:57 +00:00
|
|
|
// Same routine for stereo and mono. For stereo, unroll loop for better
|
|
|
|
// efficiency and gives slightly better resolution against rounding.
|
|
|
|
// For mono it same routine, just unrolls loop by factor of 4
|
|
|
|
for (i = 0; i < channels * overlapLength; i += 4)
|
|
|
|
{
|
|
|
|
corr += (mixingPos[i] * compare[i] +
|
2015-10-08 00:17:50 +00:00
|
|
|
mixingPos[i + 1] * compare[i + 1]) >> overlapDividerBitsNorm; // notice: do intermediate division here to avoid integer overflow
|
2015-08-22 14:16:27 +00:00
|
|
|
corr += (mixingPos[i + 2] * compare[i + 2] +
|
2015-10-08 00:17:50 +00:00
|
|
|
mixingPos[i + 3] * compare[i + 3]) >> overlapDividerBitsNorm;
|
2015-08-22 14:16:27 +00:00
|
|
|
lnorm += (mixingPos[i] * mixingPos[i] +
|
2015-10-08 00:17:50 +00:00
|
|
|
mixingPos[i + 1] * mixingPos[i + 1]) >> overlapDividerBitsNorm; // notice: do intermediate division here to avoid integer overflow
|
2015-08-22 14:16:27 +00:00
|
|
|
lnorm += (mixingPos[i + 2] * mixingPos[i + 2] +
|
2015-10-08 00:17:50 +00:00
|
|
|
mixingPos[i + 3] * mixingPos[i + 3]) >> overlapDividerBitsNorm;
|
2013-04-17 10:46:57 +00:00
|
|
|
}
|
|
|
|
|
2015-10-08 00:17:50 +00:00
|
|
|
if (lnorm > maxnorm)
|
|
|
|
{
|
2017-09-26 23:34:13 +00:00
|
|
|
// modify 'maxnorm' inside critical section to avoid multi-access conflict if in OpenMP mode
|
|
|
|
#pragma omp critical
|
|
|
|
if (lnorm > maxnorm)
|
|
|
|
{
|
|
|
|
maxnorm = lnorm;
|
|
|
|
}
|
2015-10-08 00:17:50 +00:00
|
|
|
}
|
2013-04-17 10:46:57 +00:00
|
|
|
// Normalize result by dividing by sqrt(norm) - this step is easiest
|
2010-03-18 20:04:51 +00:00
|
|
|
// done using floating point operation
|
2015-08-22 14:16:27 +00:00
|
|
|
norm = (double)lnorm;
|
|
|
|
return (double)corr / sqrt((norm < 1e-9) ? 1.0 : norm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Update cross-correlation by accumulating "norm" coefficient by previously calculated value
|
2015-10-08 00:17:50 +00:00
|
|
|
double TDStretch::calcCrossCorrAccumulate(const short *mixingPos, const short *compare, double &norm)
|
2015-08-22 14:16:27 +00:00
|
|
|
{
|
|
|
|
long corr;
|
2015-10-08 00:17:50 +00:00
|
|
|
unsigned long lnorm;
|
2015-08-22 14:16:27 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
// cancel first normalizer tap from previous round
|
|
|
|
lnorm = 0;
|
|
|
|
for (i = 1; i <= channels; i ++)
|
|
|
|
{
|
2015-10-08 00:17:50 +00:00
|
|
|
lnorm -= (mixingPos[-i] * mixingPos[-i]) >> overlapDividerBitsNorm;
|
2015-08-22 14:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
corr = 0;
|
|
|
|
// Same routine for stereo and mono. For stereo, unroll loop for better
|
|
|
|
// efficiency and gives slightly better resolution against rounding.
|
|
|
|
// For mono it same routine, just unrolls loop by factor of 4
|
|
|
|
for (i = 0; i < channels * overlapLength; i += 4)
|
|
|
|
{
|
|
|
|
corr += (mixingPos[i] * compare[i] +
|
2015-10-08 00:17:50 +00:00
|
|
|
mixingPos[i + 1] * compare[i + 1]) >> overlapDividerBitsNorm; // notice: do intermediate division here to avoid integer overflow
|
2015-08-22 14:16:27 +00:00
|
|
|
corr += (mixingPos[i + 2] * compare[i + 2] +
|
2015-10-08 00:17:50 +00:00
|
|
|
mixingPos[i + 3] * compare[i + 3]) >> overlapDividerBitsNorm;
|
2015-08-22 14:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// update normalizer with last samples of this round
|
|
|
|
for (int j = 0; j < channels; j ++)
|
|
|
|
{
|
|
|
|
i --;
|
2015-10-08 00:17:50 +00:00
|
|
|
lnorm += (mixingPos[i] * mixingPos[i]) >> overlapDividerBitsNorm;
|
2015-08-22 14:16:27 +00:00
|
|
|
}
|
2015-10-08 00:17:50 +00:00
|
|
|
|
2015-08-22 14:16:27 +00:00
|
|
|
norm += (double)lnorm;
|
2015-10-08 00:17:50 +00:00
|
|
|
if (norm > maxnorm)
|
|
|
|
{
|
|
|
|
maxnorm = (unsigned long)norm;
|
|
|
|
}
|
2015-08-22 14:16:27 +00:00
|
|
|
|
|
|
|
// Normalize result by dividing by sqrt(norm) - this step is easiest
|
|
|
|
// done using floating point operation
|
|
|
|
return (double)corr / sqrt((norm < 1e-9) ? 1.0 : norm);
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
#endif // SOUNDTOUCH_INTEGER_SAMPLES
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Floating point arithmetics specific algorithm implementations.
|
|
|
|
//
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
#ifdef SOUNDTOUCH_FLOAT_SAMPLES
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
// Overlaps samples in 'midBuffer' with the samples in 'pInput'
|
|
|
|
void TDStretch::overlapStereo(float *pOutput, const float *pInput) const
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
float fScale;
|
2013-04-17 10:46:57 +00:00
|
|
|
float f1;
|
|
|
|
float f2;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
fScale = 1.0f / (float)overlapLength;
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
f1 = 0;
|
|
|
|
f2 = 1.0f;
|
|
|
|
|
|
|
|
for (i = 0; i < 2 * (int)overlapLength ; i += 2)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2013-04-17 10:46:57 +00:00
|
|
|
pOutput[i + 0] = pInput[i + 0] * f1 + pMidBuffer[i + 0] * f2;
|
|
|
|
pOutput[i + 1] = pInput[i + 1] * f1 + pMidBuffer[i + 1] * f2;
|
|
|
|
|
|
|
|
f1 += fScale;
|
|
|
|
f2 -= fScale;
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-22 14:16:27 +00:00
|
|
|
// Overlaps samples in 'midBuffer' with the samples in 'input'.
|
|
|
|
void TDStretch::overlapMulti(float *pOutput, const float *pInput) const
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
float fScale;
|
|
|
|
float f1;
|
|
|
|
float f2;
|
|
|
|
|
|
|
|
fScale = 1.0f / (float)overlapLength;
|
|
|
|
|
|
|
|
f1 = 0;
|
|
|
|
f2 = 1.0f;
|
|
|
|
|
|
|
|
i=0;
|
|
|
|
for (int i2 = 0; i2 < overlapLength; i2 ++)
|
|
|
|
{
|
|
|
|
// note: Could optimize this slightly by taking into account that always channels > 2
|
|
|
|
for (int c = 0; c < channels; c ++)
|
|
|
|
{
|
|
|
|
pOutput[i] = pInput[i] * f1 + pMidBuffer[i] * f2;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
f1 += fScale;
|
|
|
|
f2 -= fScale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
/// Calculates overlapInMsec period length in samples.
|
|
|
|
void TDStretch::calculateOverlapLength(int overlapInMsec)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
int newOvl;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
assert(overlapInMsec >= 0);
|
|
|
|
newOvl = (sampleRate * overlapInMsec) / 1000;
|
2009-02-15 05:15:39 +00:00
|
|
|
if (newOvl < 16) newOvl = 16;
|
|
|
|
|
|
|
|
// must be divisible by 8
|
|
|
|
newOvl -= newOvl % 8;
|
|
|
|
|
|
|
|
acceptNewOverlapLength(newOvl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-22 14:16:27 +00:00
|
|
|
/// Calculate cross-correlation
|
2015-10-08 00:17:50 +00:00
|
|
|
double TDStretch::calcCrossCorr(const float *mixingPos, const float *compare, double &anorm)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
double corr;
|
2010-03-18 20:04:51 +00:00
|
|
|
double norm;
|
|
|
|
int i;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
corr = norm = 0;
|
2013-04-17 10:46:57 +00:00
|
|
|
// Same routine for stereo and mono. For Stereo, unroll by factor of 2.
|
|
|
|
// For mono it's same routine yet unrollsd by factor of 4.
|
|
|
|
for (i = 0; i < channels * overlapLength; i += 4)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
corr += mixingPos[i] * compare[i] +
|
|
|
|
mixingPos[i + 1] * compare[i + 1];
|
2013-04-17 10:46:57 +00:00
|
|
|
|
|
|
|
norm += mixingPos[i] * mixingPos[i] +
|
2010-03-18 20:04:51 +00:00
|
|
|
mixingPos[i + 1] * mixingPos[i + 1];
|
2013-04-17 10:46:57 +00:00
|
|
|
|
|
|
|
// unroll the loop for better CPU efficiency:
|
|
|
|
corr += mixingPos[i + 2] * compare[i + 2] +
|
|
|
|
mixingPos[i + 3] * compare[i + 3];
|
|
|
|
|
|
|
|
norm += mixingPos[i + 2] * mixingPos[i + 2] +
|
|
|
|
mixingPos[i + 3] * mixingPos[i + 3];
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-22 14:16:27 +00:00
|
|
|
anorm = norm;
|
|
|
|
return corr / sqrt((norm < 1e-9 ? 1.0 : norm));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Update cross-correlation by accumulating "norm" coefficient by previously calculated value
|
2015-10-08 00:17:50 +00:00
|
|
|
double TDStretch::calcCrossCorrAccumulate(const float *mixingPos, const float *compare, double &norm)
|
2015-08-22 14:16:27 +00:00
|
|
|
{
|
|
|
|
double corr;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
corr = 0;
|
|
|
|
|
|
|
|
// cancel first normalizer tap from previous round
|
|
|
|
for (i = 1; i <= channels; i ++)
|
|
|
|
{
|
|
|
|
norm -= mixingPos[-i] * mixingPos[-i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same routine for stereo and mono. For Stereo, unroll by factor of 2.
|
|
|
|
// For mono it's same routine yet unrollsd by factor of 4.
|
|
|
|
for (i = 0; i < channels * overlapLength; i += 4)
|
|
|
|
{
|
|
|
|
corr += mixingPos[i] * compare[i] +
|
|
|
|
mixingPos[i + 1] * compare[i + 1] +
|
|
|
|
mixingPos[i + 2] * compare[i + 2] +
|
|
|
|
mixingPos[i + 3] * compare[i + 3];
|
|
|
|
}
|
|
|
|
|
|
|
|
// update normalizer with last samples of this round
|
|
|
|
for (int j = 0; j < channels; j ++)
|
|
|
|
{
|
|
|
|
i --;
|
|
|
|
norm += mixingPos[i] * mixingPos[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return corr / sqrt((norm < 1e-9 ? 1.0 : norm));
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-22 14:16:27 +00:00
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
#endif // SOUNDTOUCH_FLOAT_SAMPLES
|