2009-02-15 05:15:39 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2010-04-25 00:31:27 +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.
|
|
|
|
///
|
2010-04-25 00:31:27 +00:00
|
|
|
/// Note : MMX optimized functions reside in a separate, platform-specific
|
2009-02-15 05:15:39 +00:00
|
|
|
/// file, e.g. 'mmx_win.cpp' or 'mmx_gcc.cpp'
|
|
|
|
///
|
|
|
|
/// Author : Copyright (c) Olli Parviainen
|
|
|
|
/// Author e-mail : oparviai 'at' iki.fi
|
|
|
|
/// SoundTouch WWW: http://www.surina.net/soundtouch
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2010-03-18 20:04:51 +00:00
|
|
|
// Last changed : $Date: 2009-12-28 21:27:04 +0200 (Mon, 28 Dec 2009) $
|
|
|
|
// File revision : $Revision: 1.12 $
|
2009-02-15 05:15:39 +00:00
|
|
|
//
|
2010-03-18 20:04:51 +00:00
|
|
|
// $Id: TDStretch.cpp 77 2009-12-28 19:27:04Z 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>
|
|
|
|
#include <stdexcept>
|
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
|
|
|
#include <stdio.h>
|
2009-02-15 05:15:39 +00:00
|
|
|
|
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
|
2010-03-18 20:04:51 +00:00
|
|
|
static const short _scanOffsets[5][24]={
|
|
|
|
{ 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)
|
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
bQuickSeek = FALSE;
|
2009-02-15 05:15:39 +00:00
|
|
|
channels = 2;
|
|
|
|
|
|
|
|
pMidBuffer = NULL;
|
|
|
|
pRefMidBufferUnaligned = NULL;
|
|
|
|
overlapLength = 0;
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
bAutoSeqSetting = TRUE;
|
|
|
|
bAutoSeekSetting = TRUE;
|
|
|
|
|
|
|
|
// outDebt = 0;
|
|
|
|
skipFract = 0;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
tempo = 1.0f;
|
|
|
|
setParameters(48000, 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()
|
|
|
|
{
|
|
|
|
delete[] pMidBuffer;
|
|
|
|
delete[] pRefMidBufferUnaligned;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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)
|
2010-04-25 00:31:27 +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)
|
|
|
|
|
2010-04-25 00:31:27 +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;
|
|
|
|
bAutoSeqSetting = FALSE;
|
2010-04-25 00:31:27 +00:00
|
|
|
}
|
2010-03-18 20:04:51 +00:00
|
|
|
else if (aSequenceMS == 0)
|
|
|
|
{
|
|
|
|
// if zero, use automatic setting
|
|
|
|
bAutoSeqSetting = TRUE;
|
|
|
|
}
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
if (aSeekWindowMS > 0)
|
2010-03-18 20:04:51 +00:00
|
|
|
{
|
|
|
|
this->seekWindowMs = aSeekWindowMS;
|
|
|
|
bAutoSeekSetting = FALSE;
|
2010-04-25 00:31:27 +00:00
|
|
|
}
|
|
|
|
else if (aSeekWindowMS == 0)
|
2010-03-18 20:04:51 +00:00
|
|
|
{
|
|
|
|
// if zero, use automatic setting
|
|
|
|
bAutoSeekSetting = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
int i, itemp;
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
for (i = 0; i < overlapLength ; i ++)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
itemp = overlapLength - i;
|
2010-03-18 20:04:51 +00:00
|
|
|
pOutput[i] = (pInput[i] * i + pMidBuffer[i] * itemp ) / overlapLength; // >> overlapDividerBits;
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TDStretch::clearMidBuffer()
|
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
memset(pMidBuffer, 0, 2 * sizeof(SAMPLETYPE) * overlapLength);
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TDStretch::clearInput()
|
|
|
|
{
|
|
|
|
inputBuffer.clear();
|
|
|
|
clearMidBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
void TDStretch::enableQuickSeek(BOOL enable)
|
|
|
|
{
|
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.
|
|
|
|
BOOL TDStretch::isQuickSeekEnabled() const
|
|
|
|
{
|
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
|
|
|
{
|
2010-04-25 00:31:27 +00:00
|
|
|
if (channels == 2)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
// stereo sound
|
2010-04-25 00:31:27 +00:00
|
|
|
if (bQuickSeek)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
return seekBestOverlapPositionStereoQuick(refPos);
|
2010-04-25 00:31:27 +00:00
|
|
|
}
|
|
|
|
else
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
return seekBestOverlapPositionStereo(refPos);
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
}
|
|
|
|
else
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
// mono sound
|
2010-04-25 00:31:27 +00:00
|
|
|
if (bQuickSeek)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
return seekBestOverlapPositionMonoQuick(refPos);
|
2010-04-25 00:31:27 +00:00
|
|
|
}
|
|
|
|
else
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
return seekBestOverlapPositionMono(refPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2010-04-25 00:31:27 +00:00
|
|
|
if (channels == 2)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
// stereo sound
|
2010-03-18 20:04:51 +00:00
|
|
|
overlapStereo(pOutput, pInput + 2 * ovlPos);
|
2009-02-15 05:15:39 +00:00
|
|
|
} else {
|
|
|
|
// mono sound.
|
2010-03-18 20:04:51 +00:00
|
|
|
overlapMono(pOutput, pInput + 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
|
2010-04-25 00:31:27 +00:00
|
|
|
int TDStretch::seekBestOverlapPositionStereo(const SAMPLETYPE *refPos)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
int bestOffs;
|
|
|
|
double bestCorr, corr;
|
|
|
|
int i;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
// Slopes the amplitudes of the 'midBuffer' samples
|
|
|
|
precalcCorrReferenceStereo();
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
bestCorr = FLT_MIN;
|
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.
|
2010-04-25 00:31:27 +00:00
|
|
|
for (i = 0; i < seekLength; i ++)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
// Calculates correlation value for the mixing position corresponding
|
|
|
|
// to 'i'
|
2010-03-18 20:04:51 +00:00
|
|
|
corr = (double)calcCrossCorrStereo(refPos + 2 * i, pRefMidBuffer);
|
|
|
|
// 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
|
2010-04-25 00:31:27 +00:00
|
|
|
if (corr > bestCorr)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
bestCorr = corr;
|
|
|
|
bestOffs = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// clear cross correlation routine state if necessary (is so e.g. in MMX routines).
|
|
|
|
clearCrossCorrState();
|
|
|
|
|
|
|
|
return bestOffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
2010-04-25 00:31:27 +00:00
|
|
|
int TDStretch::seekBestOverlapPositionStereoQuick(const SAMPLETYPE *refPos)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
int j;
|
|
|
|
int bestOffs;
|
|
|
|
double bestCorr, corr;
|
|
|
|
int scanCount, corrOffset, tempOffset;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
// Slopes the amplitude of the 'midBuffer' samples
|
|
|
|
precalcCorrReferenceStereo();
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
bestCorr = FLT_MIN;
|
|
|
|
bestOffs = _scanOffsets[0][0];
|
2009-02-15 05:15:39 +00:00
|
|
|
corrOffset = 0;
|
|
|
|
tempOffset = 0;
|
|
|
|
|
|
|
|
// Scans for the best correlation value using four-pass hierarchical search.
|
|
|
|
//
|
|
|
|
// The look-up table 'scans' has hierarchical position adjusting steps.
|
2010-04-25 00:31:27 +00:00
|
|
|
// In first pass the routine searhes for the highest correlation with
|
2009-02-15 05:15:39 +00:00
|
|
|
// relatively coarse steps, then rescans the neighbourhood of the highest
|
|
|
|
// correlation with better resolution and so on.
|
2010-04-25 00:31:27 +00:00
|
|
|
for (scanCount = 0;scanCount < 4; scanCount ++)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
j = 0;
|
2010-04-25 00:31:27 +00:00
|
|
|
while (_scanOffsets[scanCount][j])
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
tempOffset = corrOffset + _scanOffsets[scanCount][j];
|
2009-02-15 05:15:39 +00:00
|
|
|
if (tempOffset >= seekLength) break;
|
|
|
|
|
|
|
|
// Calculates correlation value for the mixing position corresponding
|
|
|
|
// to 'tempOffset'
|
2010-03-18 20:04:51 +00:00
|
|
|
corr = (double)calcCrossCorrStereo(refPos + 2 * tempOffset, pRefMidBuffer);
|
|
|
|
// heuristic rule to slightly favour values close to mid of the range
|
|
|
|
double tmp = (double)(2 * tempOffset - seekLength) / 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
|
2010-04-25 00:31:27 +00:00
|
|
|
if (corr > bestCorr)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
bestCorr = corr;
|
|
|
|
bestOffs = tempOffset;
|
|
|
|
}
|
|
|
|
j ++;
|
|
|
|
}
|
|
|
|
corrOffset = bestOffs;
|
|
|
|
}
|
|
|
|
// clear cross correlation routine state if necessary (is so e.g. in MMX routines).
|
|
|
|
clearCrossCorrState();
|
|
|
|
|
|
|
|
return bestOffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Seeks for the optimal overlap-mixing position. The 'mono' 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
|
2010-04-25 00:31:27 +00:00
|
|
|
int TDStretch::seekBestOverlapPositionMono(const SAMPLETYPE *refPos)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
int bestOffs;
|
|
|
|
double bestCorr, corr;
|
|
|
|
int tempOffset;
|
2009-02-15 05:15:39 +00:00
|
|
|
const SAMPLETYPE *compare;
|
|
|
|
|
|
|
|
// Slopes the amplitude of the 'midBuffer' samples
|
|
|
|
precalcCorrReferenceMono();
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
bestCorr = FLT_MIN;
|
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.
|
2010-04-25 00:31:27 +00:00
|
|
|
for (tempOffset = 0; tempOffset < seekLength; tempOffset ++)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
compare = refPos + tempOffset;
|
|
|
|
|
|
|
|
// Calculates correlation value for the mixing position corresponding
|
|
|
|
// to 'tempOffset'
|
2010-03-18 20:04:51 +00:00
|
|
|
corr = (double)calcCrossCorrMono(pRefMidBuffer, compare);
|
|
|
|
// heuristic rule to slightly favour values close to mid of the range
|
|
|
|
double tmp = (double)(2 * tempOffset - seekLength) / 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
|
2010-04-25 00:31:27 +00:00
|
|
|
if (corr > bestCorr)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
bestCorr = corr;
|
|
|
|
bestOffs = tempOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// clear cross correlation routine state if necessary (is so e.g. in MMX routines).
|
|
|
|
clearCrossCorrState();
|
|
|
|
|
|
|
|
return bestOffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Seeks for the optimal overlap-mixing position. The 'mono' 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
|
2010-04-25 00:31:27 +00:00
|
|
|
int TDStretch::seekBestOverlapPositionMonoQuick(const SAMPLETYPE *refPos)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
int j;
|
|
|
|
int bestOffs;
|
|
|
|
double bestCorr, corr;
|
|
|
|
int scanCount, corrOffset, tempOffset;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
// Slopes the amplitude of the 'midBuffer' samples
|
|
|
|
precalcCorrReferenceMono();
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
bestCorr = FLT_MIN;
|
|
|
|
bestOffs = _scanOffsets[0][0];
|
2009-02-15 05:15:39 +00:00
|
|
|
corrOffset = 0;
|
|
|
|
tempOffset = 0;
|
|
|
|
|
|
|
|
// Scans for the best correlation value using four-pass hierarchical search.
|
|
|
|
//
|
|
|
|
// The look-up table 'scans' has hierarchical position adjusting steps.
|
2010-04-25 00:31:27 +00:00
|
|
|
// In first pass the routine searhes for the highest correlation with
|
2009-02-15 05:15:39 +00:00
|
|
|
// relatively coarse steps, then rescans the neighbourhood of the highest
|
|
|
|
// correlation with better resolution and so on.
|
2010-04-25 00:31:27 +00:00
|
|
|
for (scanCount = 0;scanCount < 4; scanCount ++)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
j = 0;
|
2010-04-25 00:31:27 +00:00
|
|
|
while (_scanOffsets[scanCount][j])
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
tempOffset = corrOffset + _scanOffsets[scanCount][j];
|
2009-02-15 05:15:39 +00:00
|
|
|
if (tempOffset >= seekLength) break;
|
|
|
|
|
|
|
|
// Calculates correlation value for the mixing position corresponding
|
|
|
|
// to 'tempOffset'
|
2010-03-18 20:04:51 +00:00
|
|
|
corr = (double)calcCrossCorrMono(refPos + tempOffset, pRefMidBuffer);
|
|
|
|
// heuristic rule to slightly favour values close to mid of the range
|
|
|
|
double tmp = (double)(2 * tempOffset - seekLength) / 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
|
2010-04-25 00:31:27 +00:00
|
|
|
if (corr > bestCorr)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
bestCorr = corr;
|
|
|
|
bestOffs = tempOffset;
|
|
|
|
}
|
|
|
|
j ++;
|
|
|
|
}
|
|
|
|
corrOffset = bestOffs;
|
|
|
|
}
|
|
|
|
// clear cross correlation routine state if necessary (is so e.g. in MMX routines).
|
|
|
|
clearCrossCorrState();
|
|
|
|
|
|
|
|
return bestOffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-25 00:31:27 +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
|
|
|
|
#define AUTOSEQ_AT_MIN 125.0
|
|
|
|
#define AUTOSEQ_AT_MAX 50.0
|
|
|
|
#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))
|
|
|
|
|
|
|
|
// seek-window-ms setting values at above low & top tempo
|
|
|
|
#define AUTOSEEK_AT_MIN 25.0
|
|
|
|
#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;
|
2010-04-25 00:31:27 +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;
|
2010-04-25 00:31:27 +00:00
|
|
|
if (seekWindowLength < 2 * overlapLength)
|
2010-03-18 20:04:51 +00:00
|
|
|
{
|
|
|
|
seekWindowLength = 2 * overlapLength;
|
|
|
|
}
|
|
|
|
seekLength = (sampleRate * seekWindowMs) / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-04-25 00:31:27 +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.
|
|
|
|
void TDStretch::setTempo(float newTempo)
|
|
|
|
{
|
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();
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
// Calculate ideal skip length (according to tempo value)
|
2009-02-15 05:15:39 +00:00
|
|
|
nominalSkip = tempo * (seekWindowLength - overlapLength);
|
|
|
|
intskip = (int)(nominalSkip + 0.5f);
|
|
|
|
|
2010-04-25 00:31:27 +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;
|
|
|
|
assert(numChannels == 1 || numChannels == 2);
|
|
|
|
|
|
|
|
channels = numChannels;
|
|
|
|
inputBuffer.setChannels(channels);
|
|
|
|
outputBuffer.setChannels(channels);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
if (bMidBufferDirty)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
// If there are samples in pMidBuffer waiting for overlapping,
|
2010-04-25 00:31:27 +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
|
2010-04-25 00:31:27 +00:00
|
|
|
if (inputBuffer.numSamples() < overlapLength)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
// wait until we've got overlapLength input samples
|
|
|
|
return;
|
|
|
|
}
|
2010-04-25 00:31:27 +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
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
#include <stdio.h>
|
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()
|
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
int ovlSkip, offset;
|
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
|
2010-04-25 00:31:27 +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.
|
2010-04-25 00:31:27 +00:00
|
|
|
// while ((int)inputBuffer.numSamples() >= sampleReq - (outDebt / 4))
|
|
|
|
while ((int)inputBuffer.numSamples() >= sampleReq)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
// If tempo differs from the normal ('SCALE'), scan for the best overlapping
|
|
|
|
// position
|
|
|
|
offset = seekBestOverlapPosition(inputBuffer.ptrBegin());
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
// Mix the samples in the 'inputBuffer' at position of 'offset' with the
|
2009-02-15 05:15:39 +00:00
|
|
|
// samples in 'midBuffer' using sliding overlapping
|
|
|
|
// ... first partially overlap with the end of the previous sequence
|
|
|
|
// (that's in 'midBuffer')
|
2010-03-18 20:04:51 +00:00
|
|
|
overlap(outputBuffer.ptrEnd((uint)overlapLength), inputBuffer.ptrBegin(), (uint)offset);
|
|
|
|
outputBuffer.putSamples((uint)overlapLength);
|
|
|
|
|
|
|
|
// ... then copy sequence samples from 'inputBuffer' to output:
|
|
|
|
temp = (seekLength / 2 - offset);
|
|
|
|
|
|
|
|
// compensate cumulated output length diff vs. ideal output
|
|
|
|
// temp -= outDebt / 4;
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
// update ideal vs. true output difference
|
2010-03-18 20:04:51 +00:00
|
|
|
// outDebt += temp;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
// length of sequence
|
|
|
|
// temp += (seekWindowLength - 2 * overlapLength);
|
|
|
|
temp = (seekWindowLength - 2 * overlapLength);
|
|
|
|
|
|
|
|
// crosscheck that we don't have buffer overflow...
|
|
|
|
if ((int)inputBuffer.numSamples() < (offset + temp + overlapLength * 2))
|
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
|
|
|
}
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
outputBuffer.putSamples(inputBuffer.ptrBegin() + channels * (offset + overlapLength), (uint)temp);
|
|
|
|
|
2010-04-25 00:31:27 +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
|
2010-03-18 20:04:51 +00:00
|
|
|
assert((offset + temp + overlapLength * 2) <= (int)inputBuffer.numSamples());
|
2010-04-25 00:31:27 +00:00
|
|
|
memcpy(pMidBuffer, inputBuffer.ptrBegin() + channels * (offset + temp + overlapLength),
|
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)
|
|
|
|
{
|
|
|
|
delete[] pMidBuffer;
|
|
|
|
delete[] pRefMidBufferUnaligned;
|
|
|
|
|
|
|
|
pMidBuffer = new SAMPLETYPE[overlapLength * 2];
|
|
|
|
clearMidBuffer();
|
|
|
|
|
|
|
|
pRefMidBufferUnaligned = new SAMPLETYPE[2 * overlapLength + 16 / sizeof(SAMPLETYPE)];
|
|
|
|
// ensure that 'pRefMidBuffer' is aligned to 16 byte boundary for efficiency
|
2010-03-18 20:04:51 +00:00
|
|
|
pRefMidBuffer = (SAMPLETYPE *)((((ulong)pRefMidBufferUnaligned) + 15) & (ulong)-16);
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-25 00:31:27 +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!
|
2010-03-18 20:04:51 +00:00
|
|
|
throw std::runtime_error("Error in TDStretch::new: Don't use 'new TDStretch' directly, use 'newInstance' member instead!");
|
2009-02-15 05:15:39 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TDStretch * TDStretch::newInstance()
|
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
uint uExtensions;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
uExtensions = detectCPUextensions();
|
|
|
|
|
|
|
|
// Check if MMX/SSE/3DNow! instruction set extensions supported by CPU
|
|
|
|
|
|
|
|
#ifdef ALLOW_MMX
|
|
|
|
// MMX routines available only with integer sample types
|
|
|
|
if (uExtensions & SUPPORT_MMX)
|
|
|
|
{
|
|
|
|
return ::new TDStretchMMX;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif // ALLOW_MMX
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ALLOW_SSE
|
|
|
|
if (uExtensions & SUPPORT_SSE)
|
|
|
|
{
|
|
|
|
// SSE support
|
|
|
|
return ::new TDStretchSSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif // ALLOW_SSE
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ALLOW_3DNOW
|
|
|
|
if (uExtensions & SUPPORT_3DNOW)
|
|
|
|
{
|
|
|
|
// 3DNow! support
|
|
|
|
return ::new TDStretch3DNow;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif // ALLOW_3DNOW
|
|
|
|
|
|
|
|
{
|
|
|
|
// ISA optimizations not supported, use plain C version
|
|
|
|
return ::new TDStretch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Integer arithmetics specific algorithm implementations.
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifdef INTEGER_SAMPLES
|
|
|
|
|
|
|
|
// Slopes the amplitude of the 'midBuffer' samples so that cross correlation
|
|
|
|
// is faster to calculate
|
|
|
|
void TDStretch::precalcCorrReferenceStereo()
|
|
|
|
{
|
|
|
|
int i, cnt2;
|
|
|
|
int temp, temp2;
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
for (i=0 ; i < (int)overlapLength ;i ++)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
temp = i * (overlapLength - i);
|
|
|
|
cnt2 = i * 2;
|
|
|
|
|
|
|
|
temp2 = (pMidBuffer[cnt2] * temp) / slopingDivider;
|
|
|
|
pRefMidBuffer[cnt2] = (short)(temp2);
|
|
|
|
temp2 = (pMidBuffer[cnt2 + 1] * temp) / slopingDivider;
|
|
|
|
pRefMidBuffer[cnt2 + 1] = (short)(temp2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Slopes the amplitude of the 'midBuffer' samples so that cross correlation
|
|
|
|
// is faster to calculate
|
|
|
|
void TDStretch::precalcCorrReferenceMono()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
long temp;
|
|
|
|
long temp2;
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
for (i=0 ; i < (int)overlapLength ;i ++)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
temp = i * (overlapLength - i);
|
|
|
|
temp2 = (pMidBuffer[i] * temp) / slopingDivider;
|
|
|
|
pRefMidBuffer[i] = (short)temp2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-25 00:31:27 +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
|
|
|
|
2010-04-25 00:31:27 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2010-04-25 00:31:27 +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
|
2010-03-18 20:04:51 +00:00
|
|
|
overlapDividerBits = _getClosest2Power((sampleRate * aoverlapMs) / 1000.0) - 1;
|
2009-02-15 05:15:39 +00:00
|
|
|
if (overlapDividerBits > 9) overlapDividerBits = 9;
|
2010-03-18 20:04:51 +00:00
|
|
|
if (overlapDividerBits < 3) overlapDividerBits = 3;
|
|
|
|
newOvl = (int)pow(2.0, (int)overlapDividerBits + 1); // +1 => account for -1 above
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
acceptNewOverlapLength(newOvl);
|
|
|
|
|
2010-04-25 00:31:27 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long TDStretch::calcCrossCorrMono(const short *mixingPos, const short *compare) const
|
|
|
|
{
|
|
|
|
long corr;
|
2010-03-18 20:04:51 +00:00
|
|
|
long norm;
|
|
|
|
int i;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
corr = norm = 0;
|
2010-04-25 00:31:27 +00:00
|
|
|
for (i = 1; i < overlapLength; i ++)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
corr += (mixingPos[i] * compare[i]) >> overlapDividerBits;
|
2010-03-18 20:04:51 +00:00
|
|
|
norm += (mixingPos[i] * mixingPos[i]) >> overlapDividerBits;
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
2010-04-25 00:31:27 +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
|
|
|
|
if (norm == 0) norm = 1; // to avoid div by zero
|
|
|
|
return (long)((double)corr * SHRT_MAX / sqrt((double)norm));
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long TDStretch::calcCrossCorrStereo(const short *mixingPos, const short *compare) const
|
|
|
|
{
|
|
|
|
long corr;
|
2010-03-18 20:04:51 +00:00
|
|
|
long norm;
|
|
|
|
int i;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
corr = norm = 0;
|
2010-04-25 00:31:27 +00:00
|
|
|
for (i = 2; i < 2 * overlapLength; i += 2)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
corr += (mixingPos[i] * compare[i] +
|
|
|
|
mixingPos[i + 1] * compare[i + 1]) >> overlapDividerBits;
|
2010-03-18 20:04:51 +00:00
|
|
|
norm += (mixingPos[i] * mixingPos[i] + mixingPos[i + 1] * mixingPos[i + 1]) >> overlapDividerBits;
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
2010-04-25 00:31:27 +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
|
|
|
|
if (norm == 0) norm = 1; // to avoid div by zero
|
|
|
|
return (long)((double)corr * SHRT_MAX / sqrt((double)norm));
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // INTEGER_SAMPLES
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Floating point arithmetics specific algorithm implementations.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifdef FLOAT_SAMPLES
|
|
|
|
|
|
|
|
|
|
|
|
// Slopes the amplitude of the 'midBuffer' samples so that cross correlation
|
|
|
|
// is faster to calculate
|
|
|
|
void TDStretch::precalcCorrReferenceStereo()
|
|
|
|
{
|
|
|
|
int i, cnt2;
|
|
|
|
float temp;
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
for (i=0 ; i < (int)overlapLength ;i ++)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
temp = (float)i * (float)(overlapLength - i);
|
|
|
|
cnt2 = i * 2;
|
|
|
|
pRefMidBuffer[cnt2] = (float)(pMidBuffer[cnt2] * temp);
|
|
|
|
pRefMidBuffer[cnt2 + 1] = (float)(pMidBuffer[cnt2 + 1] * temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Slopes the amplitude of the 'midBuffer' samples so that cross correlation
|
|
|
|
// is faster to calculate
|
|
|
|
void TDStretch::precalcCorrReferenceMono()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
float temp;
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
for (i=0 ; i < (int)overlapLength ;i ++)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
temp = (float)i * (float)(overlapLength - i);
|
|
|
|
pRefMidBuffer[i] = (float)(pMidBuffer[i] * temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2010-03-18 20:04:51 +00:00
|
|
|
int cnt2;
|
2009-02-15 05:15:39 +00:00
|
|
|
float fTemp;
|
|
|
|
float fScale;
|
|
|
|
float fi;
|
|
|
|
|
|
|
|
fScale = 1.0f / (float)overlapLength;
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
for (i = 0; i < (int)overlapLength ; i ++)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
fTemp = (float)(overlapLength - i) * fScale;
|
|
|
|
fi = (float)i * fScale;
|
|
|
|
cnt2 = 2 * i;
|
2010-03-18 20:04:51 +00:00
|
|
|
pOutput[cnt2 + 0] = pInput[cnt2 + 0] * fi + pMidBuffer[cnt2 + 0] * fTemp;
|
|
|
|
pOutput[cnt2 + 1] = pInput[cnt2 + 1] * fi + pMidBuffer[cnt2 + 1] * fTemp;
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double TDStretch::calcCrossCorrMono(const float *mixingPos, const float *compare) const
|
|
|
|
{
|
|
|
|
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;
|
2010-04-25 00:31:27 +00:00
|
|
|
for (i = 1; i < overlapLength; i ++)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
corr += mixingPos[i] * compare[i];
|
2010-03-18 20:04:51 +00:00
|
|
|
norm += mixingPos[i] * mixingPos[i];
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
if (norm < 1e-9) norm = 1.0; // to avoid div by zero
|
|
|
|
return corr / sqrt(norm);
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double TDStretch::calcCrossCorrStereo(const float *mixingPos, const float *compare) const
|
|
|
|
{
|
|
|
|
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;
|
2010-04-25 00:31:27 +00:00
|
|
|
for (i = 2; i < 2 * overlapLength; i += 2)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
corr += mixingPos[i] * compare[i] +
|
|
|
|
mixingPos[i + 1] * compare[i + 1];
|
2010-04-25 00:31:27 +00:00
|
|
|
norm += mixingPos[i] * mixingPos[i] +
|
2010-03-18 20:04:51 +00:00
|
|
|
mixingPos[i + 1] * mixingPos[i + 1];
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
if (norm < 1e-9) norm = 1.0; // to avoid div by zero
|
|
|
|
return corr / sqrt(norm);
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // FLOAT_SAMPLES
|