[7b58b4]: src / vug / vug.lisp Maximize Restore History

Download this file

vug.lisp    766 lines (671 with data), 30.4 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
;;; Copyright (c) 2013 Tito Latini
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
(in-package :incudine.vug)
(defvar *vug-hash* (make-hash-table))
(declaim (type hash-table *vug-hash*))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar *constructors-for-objects-to-free* nil)
(declaim (type list *constructors-for-objects-to-free*))
(defvar *object-to-free-hash* (make-hash-table))
(declaim (type hash-table *object-to-free-hash*))
(defmacro object-to-free (name reinit-symbol)
`(progn
(pushnew ',name *constructors-for-objects-to-free*)
(setf (gethash ',name *object-to-free-hash*)
',reinit-symbol)))
(object-to-free make-array update-lisp-array)
(object-to-free make-foreign-array update-foreign-array))
;;; Virtual Unit Generator
(defstruct (vug (:copier nil))
(name nil :type symbol)
(args nil :type list)
(arg-types nil :type list))
(defstruct (vug-object (:copier nil))
(name nil)
(type t)
(block-p nil :type boolean))
;;; Parameter to control the DSP
(defstruct (vug-parameter (:include vug-object)
(:constructor %make-vug-parameter)
(:copier nil))
(value nil)
(varname nil)
(aux-varname nil)
(vars-to-update nil :type list))
(defstruct (vug-symbol (:include vug-object) (:copier nil)))
(defstruct (vug-variable (:include vug-object)
(:constructor %make-vug-variable)
(:copier nil))
(value nil)
(to-set-p t :type boolean)
(skip-init-set-p nil :type boolean)
(conditional-expansion nil :type symbol)
(input-p nil :type boolean)
(init-time-p t :type boolean)
(performance-time-p nil :type boolean)
(variables-to-recheck nil :type list))
(defstruct (vug-variables (:copier nil))
(bindings nil :type list)
(from-parameters nil :type list)
(parameter-list nil :type list)
(to-update nil :type list)
(to-free nil :type list)
;; List of the variables with type SAMPLE
(foreign-sample nil :type list)
;; List of the variables with type single and double float
(foreign-float nil :type list)
(foreign-double nil :type list)
;; List of the variables with unboxed type 32-bit integer
;; (used only on a 32-bit system)
(foreign-int32 nil :type list)
;; List of the variables with unboxed type 64-bit integer
(foreign-int64 nil :type list))
(defstruct (vug-function (:include vug-object)
(:copier nil))
(inputs nil :type list))
(defstruct (vug-declaration (:include vug-object)
(:constructor %make-vug-declaration)
(:copier nil))
(spec nil))
(defvar *vug-variables* nil)
(declaim (type (or vug-variables null) *vug-variables*))
(defmethod print-object ((obj vug) stream)
(format stream "#<VUG ~A>" (vug-name obj)))
(defmethod print-object ((obj vug-object) stream)
(format stream "#<~A ~A>" (type-of obj) (vug-object-name obj)))
(defgeneric init-time-p (obj))
(defmethod init-time-p ((obj vug-parameter))
(declare (ignore obj))
t)
(defmethod init-time-p ((obj vug-variable))
(vug-variable-init-time-p obj))
(defgeneric performance-time-p (obj))
(defmethod performance-time-p ((obj vug-parameter))
(declare (ignore obj))
nil)
(defmethod performance-time-p ((obj vug-variable))
(vug-variable-performance-time-p obj))
(declaim (inline vug))
(defun vug (name)
(declare (type symbol name))
(values (gethash name *vug-hash*)))
(declaim (inline vug-object-name-string))
(defun vug-object-name-string (obj)
(symbol-name (vug-object-name obj)))
(declaim (inline make-vug-parameter))
(defun make-vug-parameter (name value type)
(let ((par (%make-vug-parameter :name name :value value
:aux-varname (gensym (string name))
:type type)))
(push par (vug-variables-parameter-list *vug-variables*))
par))
(declaim (inline vug-input-p))
(defun vug-input-p (obj)
(and (vug-function-p obj)
(eq (vug-function-name obj) 'vug-input)))
(defun make-vug-variable (name value &optional type)
(multiple-value-bind (input-p value)
(if (vug-input-p value)
(values t (car (vug-function-inputs value)))
(values nil value))
(let ((obj (%make-vug-variable :name name :value value :type type
:input-p input-p)))
(when *vug-variables*
(if (performance-time-code-p obj value input-p)
(setf (vug-variable-init-time-p obj) nil)
(resolve-variable-to-update value obj))
(when (object-to-free-p value)
(pushnew obj (vug-variables-to-free *vug-variables*)))
(push obj (vug-variables-bindings *vug-variables*)))
obj)))
(declaim (inline vug-name-p))
(defun vug-name-p (vug name)
(eq (vug-object-name vug) name))
(declaim (inline vug-type-p))
(defun vug-type-p (vug type)
(eq (vug-object-type vug) type))
(declaim (inline init-time-vug-function-p))
(defun init-time-vug-function-p (obj)
(member (vug-object-name obj) '(init-only lambda)))
(declaim (inline performance-time-function-p))
(defun performance-time-function-p (fname)
(member fname '(now audio-in audio-out bus)))
(declaim (inline performance-time-vug-function-p))
(defun performance-time-vug-function-p (obj)
(performance-time-function-p (vug-object-name obj)))
(defun performance-time-code-p (var obj vug-input-p)
(labels ((ptime-p (obj)
(cond ((vug-object-p obj)
(cond ((vug-object-block-p obj) t)
((vug-function-p obj)
(unless (init-time-vug-function-p obj)
(or (performance-time-vug-function-p obj)
(ptime-p (vug-function-inputs obj)))))
((vug-variable-p obj)
(cond ((vug-variable-init-time-p obj)
(if (or vug-input-p
(vug-variable-input-p obj))
(pushnew var
(vug-variable-variables-to-recheck obj)))
(ptime-p (vug-variable-value obj)))
(t t)))))
((consp obj)
(or (ptime-p (car obj))
(ptime-p (cdr obj)))))))
(ptime-p obj)))
(defun resolve-variable-to-update (obj var)
(cond ((vug-function-p obj)
(resolve-variable-to-update (vug-function-inputs obj) var))
((vug-variable-p obj)
(resolve-variable-to-update (vug-variable-value obj) var))
((vug-parameter-p obj)
(unless (member var (vug-parameter-vars-to-update obj))
(variable-to-update var obj)))
((consp obj)
(resolve-variable-to-update (car obj) var)
(resolve-variable-to-update (cdr obj) var))))
(declaim (inline variable-to-update))
(defun variable-to-update (var param)
(push var (vug-parameter-vars-to-update param))
(pushnew param (vug-variables-to-update *vug-variables*)))
(declaim (inline make-vug-declaration))
(defun make-vug-declaration (spec)
(declare (type list spec))
(let ((obj (%make-vug-declaration :name (car spec) :spec spec)))
(dolist (decl spec)
(case (car decl)
(type (let ((type (second decl)))
(dolist (i (cddr decl))
(when (vug-object-p i)
(setf (vug-object-type i) type)))))
(performance-time
(dolist (i (cdr decl))
(if (vug-variable-p i)
(setf (vug-variable-performance-time-p i) t)
(msg error
"performance-time declaration, ~A is not a VUG variable" i))))))
obj))
(declaim (inline vug-block-p))
(defun vug-block-p (object)
(let ((vug (gethash object *vug-hash*)))
(and vug (not (macro-function (vug-name vug))))))
(declaim (inline function-call-p))
(defun function-call-p (lst)
(and (symbolp (car lst)) (fboundp (car lst))))
(declaim (inline init-binding-form-p))
(defun init-binding-form-p (lst)
(eq (car lst) 'with))
(declaim (inline binding-form-p))
(defun binding-form-p (lst)
(member (car lst) '(let let* symbol-macrolet compiler-let)))
(declaim (inline declare-form-p))
(defun declare-form-p (lst)
(eq (car lst) 'declare))
(declaim (inline setter-form-p))
(defun setter-form-p (obj)
(member obj '(setf setq incf decf psetf psetq)))
(declaim (inline quote-symbol-p))
(defun quote-symbol-p (lst)
(and (eq (first lst) 'quote) (atom (second lst))))
(declaim (inline quote-function-p))
(defun quote-function-p (lst)
(eq (car lst) 'function))
(declaim (inline local-buffer-p))
(defun local-buffer-p (obj)
(and (vug-function-p obj)
(vug-name-p obj 'incudine:make-buffer)))
(declaim (inline object-to-free-p))
(defun object-to-free-p (obj)
(and (vug-function-p obj)
(member (vug-object-name obj) *constructors-for-objects-to-free*)))
(declaim (inline parse-bindings))
(defun parse-bindings (lst flist mlist)
(mapcar (lambda (x)
(list (car x)
`(make-vug-variable
(gensym (string ',(car x)))
(remove-wrapped-parens
,(let ((value (cadr x)))
(parse-vug-def (if (vug-parameter-p value)
(vug-parameter-value value)
value)
nil flist mlist))))))
lst))
(declaim (inline parse-block-form))
(defun parse-block-form (form flist mlist)
`(make-vug-function :name ',(car form)
:inputs (list ',(cadr form) ,@(parse-vug-def (cddr form)
nil flist mlist))))
(declaim (inline separate-declaration))
(defun separate-declaration (form)
(let (acc)
(do ((l form (cdr l)))
((null (and (consp (car l))
(declare-form-p (car l))))
(values (nreverse acc) l))
(push (car l) acc))))
(defun parse-lambda-body (form flist mlist)
(multiple-value-bind (decl rest)
(separate-declaration form)
`(,@(mapcar (lambda (x)
`(make-vug-function :name 'declare :inputs ',(cdr x)))
decl)
,@(parse-vug-def rest nil flist mlist))))
(declaim (inline parse-let-form))
(defun parse-let-form (form flist mlist)
(let ((args (cadr form)))
`(let ,(mapcar (lambda (x) `(,(car x) ',(car x)))
args)
(declare (type symbol ,@(mapcar #'car args)))
(make-vug-function :name ',(car form)
:inputs (list (list ,@(mapcar (lambda (x)
`(list ,(car x)
,(parse-vug-def (cadr x) nil
flist mlist)))
args))
,@(parse-lambda-body (cddr form) flist mlist))))))
(declaim (inline parse-init-bindings))
(defun parse-init-bindings (form flist mlist)
`(,(car form)
,(parse-bindings (cadr form) flist mlist)
,@(multiple-value-bind (decl rest)
(separate-declaration (cddr form))
`(,@(parse-vug-def decl)
(make-vug-function :name 'progn
:inputs (list ,@(parse-vug-def rest nil flist mlist)))))))
(declaim (inline parse-lambda-form))
(defun parse-lambda-form (form flist mlist)
(let ((args (cadr form)))
`(let ,(lambda-bindings args)
(make-vug-function :name ',(car form)
:inputs (list ',args
(list ,@(parse-lambda-body (cddr form)
flist mlist)))))))
(declaim (inline parse-declare-form))
(defun parse-declare-form (form)
(case (car form)
((type ftype) `(list 'type ',(second form) ,@(cddr form)))
((dynamic-extent ignore optimize inline special ignorable notinline)
`(list ',(car form) ,@(cdr form)))
(otherwise (if (string-equal (symbol-name (car form))
"PERFORMANCE-TIME")
`(list 'performance-time ,@(cdr form))
`(list 'type ',(car form) ,@(cdr form))))))
(defmacro %with-local-args (args &body body)
`(let ,(mapcar (lambda (x) `(,x ',x)) args)
(declare (ignorable ,@args))
,@body))
(defun parse-flet-form (form flist mlist)
(declare (type list form flist mlist))
(let (acc)
`(make-vug-function :name ',(car form)
:inputs (list (list ,@(mapcar (lambda (def)
(push (car def) acc)
`(list ',(car def)
',(cadr def)
(%with-local-args ,(cadr def)
,@(parse-vug-def (cddr def)
nil flist mlist))))
(cadr form)))
,@(parse-vug-def (cddr form) nil (nconc acc flist) mlist)))))
(defun parse-labels-form (form flist mlist)
(declare (type list form flist mlist))
(let ((acc (copy-list flist))
(definitions (cadr form)))
(dolist (l definitions)
(push (car l) acc))
`(make-vug-function :name ',(car form)
:inputs (list (list ,@(mapcar (lambda (def)
`(list ',(car def)
',(cadr def)
(%with-local-args ,(cadr def)
,@(parse-vug-def (cddr def) nil
acc mlist))))
definitions))
,@(parse-vug-def (cddr form) nil acc mlist)))))
(defun parse-macrolet-form (form flist mlist)
(declare (type list form flist mlist))
(let ((acc (copy-list mlist))
(definitions (cadr form)))
(dolist (l definitions)
(push l acc))
`(make-vug-function :name 'progn
:inputs (list ,@(parse-vug-def (cddr form) nil flist acc)))))
(declaim (inline parse-locally-form))
(defun parse-locally-form (form flist mlist)
`(make-vug-function :name ',(car form)
:inputs ,(multiple-value-bind (decl rest)
(separate-declaration (cdr form))
`(list ,@(mapcar (lambda (x)
`(make-vug-function :name 'declare
:inputs ',(cdr x)))
decl)
,@(parse-vug-def rest nil flist mlist)))))
(declaim (inline parse-tagbody-form))
(defun parse-tagbody-form (form flist mlist)
`(make-vug-function :name ',(car form)
:inputs (list ,@(mapcar (lambda (x)
(if (atom x)
(list 'quote x)
(parse-vug-def x nil flist mlist)))
(cdr form)))))
(declaim (inline parse-go-form))
(defun parse-go-form (form)
`(make-vug-function :name ',(car form) :inputs '(,(cadr form))))
(declaim (inline parse-tick-form))
(defun parse-tick-form (form flist mlist)
(if (atom (cadr form))
`(make-vug-symbol :name ',(cadr form) :block-p t)
`(make-vug-function :name 'progn
:inputs (list ,@(parse-vug-def (cdr form)
t flist mlist))
:block-p t)))
(declaim (inline lambda-bindings))
(defun lambda-bindings (args)
(loop for i in args
unless (member i '(&optional &key &rest))
collect `(,i ',i)))
;;; The follow functions are only "tags"
(defun tick (&rest forms) forms)
(defun external-variable (obj) (identity obj))
(defun update (obj) (identity obj))
(defun init-only (&rest forms) forms)
(defun initialize (&rest forms) forms)
(defmacro without-follow (parameters &body body)
parameters body)
(defun vug-input (arg) arg)
;;; MAYBE-EXPAND is used inside the definition of a VUG, when the
;;; expansion of one or more performance-time variables is to inhibit
;;; after a particular point of the code (i.e. loop or condition). It
;;; avoids the obscure isolated vug-variables in the body of a VUG.
(defmacro maybe-expand (&body body)
`(progn ,@body))
(defun parse-vug-def (def &optional cdr-p flist mlist quote-expr-p)
(declare (type boolean cdr-p quote-expr-p)
(type list flist mlist))
(cond ((null def) nil)
((consp def)
(let ((name (car def)))
(cond ((consp name)
(cons (parse-vug-def name nil flist mlist)
(parse-vug-def (cdr def) t flist mlist)))
((null cdr-p)
(cond ((init-binding-form-p def)
(parse-init-bindings def flist mlist))
((declare-form-p def)
`(make-vug-declaration
(list ,@(mapcar #'parse-declare-form (cdr def)))))
((eq name 'locally)
(parse-locally-form def flist mlist))
((member name flist) ; local function
`(make-vug-function :name ',name
:inputs (list ,@(parse-vug-def (cdr def) t flist mlist))))
((member name mlist :key #'car) ; local macro
(destructuring-bind (lambda-list body) (cdr (assoc name mlist))
(parse-vug-def
(eval `(destructuring-bind ,lambda-list ',(cdr def) ,body))
nil flist mlist)))
((function-call-p def)
(cond ((binding-form-p def) (parse-let-form def flist mlist))
((vug-block-p name)
(cons name (parse-vug-def (cdr def) t flist mlist)))
((eq name 'tick) (parse-tick-form def flist mlist))
((eq name 'get-pointer)
;; Inhibit the expansion of GET-POINTER macro
`(make-vug-function :name 'get-pointer
:inputs (list ,@(parse-vug-def (cdr def) t flist mlist))))
((eq name 'external-variable)
`(make-vug-symbol :name ',(cadr def)))
((eq name 'without-follow)
`(make-vug-function :name ',name
:inputs (list (list ,@(cadr def))
,@(parse-vug-def (cddr def)))))
((quote-function-p def)
`(make-vug-function :name 'function
:inputs (list ',(cadr def))))
((eq name 'lambda) (parse-lambda-form def flist mlist))
((eq name 'flet) (parse-flet-form def flist mlist))
((eq name 'labels)
(parse-labels-form def flist mlist))
((eq name 'macrolet)
(parse-macrolet-form def flist mlist))
((and (macro-function name)
;; Avoid the expansion of the setter forms.
;; This information is used during the code generation.
(not (setter-form-p name)))
(let ((expansion (macroexpand-1
(if (member name
'(with-samples with-samples*))
;; The bindings of the initialization
;; are sequential, so there is not
;; difference between WITH-SAMPLES
;; and WITH-SAMPLES* inside a definition
;; of a VUG
`(%with-samples ,@(cdr def))
def))))
(if (vug name)
`(mark-vug-block ,(parse-vug-def expansion nil flist mlist))
(parse-vug-def expansion nil flist mlist))))
((quote-symbol-p def)
`(make-vug-symbol :name '',(second def)))
((member name '(block return-from the catch throw))
(parse-block-form def flist mlist))
((eq name 'tagbody)
(parse-tagbody-form def flist mlist))
((eq name 'go) (parse-go-form def))
((eq name 'quote)
`(make-vug-function :name 'quote :inputs ',(cdr def)))
#+(and sbcl x86)
((and (member name '(sin cos tan))
(not (and (consp (cadr def))
(eq (caadr def) 'the))))
;; X86 uses FSIN, FCOS and FPTAN
`(make-vug-function :name ',name
:inputs (list ,(parse-vug-def `(the limited-sample
,@(cdr def))))))
(t `(make-vug-function :name ',name
:inputs (list ,@(parse-vug-def (cdr def) t
flist mlist))))))
(quote-expr-p `',def)
(t (cons name (parse-vug-def (cdr def) t flist mlist)))))
(t (cons (parse-vug-def name nil flist mlist)
(parse-vug-def (cdr def) t flist mlist))))))
(quote-expr-p `',def)
((eq def 'pi) (sample pi))
((and (symbolp def)
(or (boundp def)
(eq def '%dsp-node%)))
`(make-vug-symbol :name ',def))
((and (symbolp def) (eq def 'current-channel))
`(make-vug-symbol :name 'current-channel :block-p t))
((and (numberp def) (floatp def))
(sample def))
(t def)))
(defun remove-wrapped-parens (obj)
(if (consp obj)
(if (cdr obj)
obj
(remove-wrapped-parens (car obj)))
obj))
(defun remove-lisp-declaration (obj)
(if (consp obj)
(if (vug-declaration-p (car obj))
(remove-lisp-declaration (cdr obj))
(cons (remove-lisp-declaration (car obj))
(remove-lisp-declaration (cdr obj))))
obj))
(defun recheck-variables (var)
(declare (type vug-variable var))
(let ((vardep (pop (vug-variable-variables-to-recheck var))))
(when vardep
(if #1=(vug-variable-init-time-p vardep)
(setf #1# nil))
(recheck-variables var)
(recheck-variables vardep))))
(declaim (inline set-variable-performance-time))
(defun set-variable-performance-time (var)
(unless (vug-variable-performance-time-p var)
(setf (vug-variable-performance-time-p var) t))
(recheck-variables var)
var)
(declaim (inline update-setter-form))
(defun update-setter-form (obj)
(loop for i on (vug-function-inputs obj) by #'cddr
for var = (car i) do
(when (vug-variable-p var)
(set-variable-performance-time var))
(update-vug-variables (cadr i))))
(defun update-vug-variables (obj)
(cond ((and (vug-function-p obj)
(not (eq (vug-object-name obj) 'initialize)))
(cond ((eq (vug-object-name obj) 'update)
;; An updated variable is performance-time
(let ((var (car (vug-function-inputs obj))))
(set-variable-performance-time var)
(update-vug-variables (vug-variable-value var))))
((setter-form-p (vug-object-name obj))
;; A variable in a setter form in the body of the VUG
;; becomes performance-time
(update-setter-form obj))
(t (update-vug-variables (vug-function-inputs obj)))))
((consp obj)
(update-vug-variables (car obj))
(update-vug-variables (cdr obj))))
obj)
(defmacro vug-block (&body body)
`(mark-vug-block
(update-vug-variables
(fix-sequence-of-forms
(remove-wrapped-parens
(remove-lisp-declaration
(list ,@(parse-vug-def body))))))))
(declaim (inline fix-sequence-of-forms))
(defun fix-sequence-of-forms (obj)
(if (and (consp obj) (cdr obj))
(make-vug-function :name 'progn :inputs obj)
obj))
(declaim (inline mark-vug-block))
(defun mark-vug-block (obj)
(declare (type (or vug-object null)))
(when obj
(setf (vug-object-block-p obj) t)
obj))
(declaim (inline coerce-number))
(defun coerce-number (obj output-type-spec)
(if (numberp obj) (coerce obj output-type-spec) obj))
(defmacro with-coerce-arguments (bindings &body body)
`(let ,(mapcar (lambda (x)
(if (consp x)
`(,(car x) (coerce-number ,(car x) ',(cadr x)))
`(,x (coerce-number ,x 'sample)))) ; default
bindings)
,@body))
(defmacro with-argument-bindings ((args types &optional vug-input-p)
&body body)
`(with ,(mapcar (lambda (x) `(,x ,(if vug-input-p `(vug-input ,x) x)))
args)
,@(mapcar (lambda (arg type) `(declare (type ,type ,arg)))
args types)
,@body))
(declaim (inline arg-names-and-types))
(defun arg-names-and-types (lambda-list)
(let ((names) (types))
(dolist (arg lambda-list)
(cond ((consp arg)
(push (car arg) names)
(push (cadr arg) types))
(t (push arg names)
(push 'sample types))))
(values (nreverse names) (nreverse types))))
(defmacro define-vug (name lambda-list &body body)
(with-gensyms (fn init)
(multiple-value-bind (args types)
(arg-names-and-types lambda-list)
(multiple-value-bind (doc config vug-body)
(extract-vug-config body)
(if (dsp name)
(msg error "~A was defined to be a DSP." name)
`(progn
(defun ,name ,args
,doc
(flet ((,fn ,args
(with-coerce-arguments ,lambda-list
(vug-block
(with-argument-bindings (,args ,types t)
,@vug-body)))))
(let ((,init (getf ,config :pre-hook)))
(when ,init (funcall ,init))
(,fn ,@args))))
(setf (gethash ',name *vug-hash*)
(make-vug :name ',name
:args ',args
:arg-types ',types))))))))
(defmacro define-vug-macro (name lambda-list &body body)
(if (dsp name)
(msg error "~A was defined to be a DSP." name)
`(progn
(defmacro ,name ,lambda-list ,@body)
(setf (gethash ',name *vug-hash*)
(make-vug :name ',name
:args ',lambda-list)))))
(declaim (inline extract-vug-config))
(defun extract-vug-config (code)
(declare (type list code))
(let ((doc (when (stringp (car code))
(car code))))
(do ((l (if doc (cdr code) code) (cddr l))
(acc nil))
((or (null l) (not (keywordp (car l))))
(values doc (when acc `(list ,@acc)) l))
(push (second l) acc)
(push (first l) acc))))
(declaim (inline destroy-vug))
(defun destroy-vug (name)
(when (vug name)
(remhash name *vug-hash*)
(fmakunbound name)
(values)))
(declaim (inline all-vug-names))
(defun all-vug-names ()
(sort (loop for name being the hash-keys in *vug-hash*
when (find-symbol (symbol-name name))
collect name)
#'string-lessp :key #'symbol-name))
(defmacro with (bindings &body body)
`(let* ,bindings ,@body))
(defmacro %with-samples (bindings &body body)
`(with ,(mapcar (lambda (x)
(if (consp x)
(let ((value (cadr x)))
`(,(car x)
,(if (and (numberp value)
(not (typep value 'sample)))
(sample value)
value)))
`(,x ,+sample-zero+)))
bindings)
,@(when bindings
`((declare (type sample
,@(mapcar (lambda (x) (if (consp x) (car x) x))
bindings)))))
,@body))
;;; Used only inside the definition of a VUG-MACRO to specify the
;;; inputs of the VUG.
;;; BINDINGS is a list ((vug-varname1 value1) (vug-varname2 value2) ...)
(defmacro with-vug-inputs (bindings &body body)
`(with ,(mapcar (lambda (x) `(,(car x) (vug-input ,(cadr x))))
bindings)
,@body))