-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathnyquist.lsp
2482 lines (2159 loc) · 89.7 KB
/
nyquist.lsp
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
;;;
;;; ###########################################################
;;; ### NYQUIST-- A Language for Composition and Synthesis. ###
;;; ### ###
;;; ### Copyright (c) 1994-2006 by Roger B. Dannenberg ###
;;; ###########################################################
;;;
(princ "LOADING NYQUIST RUNTIME DEBUG VERSION\n")
;; #### Error checking and reporting functions ####
(setf *SAL-CALL-STACK* nil) ; because SEQ looks at this
;; MULTICHANNEL-SOUNDP - test for vector of sounds
(defun multichannel-soundp (v)
(prog ((rslt t))
(if (not (arrayp v)) (return nil))
(dotimes (i (length v))
(cond ((not (soundp (aref v i)))
(setf rslt nil)
(return nil))))
(return rslt)))
;; MULTICHANNELP - test for vector of sounds or numbers
(defun multichannelp (v)
(prog ((rslt t))
(if (not (arrayp v)) (return nil))
(dotimes (i (length v))
(cond ((not (or (numberp (aref v i)) (soundp (aref v i))))
(setf rslt nil)
(return nil))))
(return rslt)))
;; NUMBERSP - test for vector of numbers
(defun numbersp (v)
(prog ((rslt t))
(if (not (arrayp v)) (return nil))
(dotimes (i (length v))
(cond ((not (numberp (aref v i)))
(setf rslt nil)
(return nil))))
(return rslt)))
;; PARAM-TO-STRING - make printable parameter for error message
(defun param-to-string (param)
(cond ((null param) (format nil "NIL"))
((soundp param) (format nil "a SOUND"))
((multichannel-soundp param)
(format nil "a ~A-channel SOUND" (length param)))
((eq (type-of param) 'ARRAY) ;; avoid saying "#(1 2), a ARRAY"
(format nil "~A, an ARRAY" param))
((stringp param) (format nil "~s, a STRING" param)) ;; add quotes
(t
(format nil "~A, a ~A" param (symbol-name (type-of param))))))
;; NY:TYPECHECK -- syntactic sugar for "if", used for all nyquist typechecks
(setfn ny:typecheck if)
(defun index-to-string (index)
(nth index '("" " 1st" " 2nd" " 3rd" " 4th" " 5th" " 6th" " 7th")))
(setf number-anon '((NUMBER) nil))
(setf number-sound-anon '((NUMBER SOUND) nil))
;; NY:TYPE-LIST-AS-STRING - convert permissible type list into
;; description. E.g. typs = '(NUMBER SOUND) and multi = t returns:
;; "number, sound or array thereof"
(defun ny:type-list-as-string (typs multi)
(let (lis last penultimate (string "") multi-clause)
(if (member 'NUMBER typs) (push "number" lis))
(if (member 'POSITIVE typs) (push "positive number" lis))
(if (member 'NONNEGATIVE typs) (push "non-negative number" lis))
(if (member 'INTEGER typs) (push "integer" lis))
(if (member 'STEP typs) (push "step number" lis))
(if (member 'STRING typs) (push "string" lis))
(if (member 'SOUND typs) (push "sound" lis))
(if (member 'NULL typs) (push "NIL" lis))
;; this should be handled with two entries: INTEGER and NULL, but
;; this complicates multichan-expand, where lists of arbitrary types
;; are not handled and we need INT-OR-NULL for PV-TIME-PITCH's
;; hopsize parameter.
(cond ((member 'INT-OR-NULL typs)
(push "integer" lis)
(push "NIL" lis)))
(cond ((member 'POSITIVE-OR-NULL typs)
(push "positive number" lis)
(push "NIL" lis)))
(cond (multi
(setf multi-clause
(cond ((> (length lis) 1) "array thereof")
((equal (car lis) "sound") "multichannel sound")
(t (strcat "array of " (car lis) "s"))))
(push multi-clause lis)))
(setf last (first lis))
(setf penultimate (second lis))
(setf lis (cddr lis))
(dolist (item lis)
(setf string (strcat item ", " string)))
(strcat string (if penultimate (strcat penultimate " or ") "") last)))
;; NY:ERROR -- construct an error message and raise an error
(defun ny:error (src index typ val &optional multi (val2 nil second-val))
(let ((types-string (ny:type-list-as-string (first typ) multi)))
(error (strcat "In " src "," (index-to-string index) " argument"
(if (second typ) (strcat " (" (second typ) ")") "")
(if (eq (char types-string 0) #\i) " must be an " " must be a ")
types-string
", got " (param-to-string val)
(if second-val (strcat ", and" (param-to-string val2)) "")))))
(prog ()
(setq lppp -12.0) (setq lpp -9.0) (setq lp -6.0) (setq lmp -3.0)
(setq lfff 12.0) (setq lff 9.0) (setq lf 6.0) (setq lmf 3.0)
(setq dB0 1.00) (setq dB1 1.122) (setq dB10 3.1623)
(setq s 0.25) (setq sd 0.375) (setq st (/ 0.5 3.0))
(setq i 0.5) (setq id 0.75) (setq it (* st 2.0))
(setq q 1.0) (setq qd 1.5) (setq qt (* st 4.0))
(setq h 2.0) (setq hd 3.0) (setq ht (* st 8.0))
(setq w 4.0) (setq wd 6.0) (setq wt (* st 16.0))
)
(init-global *A4-Hertz* 440.0)
; next pitch, for initializations below
;
(defun np () (incf nyq:next-pitch))
(defun set-pitch-names ()
(setq no-pitch 116.0)
; note: 58.0 is A4 - (C0 - 1) = 69 - (12 - 1)
(setf nyq:next-pitch (- (hz-to-step *A4-Hertz*) 58.0))
(setf nyq:pitch-names
'(c0 (cs0 df0) d0 (ds0 ef0) e0 f0 (fs0 gf0) g0 (gs0 af0) a0
(as0 bf0) b0
c1 (cs1 df1) d1 (ds1 ef1) e1 f1 (fs1 gf1) g1 (gs1 af1) a1
(as1 bf1) b1
c2 (cs2 df2) d2 (ds2 ef2) e2 f2 (fs2 gf2) g2 (gs2 af2) a2
(as2 bf2) b2
c3 (cs3 df3) d3 (ds3 ef3) e3 f3 (fs3 gf3) g3 (gs3 af3) a3
(as3 bf3) b3
c4 (cs4 df4) d4 (ds4 ef4) e4 f4 (fs4 gf4) g4 (gs4 af4) a4
(as4 bf4) b4
c5 (cs5 df5) d5 (ds5 ef5) e5 f5 (fs5 gf5) g5 (gs5 af5) a5
(as5 bf5) b5
c6 (cs6 df6) d6 (ds6 ef6) e6 f6 (fs6 gf6) g6 (gs6 af6) a6
(as6 bf6) b6
c7 (cs7 df7) d7 (ds7 ef7) e7 f7 (fs7 gf7) g7 (gs7 af7) a7
(as7 bf7) b7
c8 (cs8 df8) d8 (ds8 ef8) e8 f8 (fs8 gf8) g8 (gs8 af8) a8
(as8 bf8) b8))
(dolist (p nyq:pitch-names)
(cond ((atom p) (set p (np)))
(t (let ((pitch (np)))
(dolist (s p) (set s pitch)))))))
(set-pitch-names)
(init-global *default-sound-srate* 44100.0)
(init-global *default-control-srate* 2205.0)
(setf *environment-variables*
'(*WARP* *SUSTAIN* *START* *LOUD* *TRANSPOSE*
*STOP* *CONTROL-SRATE* *SOUND-SRATE*))
(setfn environment-time car)
(setfn environment-stretch cadr)
; ENVIRONMENT-MAP - map virtual time using an environment
;
;(defun environment-map (env tim)
; (+ (environment-time env)
; (* (environment-stretch env) tim)))
(defun nyq:the-environment () (mapcar 'eval *environment-variables*))
;; GLOBAL ENVIRONMENT VARIABLES and their startup values:
(defun nyq:environment-init ()
(setq *WARP* '(0.0 1.0 nil))
(setq *LOUD* 0.0) ; now in dB
(setq *TRANSPOSE* 0.0)
(setq *SUSTAIN* 1.0)
(setq *START* MIN-START-TIME)
(setq *STOP* MAX-STOP-TIME)
(setq *CONTROL-SRATE* *DEFAULT-CONTROL-SRATE*)
(setq *SOUND-SRATE* *DEFAULT-SOUND-SRATE*)
t) ; return nothing in particular
(nyq:environment-init)
(defun get-duration (dur)
(ny:typecheck (not (numberp dur))
(ny:error "GET-DURATION" 0 number-anon dur))
(let ((duration
(- (local-to-global (* (get-sustain) dur))
(setf *rslt* (local-to-global 0)))))
(cond ((minusp duration)
(error
"duration is less than zero: perhaps a warp or stretch
is ill-formed. Nyquist cannot continue because synthesis
functions assume durations are always positive.")))
duration))
(defun get-loud ()
(cond ((numberp *loud*) *loud*)
((soundp *loud*)
(sref *loud* 0))
(t
(error (format t "*LOUD* should be a number or sound: ~A" *LOUD*)))))
(defun get-sustain ()
(cond ((numberp *SUSTAIN*) *SUSTAIN*)
((soundp *SUSTAIN*)
;(display "get-sustain: lookup " (local-to-global 0) 0))
(sref *SUSTAIN* 0))
(t
(error (format t "*SUSTAIN* should be a number or sound: ~A" *SUSTAIN*)))))
(defun get-tempo ()
(if (warp-function *WARP*)
(slope (snd-inverse (get-warp) (local-to-global 0)
*control-srate*))
(/ 1.0 (warp-stretch *WARP*))))
(defun get-transpose ()
(cond ((numberp *TRANSPOSE*) *TRANSPOSE*)
((soundp *TRANSPOSE*)
(sref *TRANSPOSE* 0))
(t
(error (format t "*TRANSPOSE* should be a number or sound: ~A" *TRANSPOSE*)))))
(defun get-warp ()
(let ((f (warp-function *WARP*)))
(ny:typecheck (null f)
(error "In GET-WARP, there is no warp function, probably because you are not within WARP or WARP-ABS"))
(shift-time (scale-srate f (/ (warp-stretch *WARP*)))
(- (warp-time *WARP*)))))
(load "dspprims.lsp" :verbose NIL)
(load "fileio.lsp" :verbose NIL)
;;;;;;;;;;;;;;;;;;;;;;
;; OSCILATORS
;;;;;;;;;;;;;;;;;;;;;;
(defun build-harmonic (n table-size)
(ny:typecheck (not (integerp n))
(ny:error "BUILD-HARMONIC" 1 '((INTEGER) "n") n))
(ny:typecheck (not (integerp table-size))
(ny:error "BUILD-HARMONIC" 2 '((INTEGER) "table-size") table-size))
(ny:typecheck (>= n (/ table-size 2))
(error "In BUILD-HARMONIC, harmonic number should be less than half the table size"
(list n table-size)))
(snd-sine 0 n table-size 1))
(setf *SINE-TABLE* (list (build-harmonic 1 2048)
(hz-to-step 1.0)
T))
(setf *TABLE* *SINE-TABLE*)
(defun calculate-hz (pitch what &optional (max-fraction 0.5) maxlength)
(let ((hz (step-to-hz (+ pitch (get-transpose))))
(octaves 0) original)
(setf original hz)
(while (>= hz (* *SOUND-SRATE* max-fraction))
(setf octaves (1+ octaves)
hz (* hz 0.5)))
(cond ((> octaves 0)
(format t
"Warning: ~A frequency reduced by ~A octaves from ~A to ~A hz to avoid aliasing.\n"
what octaves original hz)
(setf octaves 0)))
(while (and maxlength (<= hz (/ *SOUND-SRATE* maxlength)))
(setf octaves (1+ octaves)
hz (* hz 2.0)))
(cond ((> octaves 0)
(format t
"Warning: ~A frequency increased by ~A octaves from ~A to ~A hz due to restriction on maximum table length.\n"
what octaves original hz)))
hz))
(defun ny:assert-env-spec (env-spec message)
(if (not (ny:env-spec-p env-spec))
(error message env-spec)))
(defun ny:assert-table (fun-name index formal actual)
(if (not (and (listp actual) (= 3 (length actual))))
(error (format nil
"In ~A,~A argument (~A) should be a list of 3 elements, got ~A"
fun-name (index-to-string index) formal actual)))
(if (not (soundp (car actual)))
(error (format nil
"In ~A,~A argument (~A) should be a list beginning with a sound, got ~A"
fun-name (index-to-string index) formal actual)))
(if (not (numberp (second actual)))
(error (format nil
"In ~A,~A argument (~A) should be a list whose 2nd element is a step number (pitch), got ~A"
fun-name (index-to-string index) formal actual)))
(if (not (third actual))
(error (format nil
"In ~A,~A argument (~A) should be a list whose 3rd element is true, got ~A"
fun-name (index-to-string index) formal actual))))
(defun ny:assert-sample (fun-name index formal actual)
(if (not (and (listp actual) (= 3 (length actual))))
(error (format nil
"In ~A,~A argument (~A) should be a list of 3 elements, got ~A"
fun-name (index-to-string index) formal actual)))
(if (not (soundp (car actual)))
(error (format nil
"In ~A,~A argument (~A) should be a list beginning with a sound, got ~A"
fun-name (index-to-string index) formal actual)))
(if (not (numberp (second actual)))
(error (format nil
"In ~A,~A argument (~A) should be a list whose 2nd element is a step number (pitch), got ~A"
fun-name (index-to-string index) formal actual)))
(if (not (numberp (third actual)))
(error (format nil
"In ~A,~A argument (~A) should be a list whose 3rd element is the sample start time, got ~A"
fun-name (index-to-string index) formal actual))))
(defun ny:env-spec-p (env-spec)
(prog (len (rslt t))
(if (not (listp env-spec)) (return nil))
(setf len (length env-spec))
(if (< len 6) (return nil))
(if (> len 7) (return nil))
(dolist (x env-spec)
(cond ((not (numberp x))
(setf rslt nil)
(return nil))))
(return rslt)))
;; AMOSC
;;
(defun amosc (pitch modulation &optional (sound *table*) (phase 0.0))
(ny:typecheck (not (numberp pitch))
(ny:error "AMOSC" 1 '((STEP) "pitch") pitch))
(ny:typecheck (not (soundp modulation))
(ny:error "AMOSC" 2 '((SOUND) "modulation") modulation))
(ny:assert-table "AMOSC" 3 "table" sound)
(ny:typecheck (not (numberp phase))
(ny:error "AMOSC" 4 '((NUMBER) "phase") phase))
(let ((modulation-srate (snd-srate modulation))
(hz (calculate-hz pitch "amosc")))
(ny:scale-db (get-loud)
(snd-amosc
(car sound) ; samples for table
(cadr sound) ; step represented by table
*SOUND-SRATE* ; output sample rate
hz ; output hz
(local-to-global 0) ; starting time
modulation ; modulation
phase)))) ; phase
;; FMOSC
;;
;; modulation rate must be less than or equal to sound-srate, so
;; force resampling and issue a warning if necessary. snd-fmosc can
;; handle upsampling cases internally.
;;
(defun fmosc (pitch modulation &optional (sound *table*) (phase 0.0))
(ny:typecheck (not (numberp pitch))
(ny:error "FMOSC" 1 '((STEP) "pitch") pitch))
(ny:typecheck (not (soundp modulation))
(ny:error "FMOSC" 2 '((SOUND) "modulation") modulation))
(ny:assert-table "FMOSC" 3 "table" sound)
(ny:typecheck (not (numberp phase))
(ny:error "FMOSC" 4 '((NUMBER) "phase") phase))
(let ((modulation-srate (snd-srate modulation))
(hz (calculate-hz pitch "fmosc")))
(ny:scale-db (get-loud)
(snd-fmosc
(car sound) ; samples for table
(cadr sound) ; step represented by table
*SOUND-SRATE* ; output sample rate
hz ; output hz
(local-to-global 0) ; starting time
modulation ; modulation
phase)))) ; phase
;; FMFB
;;
;; this code is based on FMOSC above
;;
(defun fmfb (pitch index &optional (dur 1.0))
(ny:typecheck (not (numberp pitch))
(ny:error "FMFB" 1 '((STEP) "pitch") pitch))
(ny:typecheck (not (or (numberp index) (soundp index)))
(ny:error "FMFB" 2 '((NUMBER SOUND) "index") index))
(ny:typecheck (not (numberp dur))
(ny:error "FMFB" 3 '((NUMBER) "dur") dur))
(let ((hz (calculate-hz pitch "fmfb")))
(setf dur (get-duration dur))
(cond ((soundp index) (ny:fmfbv hz index))
(t
(ny:scale-db (get-loud)
(snd-fmfb (local-to-global 0)
hz *SOUND-SRATE* index dur))))))
;; private variable index version of fmfb
(defun ny:fmfbv (hz index)
(let ((modulation-srate (snd-srate index)))
(cond ((< *SOUND-SRATE* modulation-srate)
(format t "Warning: down-sampling FM modulation in fmfb~%")
(setf index (snd-down *SOUND-SRATE* index))))
(ny:scale-db (get-loud)
(snd-fmfbv (local-to-global 0) hz *SOUND-SRATE* index))))
;; BUZZ
;;
;; (ARGUMENTS ("long" "n") ("rate_type" "sr") ("double" "hz")
;; ("time_type" "t0") ("sound_type" "s_fm"))
;;
(defun buzz (n pitch modulation)
(ny:typecheck (not (integerp n))
(ny:error "BUZZ" 1 '((INTEGER) "number of harmonics") n))
(ny:typecheck (not (numberp pitch))
(ny:error "BUZZ" 2 '((STEP) "pitch") pitch))
(ny:typecheck (not (soundp modulation))
(ny:error "BUZZ" 3 '((SOUND) "modulation") modulation))
(let ((modulation-srate (snd-srate modulation))
(hz (calculate-hz pitch "buzz nominal")))
(cond ((< *SOUND-SRATE* modulation-srate)
(format t "Warning: down-sampling modulation in buzz~%")
(setf modulation (snd-down *SOUND-SRATE* modulation))))
(setf n (max n 1)) ; avoid divide by zero problem
(ny:scale-db (get-loud)
(snd-buzz n ; number of harmonics
*SOUND-SRATE* ; output sample rate
hz ; output hz
(local-to-global 0) ; starting time
modulation)))) ; freq. modulation
;; (HZOSC hz [table [phase]])
;;
;; similar to FMOSC, but without "carrier" frequency parameter
;; also, hz may be a scalar or a sound
;;
(defun hzosc (hz &optional (sound *table*) (phase 0.0))
(ny:typecheck (not (or (numberp hz) (soundp hz)))
(ny:error "HZOSC" 1 '((NUMBER SOUND) "hz") hz))
(ny:assert-table "HZOSC" 2 "table" sound)
(ny:typecheck (not (numberp phase))
(ny:error "HZOSC" 3 '((NUMBER) "phase") phase))
(let (hz-srate)
(cond ((numberp hz)
(osc (hz-to-step hz) 1.0 sound phase))
(t
(setf hz-srate (snd-srate hz))
(cond ((< *SOUND-SRATE* hz-srate)
(format t "Warning: down-sampling hz in hzosc~%")
(setf hz (snd-down *SOUND-SRATE* hz))))
(ny:scale-db (get-loud)
(snd-fmosc (car sound) ; samples for table
(cadr sound) ; step repr. by table
*SOUND-SRATE* ; output sample rate
0.0 ; dummy carrier
(local-to-global 0) ; starting time
hz phase))))))
;; (SIOSC-BREAKPOINTS tab0 t1 tab1 ... tn tabn)
;; converts times to sample numbers
;; NOTE: time-warping the spectral envelope seems
;; like the wrong thing to do (wouldn't it be better
;; to warp the parameters that control the spectra,
;; or don't warp at all?). Nominally, a note should
;; have a "score" or local time duration equal to the
;; SUSTAIN environment variable. (When sustain is 1.0
;; and no time-warping is in effect, the duration is 1).
;; So, scale all times by
;; (local-to-global (get-sustain))
;; so that if the final time tn = 1.0, we get a nominal
;; length note.
(defun siosc-breakpoints (breakpoints)
(prog (sample-count result (last-count 0) time-factor (index 0))
(setf time-factor
(- (local-to-global (get-sustain))
(local-to-global 0.0)))
(setf time-factor (* time-factor *SOUND-SRATE*))
(ny:typecheck (not (and (listp breakpoints)
(cdr breakpoints)
(cddr breakpoints)))
(error "In SIOSC, 3rd argument (breakpoints) must be a list with at least 3 elements"
breakpoints))
loop
(ny:typecheck (not (and (listp breakpoints)
(soundp (car breakpoints))))
(error (format nil
"In SIOSC, expected a sound in breakpoints list at index ~A"
index)
(car breakpoints)))
(push (car breakpoints) result)
(setf breakpoints (cdr breakpoints))
(setf index (1+ index))
(cond (breakpoints
(ny:typecheck (not (and (listp breakpoints)
(numberp (car breakpoints))))
(error (format nil
"In SIOSC, expected a number (time) in breakpoints list at index ~A"
index)
(car breakpoints)))
(setf sample-count (truncate
(+ 0.5 (* time-factor (car breakpoints)))))
(cond ((< sample-count last-count)
(setf sample-count (1+ last-count))))
(push sample-count result)
(setf last-count sample-count)
(setf breakpoints (cdr breakpoints))
(setf index (1+ index))
(cond (breakpoints
(go loop)))))
(setf result (reverse result))
(return result)))
;; SIOSC -- spectral interpolation oscillator
;;
;; modulation rate must be less than or equal to sound-srate, so
;; force resampling and issue a warning if necessary. snd-fmosc can
;; handle upsampling cases internally.
;;
(defun siosc (pitch modulation breakpoints)
(ny:typecheck (not (numberp pitch))
(ny:error "SIOSC" 1 '((STEP) "pitch") pitch))
(ny:typecheck (not (soundp modulation))
(ny:error "SIOSC" 2 '((SOUND) "modulation") modulation))
(let ((modulation-srate (snd-srate modulation))
(hz (calculate-hz pitch "siosc nominal")))
(cond ((< *SOUND-SRATE* modulation-srate)
(format t "Warning: down-sampling FM modulation in siosc~%")
(setf modulation (snd-down *SOUND-SRATE* modulation))))
(ny:scale-db (get-loud)
(snd-siosc (siosc-breakpoints breakpoints) ; tables
*SOUND-SRATE* ; output sample rate
hz ; output hz
(local-to-global 0) ; starting time
modulation)))) ; modulation
;; LFO -- freq &optional duration sound phase)
;;
;; Default duration is 1.0 sec, default sound is *TABLE*,
;; default phase is 0.0.
;;
(defun lfo (freq &optional (duration 1.0)
(sound *SINE-TABLE*) (phase 0.0))
(ny:typecheck (not (numberp freq))
(ny:error "LFO" 1 '((NUMBER) "freq") freq))
(ny:typecheck (not (numberp duration))
(ny:error "LFO" 2 '((NUMBER) "duration") duration))
(ny:assert-table "LFO" 3 "table" sound)
(ny:typecheck (not (numberp phase))
(ny:error "LFO" 4 '((NUMBER) "phase") phase))
(let ((d (get-duration duration)))
(if (minusp d) (setf d 0))
(cond ((> freq (/ *CONTROL-SRATE* 2))
(format t "Warning: lfo frequency (~A hz) will alias at current control rate (~A hz).\n"
freq *CONTROL-SRATE*)))
(ny:set-logical-stop
(snd-osc
(car sound) ; samples for table
(cadr sound) ; step represented by table
*CONTROL-SRATE* ; output sample rate
freq ; output hz
*rslt* ; starting time
d ; duration
phase) ; phase
duration)))
;; FMLFO -- like LFO but uses frequency modulation
;;
(defun fmlfo (freq &optional (sound *SINE-TABLE*) (phase 0.0))
(ny:typecheck (not (soundp freq))
(ny:error "FMLFO" 1 '((SOUND) "freq") freq))
(ny:assert-table "FMLFO" 2 "table" sound)
(ny:typecheck (not (numberp phase))
(ny:error "FMLFO" 3 '((NUMBER) "phase") phase))
(let ()
(cond ((numberp freq)
(lfo freq 1.0 sound phase))
((soundp freq)
(cond ((> (snd-srate freq) *CONTROL-SRATE*)
(setf freq (force-srate *CONTROL-SRATE* freq))))
(snd-fmosc (car sound) (cadr sound) *CONTROL-SRATE* 0.0
(local-to-global 0) freq phase))
(t
(error "frequency must be a number or sound")))))
;; OSC - table lookup oscillator
;;
(defun osc (pitch &optional (duration 1.0)
(sound *TABLE*) (phase 0.0))
(ny:typecheck (not (numberp pitch))
(ny:error "OSC" 1 '((STEP) "pitch") pitch))
(ny:typecheck (not (numberp duration))
(ny:error "OSC" 2 '((NUMBER) "duration") duration))
(ny:assert-table "OSC" 3 "table" sound)
(ny:typecheck (not (numberp phase))
(ny:error "OSC" 4 '((NUMBER) "phase") phase))
(let ((d (get-duration duration))
(hz (calculate-hz pitch "osc")))
(ny:set-logical-stop
(snd-scale (db-to-linear (get-loud))
(snd-osc
(car sound) ; samples for table
(cadr sound) ; step represented by table
*SOUND-SRATE* ; output sample rate
hz ; output hz
*rslt* ; starting time
d ; duration
phase)) ; phase
duration)))
;; PARTIAL -- sine osc with built-in envelope scaling
;;
(defun partial (steps env)
(ny:typecheck (not (numberp steps))
(ny:error "PARTIAL" 1 '((STEP) "steps") steps))
(ny:typecheck (not (soundp env))
(ny:error "PARTIAL" 2 '((SOUND) "env") env))
(let ((hz (calculate-hz steps "partial")))
(ny:scale-db (get-loud)
(snd-partial *sound-srate* hz
(force-srate *sound-srate* env)))))
(setf *SINE-SAMPLE* (list (first *TABLE*) (second *TABLE*) 0.0))
;; SAMPLER -- simple attack + sustain sampler
;;
(defun sampler (pitch modulation
&optional (sample *SINE-SAMPLE*) (npoints 2))
(ny:typecheck (not (numberp pitch))
(ny:error "SAMPLER" 1 '((STEP) "pitch") pitch))
(ny:typecheck (not (soundp modulation))
(ny:error "SAMPLER" 2 '((SOUND) "modulation") modulation))
(ny:assert-sample "SAMPLER" 3 "table" sample)
(ny:typecheck (not (integerp npoints))
(ny:error "SAMPLER" 3 '((INTEGER) "npoints") npoints))
(let ((samp (car sample))
(samp-pitch (cadr sample))
(samp-loop-start (caddr sample))
(hz (calculate-hz pitch "sampler nominal")))
; make a waveform table look like a sample with no attack:
(cond ((not (numberp samp-loop-start))
(setf samp-loop-start 0.0)))
(ny:scale-db (get-loud)
(snd-sampler
samp ; samples for table
samp-pitch ; step represented by table
samp-loop-start ; time to start loop
*SOUND-SRATE* ; output sample rate
hz ; output hz
(local-to-global 0) ; starting time
modulation ; modulation
npoints)))) ; number of interpolation points
;; SINE -- simple sine oscillator
;;
(defun sine (steps &optional (duration 1.0))
(ny:typecheck (not (numberp steps))
(ny:error "SINE" 1 '((STEP) "steps") steps))
(ny:typecheck (not (numberp duration))
(ny:error "SINE" 2 '((NUMBER) "duration") duration))
(let ((hz (calculate-hz steps "sine"))
(d (get-duration duration)))
(ny:set-logical-stop
(ny:scale-db (get-loud)
(snd-sine *rslt* hz *sound-srate* d))
duration)))
;; PLUCK
;;
;; (ARGUMENTS ("double" "sr") ("double" "hz") ("time_type" "t0")
;; ("time_type" "d") ("double" "final_amp"))
;;
(defun pluck (steps &optional (duration 1.0) (final-amp 0.001))
(ny:typecheck (not (numberp steps))
(ny:error "PLUCK" 1 '((NUMBER) "steps") steps))
(ny:typecheck (not (numberp duration))
(ny:error "PLUCK" 2 '((NUMBER) "duration") duration))
(ny:typecheck (not (numberp final-amp))
(ny:error "PLUCK" 3 '((NUMBER) "final-amp") final-amp))
;; 200000 is MAXLENGTH in nyquist/tran/pluck.alg - the max table length
(let ((hz (calculate-hz steps "pluck" (/ 1.0 3) 200000))
(d (get-duration duration)))
(ny:set-logical-stop
(ny:scale-db (get-loud)
(snd-pluck *SOUND-SRATE* hz *rslt* d final-amp))
duration)))
;; abs-env -- restore the standard environment
;;
(defmacro abs-env (s)
`(progv '(*WARP* *LOUD* *TRANSPOSE* *SUSTAIN*
*START* *STOP*
*CONTROL-SRATE* *SOUND-SRATE*)
(list '(0.0 1.0 NIL) 0.0 0.0 1.0
MIN-START-TIME MAX-STOP-TIME
*DEFAULT-CONTROL-SRATE* *DEFAULT-SOUND-SRATE*)
,s))
;; (NYQ:TO-ARRAY SOUND N) - duplicate SOUND to N channels
;
(defun nyq:to-array (value len)
(let ((a (make-array len)))
(dotimes (i len)
(setf (aref a i) value))
a))
; nyq:add2 - add two arguments.
;
; Assumes s1 and s2 are numbers, sounds, or multichannel sounds or numbers
;
; Semantics: numbers and sounds can be freely mixed and
; add as expected. Arrays (multichannel) arguments are
; added channel-by-channel, and if one array is larger,
; the "extra" channels are simply copied to the result.
; Therefore the result has the channel count of the
; maximum channel count in s1 or s2. When adding a
; multichannel sound to a (non-multichannel) sound, the
; sound is coerced to a 1-channel multi-channel sound,
; and therefore adds to channel 1 of the multi-channel
; sound. However, when adding a multichannel sound to a
; number, the number is added to *every* channel.
; Semantics differ from the normal multichan-expand processing
; in that sounds are considered to be a multichannel sound
; with 1 channel, and channel counts do not have to match
; when processing array arguments.
;
(defun nyq:add2 (s1 s2)
; make number + number as fast as possible:
(cond ((and (numberp s1) (numberp s2)) (+ s1 s2))
; if not 2 numbers, the overhead here is amortized by
; computing samples of at least one sound
((and (arrayp s1) (numberp s2))
(sum-of-arrays s1 (nyq:to-array s2 (length s1))))
((and (arrayp s2) (numberp s1))
(sum-of-arrays (nyq:to-array s1 (length s2)) s2))
((and (arrayp s1) (soundp s2))
(sum-of-arrays s1 (vector s2)))
((and (arrayp s2) (soundp s1))
(sum-of-arrays (vector s1) s2))
((and (arrayp s1) (arrayp s2))
(sum-of-arrays s1 s2))
((numberp s1)
(snd-offset s2 s1))
((numberp s2)
(snd-offset s1 s2))
(t
(nyq:add-2-sounds s1 s2))))
; (NYQ:ADD-2-SOUNDS S1 S2) - add two sound arguments
;
; assumes s1 and s2 are sounds
;
(defun nyq:add-2-sounds (s1 s2)
(let ((s1sr (snd-srate s1))
(s2sr (snd-srate s2)))
(cond ((> s1sr s2sr)
(snd-add s1 (snd-up s1sr s2)))
((< s1sr s2sr)
(snd-add (snd-up s2sr s1) s2))
(t
(snd-add s1 s2)))))
(defmacro at (x s)
`(progv '(*WARP*)
(let ((shift ,x))
(ny:typecheck (not (numberp shift))
(error "1st argument of AT (or 2nd argument of SAL's @ operator) should be a time offset number" shift))
(list (list (+ (warp-time *WARP*)
(* (warp-stretch *WARP*) shift))
(warp-stretch *WARP*)
(warp-function *WARP*))))
,s))
;; (AT-ABS t behavior) evaluate behavior at global time t
;;
;; *WARP* is the triple (d s f) denoting the function f(st+d),
;; a mapping from local to global time.
;; We want (d' s f) such that f(s*0 + d') = t
;; (Note that we keep the same s and f, and only change the offset.
;; To eliminate the warp and stretch use "(abs-env (at t behavior))")
;; Applying the inverse of f, d' = f-1(t), or (sref (snd-inverse f ...) t)
;; Rather than invert the entire function just to evaluate at one point,
;; we use SREF-INVERSE to find d'.
;;
(defmacro at-abs (x s)
`(progv '(*WARP*)
(let ((tim ,x))
(ny:typecheck (not (numberp tim))
(error "1st argument of AT-ABS (or 2nd argument of SAL's @@ operator) should be a number (start time)" tim))
(if (warp-function *WARP*)
(list (list (sref-inverse (warp-function *WARP*) tim)
(warp-stretch *WARP*)
(warp-function *WARP*)))
(list (list tim (warp-stretch *WARP*) NIL))))
;; issue warning if sound starts in the past
(check-t0 ,s ',s)))
(defun check-t0 (s src)
(let (flag t0 (now (local-to-global 0)))
(cond ((arrayp s)
(dotimes (i (length s))
(setf t0 (snd-t0 (aref s i))))
(if (< t0 now) (setf flag t0)))
(t
(setf t0 (snd-t0 s))
(if (< t0 now) (setf flag t0))))
(if flag
(format t "Warning: cannot go back in time to ~A, sound came from ~A~%"
flag src))
; (display "check-t0" t0 now src)
; return s whether or not warning was reported
s))
;; (CLIP S1 VALUE) - clip maximum amplitude to value
;
(defun clip (x v)
(ny:typecheck (not (or (numberp x) (soundp x) (multichannelp x)))
(ny:error "CLIP" 1 number-sound-anon x t))
(ny:typecheck (not (numberp v))
(ny:error "CLIP" 2 number-anon v))
(cond ((numberp x)
(max (min x v) (- v)))
((arrayp x)
(let* ((len (length x))
(result (make-array len)))
(dotimes (i len)
(setf (aref result i)
(snd-clip (aref x i) v)))
result))
(t ;; x is a sound
(snd-clip x v))))
;; (NYQ:COERCE-TO S1 S2) - expand sound s1 to type of s2
;
(defun nyq:coerce-to (s1 s2)
(cond ((or (soundp s1) (numberp s1))
(cond ((arrayp s2)
(nyq:to-array s1 (length s2)))
(t s1)))
(t s1)))
(defmacro continuous-control-warp (beh)
`(snd-compose (warp-abs nil ,beh)
(snd-inverse (get-warp)
(local-to-global 0) *control-srate*)))
(defmacro continuous-sound-warp (beh)
`(snd-compose (warp-abs nil ,beh)
(snd-inverse (get-warp)
(local-to-global 0) *sound-srate*)))
(defmacro control-srate-abs (r s)
`(let ((rate ,r))
(progv '(*CONTROL-SRATE*)
(progn (ny:typecheck (not (numberp rate))
(ny:error "CONTROL-SRATE-ABS" 1 '((NUMBER) "sample rate") rate))
(list rate))
,s)))
; db = 20log(ratio)
; db = 20 ln(ratio)/ln(10)
; db/20 = ln(ratio)/ln(10)
; db ln(10)/20 = ln(ratio)
; e^(db ln(10)/20) = ratio
;
(setf ln10over20 (/ (log 10.0) 20))
(defun db-to-linear (x)
(ny:typecheck (not (or (numberp x) (soundp x) (multichannelp x)))
(ny:error "DB-TO-LINEAR" 0 number-sound-anon x t))
(cond ((numberp x)
(exp (* ln10over20 x)))
((arrayp x)
(let* ((len (length x))
(result (make-array len)))
(dotimes (i len)
(setf (aref result i)
(snd-exp (snd-scale ln10over20 (aref x i)))))
result))
(t
(snd-exp (snd-scale ln10over20 x)))))
(defun linear-to-db (x)
(ny:typecheck (not (or (numberp x) (soundp x) (multichannelp x)))
(ny:error "LINEAR-TO-DB" 0 number-sound-anon x t))
(cond ((numberp x)
(/ (log (float x)) ln10over20))
((arrayp x)
(let* ((len (length x))
(result (make-array len)))
(dotimes (i len)
(setf (aref result i)
(snd-scale (/ 1.0 ln10over20) (snd-log (aref x i)))))
result))
(t
(snd-scale (/ 1.0 ln10over20) (snd-log x)))))
(cond ((not (fboundp 'scalar-step-to-hz))
(setfn scalar-step-to-hz step-to-hz)
(setfn scalar-hz-to-step hz-to-step)))
(defun step-to-hz (x)
(ny:typecheck (not (or (numberp x) (soundp x) (multichannelp x)))
(ny:error "STEP-TO-HZ" 0 number-sound-anon x t))
(cond ((numberp x)
(scalar-step-to-hz x))
((arrayp x)
(let* ((len (length x))
(result (make-array len)))
(dotimes (i len)
(setf (aref result i) (step-to-hz (aref x i))))
result))
(t
(s-exp (snd-offset (snd-scale 0.0577622650466621 x)
2.1011784386926213)))))
(defun hz-to-step (x)
(ny:typecheck (not (or (numberp x) (soundp x) (multichannelp x)))
(ny:error "HZ-TO-STEP" 0 number-sound-anon x t))
(cond ((numberp x)
(scalar-hz-to-step x))
((arrayp x)
(let* ((len (length x))
(result (make-array len)))
(dotimes (i len)
(setf (aref result i) (hz-to-step (aref x i))))
result))
(t
(snd-scale 17.312340490667565
(snd-offset (s-log x) -2.1011784386926213)))))
; sref - access a sound at a given time point
; note that the time is transformed to global
(defun sref (sound point)
(ny:typecheck (not (soundp sound))
(ny:error "SREF" 1 '((SOUND) "sound") sound))
(ny:typecheck (not (numberp point))
(ny:error "SREF" 2 '((NUMBER) "time") point))
(snd-sref sound (local-to-global point)))
; extract - start is stretched and shifted as is stop
; result is shifted to start at local time zero
(defun extract (start stop sound)
(ny:typecheck (not (numberp start))
(ny:error "EXTRACT" 1 '((NUMBER) "start") start))
(ny:typecheck (not (numberp stop))
(ny:error "EXTRACT" 2 '((NUMBER) "stop") stop))