-
Notifications
You must be signed in to change notification settings - Fork 1
/
tutorial4.scd
224 lines (195 loc) · 4.95 KB
/
tutorial4.scd
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// The scripts in Eli Fieldsteel's
// SuperCollider Tutorial: 4. Envelopes and doneAction
//https://www.youtube.com/watch?v=-wDAPo9hpCg&list=PLPYzvS8A_rTaNDweXe6PX4CXSGq4iEWYC&index=5
//shows what synths are still running
s.plotTree;
//introducing, Line envelope. A linear function
(
{
var sig, env;
env = Line.kr(1,0,1);
sig = Pulse.ar(ExpRand(30,500)) * env;
}.play;
)
x.free;
s.freeAll;
//if you play the above function multiple times, you must use s.freeAll to stop all running synths. to automatically end the synth when it is done, use doneAction 2.
(
{
var sig, env;
env = Line.kr(1,0,1, doneAction:2);
sig = Pulse.ar(ExpRand(30,500)) * env;
}.play;
)
//introducing, XLine envelope, which is an exponential function. Input values must be nonzero and the same sign. This sounds more natural since we perceive amplitude exponentially.
(
{
var sig, env;
env = XLine.kr(1,0.01,1, doneAction:2);
sig = Pulse.ar(ExpRand(30,500)) * env;
}.play;
)
// same with decibels, use Line instead of XLine, since decibels is a linear measurement of loudness
(
{
var sig, env;
env = Line.kr(0,-40,1, doneAction:2);
sig = Pulse.ar(ExpRand(30,500)) * env.dbamp;
}.play;
)
//convert amplitude to decibels
0.25.ampdb
//Xline for frequency. We also perceive frequency exponentially.
(
{
var sig, freq, env;
env = XLine.kr(1,0.01,1, doneAction:2);
freq = XLine.kr(800,110,1, doneAction:2);
sig = Pulse.ar(freq) * env;
}.play;
)
//With multiplication of functions with doneAction:2, the product ends whenever the first function with doneAction:2 ends.
(
{
var sig, freq, env;
env = XLine.kr(1,0.01,1, doneAction:2);
freq = XLine.kr(800,110,5, doneAction:2);
sig = Pulse.ar(freq) * env;
}.play;
)
(
{
var sig, freq, env;
env = XLine.kr(1,0.01,5, doneAction:2);
freq = XLine.kr(800,110,1, doneAction:2);
sig = Pulse.ar(freq) * env;
}.play;
)
//to avoid cutoff, only apply doneAction:2 on longest function.
(
{
var sig, freq, env;
env = XLine.kr(1,0.01,5, doneAction:2);
freq = XLine.kr(800,110,1, doneAction:0);
sig = Pulse.ar(freq) * env;
}.play;
)
//Introducing Env. By default, it is a triangle envelope
Env.new.plot;
//Hear default Env
(
{
var sig, env;
env = EnvGen.kr(Env.new, doneAction:2);
sig = Pulse.ar(ExpRand(30,500)) * env;
}.play;
)
//Define custom envelope
Env.new([0,1,0.2,0], [0.5,1,2]).plot;
//New with exponential interpolation, must make 0's small
Env.new([0.01,1,0.2,0.01], [0.5,1,2],\exp).plot;
//Alternatively, input a third vector with curvature values
Env.new([0,1,0.2,0], [0.5,1,2], [3,-3,0]).plot;
Env.new([0,1,0.2,0], [0.5,1,2], [12,-12,0]).plot;
Env.new([0,1,0.2,0], [0.5,1,2], [-3,3,0]).plot;
Env.new([0,1,0.2,0], [0.5,1,2], [\sine,\sine,0]).plot;
//Hear custom Env
(
{
var sig, env;
env = EnvGen.kr(Env.new(
[0,1,0.2,0],
[0.5,1,2],
[3,-3,0]),
doneAction:2);
sig = Pulse.ar(ExpRand(30,500)) * env;
}.play;
)
//Now with gate, which can act as a trigger, which resets the envelope. For gate to trigger, it must move from a non-positive value to positive value.
(
x = {
arg gate = 0;
var sig, env;
env = EnvGen.kr(Env.new(
[0,1,0.2,0],
[0.5,1,2],
[3,-3,0]), gate);
sig = Pulse.ar(LFPulse.kr(8).range(600,900)) * env;
}.play;
)
x.set(\gate, 1);
x.set(\gate, 1); //does not retrigger second time, since gate is already at one. Can set manually, back to zero.
x.set(\gate, 0);
//with trigger argument, which creates a control-rate impulse, which will return value to zero automatically.
(
x = {
arg t_gate = 0;
var sig, env;
env = EnvGen.kr(Env.new(
[0,1,0.2,0],
[0.5,1,2],
[3,-3,0]), t_gate);
sig = Pulse.ar(LFPulse.kr(8).range(600,900)) * env;
}.play;
)
x.set(\gate, 1);
x.set(\gate, 1); //can retrigger now multiple times.
//set t_gate=1 to play upon definition, and it is still retriggerable
(
x = {
arg t_gate = 1;
var sig, env;
env = EnvGen.kr(Env.new(
[0,1,0.2,0],
[0.5,1,2],
[3,-3,0]), t_gate);
sig = Pulse.ar(LFPulse.kr(8).range(600,900)) * env;
}.play;
)
x.set(\gate, 1);
x.set(\gate, 1); //can retrigger now multiple times.
//if use doneAction:2, can only retrigger while envelope is still playing. If it is allowed to finish, it will not be retriggerable.
(
x = {
arg t_gate = 1;
var sig, env;
env = EnvGen.kr(Env.new(
[0,1,0.2,0],
[0.5,1,2],
[3,-3,0]), t_gate, doneAction:2);
sig = Pulse.ar(LFPulse.kr(8).range(600,900)) * env;
}.play;
)
x.set(\gate, 1);
//adsr=attack-decay-sustain-release
(
x = {
arg gate = 0;
var sig, env;
env = EnvGen.kr(Env.adsr, gate);
sig = VarSaw.ar(SinOsc.kr(16).range(500,1000)) * env;
}.play;
)
x.set(\gate, 1);
x.set(\gate, 0);
//if use t_gate, cannot sustain adsr, so best to use regular gate argument
(
x = {
arg t_gate = 0;
var sig, env;
env = EnvGen.kr(Env.adsr, t_gate);
sig = VarSaw.ar(SinOsc.kr(16).range(500,1000)) * env;
}.play;
)
x.set(\t_gate, 1);
(
x = {
arg gate = 0;
var sig, env, freq;
freq = EnvGen.kr(Env.adsr(1), gate, 2000, 0.1);
env = EnvGen.kr(Env.adsr, gate, doneAction:2);
sig = VarSaw.ar(SinOsc.kr(freq).range(500,1000)) * env;
}.play;
)
x.set(\gate, 1);
x.set(\gate, 0);