aboutsummaryrefslogtreecommitdiff
path: root/BUG3/base.csd
blob: e7df1afe271375fc41bfb322a28b7117141f95c1 (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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<CsoundSynthesizer>
<CsOptions>
-g -odac 
-m0
</CsOptions>
<CsInstruments>
/*
	Debugger - Unfixed Bugs : BUG #3

	By Richard Knight 2021
			http://1bpm.net
	mailto:		_@1bpm.net

*/

sr = 44100
ksmps = 128
nchnls = 2
0dbfs = 1
seed 0

#define BUG_VERSION #1.0#


gitempo init -1
gSprepare[] fillarray "hat909"

gSequenced[] fillarray "hat909", "clap909", "kick", "rim", "chord1", "chord2", "hat909c", "mel1", "chord3"

gisectionlengths ftgen 0, 0, -64, -7, 0 
gisections[][] init 64, 64
gisectiondataindex = 0
gisectiondata[][] init 99, 6

gichordgroups[][] init 16, 12
gisectionchords[] init 16

gipreparedfns[] init lenarray(gSprepare)
gipreparedfnindex init 0

gicurrentchordfn init -1
gkcurrentchordlength init -1

gkpump = 0
gkreverse init 0

#include "wavetables.udo"
#include "bussing.udo"
#include "hat909.src"
#include "text_tools.udo"
#include "effects.udo"

gicurrentchordnum = 0
gilastchordgroup = -1
gilastsection = -1
instr setcurrentchord
	isection = p4
	ichordincrement = p5
	ichordgroup = gisectionchords[isection]
	if (ichordgroup != gilastchordgroup || isection != gilastsection) then
		gicurrentchordnum = 0
		gilastchordgroup = ichordgroup
		gilastsection = isection
	elseif (ichordincrement == 1) then
		if (gicurrentchordnum + 1 < lenarray(gichordgroups, 2) - 1) then

			if (gichordgroups[ichordgroup][gicurrentchordnum + 1] == 0) then
				gicurrentchordnum = 0
			else
				gicurrentchordnum += 1
			endif
		else
			gicurrentchordnum = 0
		endif
	else 
		gicurrentchordnum = 0
	endif

	ifn = gichordgroups[ichordgroup][gicurrentchordnum]

	gkcurrentchordlength = tab:k(0, ifn)
	gicurrentchordfn = ifn
	turnoff
endin



instr bootstrap
	iprepareindex = p4
	if (iprepareindex >= lenarray(gSprepare)) then
		notify("Parsing events")
		parsetext("/home/bargepole/bpcheckout/csTech/TechnoPieces/BUG3/source.txt"); "source.txt")
		notify("Running sequencer")
		event_i "i", "sequencer", 0, 3600
		turnoff
	else 
		Sprepareinstr = gSprepare[iprepareindex]
		notify(sprintf("Preparing audio for %s", Sprepareinstr))
		Srcinstr = sprintf("src_%s", Sprepareinstr)
		ilen = 0.3
		p3 = ilen
		ifn ftgen 0, 0, sr*ilen, 7, 0
		gipreparedfns[gipreparedfnindex] = ifn
		gipreparedfnindex += 1
		ktimek timeinstk
		if (ktimek == 1) then
			kcycles = ilen * kr
			kcount init 0
loop:
			apos phasor (1/(ftlen(ifn)/sr))
			aproc subinstr Srcinstr, 1, 0.1
			tabw aproc, apos, ifn, 1
			loop_lt kcount, 1, kcycles, loop
		elseif (ktimek == 5) then
			event "i", "bootstrap", 0, 1, iprepareindex+1
			turnoff
		endif
	endif
endin


