blob: 291c5f21be55919575be2bcc6bdae78a0f96835b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
/******************************************/
/* Karplus-Strong sitar string model */
/* by Perry Cook, 1995-96 */
/* */
/* There exist at least two patents, */
/* assigned to Stanford, bearing the */
/* names of Karplus and/or Strong. */
/******************************************/
#include "sitar.h"
sitar :: sitar(StkFloat lowestFreq)
{
length = (long) (SRATE / lowestFreq + 1);
loopGain = (StkFloat) 0.999;
loopFilt = new OneZero();
loopFilt->setZero(0.01);
delayLine = new DelayA(0.5 * length, length);
delay = length/2;
delayTarg = delay;
envelope = new ADSR();
noise = new Noise;
envelope->setAllTimes(0.001,0.04,0.0,0.5);
this->clear();
}
sitar :: ~sitar()
{
delete loopFilt;
delete delayLine;
delete envelope;
delete noise;
}
void sitar :: clear()
{
loopFilt->clear();
delayLine->clear();
}
void sitar :: setFreq(StkFloat frequency)
{
delayTarg = (SRATE / frequency);
delay = delayTarg * (1.0 + (0.05 * noise->tick()));
delayLine->setDelay(delay);
loopGain = (StkFloat) 0.995 + (frequency * (StkFloat) 0.000001);
if (loopGain>1.0) loopGain = (StkFloat) 0.9995;
}
void sitar :: pluck(StkFloat amplitude)
{
envelope->keyOn();
}
void sitar :: noteOn(StkFloat freq, StkFloat amp)
{
this->setFreq(freq);
this->pluck(amp);
amPluck = 0.05 * amp;
#if defined(_debug_)
printf("sitar : NoteOn: Freq=%lf Amp=%lf\n",freq,amp);
#endif
}
void sitar :: noteOff(StkFloat amp)
{
loopGain = (StkFloat) 1.0 - amp;
#if defined(_debug_)
printf("sitar : NoteOff: Amp=%lf\n",amp);
#endif
}
StkFloat sitar :: tick()
{
StkFloat temp;
temp = delayLine->lastOut();
if (fabs(temp) > 1.0) {
loopGain = 0.1;
this->noteOff(0.9);
delay = delayTarg;
delayLine->setDelay(delay);
}
temp *= loopGain;
if (fabs(delayTarg - delay) > 0.001) {
if (delayTarg < delay)
delay *= 0.99999;
else
delay *= 1.00001;
delayLine->setDelay(delay);
}
lastOutput_ = delayLine->tick(loopFilt->tick(temp)
+ (amPluck * envelope->tick() * noise->tick()));
return lastOutput_;
}
|