-
Notifications
You must be signed in to change notification settings - Fork 15
/
livecode.orc
2226 lines (1750 loc) · 62.6 KB
/
livecode.orc
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Live Coding Functions
Author: Steven Yi
*/
instr S1
ifreq = p4
iamp = p5
endin
instr P1
ibeat = p4
endin
;; TIME
gk_tempo init 120
/** Set tempo of global clock to itempo value in beats per minute. */
opcode set_tempo,0,i
itempo xin
gk_tempo init itempo
endop
/** Returns tempo of global clock in beats per minute. */
opcode get_tempo,i,0
xout i(gk_tempo)
endop
/** Adjust tempo of global clock towards by inewtempo by incr amount. */
opcode go_tempo, 0, ii
inewtempo, incr xin
icurtempo = i(gk_tempo)
itemp init icurtempo
if(inewtempo > icurtempo) ithen
itemp = min:i(inewtempo, icurtempo + abs(incr))
gk_tempo init itemp
elseif (inewtempo < icurtempo) ithen
itemp = max:i(inewtempo, icurtempo - abs(incr))
gk_tempo init itemp
endif
endop
instr Perform
ibeat = p4
schedule("P1", 0, p3, ibeat)
endin
gk_clock_internal init 0
gk_clock_tick init 0
gk_now init 0
/** Returns value of now beat time
(Code used from Thorin Kerr's LivecodeLib.csd) */
opcode now, i, 0
xout i(gk_now)
endop
/** Returns current clock tick at init time */
opcode now_tick, i, 0
xout i(gk_clock_tick)
endop
/** Returns duration of time in given number of beats (quarter notes) */
opcode beats, i, i
inumbeats xin
ibeatdur = divz(60, i(gk_tempo), -1)
xout ibeatdur * inumbeats
endop
/** Returns duration of time in given number of measures (4 quarter notes) */
opcode measures, i, i
inummeasures xin
xout beats(inummeasures * 4)
endop
/** Returns duration of time in given number of ticks (16th notes) */
opcode ticks, i, i
inumbeats xin
ibeatdur = divz(60, i(gk_tempo), -1)
ibeatdur = ibeatdur / 4
xout ibeatdur * inumbeats
endop
/** Returns time from now for next beat, rounding to align
on beat boundary.
(Code used from Thorin Kerr's LivecodeLib.csd) */
opcode next_beat, i, p
ibeatcount xin
inow = now()
ibc = frac(ibeatcount)
inudge = int(ibeatcount)
iresult = inudge + ibc + (round(divz(inow, ibc, inow)) * (ibc == 0 ? 1 : ibc)) - inow
xout beats(iresult)
endop
/** Returns time from now for next measure, rounding to align to measure
boundary. */
opcode next_measure, i,0
inow = now() % 4
ival = 4 - inow
if(ival < 0.25) then
ival += 4
endif
inext = beats(ival)
xout inext
endop
/** Reset clock so that next tick starts at 0 */
opcode reset_clock, 0, 0
gk_clock_internal init 0
gk_clock_tick init -1
gk_now init -(ksmps / sr)
endop
/** Adjust clock by iadjust number of beats.
Value may be positive or negative. */
opcode adjust_clock, 0, i
iadjust xin
gk_now init i(gk_now) + iadjust
endop
instr Clock ;; our clock
;; tick at 1/16th note
kfreq = (4 * gk_tempo) / 60 ;; frequency of 16th note
kdur = 1 / kfreq ;; duration of 16th note in seconds
kstep = (gk_tempo / 60) / kr ;; step size in quarter notes per buffer
kstep16th = kfreq / kr ;; step size in 16th notes per buffer
gk_now += kstep ;; advance beat clock
gk_clock_internal += kstep16th ;; advance 16th note clock
// checks if next buffer will be one where clock will
// trigger. If so, then schedule event for time 0
// which will get processed next buffer.
if(gk_clock_internal + kstep16th >= 1.0 ) then
gk_clock_internal -= 1.0
gk_clock_tick += 1
event("i", "Perform", 0, kdur, gk_clock_tick)
endif
endin
;; Randomization
/** Given a random chance value between 0 and 1, calculates a random value and
returns 1 if value is less than chance value. For example, giving a value of 0.7,
it can read as "70 percent of time, return 1; else 0" */
opcode choose, i, i
iamount xin
ival = 0
if(random(0,1) < limit:i(iamount, 0, 1)) then
ival = 1
endif
xout ival
endop
;; Array Functions
/** Cycles through karray using index. */
opcode cycle, i, ik[]
indx, kvals[] xin
ival = i(kvals, indx % lenarray(kvals))
xout ival
endop
/** Checks to see if item exists within array. Returns 1 if
true and 0 if false. */
opcode contains, i, ii[]
ival, iarr[] xin
indx = 0
iret = 0
while (indx < lenarray:i(iarr)) do
if (iarr[indx] == ival) then
iret = 1
igoto end
endif
indx += 1
od
end:
xout iret
endop
/** Checks to see if item exists within array. Returns 1 if
true and 0 if false. */
opcode contains, i, ik[]
ival, karr[] xin
indx = 0
iret = 0
while (indx < lenarray:i(karr)) do
if (i(karr,indx) == ival) then
iret = 1
igoto end
endif
indx += 1
od
end:
xout iret
endop
/** Create a new array by removing all instances of a
given number from an existing array. */
opcode remove, k[], ik[]
ival, karr[] xin
ifound = 0
indx = 0
while (indx < lenarray:i(karr)) do
if(i(karr, indx) == ival) then
ifound += 1
endif
indx += 1
od
kout[] init (lenarray:i(karr) - ifound)
indx = 0
iwriteIndx = 0
while (indx < lenarray:i(karr)) do
iv = i(karr, indx)
if(iv != ival) then
kout[iwriteIndx] init iv
iwriteIndx += 1
endif
indx += 1
od
xout kout
endop
/** Returns random item from karray. */
opcode rand, i, k[]
kvals[] xin
indx = int(random(0, lenarray(kvals)))
ival = i(kvals, indx)
xout ival
endop
/** Returns random item from String array. */
opcode rand, S, S[]
Svals[] xin
indx = int(random(0, lenarray(Svals)))
Sval = Svals[indx]
xout Sval
endop
/** Returns random item from karray. */
opcode randk, k, k[]
kvals[] xin
kndx = int(random:k(0, lenarray:k(kvals)))
kval = kvals[kndx]
xout kval
endop
/** Returns random item from karray. */
opcode randk, S, S[]
Svals[] xin
kndx = int(random:k(0, lenarray:k(Svals)))
Sval = Svals[kndx]
xout Sval
endop
;; Event
/** Wrapper opcode that calls schedule only if iamp > 0 and ifreq > 0. */
opcode cause, 0, Siiii
Sinstr, istart, idur, ifreq, iamp xin
if(ifreq > 0 && iamp > 0) then
schedule(Sinstr, istart, idur, ifreq, iamp)
endif
endop
;; Beats
/** Given a hexadecimal beat string pattern and optional
itick (defaults to current now_tick()), returns value 1 if
the given tick matches a hit in the hexadecimal beat, or
returns 0 otherwise. */
opcode hexbeat, i, So
Spat, itick xin
if(itick <= 0) then
itick = now_tick()
endif
istrlen = strlen(Spat)
iout = 0
if (istrlen > 0) then
;; 4 bits/beats per hex value
ipatlen = strlen(Spat) * 4
;; get beat within pattern length
itick = itick % ipatlen
;; figure which hex value to use from string
ipatidx = int(itick / 4)
;; figure out which bit from hex to use
ibitidx = itick % 4
;; convert individual hex from string to decimal/binary
ibeatPat = strtol(strcat("0x", strsub(Spat, ipatidx, ipatidx + 1)))
;; bit shift/mask to check onset from hex's bits
iout = (ibeatPat >> (3 - ibitidx)) & 1
endif
xout iout
endop
/** Given hex beat pattern, use given itick to fire
events for given instrument, duration, frequency, and
amplitude */
opcode hexplay, 0, SiSiip
Spat, itick, Sinstr, idur, ifreq, iamp xin
if(ifreq > 0 && iamp > 0 && strlen(Sinstr) > 0 && hexbeat(Spat, itick) == 1) then
schedule(Sinstr, 0, idur, ifreq, iamp )
endif
endop
/** Given hex beat pattern, use global clock to fire
events for given instrument, duration, frequency, and
amplitude */
opcode hexplay, 0, SSiip
Spat, Sinstr, idur, ifreq, iamp xin
itick = i(gk_clock_tick)
if(ifreq > 0 && iamp > 0 && strlen(Sinstr) > 0 && hexbeat(Spat, itick) == 1) then
schedule(Sinstr, 0, idur, ifreq, iamp )
endif
endop
/** Given an octal beat string pattern and optional
itick (defaults to current now_tick()), returns value 1 if
the given tick matches a hit in the octal beat, or
returns 0 otherwise. */
opcode octalbeat, i, Si
Spat, itick xin
;; 3 bits/beats per octal value
ipatlen = strlen(Spat) * 4
;; get beat within pattern length
itick = itick % ipatlen
;; figure which octal value to use from string
ipatidx = int(itick / 3)
;; figure out which bit from octal to use
ibitidx = itick % 3
;; convert individual octal from string to decimal/binary
ibeatPat = strtol(strcat("0", strsub(Spat, ipatidx, ipatidx + 1)))
;; bit shift/mask to check onset from hex's bits
xout (ibeatPat >> (2 - ibitidx)) & 1
endop
opcode octalplay, 0, SiSiip
Spat, ibeat, Sinstr, idur, ifreq, iamp xin
if(octalbeat(Spat, ibeat) == 1) then
schedule(Sinstr, 0, idur, ifreq, iamp )
endif
endop
opcode octalplay, 0, SSiip
Spat, Sinstr, idur, ifreq, iamp xin
itick = i(gk_clock_tick)
if(octalbeat(Spat, itick) == 1) then
schedule(Sinstr, 0, idur, ifreq, iamp )
endif
endop
;; Phase Functions
/** Given count and period, return phase value in range [0-1) */
opcode phs, i, ii
icount, iperiod xin
xout (icount % iperiod) / iperiod
endop
/** Given period in ticks, return current phase of global
clock in range [0-1) */
opcode phs, i, i
iticks xin
xout (i(gk_clock_tick) % iticks) / iticks
endop
/** Given period in beats, return current phase of global
clock in range [0-1) */
opcode phsb, i, i
ibeats xin
iticks = ibeats * 4
xout (i(gk_clock_tick) % iticks) / iticks
endop
/** Given period in measures, return current phase of global
clock in range [0-1) */
opcode phsm, i, i
imeasures xin
iticks = imeasures * 4 * 4
xout (i(gk_clock_tick) % iticks) / iticks
endop
;; Iterative Euclidean Beat Generator
;; Returns string of 1 and 0's
opcode euclid_str, S, ii
ihits, isteps xin
Sleft = "1"
Sright = "0"
ileft = ihits
iright = isteps - ileft
while iright > 1 do
if (iright > ileft) then
iright = iright - ileft
Sleft = strcat(Sleft, Sright)
else
itemp = iright
iright = ileft - iright
ileft = itemp
Stemp = Sleft
Sleft = strcat(Sleft, Sright)
Sright = Stemp
endif
od
Sout = ""
indx = 0
while (indx < ileft) do
Sout = strcat(Sout, Sleft)
indx += 1
od
indx = 0
while (indx < iright) do
Sout = strcat(Sout, Sright)
indx += 1
od
xout Sout
endop
/** Given number of ihits for a period of isteps and an optional
itick (defaults to current now_tick()), returns value 1 if
the given tick matches a hit in the euclidean rhythm, or
returns 0 otherwise. */
opcode euclid, i, iio
ihits, isteps, itick xin
if(itick <= 0) then
itick = now_tick()
endif
Sval = euclid_str(ihits, isteps)
indx = itick % strlen(Sval)
xout strtol(strsub(Sval, indx, indx + 1))
endop
opcode euclidplay, 0, iiiSiip
ihits, isteps, itick, Sinstr, idur, ifreq, iamp xin
if(euclid(ihits, isteps, itick) == 1) then
schedule(Sinstr, 0, idur, ifreq, iamp)
endif
endop
opcode euclidplay, 0, iiSiip
ihits, isteps, Sinstr, idur, ifreq, iamp xin
itick = i(gk_clock_tick)
if(euclid(ihits, isteps, itick) == 1) then
schedule(Sinstr, 0, idur, ifreq, iamp)
endif
endop
;; Phase-based Oscillators
/** Returns cosine of given phase (0-1.0) */
opcode xcos, i,i
iphase xin
xout cos(2 * $M_PI * iphase)
endop
/** Range version of xcos, similar to Impromptu's cosr */
opcode xcos, i,iii
iphase, ioffset, irange xin
xout ioffset + (cos(2 * $M_PI * iphase) * irange)
endop
/** Returns sine of given phase (0-1.0) */
opcode xsin, i,i
iphase xin
xout sin(2 * $M_PI * iphase)
endop
/** Range version of xsin, similar to Impromptu's sinr */
opcode xsin, i,iii
iphase, ioffset, irange xin
xout ioffset + (sin(2 * $M_PI * iphase) * irange)
endop
/** Non-interpolating oscillator. Given phase in range 0-1,
returns value within the give k-array table. */
opcode xosc, i, ik[]
iphase, kvals[] xin
indx = int(lenarray:i(kvals) * (iphase % 1))
xout i(kvals, indx)
endop
/** Non-interpolating oscillator. Given phase duration in beats,
returns value within the give k-array table. (shorthand for xosc(phsb(ibeats), karr) )*/
opcode xoscb, i,ik[]
ibeats, kvals[] xin
xout xosc(phsb(ibeats), kvals)
endop
/** Non-interpolating oscillator. Given phase duration in measures,
returns value within the give k-array table. (shorthand for xosc(phsm(ibeats), karr) )*/
opcode xoscm, i, ik[]
ibeats, kvals[] xin
xout xosc(phsm(ibeats), kvals)
endop
/** Linearly-interpolating oscillator. Given phase in range 0-1,
returns value intepolated within the two closest points of phase within k-array
table. */
opcode xosci, i, ik[]
iphase, kvals[] xin
ilen = lenarray:i(kvals)
indx = ilen * (iphase % 1)
ibase = int(indx)
ifrac = indx - ibase
iv0 = i(kvals, ibase)
iv1 = i(kvals, (ibase + 1) % ilen)
xout iv0 + (iv1 - iv0) * ifrac
endop
/** Linearly-interpolating oscillator. Given phase duration in beats,
returns value intepolated within the two closest points of phase within k-array
table. (shorthand for xosci(phsb(ibeats), karr) )*/
opcode xoscib, i,ik[]
ibeats, kvals[] xin
xout xosci(phsb(ibeats), kvals)
endop
/** Linearly-interpolating oscillator. Given phase duration in measures,
returns value intepolated within the two closest points of phase within k-array
table. (shorthand for xosci(phsm(ibeats), karr) )*/
opcode xoscim, i,ik[]
ibeats, kvals[] xin
xout xosci(phsm(ibeats), kvals)
endop
/** Line (Ramp) oscillator. Given phase in range 0-1, return interpolated value between given istart and iend. */
opcode xlin, i, iii
iphase, istart, iend xin
xout istart + (iend - istart) * iphase
endop
;; Duration Sequences
/** Given a tick value and array of durations, returns new duration value for tick. */
opcode xoscd, i, ik[]
itick, kdurs[] xin
indx = 0
isum = 0
ilen = lenarray:i(kdurs)
ival = 0
while (indx < ilen) do
isum += i(kdurs, indx)
indx += 1
od
itick = itick % isum
indx = 0
ival = 0
icur = 0
while (indx < ilen) do
itemp = i(kdurs, indx)
if(itick < icur + itemp) then
ival = itemp
indx += ilen
else
icur += abs(itemp)
endif
indx += 1
od
xout ival
endop
/** Given an array of durations, returns new duration value for current clock tick. Useful with mod division and cycle for additive/subtractive rhythms. */
opcode xoscd, i, k[]
kdurs[] xin
xout xoscd(now_tick(), kdurs)
endop
/** Given a tick value and array of durations, returns new duration or 0 depending upon whether tick hits a new duration value. Values
may be positive or negative, but not zero. Negative values can be interpreted as rest durations. */
opcode dur_seq, i, ik[]
itick, kdurs[] xin
ival = 0
icur = 0
ilen = lenarray:i(kdurs)
itotal = 0
indx = 0
while (indx < ilen) do
itotal += abs:i(i(kdurs, indx))
indx += 1
od
;print itotal
indx = 0
itick = itick % itotal
while (indx < ilen) do
itemp = i(kdurs, indx)
if(icur == itick) then
ival = itemp
indx += ilen
elseif (icur > itick) then
indx += ilen
else
icur += abs(itemp)
endif
indx += 1
od
xout ival
endop
/** Given an array of durations, returns new duration or 0 depending upon
* whether current clock tick hits a new duration value. Values
may be positive or negative, but not zero. Negative values can be interpreted
as rest durations. */
opcode dur_seq, i, k[]
kdurs[] xin
xout dur_seq(now_tick(), kdurs)
endop
/** Experimental opcode for generating melodic lines given array of durations, pitches, and amplitudes. Durations follow dur_seq practice that negative values are rests. Pitch and amp array indexing wraps according to their array lengths given index of non-rest duration value currently fired. */
opcode melodic, iii, ik[]k[]k[]
itick, kdurs[], kpchs[], kamps[] xin
idur = dur_seq(itick, kdurs)
ipch = 0
iamp = 0
indx = 0
itotal = 0
ilen = lenarray:i(kdurs)
while (indx < ilen) do
itotal += abs:i(i(kdurs, indx))
indx += 1
od
itick = itick % itotal
if(idur > 0) then
indx = 0
icur = 0
ivalindx = 0
while (indx < ilen) do
itemp = i(kdurs, indx)
if(icur == itick) then
indx += ilen
elseif (icur > itick) then
indx += ilen
else
if (itemp > 0) then
ivalindx += 1
endif
icur += abs(itemp)
endif
indx += 1
od
ipch = i(kpchs, ivalindx % lenarray:i(kpchs))
iamp = i(kamps, ivalindx % lenarray:i(kamps))
endif
xout idur, ipch, iamp
endop
/** Experimental opcode for generating melodic lines given array of durations, pitches, and amplitudes. Durations follow dur_seq practice that negative values are rests. Pitch and amp array indexing wraps according to their array lengths given index of non-rest duration value currently fired. */
opcode melodic, iii, k[]k[]k[]
kdurs[], kpchs[], kamps[] xin
idur, ipch, iamp = melodic(now_tick(), kdurs, kpchs, kamps)
xout idur, ipch, iamp
endop
;; String functions
/**
rotate - Rotates string by irot number of values.
(Inspired by rotate from Charlie Roberts' Gibber.)
*/
opcode rotate, S, Si
Sval, irot xin
ilen = strlen(Sval)
irot = irot % ilen
Sout = strcat(strsub(Sval, irot, ilen), strsub(Sval, 0, irot))
xout Sout
endop
/** Repeats a given String x number of times. For example, `Sval = strrep("ab6a", 2)` will produce the value of "ab6aab6a". Useful in working with Hex beat strings. */
opcode strrep, S, Si
Sval, inum xin
Sout = Sval
indx = 1
while (indx < inum) do
Sout = strcat(Sout, Sval)
indx += 1
od
xout Sout
endop
;; Channel Helper
/** Sets i-rate value into channel and sets initialization to true. Works together
with xchan */
opcode xchnset, 0, Si
SchanName, ival xin
Sinit = sprintf("%s_initialized", SchanName)
chnset(1, Sinit)
chnset(ival, SchanName)
endop
/** xchan
Initializes a channel with initial value if channel has default value of 0 and
then returns the current value from the channel. Useful in live coding to define
a dynamic point that will be automated or set outside of the instrument that is
using the channel.
Opcode is overloaded to return i- or k- value. Be sure to use xchan:i or xchan:k
to specify which value to use.
*/
opcode xchan, i,Si
SchanName, initVal xin
Sinit = sprintf("%s_initialized", SchanName)
if(chnget:i(Sinit) == 0) then
chnset(1, Sinit)
chnset(initVal, SchanName)
endif
xout chnget:i(SchanName)
endop
/** xchan
Initializes a channel with initial value if channel has default value of 0 and
then returns the current value from the channel. Useful in live coding to define
a dynamic point that will be automated or set outside of the instrument that is
using the channel.
Opcode is overloaded to return i- or k- value. Be sure to use xchan:i or xchan:k
to specify which value to use.
*/
opcode xchan, k,Si
SchanName, initVal xin
Sinit = sprintf("%s_initialized", SchanName)
if(chnget:i(SchanName) == 0) then
chnset(1, Sinit)
chnset(initVal, SchanName)
endif
xout chnget:k(SchanName)
endop
;; SCALE/HARMONY (experimental)
gi_scale_major[] = array(0, 2, 4, 5, 7, 9, 11)
gi_scale_minor[] = array(0, 2, 3, 5, 7, 8, 10)
gi_cur_scale[] = gi_scale_minor
gi_scale_base = 60
gi_chord_offset = 0
/** Set root note of scale in MIDI note number. */
opcode set_root, 0,i
iscale_root xin
gi_scale_base = iscale_root
endop
/** Calculate frequency from root note of scale, using
octave and pitch class. */
opcode from_root, i, ii
ioct, ipc xin
ival = gi_scale_base + (ioct * 12) + ipc
xout cpsmidinn(ival)
endop
/** Set the global scale. Currently supports "maj" for major and "min" for minor scales. */
opcode set_scale, 0,S
Scale xin
if(strcmp("maj", Scale) == 0) then
gi_cur_scale = gi_scale_major
else
gi_cur_scale = gi_scale_minor
endif
endop
/** Calculate frequency from root note of scale, using
octave and scale degree. */
opcode in_scale, i, ii
ioct, idegree xin
ibase = gi_scale_base + (ioct * 12)
idegrees = lenarray(gi_cur_scale)
ioct = int(idegree / idegrees)
indx = idegree % idegrees
if(indx < 0) then
ioct -= 1
indx += idegrees
endif
xout cpsmidinn(ibase + (ioct * 12) + gi_cur_scale[int(indx)])
endop
/** Calculate frequency from root note of scale, using
octave and scale degree. (k-rate version of opcode) */
opcode in_scale, k, kk
koct, kdegree xin
kbase = gi_scale_base + (koct * 12)
idegrees = lenarray(gi_cur_scale)
koct = int(kdegree / idegrees)
kndx = kdegree % idegrees
if(kndx < 0) then
koct -= 1
kndx += idegrees
endif
xout cpsmidinn(kbase + (koct * 12) + gi_cur_scale[int(kndx)])
endop
/** Quantizes given MIDI note number to the given scale
(Base on pc:quantize from Extempore) */
opcode pc_quantize, i, ii[]
ipitch_in, iscale[] xin
inotenum = round:i(ipitch_in)
ipc = inotenum % 12
iout = inotenum
indx = 0
while (indx < 7) do
if(contains(ipc + indx, iscale) == 1) then
iout = inotenum + indx
goto end
elseif (contains(ipc - indx, iscale) == 1) then
iout = inotenum - indx
goto end
endif
indx += 1
od
end:
xout iout
endop
/** Quantizes given MIDI note number to the current active scale
(Base on pc:quantize from Extempore) */
opcode pc_quantize, i, i
ipitch_in xin
ival = pc_quantize(ipitch_in, gi_cur_scale)
xout ival
endop
/* BELOW CHORD SYSTEM IS EXPERIMENTAL */
gi_chord_base = 0
gi_chord_maj[] = array(0,4,7)
gi_chord_maj7[] = array(0,4,7,11)
gi_chord_min[] = array(0,3,7)
gi_chord_min7[] = array(0,3,7,10)
gi_chord_dim[] = array(0,3,6)
gi_chord_dim7[] = array(0,3,6,9)
gi_chord_aug[] = array(0,4,8)
gi_chord_current[] = gi_chord_maj
opcode set_chord, 0, ii[]
ichord_root, ichord_intervals[] xin
gi_chord_base = ichord_root
gi_chord_current = ichord_intervals
endop
opcode set_chord, 0, S
Schord xin
endop
opcode in_chord, i, ii
ioct, idegree xin
ibase = gi_scale_base + (ioct * 12) + gi_chord_base
idegrees = lenarray(gi_chord_current)
ioct = int(idegree / idegrees)
indx = idegree % idegrees
if(indx < 0) then
ioct -= 1
indx += idegrees
endif
xout cpsmidinn(ibase + (ioct * 12) + gi_chord_current[indx])
endop
;; AUDIO
/** Utility opcode for declicking an audio signal. Should only be used in instruments that have positive p3 duration. */
opcode declick, a, a
ain xin
aenv = linseg:a(0, 0.01, 1, p3 - 0.02, 1, 0.01, 0, 0.01, 0)
xout ain * aenv
endop
/** Custom non-interpolating oscil that takes in kfrequency and array to use as oscillator table
data. Outputs k-rate signal. */
opcode oscil, k, kk[]
kfreq, kin[] xin
ilen = lenarray(kin)
kphs = phasor:k(kfreq)
kout = kin[int(kphs * ilen) % ilen]
xout kout
endop
;; KILLING INSTANCES
instr KillImpl
Sinstr = p4
if (nstrnum(Sinstr) > 0) then
turnoff2(Sinstr, 0, 0)
endif
turnoff
endin
/** Turns off running instances of named instruments. Useful when livecoding
audio and control signal process instruments. May not be effective if for
temporal recursion instruments as they may be non-running but scheduled in the
event system. In those situations, try using clear_instr to overwrite the
instrument definition. */
opcode kill, 0,S
Sinstr xin
schedule("KillImpl", 0, 0.01, Sinstr)
endop
/** Redefines instr to empty body. Useful for killing
temporal recursion or clock callback functions */
opcode clear_instr, 0,S
Sinstr xin