opcode arrseq, 0, Skk
	Splayer, kpos, ksection xin
	idel = 0.3 ; delay for chord update
	Sinstrument = sprintf("play_%s", Splayer)
	instrindex = gettextindex(Splayer)
	kdata = gisections[ksection][instrindex] ;ksection

	if (kdata != -1) then ; null section
		kon = tablekt:k(kpos, gisectiondata[kdata][0])
		
		if (kon == 1) then
			kchance = tablekt:k(kpos, gisectiondata[kdata][3])
			if (random:k(0, 1) < kchance) then
				ktime = (kpos % 2 == 0) ? 0 : 0.04
				kdur = tablekt:k(kpos, gisectiondata[kdata][1])
				;kamp = tablekt:k(kpos, gisectiondata[kdata][2])
				event "i", Sinstrument, idel+ktime, kdur, kpos, kdata
			endif		
		endif
	endif
endop


;gintervals1[] fillarray 0, 4, 6, 7, 11
gintervals1[] fillarray 0, 3, 7, 9, 10
instr play_bass
	iamp = getparameter(p4, p5, 2)
	iaugment = (random(0, 1) > 0.9) ? 24 : 36
	inote = tab_i(random(1, ftlen(gicurrentchordfn) - 3), gicurrentchordfn) - iaugment
	a1 foscil 0.5, cpsmidinn(inote), 1, 2, random(0, 6), gifnSine
	a1 pareq a1, 150, 0.24, 0.9
	a1 pareq a1, 80, 1.3, 0.9
	kenv linseg 1, p3, 0
	aout = a1*kenv
	bus_mix("master", aout, aout)
endin

instr play_mel1
	iamp = getparameter(p4, p5, 2)
	ihigher = getparameter(p4, p5, 4)
	iaugment1 = (ihigher == 1 && random(0, 1) > 0.6) ? 12 : 0
	ifreq1 = cpsmidinn(tab_i(random(1, ftlen(gicurrentchordfn) - 1), gicurrentchordfn) + iaugment1)
	ifreq2 = cpsmidinn(tab_i(random(1, ftlen(gicurrentchordfn) - 1), gicurrentchordfn))
	kfreq linseg ifreq1, p3*0.4, ifreq1, p3*0.2, ifreq2, p3*04, ifreq2
	kfreq += oscil(10, 6)
	kcar = abs(oscil(4, 0.001)) + 1
	a1 foscil 0.5, kfreq, kcar, 1, random(0, 6), gifnSine
	a1 butterlp a1, 10000
	a1 butterhp a1, 600
	kenv linseg 0, p3*0.2, random(0.8, iamp), p3*0.6, random(0.8, iamp), p3*0.2, 0
	aL, aR pan2 a1*kenv*0.7, random(0.2, 0.8)
	bus_mix("reverb", aL, aR)
	bus_mix("master", aL, aR)
endin

instr play_chorditem1
	inote = p4
	kfreq1 linseg cpsmidinn(inote-36), p3*0.1, cpsmidinn(inote+12), p3*0.9, cpsmidinn(inote+12)
	kindex linseg 1, p3, 5
	aL1 foscil 0.1, kfreq1 + oscil:k(3, 10), 5, 1, kindex, gifnSquare
	aR1 foscil 0.1, kfreq1 + oscil:k(4, 6), 5, 1, kindex, gifnSquare
	aL1 butterlp aL1, abs(oscil(1000, 3)) + 500
	aR1 butterlp aR1, abs(oscil(1000, 5)) + 500
	
	kfreq2 linseg cpsmidinn(inote-12), p3*0.1, cpsmidinn(inote), p3*0.7, cpsmidinn(inote), p3*0.2, cpsmidinn(inote-36)
	aL2 foscil 0.1, kfreq2 + oscil:k(3, 7), 2, 5, 5-kindex, gifnSine
	aR2 foscil 0.1, kfreq2 + oscil:k(3, 5), 3, 3, 5-kindex, gifnSine
	kenv1 linseg 1, p3*0.8, 1, p3*0.2, 0
	kenv2 linseg 0, p3*0.3, 1, p3*0.69, 1, p3*0.01, 0
	aL butterhp (aL1*kenv1)+(aL2*kenv2), 500
	aR butterhp (aR1*kenv1)+(aR2*kenv2), 500
	
	bus_mix("reverb", aL*0.4, aR*0.4)
	aL *= 1-gkpump
	aR *= 1-gkpump
	bus_mix("master", aL, aR)
