-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTapioca.sc
83 lines (74 loc) · 2.22 KB
/
Tapioca.sc
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
Tapioca {
var <>x, <>amp, <>synth, <>env, <>state, <>start_time, <>group, <>selected;
var <>attackTime, <>decayTime, <>sustainLevel, <>releaseTime;
*new { |x, group, attackTime, decayTime, sustainLevel, releaseTime, standby = false|
^super.new.init(x, group, attackTime, decayTime, sustainLevel, releaseTime, standby);
}
init { |x, group, attackTime, decayTime, sustainLevel, releaseTime, standby|
this.x = x;
this.amp = 0;
this.group = group;
this.attackTime = attackTime;
this.decayTime = decayTime;
this.sustainLevel = sustainLevel;
this.releaseTime = releaseTime;
if((sustainLevel == 0),
{this.env = Env.perc(attackTime, decayTime)},
{this.env = Env.adsr(attackTime, decayTime, sustainLevel, releaseTime, curve:-2)}
);
if(standby, {
this.state = "standby";
this.selected = true;
},{
this.play;
this.selected = false;
});
}
play {
var lfo_mode;
synth = Synth(\sine, [\out, 0, \freq, x.linexp(0, 600, 80, 4000), \amp, 1.0, \env, env]);
start_time = Main.elapsedTime;
state = "attack";
}
changeSustainLevel {|amp_new|
amp = amp_new;
synth.set(\mode, 1);
synth.set(\amp, amp);
state = "manual";
sustainLevel = amp_new;
env = Env.adsr(attackTime, decayTime, sustainLevel, releaseTime);
synth.set(\env, env);
}
setSustainLevelWithCurrentAmp {
env = Env.adsr(attackTime, decayTime, amp, releaseTime);
}
refresh {
if((state != "manual") && ((state == "attack") || (state == "release") ) ) {
var t = Main.elapsedTime - start_time;
if (state == "attack") {
if (t <= (attackTime + decayTime)) {
amp = env.at(t);
};
if (t > (attackTime + decayTime)) {
if(sustainLevel != 0) {
amp = sustainLevel;
state = "sustain";
};
if(sustainLevel == 0) {
amp = 0;
state = "finished";
};
};
};
if (state == "release") {
if (t <= releaseTime) {
amp = env.at(t + attackTime + decayTime);
};
if (t > releaseTime) {
amp = 0;
state = "finished";
};
};
};
}
}