-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.lisp
More file actions
1740 lines (1353 loc) · 48 KB
/
core.lisp
File metadata and controls
1740 lines (1353 loc) · 48 KB
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
(in-package :sqnc)
; envpt and ctl classes
(classy envpt ()
(x nil)
(y nil)
(i nil))
(classy ctl ()
(name nil)
(val nil)
(lo nil)
(hi nil))
(classy nctl (ctl)) ; numeric ctl
(classy dctl (nctl)) ; dynamic ctl
(classy cctl (nctl)) ; constant ctl
(classy ictl (nctl)) ; integer ctl
(classy fctl (nctl)) ; float ctl
(classy dfctl (dctl fctl)) ; dynamic float ctl
(classy dictl (dctl ictl)) ; dynamic int ctl
(classy cfctl (cctl fctl)) ; constant float ctl
(classy cictl (cctl ictl)) ; constant int ctl
(classy mixerctl (cictl))
(classy chnctl (cfctl))
(classy kchnctl (chnctl))
(classy ichnctl (chnctl))
(def mk-env pts
(mc [dbind (x y i) _ (new 'envpt :x x :y y :i i)]
(group pts 3)))
(def mk-ctl (type name lo hi val)
(new type :name name :val val :lo lo :hi hi))
(def dfctl (name lo hi . env)
(mk-ctl 'dfctl name lo hi (-> mk-env env)))
(def cfctl (name lo hi val)
(mk-ctl 'cfctl name lo hi val))
(classy channel ()
(name nil)
(amp (mk-ctl 'kchnctl "Amp" 0 1 .7))
(mute (mk-ctl 'kchnctl "Mute" 0 1 1))
(sends nil)
(inserts nil))
(classy send (channel))
(classy mixer ()
(inum 1000.1)
(options "-dWodac")
(offline-options "-dW")
(score "f 0 1000000")
(channels nil)
(sends nil)
(amp (mk-ctl 'kchnctl "amp" 0 1 .7))
(pan (mk-ctl 'kchnctl "pan" 0 1 .5))
(sr (mk-ctl 'mixerctl "sr" 1000 100000 44100))
(kr (mk-ctl 'mixerctl "kr" 10 10000 441))
(ksmps (mk-ctl 'mixerctl "ksmps" 1 100000 100))
(nchnls (mk-ctl 'mixerctl "nchnls" 1 32 2))
(0dbfs (mk-ctl 'mixerctl "0dbfs" 1 1 1)))
(classy pattern ()
(name "Default Pattern")
(bpm 170)
(resol 8)
(beats 7)
(measures 8)
(time@pt 0)
(track nil)
(trg-hash (table)))
(defm dur ((p pattern)) (beats->s (beats/pat p) (bpm p)))
(defs beats/pat (pat) (* (beats pat) (measures pat))
ticks/pat (pat) (* (beats/pat pat) (resol pat))
ticks/meas (pat) (* (beats pat) (resol pat)))
(classy track ()
(name "Default Track")
(keys 128)
(keynum 64)
(amp 0.8)
(solo nil)
(muted nil))
(defps *trg-dur* 1/4
*min-trg-dur* 1/1000
*maxres* 200)
(classy trg ()
(pattern nil :nocp t)
(track nil :nocp t)
(inst nil :nocp t)
(name nil)
(start 0)
(dur *trg-dur*)
(keynum nil)
(res (mk-ctl 'dfctl "Resolution" 1 *maxres* (mk-env 0 20 :l)))
(muted nil)
(params nil))
(classy pattern-trg (trg)
(pat nil :nocp))
(defg generator (trg-type))
(defms start-tick ((trg trg)) (s->ticks (start trg) (tpm trg))
dur-tick ((trg trg)) (s->ticks (dur trg) (tpm trg))
end-tick ((trg trg)) (+ (start-tick trg) (dur-tick trg))
end ((trg trg)) (+ (start trg) (dur trg)))
(classy inst ()
(name nil)
(csmac-name nil)
(presets nil)
(preset nil)
(stmts nil))
(classy sample-inst (inst)
(sample-map nil))
(classy effect ()
(name nil)
(csmac-name nil)
(params nil)
(stmts nil)
(presets nil)
(preset nil))
(classy transport ()
(current-time 0)
(loop-start 0)
(loop-end 1))
(classy sample ()
(table-num nil)
(path nil)
(id nil)
(slices nil)
(samples nil)
(bpm nil)
(sr nil)
(base-freq nil))
(defm secs ((sample sample))
(* (sr sample) (samples sample)))
(classy sample-table ()
(hash (table :test #'equal))
(table-num 1000))
(classy song ()
(name nil)
(path nil)
(inst nil)
(insts nil)
(pattern nil)
(patterns nil)
(tracks nil)
(trg nil)
(sample-maps nil)
(samples (new 'sample-table))
(mixer (new 'mixer))
(transport (new 'transport)))
(defms track ((s song)) (track (pattern s))
presets ((s song)) (awhen (inst s) (presets it))
preset ((s song)) (awhen (inst s) (preset it))
time@pt ((s song)) (time@pt (pattern s))
keynum ((s song)) (keynum (track s)))
(def time@pt+1 (song)
(w/ (tpm (tpm song))
(ticks->s (1+ (s->ticks (time@pt song) tpm)) tpm)))
(defms tpm ((p pattern)) (* (resol p) (bpm p))
tpm ((s song)) (tpm (pattern s))
tpm ((r trg)) (tpm (pattern r)))
; utils
(defp *song* nil)
(def s () *song*)
(def set-song (s) (=! *song* s))
(def meter-time (pat tick)
(w/ (bs (beats pat) rs (resol pat))
(mvbind (meas rem) (trunc tick (* bs rs))
(mvbind (beat res) (trunc rem rs)
(values meas beat res)))))
(def root-pattern (song) (car (patterns song)))
(def root-pattern-p (pat song) (eq pat (root-pattern song)))
(defs prev-pat (song) (cprev (pattern song) (patterns song))
next-pat (song) (cnext (pattern song) (patterns song))
prev-trk (song) (cprev (track song) (tracks song))
next-trk (song) (cnext (track song) (tracks song))
prev-inst (song) (cprev (inst song) (insts song))
next-inst (song) (cnext (inst song) (insts song))
prev-preset (song) (cprev (preset song) (presets song))
next-preset (song) (cnext (preset song) (presets song))
prev-pat! (song) (=! (pattern song) (prev-pat song))
next-pat! (song) (=! (pattern song) (next-pat song))
prev-trk! (song) (=! (track (pattern song)) (prev-trk song))
next-trk! (song) (=! (track (pattern song)) (next-trk song))
prev-inst! (song) (=! (inst song) (prev-inst song))
next-inst! (song) (=! (inst song) (next-inst song))
prev-preset! (song) (=! (preset (inst song)) (prev-preset song))
next-preset! (song) (=! (preset (inst song)) (next-preset song)))
; ctl utils
(defm interp ((n number) &o secs)
"Returns the number."
(declare (ignore secs))
n)
(defm interp ((c cctl) &o secs)
"Returns the current value of c, since it's not an envelope."
(declare (ignore secs))
(val c))
(def normalize-env (env)
"Adds an envpt at x=0 unless one already exists."
(if (> (x (car env)) 0)
(cons (new 'envpt :x 0 :y (y (car env)) :i :l) env)
env))
(defm interp ((ctl dctl) &o (secs 0))
"Interpolates the value of ctl's envelope at secs seconds."
(w/* (env (val ctl)
res (maplp [aand (cadr _) (< (-! secs (x it)) 0) _]
(normalize-env env))
p1 (car res)
p2 (cadr res))
(if (no (and p1 p2))
(y (last1 env))
(w/ (x1 (x p1) x2 (x p2))
(if (= x1 x2)
(y p1)
(rescale (+ secs x2) 0 x2 (y p1) (y p2)))))))
(defm interp ((c dictl) &o (secs 0))
"Truncates the value of interpolating c."
(declare (ignore secs))
(trunc (cnm)))
(defm interp ((c cictl) &o (secs 0))
"Truncates the value of c."
(declare (ignore secs))
(trunc (cnm)))
(mac map-ctl (ctl . body)
(w/uniq (new-ctl)
`(w/n (,new-ctl (copy ,ctl))
(w/slots (lo hi val typ) ,new-ctl
(mapc [w/slots (y x i) _ ,@body] val)))))
(def rescale-ctl (ctl nlo nhi)
(w/n (c (map-ctl ctl (=! y (rescale y lo hi nlo nhi))))
(=! (lo c) nlo (hi c) nhi)))
(def repitch-ctl (ctl keynum base-key)
(map-ctl ctl (=! y (repitch y keynum base-key))))
(def pitchbend-ctl (keynum pitchbend)
(map-ctl pitchbend (=! y (midi-cps (+ y keynum)))))
(defm expt-ctl ((c dfctl) e) (map-ctl c (=! y (expt y e))))
(defm expt-ctl ((c cfctl) e) (expt (val c) e))
;;; trg copying
(def tag-trg (trg song . defs)
(-> slotf trg
'pattern (pattern song)
'track (track song)
'start (time@pt song)
'keynum (keynum song)
defs))
(def copy-and-tag-trg (trg song . defs)
(-> tag-trg (copy trg) song defs))
(def c-new-trg (song . defs)
(awhen (trg song) (-> copy-and-tag-trg it song defs)))
(def convert-dur (from)
(ticks->s (ticks/pat from) (tpm from)))
(def c-new-ptrg (pat song . defs)
(-> tag-trg (new 'pattern-trg :pat pat :dur (convert-dur pat))
song defs))
;;; trg utils
(mac trg-hash-get (trg)
`(gethash (start ,trg) (trg-hash (pattern ,trg))))
(def trg-hash-add (trg) (push trg (trg-hash-get trg)))
(def trg-hash-del (trg)
(a=! (trg-hash-get trg) (remove trg it))
(when (null (trg-hash-get trg))
(remhash (start trg) (trg-hash (pattern trg)))))
(def trg-within-bounds (trg)
(and (within 0 (dur (pattern trg)) (start trg))
(within 0 (keys (track trg)) (keynum trg))))
(def min-keynum (objs) (-> keyed-min #'keynum objs))
(def max-keynum (objs) (-> keyed-max #'keynum objs))
(def sort-trgs-inc (trgs)
(sort (sort trgs #'< :key #'keynum) #'< :key #'start))
(def sort-trgs-dec (trgs)
(sort (sort trgs #'> :key #'keynum) #'> :key #'start))
;;; query-trgs
(def query-trgs (pat &k (beg 0) (end (dur pat)) all-active preds inclusive-end)
(w/n (end-test (if inclusive-end #'<= #'<) res)
(dohash (start trgs (trg-hash pat))
(dolist (trg trgs)
(and (if all-active
(and (=> end-test start end) (> (+ start (dur trg)) beg))
(and (<= beg start) (=> end-test start end)))
(-> passes trg preds)
(push trg res))))))
(def inst-trgs (i &o (s (s)))
(ma [query-trgs _ :preds (ls [eq i (inst _)])] (patterns s)))
(def trk-trgs (trk . keys)
(ma [-> pat-trk-trgs _ trk keys] (patterns (s))))
(def pat-trk-trgs (pat trk &r keys &k preds &a)
(-> query-trgs pat :preds (cons [eq trk (track _)] preds) keys))
(def pat-range-trgs (pat beg end . keys)
(-> query-trgs pat :beg beg :end end keys))
(def pat-trk-time-trgs (pat trk time . keys)
(-> pat-trk-trgs pat trk :beg time :end time :inclusive-end t keys))
(def pat-trk-time-kn-trgs (pat trk time kn &r keys &k preds &a)
(-> pat-trk-time-trgs pat trk time :preds (cons [= kn (keynum _)] preds) keys))
(def cpat-ctrk-trgs (song . keys)
(-> pat-trk-trgs (pattern song) (track song) keys))
(def cpat-ctrk-time-trgs (time song . keys)
(-> pat-trk-time-trgs (pattern song) (track song) time keys))
(def cpat-ctrk-ctime-trgs (song . keys)
(-> cpat-ctrk-time-trgs (time@pt song) song keys))
(def cpat-ctrk-ctime-kn-trgs (kn song &r keys &k preds &a)
(-> cpat-ctrk-ctime-trgs song :preds (cons [= kn (keynum _)] preds) keys))
(def pat-trk-trgs@time+tick (time keynum pat trk)
(sort (pat-trk-trgs pat trk
:beg time :end (+ time (ticks->s 1 (tpm pat)))
:preds (ls [= keynum (keynum _)])
:all-active t)
#'< :key #'start))
(def pat-trk-trg@time+tick (time keynum pat trk)
(car (pat-trk-trgs@time+tick time keynum pat trk)))
(def trgs@time+tick (time song)
(pat-trk-trgs@time+tick time (keynum song) (pattern song) (track song)))
(def trg@time+tick (time song) (car (trgs@time+tick time song)))
(def trg-in-ctick (song) (trg@time+tick (time@pt song) song))
(def trgs@p (song . keys)
(-> cpat-ctrk-ctime-kn-trgs (keynum song) song keys))
(def trg@p (song) (car (trgs@p song)))
(def c-active-trgs (song) (trgs@p song :all-active t))
(def c-active-trg (song) (car (c-active-trgs song)))
(def trgs@trg (trg)
(pat-trk-time-kn-trgs (pattern trg) (track trg) (start trg) (keynum trg)))
(def all-ptrgs-in-range (pat beg end)
(pat-range-trgs pat beg end :all-active t :preds (ls #'pattern-trg-p)))
(def trgs-in-range (pat beg end)
(pat-range-trgs pat beg end :preds (ls [no (pattern-trg-p _)])))
(def trgs<time (song)
(cpat-ctrk-trgs song :preds (ls [< (start _) (time@pt song)])))
(def trgs>tick (song)
(cpat-ctrk-trgs song :preds (ls [>= (start _) (time@pt+1 song)])))
(def next-trg (song)
(car (sort-trgs-inc
(or (cpat-ctrk-trgs song :beg (time@pt song)
:end (time@pt+1 song)
:preds (ls [> (keynum _) (keynum song)]))
(trgs>tick song)
(cpat-ctrk-trgs song)))))
(def prev-trg (song)
(car (sort-trgs-dec
(or (cpat-ctrk-ctime-trgs song :preds (ls [< (keynum _) (keynum song)]))
(cpat-ctrk-trgs song :preds (ls [< (start _) (time@pt song)]))
(cpat-ctrk-trgs song)))))
(def get-trgs-in-range (time-lo time-hi kn-lo kn-hi song)
(cpat-ctrk-trgs song :beg time-lo :end time-hi
:preds (ls [<= kn-lo (keynum _)] [>= kn-hi (keynum _)])
:inclusive-end t))
;;; csd mixer gen
(defps *send-offset* 10 *chan-mult* 1000)
(def set-csoptions (str &o (s (s))) (=! (options (mixer s)) str))
; chnctl registry
(defps *chnctls* (table) *chnctl-idx* -1)
(def reset-chnctl-registry ()
(=! *chnctls* (table) *chnctl-idx* -1))
(def chnctl-name (chnctl)
(or (gethash chnctl *chnctls*)
(putab chnctl (mkstr "chn~a" (+! *chnctl-idx*)) *chnctls*)))
(defm chnctl-var ((c kchnctl)) (mksym 'k (chnctl-name c)))
(defm chnctl-var ((c ichnctl)) (mksym 'i (chnctl-name c)))
; channel registry
(defps *channels* (table) *channel-idx* -1)
(def reset-channel-registry ()
(=! *channels* (table) *channel-idx* -1))
(def record-channel (channel)
(w/ (idx (+! *channel-idx*))
(putab channel (ls (mksym 'at idx 0) (mksym 'at idx 1))
*channels*)))
(def lch (channel)
(or (car (gethash channel *channels*))
(car (record-channel channel))))
(def rch (channel)
(or (cadr (gethash channel *channels*))
(cadr (record-channel channel))))
; inst registry
(defps *inst-registry* (table) *inst-idx* 0)
(def reset-inst-registry ()
(=! *inst-registry* (table) *inst-idx* 0))
(def inst-inum (inst)
(or (gethash inst *inst-registry*)
(putab inst (+! *inst-idx*) *inst-registry*)))
; mixer-gen csmacs
(defcsmac ahdsr (a0 a1 iatt ihol idec isus irel)
`(orc (maxes .000001 iatt ihol idec irel)
(tigoto _ skipinit2)
(linseg aenv 0 ,iatt 1 ,ihol 1 ,idec ,isus .01 ,isus)
(label skipinit2)
(tiestatus itie)
(if (== itie 2) (linsegr aenv ,isus ,irel 0))
(= ,a0 (* ,a0 aenv))
(= ,a1 (* ,a1 aenv))))
(defcsmac balancer (a0 a1 bal)
`(orc (if (> ,bal .5) (= klpan (* 2 (- ,bal .5))) (= klpan 0))
(if (< ,bal .5) (= krpan (* 2 ,bal)) (= krpan 1))
(pan2 (anl0 anr0) ,a0 klpan)
(pan2 (anl1 anr1) ,a1 krpan)
(= ,a0 (+ anl0 anl1))
(= ,a1 (+ anr0 anr1))))
(defcsmac trackout (a0 a1 itrk)
(w/ (from `(+ 1000 ,itrk) to `(+ 2000 ,itrk))
`(orc (-mixer-send _ ,a0 ,from ,to 0)
(-mixer-send _ ,a1 ,from ,to 1))))
(defcsmac trackin (a0 a1 itrk)
(w/ (from `(+ 1000 ,itrk) to `(+ 2000 ,itrk))
`(orc (-mixer-set-level _ ,from ,to 1)
(-mixer-receive ,a0 ,to 0)
(-mixer-receive ,a1 ,to 1))))
(def def-chnctl (ctl)
(w/ (name (chnctl-name ctl) var (chnctl-var ctl))
`(pn (chn_k _ ,name 1 2 ,(interp ctl) ,(lo ctl) ,(hi ctl))
(chnset _ ,(interp ctl) ,name)
(chnget ,var ,name))))
(def channel-knobs (ch)
`(pn ,(def-chnctl (amp ch))
,(def-chnctl (mute ch))
,@(mc #'def-chnctl (sends ch))))
(def channel-in (ch chs)
`(trackin ,(lch ch) ,(rch ch) ,(idx ch chs)))
(def channel-send (op send n channels)
`(= ,(=> op send)
(+ ,@(mc [pn `(* ,(=> op _) ,(chnctl-var (nth n (sends _))))]
channels))))
(def channel-sends (send sends channels)
(w/ (n (idx send sends))
`(pn ,(channel-send #'lch send n channels)
,(channel-send #'rch send n channels))))
(def channel-insert (ch ins)
`(pn ,@(mc [def-chnctl _] (params ins))
(,(csmac-name ins) ,(lch ch) ,(rch ch)
,@(mc [chnctl-var _] (params ins)))))
(def channel-inserts (ch)
`(pn ,@(mc [channel-insert ch _] (inserts ch))))
(def channel-level (ch)
(w/ (a0 (lch ch)
a1 (rch ch)
amp (chnctl-var (amp ch))
mute (chnctl-var (mute ch)))
`(pn (= ,a0 (* ,a0 ,amp ,mute))
(= ,a1 (* ,a1 ,amp ,mute)))))
(def compile-master-channel (chs-sends m)
`(pn (= am0 (+ ,@(mc [lch _] chs-sends)))
(= am1 (+ ,@(mc [rch _] chs-sends)))
,(def-chnctl (pan m))
,(def-chnctl (amp m))
(balancer am0 am1 ,(chnctl-var (pan m)))
(= am0 (* am0 ,(chnctl-var (amp m))))
(= am1 (* am1 ,(chnctl-var (amp m))))
(outs _ am0 am1)
(-mixer-clear)))
(def compile-mixer (song)
(w/* (m (mixer song) chs (channels m) sends (sends m) chs-sends (ap chs sends))
`(instr (master ,(inum m))
,@(mc [channel-knobs _] chs-sends)
,@(mc [channel-in _ chs] chs)
,@(mc [channel-sends _ sends chs] sends)
,@(mc [channel-inserts _] chs-sends)
,@(mc [channel-level _] chs-sends)
,(compile-master-channel chs-sends m))))
(def channel-setter (inum)
`(instr (,inum)
(strget -sname p5)
(chnset _ p4 -sname)))
(def compile-header (song)
(w/ (m (mixer song))
`(pn (= sr ,(interp (sr m)))
(= kr ,(interp (kr m)))
(= ksmps ,(interp (ksmps m)))
(= nchnls ,(interp (nchnls m)))
(= 0dbfs ,(interp (0dbfs m)))
,@(ma [copy (stmts _)] (insts song)))))
(def compile-insts (song)
`(pn ,@(mc [ls (csmac-name _) (inst-inum _)] (insts song))))
(def orchestra-gen (song)
(reset-chnctl-registry)
(reset-channel-registry)
(reset-inst-registry)
`(orc ,(compile-header song)
,(compile-insts song)
,(compile-mixer song)
,(channel-setter 1001)))
(def csd-gen (song &k options score)
(with-output-to-string (str)
(w/ (*standard-output* str m (mixer song))
(w/error-screen (mkstr "There was an error generating the csd file:~%~%~A" e)
(eval `(with "CsoundSynthesizer"
(as "CsOptions" ,(or options (options m)))
(with "CsInstruments" ,(orchestra-gen song))
(as "CsScore" ,(or score (score m)))))))))
;;; sample lib
(def add-sample (sample song)
(w/ (st (samples song))
(=! (table-num sample) (aif (getab (id sample) (hash st))
(table-num it)
(incf (table-num st))))
(putab (id sample) sample (hash st))))
(def get-sample (id &o (s (s))) (getab id (hash (samples s))))
(mac def-sample def
`(add-sample (new 'sample ,@def) (s)))
(def set-slices (id . slices)
(slotf (get-sample id) 'slices slices))
; sample fns
(classy wav ()
(compression nil)
(channels nil)
(sample-rate nil)
(bytes/sample nil)
(block-align nil)
(bits/sample nil)
(data-size nil))
(def mk-wav (path)
(with-open-file (stream path :direction :input :element-type '(unsigned-byte 8))
(w/ (wav (new 'wav))
(and (equal "RIFF" (byte-str stream 4))
(read-32 stream)
(equal "WAVE" (byte-str stream 4))
(equal "fmt " (byte-str stream 4))
(= 16 (read-32 stream))
(=! (compression wav) (read-16 stream)
(channels wav) (read-16 stream)
(sample-rate wav) (read-32 stream)
(bytes/sample wav) (read-32 stream)
(block-align wav) (read-16 stream)
(bits/sample wav) (read-16 stream))
(equal "data" (byte-str stream 4))
(=! (data-size wav) (read-32 stream))
wav))))
(defm samples ((w wav))
(/ (data-size w) (/ (bits/sample w) 8)))
(w/ (powers2 (mapa<b [expt 2 _] 0 50))
(def max-power-of-2 (num)
(w/ last
(mapp [if (> _ num) last (pn (=! last _) nil)]
powers2))))
(def import-sample-dir (pathspec &k power2)
(mc [awhen (and (string= "WAV" (string-upcase (pathname-type _)))
(mk-wav _))
(def-sample :id (cat (pathname-name _) (when power2 "^2"))
:path (namestring _)
:samples (if power2 (max-power-of-2 (samples it)) 0)
:sr (sample-rate it))]
(ls-dir pathspec)))
; sample range
(classy sample-range ()
(id nil)
(start nil)
(end nil)
(base-key nil)
(start-slice nil)
(end-slice nil))
(def start-samples (range)
(w/when (sample (get-sample (id range))
slice (start-slice range))
(if (negp slice) 0 (nth slice (slices sample)))))
(def start-secs (range)
(awhen (start-samples range)
(/ it (sr (get-sample (id range))))))
(def end-samples (range)
(w/when (sample (get-sample (id range))
slice (end-slice range))
(if (negp slice) (samples sample) (nth slice (slices sample)))))
(def end-secs (range)
(awhen (end-samples range)
(/ it (sr (get-sample (id range))))))
(def mk-srange (id start end base-key &o (start-slice -1) (end-slice -1))
(new 'sample-range :id id
:start start
:end end
:base-key base-key
:start-slice start-slice
:end-slice end-slice))
; sample map
(classy sample-map ()
(id nil)
(ranges nil))
(def mk-smap (id . ranges)
(new 'sample-map :id id :ranges ranges))
(def get-sample-map (id song)
(mapp [and (equal (id _) id) _] (sample-maps song)))
(def store-sample-map (smap &o (s (s)))
(awhen (get-sample-map (id smap) s)
(remove! it (sample-maps s)))
(rpush smap (sample-maps s)))
(mac def-sample-map (id . defs)
`(store-sample-map
(mk-smap ,id ,@(mc [pn `(mk-srange ,@_)]
(group defs 6)))))
(def keynum->map-range (keynum sample-map)
(mapp [when (within (start _) (end _) keynum) _]
(ranges sample-map)))
(mac def-sample-map-from-dir (id pathspec &k (start 0) (range-length 1) (offset 0) power2)
(w/ (idx start)
`(def-sample-map ,id
,@(ma [when (string= "WAV" (string-upcase (pathname-type _)))
(prog1 (ls (cat (pathname-name _) (if power2 "^2" ""))
idx (+ -1 idx range-length) (+ idx offset) -1 -1)
(+! idx range-length))]
(ls-dir pathspec)))))
; sample-map pprinter
(set-pprint-dispatch 'sample-map
(fn (stream obj)
(format stream "(def-sample-map '~s" (id obj))
(dolist (r (ranges obj))
(format stream "~&~10@s ~3@a ~3@a ~3@a ~3@a ~3@a"
(id r) (start r) (end r) (base-key r)
(start-slice r) (end-slice r)))
(format stream ")")))
;;; csound
; csound predicates
(defm online ((n null)))
(defm offline (obj) (no (online obj)))
(def csi-p (csi) (no (fnullp csi)))
(def cs-state (cs)
(if cs
(if (csi-p (csi cs))
(if (csd cs)
(aif (compile-code cs)
(if (= it 0)
(if (online cs) ; - - - - - - - Pew! Pew!
0
1)
2)
3)
4)
5)
6))
(defp *cs-state-strings*
'("Online"
"Offline"
"Compile error"
"Uncompiled"
"No csd"
"csi-create error"
"Uninitialized"))
(def cs-state-str () (nth (cs-state (c-cs)) *cs-state-strings*))
(defs online-p (cs) (= (cs-state cs) 0)
offline-p (cs) (= (cs-state cs) 1)
ready-p (cs) (< (cs-state cs) 2)
csd-p (cs) (< (cs-state cs) 4)
init-p (cs) (< (cs-state cs) 5))
; csound class
(defps *csound* nil *csd-name* "sqnc.csd")
(def c-cs () *csound*)
(def get-csi ()
(when (>= (csoundinitialize (fnull) (fnull) 1) 0)
(csoundcreate (fnull))))
(classy csound ()
(csi (get-csi))
(csd (namestring (merge-pathnames *sqnc-dir* *csd-name*)))
(compile-code nil)
(online nil)
(processing nil)
(msgs (make-array 0 :fill-pointer 0 :adjustable t))
(num-msgs 30))
; cs messages
(defp *dump-cs-msgs* nil)
(def pop-cs-msg (cs)
(prog1 (csoundgetfirstmessage (csi cs))
(csoundpopfirstmessage (csi cs))))
(def cs-msgs () (aif (c-cs) (msgs it) ""))
(def dump-cs-msgs (cs)
(w/ (msgs (msgs cs))
(awhile (pop-cs-msg cs)
(map nil [vpushx _ msgs] it))))
; csound thread
(def csound-perform (cs)
(w/error-screen (mkstr "There was an error in the csound performance:~%~%~a" e)
(t-within ((processing cs) (online cs))
(w/ (csi (csi cs))
(while (and (online cs) (zerop (csoundperformksmps csi)))
(if *dump-cs-msgs* (dump-cs-msgs cs)))))))
(def csound-start-performance (cs)
(when (offline-p cs)
(mk-thread (fn () (csound-perform cs)) "csound")))
(def csound-stop-performance (cs)
(when (online-p cs)
(nil! (online cs))
(sleep-while (processing cs))))
(def csound-kill (cs)
(csound-stop-performance cs)
(csoundreset (csi cs))
(csoundcleanup (csi cs)))
; stmt
(classy event ())
(classy stmt (event)
(type-code nil)
(pfields nil)
(len nil))
(classy csstr (event)
(type-str nil)
(pf-str nil))
(def mk-stmt (cc lst)
(w/ (l (ln lst))
(new 'stmt :type-code (char-code cc)
:len l
:pfields (list->farr lst (get-farr l)))))
(def mk-csstr (cc lst)
(new 'csstr :type-str (mkstr "~a " cc)
:pf-str (mkstr "~{~S~^ ~}" lst)))
(defs -i pf (mk-stmt #\i pf)
-f pf (mk-stmt #\f pf)
-is pf (mk-csstr #\i pf)
-fs pf (mk-csstr #\f pf))
; output-buffer
(classy output-buffer ()
(buf (make-array 100 :fill-pointer 0 :adjustable t))
(buflen 1/4)
(latency 1/4)
(scheduling nil)
(processing nil))
(defp *opb* (new 'output-buffer))
(def set-latency (latency &o (opb *opb*))
(w/ (l (/ latency 2))
(=! (latency opb) l (buflen opb) l)))
; output
(defm output (obj (n null))
(mb-msg "Output's destination is nil"))
(defm output ((o output-buffer) dest)
(output (buf o) dest))
(defm output ((v vector) dest)
(map nil [output _ dest] v)
(=! (fill-pointer v) 0))
(defm output ((h hash-table) dest)
(dohash (k v h) (output v dest)))
(defm output ((l list) dest)
(mapc [output _ dest] l))
;; (defm output ((r trg) dest)
;; (output (generator r (start r)) dest))
(defm output ((r trg) dest)
(output (offset (generator r) (start r)) dest))
(defp *output-mutex* (make-mutex "Csound output mutex"))
(defm output ((s stmt) (cs csound))
(when (online-p cs)
(w/mutex (*output-mutex*)
(csoundscoreevent (csi cs) (type-code s) (pfields s) (len s))))
(free-farr (pfields s) (len s)))
(defm output ((stmt stmt) (stream stream))
(format stream "~a ~{~,5f~^ ~}~%" (code-char (type-code stmt))
(nrev (for (n 0 (len stmt) res)
(push (faref n (pfields stmt)) res)))))
(def csstr (css) (cat (type-str css) " " (pf-str css)))
(defm output ((css csstr) (cs csound))
(when (online-p cs)
(w/mutex (*output-mutex*)
(w/fstr (s (csstr css))
(csoundinputmessage (csi cs) s)))))
(defm output ((css csstr) (stream stream))
(format stream "~a~%" (csstr css)))
(def chnctl-css (ctl)
(new 'csstr
:type-str "i "
:pf-str (mkstr "1001 0 0.0001 ~,5f ~s" (interp ctl) (chnctl-name ctl))))
(defm output ((c chnctl) (cs csound))
(output (chnctl-css c) cs))
(defm output ((c chnctl) (stream stream))
(output (chnctl-css c) stream))
(defm output ((e effect) dest)
(mapc [output _ dest] (params e)))
(defm output ((ch channel) dest)
(output (amp ch) dest)
(output (mute ch) dest)
(mapc [output _ dest] (inserts ch))
(mapc [output _ dest] (sends ch)))
(defm output ((s sample) dest)
(output (-fs (table-num s) 0 (samples s) 1 (namestring (path s)) 0 0 0)
dest))
(defm output ((s sample-table) dest)
(output (hash s) dest))
(defm output ((m mixer) dest)
(output (-i (inum m) 0 -1) dest)
(output (amp m) dest)
(output (pan m) dest)
(mapc [output _ dest] (channels m)))
(defm output ((s song) dest)
(output (mixer s) dest)
(output (samples s) dest))
; offset
(defm offset ((opb output-buffer) secs)
(offset (buf opb) secs)