endin



instr global_reverb
	aL, aR bus_read "reverb"
	;aL nreverb aL, 2.4, 0.3
	;aR nreverb aR, 2.1, 0.2
	aL, aR freeverb aL, aR, 0.4, 0.3
	;outs aL, aR
	bus_mix("master", aL, aR)
endin


instr play_chorditem2
	inote = p4
	a1 fmbell 0.2, cpsmidinn(inote+12), 1, 3, 0.05, 10, gifnSine, gifnSquare, gifnSine, gifnSquare, gifnSine
	a1 butterhp a1, 700
	kenv linseg 0, p3*0.5, 1, p3*0.4, 1, p3*0.1, 0
	aL, aR ensembleChorus a1*10*kenv, .07, .003, .75, 1, 12, gifnSine
	
	bus_mix("reverb", aL*0.2, aR*0.2)
	aL *= 1-gkpump
	aR *= 1-gkpump
	bus_mix("master", aL, aR)
endin


instr play_chorditem3
	inote = p4 - 12
	aL fmb3 0.2, cpsmidinn(inote), 1, 2, 0.07, 10, gifnSine, gifnSquare, gifnSine, gifnSquare, gifnSine
	aR fmb3 0.2, cpsmidinn(inote), 1, 1.4, 0.02, 10, gifnSine, gifnSine, gifnSine, gifnSquare, gifnSine
	aL butterhp aL, 300
	aR butterhp aR, 300
	kenv linseg 1, p3*0.5, 1, p3*0.4, 1, p3*0.1, 0
	aL *= kenv
	aR *= kenv
	bus_mix("reverb", aL*0.8, aR*0.8)
	bus_mix("master", aL, aR)
endin


instr play_chord1
	iamp = getparameter(p4, p5, 2)
	index = 1
	while (index < ftlen(gicurrentchordfn)) do
		event_i "i", "play_chorditem1", 0, p3, tab_i(index, gicurrentchordfn) 
		index += 1
	od
endin

instr play_chord2
	iamp = getparameter(p4, p5, 2)
	index = 1
	while (index < ftlen(gicurrentchordfn)) do
		event_i "i", "play_chorditem2", 0, p3, tab_i(index, gicurrentchordfn) 
		index += 1
	od
endin

instr play_chord3
	iamp = getparameter(p4, p5, 2)
	index = 1
	while (index < ftlen(gicurrentchordfn)) do
		event_i "i", "play_chorditem3", 0, p3, tab_i(index, gicurrentchordfn) 
		index += 1
	od
endin

/*instr play_chord1
	index = 0
	Sinstrument = sprintf("play_chorditem%d", p4)
	while (index < lenarray(gintervals1)) do
		event_i "i", Sinstrument, 0, p3, gintervals1[index] + 52
		index += 1
	od
endin
*/

instr global_out
	imastergain = 0.7
	aL, aR bus_read "master" 
	;aL butterhp aL, 150
	;aR butterhp aR, 150
	ilooptime = (60/gitempo)*4

	kreverse delayk gkreverse, 0.
	if (kreverse == 1) then
		aL butterhp aL, 150
		aR butterhp aR, 150
		aL, krecL sndloop aL, -1, kreverse, ilooptime, 0
		aR, krecR sndloop aR, -1, kreverse, ilooptime, 0
	endif
	outs aL*imastergain, aR*imastergain
endin

