mirror of
https://github.com/thestk/stk
synced 2026-01-12 12:31:53 +00:00
Version 4.2.0
This commit is contained in:
committed by
Stephen Sinclair
parent
cf06b7598b
commit
a6381b9d38
114
src/PitShift.cpp
114
src/PitShift.cpp
@@ -5,93 +5,93 @@
|
||||
This class implements a simple pitch shifter
|
||||
using delay lines.
|
||||
|
||||
by Perry R. Cook and Gary P. Scavone, 1995 - 2002.
|
||||
by Perry R. Cook and Gary P. Scavone, 1995 - 2004.
|
||||
*/
|
||||
/***************************************************/
|
||||
|
||||
#include "PitShift.h"
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
|
||||
const int maxDelay = 5024;
|
||||
|
||||
PitShift :: PitShift()
|
||||
{
|
||||
delay[0] = 12;
|
||||
delay[1] = 512;
|
||||
delayLine[0] = new DelayL(delay[0], (long) 1024);
|
||||
delayLine[1] = new DelayL(delay[1], (long) 1024);
|
||||
effectMix = (MY_FLOAT) 0.5;
|
||||
rate = 1.0;
|
||||
delayLength = maxDelay - 24;
|
||||
halfLength = delayLength / 2;
|
||||
delay_[0] = 12;
|
||||
delay_[1] = maxDelay / 2;
|
||||
|
||||
delayLine_[0].setMaximumDelay( maxDelay );
|
||||
delayLine_[0].setDelay( delay_[0] );
|
||||
delayLine_[1].setMaximumDelay( maxDelay );
|
||||
delayLine_[1].setDelay( delay_[1] );
|
||||
effectMix_ = 0.5;
|
||||
rate_ = 1.0;
|
||||
}
|
||||
|
||||
PitShift :: ~PitShift()
|
||||
{
|
||||
delete delayLine[0];
|
||||
delete delayLine[1];
|
||||
}
|
||||
|
||||
void PitShift :: clear()
|
||||
{
|
||||
delayLine[0]->clear();
|
||||
delayLine[1]->clear();
|
||||
lastOutput = 0.0;
|
||||
delayLine_[0].clear();
|
||||
delayLine_[1].clear();
|
||||
lastOutput_[0] = 0.0;
|
||||
lastOutput_[1] = 0.0;
|
||||
}
|
||||
|
||||
void PitShift :: setEffectMix(MY_FLOAT mix)
|
||||
void PitShift :: setShift(StkFloat shift)
|
||||
{
|
||||
effectMix = mix;
|
||||
if ( mix < 0.0 ) {
|
||||
std::cerr << "PitShift: setEffectMix parameter is less than zero!" << std::endl;
|
||||
effectMix = 0.0;
|
||||
if (shift < 1.0) {
|
||||
rate_ = 1.0 - shift;
|
||||
}
|
||||
else if ( mix > 1.0 ) {
|
||||
std::cerr << "PitShift: setEffectMix parameter is greater than 1.0!" << std::endl;
|
||||
effectMix = 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
void PitShift :: setShift(MY_FLOAT shift)
|
||||
{
|
||||
if (shift < 1.0) {
|
||||
rate = 1.0 - shift;
|
||||
}
|
||||
else if (shift > 1.0) {
|
||||
rate = 1.0 - shift;
|
||||
else if (shift > 1.0) {
|
||||
rate_ = 1.0 - shift;
|
||||
}
|
||||
else {
|
||||
rate = 0.0;
|
||||
delay[0] = 512;
|
||||
rate_ = 0.0;
|
||||
delay_[0] = halfLength+12;
|
||||
}
|
||||
}
|
||||
|
||||
MY_FLOAT PitShift :: lastOut() const
|
||||
StkFloat PitShift :: tick(StkFloat input)
|
||||
{
|
||||
return lastOutput;
|
||||
// Calculate the two delay length values, keeping them within the
|
||||
// range 12 to maxDelay-12.
|
||||
delay_[0] += rate_;
|
||||
while (delay_[0] > maxDelay-12) delay_[0] -= delayLength;
|
||||
while (delay_[0] < 12) delay_[0] += delayLength;
|
||||
|
||||
delay_[1] = delay_[0] + halfLength;
|
||||
while (delay_[1] > maxDelay-12) delay_[1] -= delayLength;
|
||||
while (delay_[1] < 12) delay_[1] += delayLength;
|
||||
|
||||
// Set the new delay line lengths.
|
||||
delayLine_[0].setDelay((long)delay_[0]);
|
||||
delayLine_[1].setDelay((long)delay_[1]);
|
||||
|
||||
// Calculate a triangular envelope.
|
||||
env_[1] = fabs( (delay_[0] - halfLength + 12) * (1.0 / (halfLength+12) ) );
|
||||
env_[0] = 1.0 - env_[1];
|
||||
|
||||
// Delay input and apply envelope.
|
||||
lastOutput_[0] = env_[0] * delayLine_[0].tick(input);
|
||||
lastOutput_[0] += env_[1] * delayLine_[1].tick(input);
|
||||
|
||||
// Compute effect mix and output.
|
||||
lastOutput_[0] *= effectMix_;
|
||||
lastOutput_[0] += (1.0 - effectMix_) * input;
|
||||
lastOutput_[1] = lastOutput_[0];
|
||||
return lastOutput_[0];
|
||||
}
|
||||
|
||||
MY_FLOAT PitShift :: tick(MY_FLOAT input)
|
||||
StkFloat *PitShift :: tick(StkFloat *vector, unsigned int vectorSize)
|
||||
{
|
||||
delay[0] = delay[0] + rate;
|
||||
while (delay[0] > 1012) delay[0] -= 1000;
|
||||
while (delay[0] < 12) delay[0] += 1000;
|
||||
delay[1] = delay[0] + 500;
|
||||
while (delay[1] > 1012) delay[1] -= 1000;
|
||||
while (delay[1] < 12) delay[1] += 1000;
|
||||
delayLine[0]->setDelay((long)delay[0]);
|
||||
delayLine[1]->setDelay((long)delay[1]);
|
||||
env[1] = fabs(delay[0] - 512) * 0.002;
|
||||
env[0] = 1.0 - env[1];
|
||||
lastOutput = env[0] * delayLine[0]->tick(input);
|
||||
lastOutput += env[1] * delayLine[1]->tick(input);
|
||||
lastOutput *= effectMix;
|
||||
lastOutput += (1.0 - effectMix) * input;
|
||||
return lastOutput;
|
||||
return Effect::tick( vector, vectorSize );
|
||||
}
|
||||
|
||||
MY_FLOAT *PitShift :: tick(MY_FLOAT *vector, unsigned int vectorSize)
|
||||
StkFrames& PitShift :: tick( StkFrames& frames, unsigned int channel )
|
||||
{
|
||||
for (unsigned int i=0; i<vectorSize; i++)
|
||||
vector[i] = tick(vector[i]);
|
||||
|
||||
return vector;
|
||||
return Effect::tick( frames, channel );
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user