Version 4.0

This commit is contained in:
Gary Scavone
2013-09-25 14:50:19 +02:00
committed by Stephen Sinclair
parent 3f126af4e5
commit 81475b04c5
473 changed files with 36355 additions and 28396 deletions

View File

@@ -1,21 +1,31 @@
/******************************************/
/* Karplus-Strong plucked string model */
/* by Perry Cook, 1995-96 */
/* */
/* There exist at least two patents, */
/* assigned to Stanford, bearing the */
/* names of Karplus and/or Strong. */
/******************************************/
/***************************************************/
/*! \class Plucked
\brief STK plucked string model class.
This class implements a simple plucked string
physical model based on the Karplus-Strong
algorithm.
This is a digital waveguide model, making its
use possibly subject to patents held by
Stanford University, Yamaha, and others.
There exist at least two patents, assigned to
Stanford, bearing the names of Karplus and/or
Strong.
by Perry R. Cook and Gary P. Scavone, 1995 - 2002.
*/
/***************************************************/
#include "Plucked.h"
Plucked :: Plucked(MY_FLOAT lowestFreq)
Plucked :: Plucked(MY_FLOAT lowestFrequency)
{
length = (long) (SRATE / lowestFreq + 1);
length = (long) (Stk::sampleRate() / lowestFrequency + 1);
loopGain = (MY_FLOAT) 0.999;
delayLine = new DLineA(length);
loopFilt = new OneZero;
pickFilt = new OnePole;
delayLine = new DelayA( (MY_FLOAT)(length / 2.0), length );
loopFilter = new OneZero;
pickFilter = new OnePole;
noise = new Noise;
this->clear();
}
@@ -23,62 +33,85 @@ Plucked :: Plucked(MY_FLOAT lowestFreq)
Plucked :: ~Plucked()
{
delete delayLine;
delete loopFilt;
delete pickFilt;
delete loopFilter;
delete pickFilter;
delete noise;
}
void Plucked :: clear()
{
delayLine->clear();
loopFilt->clear();
pickFilt->clear();
loopFilter->clear();
pickFilter->clear();
}
void Plucked :: setFreq(MY_FLOAT frequency)
void Plucked :: setFrequency(MY_FLOAT frequency)
{
MY_FLOAT delay;
delay = (SRATE / frequency) - (MY_FLOAT) 0.5; /* length - delays */
MY_FLOAT freakency = frequency;
if ( frequency <= 0.0 ) {
cerr << "Plucked: setFrequency parameter is less than or equal to zero!" << endl;
freakency = 220.0;
}
// Delay = length - approximate filter delay.
MY_FLOAT delay = (Stk::sampleRate() / freakency) - (MY_FLOAT) 0.5;
if (delay <= 0.0) delay = 0.3;
else if (delay > length) delay = length;
delayLine->setDelay(delay);
loopGain = (MY_FLOAT) 0.995 + (frequency * (MY_FLOAT) 0.000005);
if (loopGain>1.0) loopGain = (MY_FLOAT) 0.99999;
loopGain = 0.995 + (freakency * 0.000005);
if ( loopGain >= 1.0 ) loopGain = (MY_FLOAT) 0.99999;
}
void Plucked :: pluck(MY_FLOAT amplitude)
{
long i;
pickFilt->setPole((MY_FLOAT) 0.999 - (amplitude * (MY_FLOAT) 0.15));
pickFilt->setGain(amplitude * (MY_FLOAT) 0.5);
for (i=0;i<length;i++)
// fill delay with noise additively with current contents
delayLine->tick(delayLine->lastOut() * (MY_FLOAT) 0.6
+ pickFilt->tick(noise->tick()));
MY_FLOAT gain = amplitude;
if ( gain > 1.0 ) {
cerr << "Plucked: pluck amplitude greater than 1.0!" << endl;
gain = 1.0;
}
else if ( gain < 0.0 ) {
cerr << "Plucked: pluck amplitude less than zero!" << endl;
gain = 0.0;
}
pickFilter->setPole((MY_FLOAT) 0.999 - (gain * (MY_FLOAT) 0.15));
pickFilter->setGain(gain * (MY_FLOAT) 0.5);
for (long i=0; i<length; i++)
// Fill delay with noise additively with current contents.
delayLine->tick( 0.6 * delayLine->lastOut() + pickFilter->tick( noise->tick() ) );
}
void Plucked :: noteOn(MY_FLOAT freq, MY_FLOAT amp)
void Plucked :: noteOn(MY_FLOAT frequency, MY_FLOAT amplitude)
{
this->setFreq(freq);
this->pluck(amp);
#if defined(_debug_)
printf("Plucked : NoteOn: Freq=%lf Amp=%lf\n",freq,amp);
#endif
this->setFrequency(frequency);
this->pluck(amplitude);
#if defined(_STK_DEBUG_)
cerr << "Plucked: NoteOn frequency = " << frequency << ", amplitude = " << amplitude << endl;
#endif
}
void Plucked :: noteOff(MY_FLOAT amp)
void Plucked :: noteOff(MY_FLOAT amplitude)
{
loopGain = (MY_FLOAT) 1.0 - amp;
#if defined(_debug_)
printf("Plucked : NoteOff: Amp=%lf\n",amp);
#endif
loopGain = (MY_FLOAT) 1.0 - amplitude;
if ( loopGain < 0.0 ) {
cerr << "Plucked: noteOff amplitude greater than 1.0!" << endl;
loopGain = 0.0;
}
else if ( loopGain > 1.0 ) {
cerr << "Plucked: noteOff amplitude less than or zero!" << endl;
loopGain = (MY_FLOAT) 0.99999;
}
#if defined(_STK_DEBUG_)
cerr << "Plucked: NoteOff amplitude = " << amplitude << endl;
#endif
}
MY_FLOAT Plucked :: tick()
{
/* check this out */
/* here's the whole inner loop of the instrument!! */
lastOutput = delayLine->tick(loopFilt->tick(delayLine->lastOut() * loopGain));
// Here's the whole inner loop of the instrument!!
lastOutput = delayLine->tick( loopFilter->tick( delayLine->lastOut() * loopGain ) );
lastOutput *= (MY_FLOAT) 3.0;
return lastOutput;
}