instr sequencer
	isection = 1
	event_i "i", "setcurrentchord", 0, 1, isection
	event_i "i", "global_reverb", 0, p3
	event_i "i", "global_out", 0, p3
	kmetro metro (gitempo/60)*4
	kposabs init 0
	kposchord init 0
	kpos init 0
	ksection init isection
	if (kmetro == 1) then
		

		/*if (kpos == 0) then
			event "i", "play_chord1", 0, 2, 1
		endif
		if (kpos == 10) then
			event "i", "play_chord1", 0, 2, 2
		endif
*/

		;if (random:k(0, 1) > 0.6) then
		;	event "i", "play_bass", (kpos % 2 == 0) ? 0 : 0.04, 0.1
		;endif

		if (random:k(0, 1) > 0.6) then
			;event "i", "play_fm2", (kpos % 2 == 0) ? 0 : 0.04, 0.1
		endif

		arrseq "chord1", kpos, ksection
		arrseq "chord2", kpos, ksection
		arrseq "chord3", kpos, ksection
		arrseq "bass", kpos, ksection
		arrseq "hat909", kpos, ksection
		arrseq "hat909c", kpos, ksection
		arrseq "clap909", kpos, ksection
		arrseq "kick", kpos, ksection
		arrseq "rim", kpos, ksection
		arrseq "mel1", kpos, ksection

		if (kpos < 31) then
			kpos += 1
		else
			kpos = 0
		endif
		if (kposabs+16 > tab:k(ksection, gisectionlengths)*4) then

			gkreverse = 1
		endif
		
		if (kposabs < tab:k(ksection, gisectionlengths)*4) then
			kposabs += 1
		else
			gkreverse = 0
			ksection += 1
			kposabs = 0
			kposchord = 0
			event "i", "play_crash", 0, 1
			event "i", "setcurrentchord", 0, 1, ksection, 0
		endif
		
		if (kposchord < gkcurrentchordlength*4) then
			kposchord += 1
		else
			kposchord = 0
			event "i", "setcurrentchord", 0, 1, ksection, 1
		endif
		
	endif
endin	

instr play_rim
	iamp = getparameter(p4, p5, 2)
	icps = 490*exp(log(2.0)*(57.0-69.0)/12.0)
	acps expon icps, 0.0025, icps * 0.5
	acps = acps + icps
	iamp = 1
	a1a phasor acps, 0.0	
	a1b phasor acps, 0.5
	afmenv expon 1.0, 0.02, 0.5
	a1 = (a1a-a1b)*6.0*afmenv
	acps = acps*(1.0+a1)
	a0 oscil3 1.0, acps
	a1 unirand 2.0
	a1 tone a1-1.0, 2000
	a0 = a0 + a1*0.1
	aenv expon 1.0, 0.005, 0.5

	a0 limit 4.0*iamp*a0*aenv, -1.0, 1.0
	a0 table3 a0*4096.0, gifnSine, 0, 0, 1

	kffrq expseg 2000, 0.07, 100, 1, 100

	a0x tone a0, 10000
	a0y = a0 - a0x
	a0x delay a0y, 0.0002
	a0 = a0 - a0x*4.0
	a0 pareq a0, kffrq, 0, 0.7071, 2

	a_ linseg 1, p3-0.1, 1, 0.025, 0, 1, 0
	a0 = a0*a_
	aL, aR pan2 a0*iamp, random(0.3, 0.8)

	bus_mix("reverb", aL*0.3, aR*0.3)
	bus_mix("master", aL, aR)
endin


instr play_hat909
	iamp = getparameter(p4, p5, 2)
	index = 0
	ifn = gipreparedfns[index]
	apos phasor (1/(ftlen(ifn)/sr))
	aout1 table3 apos*0.4, ifn, 1
	aout2 table3 apos*0.9, ifn, 1
	aout3 table3 apos*1.1, ifn, 1
	amix1 = delay(aout1*0.6, 0.02) + delay(aout2*0.8, 0.01)
	amix2 = delay(aout1*0.8, 0.01) + delay(aout2*0.6, 0.02)
	aL = (aout3 + amix1 + (amix2 * 0.3))*0.5*iamp
	aR = (aout3 + amix2 + (amix1 * 0.3))*0.5*iamp
	bus_mix("reverb", aL*0.1, aR*0.1)
	bus_mix("master", aL, aR)
