-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment4_SDPowerPlantFunctions.ck
373 lines (314 loc) · 10.7 KB
/
Assignment4_SDPowerPlantFunctions.ck
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Assignment 4: SD Power Plant
/* Title: Elevator Lounge
Author: Brian Harrington
Assignment: 4 - Functions
Soundcloud link: https://soundcloud.com/destruction_synthesis/assignment4
*/
// requirements:
// 30 seconds
// only midi notes in Eb Mixolydian mode: 51, 53, 55, 56, 58, 60, 61, 63
// quarter notes 0.6::second long
// Use of Osc, SndBuf, if / else, for / while, vars, comments,
// Std.mtof(), random nums, Arrays, panning, >= 3 functions
0 => int debug; // flag used for debugging purposes. set to 0 to hide all the text gen
<<< "Assignment 3: San Diego Power Plant" >>>; // just in case you didn't know
[51, 53, 55, 56, 58, 60, 61, 63] @=> int notes[];
// setup durations
2.4 => float wLen; // whole note length of time
wLen::second => dur w; //hole
w / 2 => dur h; //alf
w / 4 => dur q; //uarter, 2.4 / 4 = 0.6
w / 8 => dur e; //ighth
w / 16 => dur s; //ixteenth
w / 32 => dur t; //hirtysecond
// load filenames
me.dir() + "/audio/" => string filepath;
string clickFn[6]; // there are only 5 clicks, but leaving index 0 blank to make it easier to remember
string hihatFn[5];
string kickFn[6];
string snareFn[4];
string stereofxFn[6];
for (1 => int i; i < 6; i++){
if (i < clickFn.cap()){ // only add to the array if there's room
filepath + "click_0" + i + ".wav" => clickFn[i];
if(debug) <<< clickFn[i] >>>; // for debugging, you'll see this a lot more below
}
if (i < hihatFn.cap()){
filepath + "hihat_0" + i + ".wav" => hihatFn[i];
if(debug) <<< hihatFn[i] >>>;
}
if (i < kickFn.cap()){
filepath + "kick_0" + i + ".wav" => kickFn[i];
if(debug) <<< kickFn[i] >>>;
}
if (i < snareFn.cap()){
filepath + "snare_0" + i + ".wav" => snareFn[i];
if(debug) <<< snareFn[i] >>>;
}
if (i < stereofxFn.cap()){
filepath + "stereo_fx_0" + i + ".wav" => stereofxFn[i];
if(debug) <<< stereofxFn[i] >>>;
}
}
// id's
"snare" => string snare;
"kick" => string kick;
"hh" => string hh; // hihat
"hho" => string hho; //hihat open
"hclick" => string hc; // hi click
"lclick" => string lc; // lo click
"mel" => string mel;
"bass" => string bass;
"chord" => string chord;
// set up sound buffers
int inst[0]; // instrument indices
0 => inst[kick];
1 => inst[snare];
2 => inst[hh]; // hihat
3 => inst[hho]; // hihat open
4 => inst[hc];
5 => inst[lc];
6 => inst[mel]; // melody, some osc
7 => inst[bass]; // bass
8 => inst[chord];
SndBuf sndbuf[6];
Pan2 pan[sndbuf.cap() + 3]; // sndbuf.cap() + mel & bass
SinOsc chordOsc[3];
for (0 => int i; i < chordOsc.cap(); i++){
chordOsc[i] => pan [ inst[ chord ] ]; // send to the same pan
1.0 / 3 => chordOsc[i].gain; // set the gain, chordOsc main will me [0]
}
TriOsc bassOsc => pan [ inst[ bass ] ];
SqrOsc melOsc => pan[ inst [mel] ];
0 => melOsc.gain;
// set up sound chains
Gain masterL => dac.left;
Gain masterR => dac.right;
for (0 => int i; i < pan.cap(); i++){
if (i < sndbuf.cap()){ // only chuck existing SndBufs
if (debug) { <<< "chuck sndbuf", i, "to pan" >>>; }
sndbuf[i] => pan[i];
}
if (debug) { <<< "chuck pan", i, "to master" >>>; }
// chuck each side of the pan to the corresponding master channel
pan[i].left => masterL;
pan[i].right => masterR;
}
// load sound files into SndBuf's
kickFn[1] => sndbuf[ inst[ kick ] ].read;
snareFn[2] => sndbuf[ inst[ snare ] ].read;
hihatFn[2] => sndbuf[ inst[ hh ] ].read;
hihatFn[3] => sndbuf[ inst[ hho ] ].read;
clickFn[2] => sndbuf[ inst[ hc ] ].read;
clickFn[1] => sndbuf[ inst[ lc ] ].read;
// set positions and volumes for drums as they won't change
1 => sndbuf[ inst[ kick ] ].gain;
1 => sndbuf[ inst[ snare ] ].gain;
0.25 => sndbuf[ inst[ hh ] ].gain;
0.25 => sndbuf[ inst[ hho ] ].gain;
-0.5 => pan[ inst[ hh ] ].pan => pan[ inst[ hho ] ].pan;
0.5 => pan[ inst[ snare ] ].pan;
// prevent all sndbufs from playing
for (0 => int i; i < sndbuf.cap(); i++){
sndbuf[i].samples() => sndbuf[i].pos;
}
/***** Fuctions *****/
// separate functions to set up melodies / chords,
// won't have any timing built in, will be taken care of by the drums,
// but will get note params
// drums functions, pass durs in to determine how long to run
[1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0] @=> int kick_ptn[];
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1] @=> int snare_ptn[];
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2] @=> int hh_ptn[];
0::second => dur accrued; // total time accrued
0 => int beat; // what beat we're on
-1 => int lastBeat; // keep track if we increment to the next element in the pattern
0 => int canReset; // prevent endless repositioning if we stay on the same position for a while
// drum function:
fun void drumFor(dur len){
Math.min(kick_ptn.cap(), Math.min(snare_ptn.cap(), hh_ptn.cap())) $ int => int al; // smallest array length
t/al => dur resolution; // resolution, must be <= smallest note, div by array size to make even
// get the length of the array easier calcs
while ( len > 0::second ) {
// index is the ((accrued / ((wLen/al)::second)) $ int) % al
// ((wLen/al)::second)) is the fraction of the array we've been through,
// cast to an int to get a valid index (floor of the float), mod by al to loop.
// this allows the time signature to be flexible.
((accrued / ((wLen/al)::second)) $ int) % al => beat;
//if (debug) { <<< "Beat:", beat >>>; }
if (lastBeat != beat){ // the beat incremented
1 => canReset;
beat => lastBeat;
}
if (canReset && kick_ptn[beat] == 1){
if (debug) { <<< "=Kick" >>>; }
0 => sndbuf [ inst[ kick ] ].pos;
}
if (canReset && snare_ptn[beat] == 1){
if (debug) { <<< "==Snare" >>>; }
0 => sndbuf [ inst[ snare ] ].pos;
}
if(canReset){
if (debug) { <<< "click", hh_ptn[beat] & 1, hh_ptn[beat] & 2 >>>; }
if ( (hh_ptn[beat] & 0x1) > 0 ){ // bitwise ops let me put more instruments in
if (debug) { <<< "===HiHat", hh_ptn[beat] & 1 >>>; }
0 => sndbuf [ inst[ hh ] ].pos;
sndbuf [ inst[ hho ] ].samples() => sndbuf [ inst[ hho ] ].pos; // stop the hho
}
if ( (hh_ptn[beat] & 0x2) > 0){
if (debug) { <<< "===HiHatOpen", hh_ptn[beat] & 2 >>>; }
0 => sndbuf [ inst[ hho ] ].pos;
}
if ( (hh_ptn[beat] & 0x4) > 0){
if (debug) { <<< "===Lo Click", hh_ptn[beat] & 4 >>>; }
0 => sndbuf [ inst[ lc ] ].pos;
}
if ( (hh_ptn[beat] & 0x8) > 0){
if (debug) { <<< "===Hi Click", hh_ptn[beat] & 8 >>>; }
0 => sndbuf [ inst[ hc ] ].pos;
}
}
// set the flag to false after all checks have been made
0 => canReset;
// let time fly
resolution => now;
// account for the time spent
accrued + resolution => accrued;
len - resolution => len;
}
}
// sets all the melody oscs to the same level / 3
fun void setChordGain(float gain){
for (0 => int i; i < chordOsc.cap(); i++){
gain / 3 => chordOsc[i].gain;
}
}
// sets a chord based on the melody root note
fun void setChord(int rootNote, string flavor){
4 => int mid;
if (flavor == "minor") {
3 => mid;
} else { // default to major
}
Std.mtof(rootNote) => chordOsc[0].freq;
Std.mtof(rootNote + mid) => chordOsc[1].freq;
Std.mtof(rootNote + 7) => chordOsc[2].freq;
}
fun void setBassNote(int note){
Std.mtof(note-24) => bassOsc.freq;
}
fun void setBassGain(float gain){
gain => bassOsc.gain;
}
fun void setBassNote(int note, float vol){
setBassNote(note);
vol/2 => bassOsc.gain;
}
// helper functions
fun void setMasterGain(float gain){
gain => masterL.gain;
gain => masterR.gain;
}
fun void setPan(Pan2 p, float dir){
if (dir > 1){
1 => p.pan;
} else if (dir < -1){
-1 => p.pan;
} else {
dir => p.pan;
}
}
fun void setMelN(int note){ // set melody note
Std.mtof(note) => melOsc.freq;
}
fun void setMelNV(int note, float gain){ // note and vol
Std.mtof(note) => melOsc.freq;
gain => melOsc.gain;
}
fun void setMelNVP(int note, float gain, float p){ // note, vol, pan
Std.mtof(note) => melOsc.freq;
gain => melOsc.gain;
Math.sin(p) => pan [inst [mel] ].pan;
}
fun void setMelNP(int note, float p){ // note and pan
Std.mtof(note) => melOsc.freq;
Math.sin(p) => pan [inst [mel] ].pan;
}
fun void setPattern(string which, int pattern[]){
if (which == snare){
pattern @=> snare_ptn;
} else if (which == hh){
pattern @=> hh_ptn;
} else {
pattern @=> kick_ptn;
}
}
// setup
"major" => string major;
"minor" => string minor;
/***** Main function ****/
setChordGain(1);
0.7 => bassOsc.gain;
//now + 3::second => time future;
0 => int count;
// intro
while( count < 2 ) {
// call the functions
setChord(notes[0], major);
setBassNote(notes[0]);
drumFor(w);
setChord(notes[5], major);
setBassNote(notes[5]);
drumFor(h);
setChord(notes[6], minor);
drumFor(q);
setChord(notes[3], major);
setBassNote(notes[3]);
if ( count == 1) break;
drumFor(q);
++count;
}
setChordGain(0);
setBassGain(0);
setPattern(snare, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
setPattern(kick, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
setPattern(hh, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
drumFor(q);
setPattern(snare, [0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1]);
setPattern(kick, [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0]);
setPattern(hh, [1, 5, 1, 10, 1, 5, 1, 1, 1, 1, 1, 2]);
snare_ptn.cap() => int len;
setChordGain(1);
setBassGain(1);
0 => count;
while (count < 4 ){ // change it up
setChord(notes[0], major);
setBassNote(notes[0]);
setMelNVP(notes[0], 0.07, Math.random());
drumFor(q);
setMelN(notes[7]);
drumFor(q);
setMelNV(0, 0);
setChord(notes[2], minor);
setBassNote(notes[2]);
drumFor(h); // rest melody
setChord(notes[4], major);
setBassNote(notes[1]);
drumFor(q-(3*(w/len)));
setMelNVP(notes[0]+12, 0.07, Math.random()); // pan?
drumFor(w/len); // putting notes on the beat
setMelNP(notes[1]+12, Math.random());
drumFor(w/len);
setMelNP(notes[2]+12, Math.random());
drumFor(w/len);
setMelNP(notes[1]+12, Math.random());
drumFor(q);
setMelNV(0, 0);
setChord(notes[6], major);
setBassNote(notes[6]-12);
drumFor(q+(3*(w/len)));
++count;
}
setChord(notes[0], major);
setBassNote(notes[0]);
w => now;