endin


instr play_hat909c
	iamp = getparameter(p4, p5, 2)
	index = 0
	ifn = gipreparedfns[index]
	apos phasor (1/(ftlen(ifn)/sr))
	aL table3 apos*0.65, ifn, 1
	aR table3 apos*0.76, ifn, 1
	aL *= iamp * 0.6
	aR *= iamp * 0.6
	aL *= 1-gkpump
	aR *= 1-gkpump
	bus_mix("reverb", aL*0.1, aR*0.1)
	bus_mix("master", aL, aR)
endin


instr play_crash
	iamp = 1
	index = 0
	ifn = gipreparedfns[index]
	apos phasor (1/(ftlen(ifn)/sr))
	aL table3 apos, ifn, 1
	aR table3 apos, ifn, 1
	aL reverb aL, 8
	aR reverb aR, 8
	kfreq line 1400, p3, 100
	aL *= oscil(0.9, kfreq) * iamp * 0.2 
	aR *= oscil(0.9, kfreq) * iamp * 0.2
	;aL *= 1-gkpump
	;aR *= 1-gkpump
	bus_mix("reverb", aL*0.9, aR*0.9)
	bus_mix("master", aL, aR)
endin


instr play_clap909
	iamp = getparameter(p4, p5, 2)
	ilowpass = getparameter(p4, p5, 4)
	p3 = 0.065
	aL noise 1, 0.9
	aR noise 0.5, 0.4
	aL butterbp aL, 1200, 1100
	aR butterbp aR, 1200, 1100
	irevsend = 0.2
	if (ilowpass == 1) then
		aL butterlp aL, 900
		aR butterlp aR, 900
		irevsend = 0.6
	endif
	kamp1 linseg 1, p3*0.1, 0, p3*0.1, 1, p3*0.2, 0.1, p3*0.3, 0.5, p3*0.2, 0, p3*0.05, 0.4, p3*0.05, 0
	kamp2 linseg 1, p3*0.15, 0, p3*0.15, 1, p3*0.1, 0.1, p3*0.2, 0.5, p3*0.3, 0, p3*0.05, 0.4, p3*0.05, 0

	aL = aL*kamp1*9*iamp
	aR = aR*kamp2*9*iamp

	bus_mix("reverb", aL*irevsend, aR*irevsend)
	bus_mix("master", aL, aR)
endin

instr play_kick	; 808 style kick
	iamp = getparameter(p4, p5, 2)
	xtratim    0.1
	krelease release
	ktune init 0
	kmul transeg 0.2, p3*0.5, -15, 0.01, p3*0.5, 0, 0
	kbend transeg 0.5, 1.2, -4, 0, 1, 0, 0
	asig gbuzz 0.5, 50*octave(ktune)*semitone(kbend), 20, 1, kmul, gifnCosine
	aenv transeg 1, p3-0.004, -6, 0
	gkpump = k(aenv)
	aatt linseg 0, 0.004, 1
	asig = asig*aenv*aatt
	aenv linseg 1, 0.07, 0
	acps expsega 400, 0.07, 0.001, 1, 0.001
	aimp oscili aenv, acps*octave(ktune*0.25)
	amix = ((asig*0.7)+(aimp*0.35))*2*iamp
	gkpump = min(rms(amix) * 7, 1)
	amix distort amix, 0.4, gifnSine
	;amix butterhp amix, 150
	bus_mix("master", amix, amix)
endin


</CsInstruments>
<CsScore>
i"bootstrap" 0 1 0

</CsScore> 
</CsoundSynthesizer>