-
Notifications
You must be signed in to change notification settings - Fork 11
/
incudine.lisp
176 lines (140 loc) · 7.78 KB
/
incudine.lisp
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
;;;; incudine.lisp - the Incudine backend for cl-patterns.
;;; http://incudine.sourceforge.net
(in-package #:cl-patterns)
(defclass incudine (backend)
((name :initform "Incudine")
(buffer-preview-synth :initarg :buffer-preview-synth :initform :spt :accessor backend-buffer-preview-synth :type symbol :documentation "The name of the synth to use to preview buffers.")
(start-timestamp :initform nil :accessor backend-start-timestamp :type (or null local-time:timestamp) :documentation "The local-time timestamp when Incudine was started.")
(start-samples :initform 0 :accessor backend-start-samples :type integer :documentation "The result of `incudine:now' when `backend-start-timestamp' was generated."))
(:documentation "cl-patterns Incudine backend."))
;; (defmethod make-backend ((backend (eql 'incudine)) &rest rest &key &allow-other-keys)
;; (apply #'make-instance 'incudine rest))
;;; helper functions
(defun timestamp-incudine-samples (timestamp &optional (backend (find-backend 'incudine)))
"Convert a `local-time:timestamp' to an Incudine sample number.
See also: `incudine-samples-timestamp'"
(* (local-time:timestamp-difference timestamp (backend-start-timestamp backend))
(incudine:rt-sample-rate)))
(defun incudine-samples-timestamp (samples &optional (backend (find-backend 'incudine)))
"Convert an Incudine sample number to a `local-time:timestamp'.
See also: `timestamp-incudine-samples'"
(let* ((start-samples (backend-start-samples backend))
(diff (- samples start-samples)))
(when (minusp diff)
(warn "incudine-samples-timestamp: SAMPLES (~S) occurs before backend-start-samples (~S); result may be inaccurate." samples start-samples))
(local-time:timestamp+ (backend-start-timestamp backend) (/ diff (incudine:rt-sample-rate)) :sec)))
(defvar *incudine-start-timestamp* nil
"The local-time timestamp when Incudine was started.")
(defvar *incudine-start-samples* 0
"The result of `incudine:now' when `*incudine-start-timestamp*' was generated.")
(defun timestamp-to-incudine (timestamp) ; FIX: deprecated; remove
"Convert a local-time timestamp to an Incudine sample number."
(* (local-time:timestamp-difference timestamp *incudine-start-timestamp*)
(incudine:rt-sample-rate)))
(defun incudine-dsp-name (instrument)
"Get the name of an Incudine DSP."
(etypecase instrument
(symbol (find instrument (incudine.vug:all-dsp-names) :test #'string-equal))
(incudine:node (slot-value instrument 'incudine::name))))
;; (defmethod synth-controls ((backend incudine) synth)
;; (incudine.vug::dsp-properties-arguments (incudine.vug::get-dsp-properties (incudine-dsp-name synth))))
;; (defun incudine-make-dsp-args-list (event &optional use-kw)
;; "Given EVENT, return a list that can be applied to the DSP's creation function. With USE-KW, generate a plist.
;; NOTE: This function may produce incorrect results if you fail to provide a required parameter for a DSP."
;; (loop :for ctl :in (backend-instrument-controls 'incudine (incudine-dsp-name (instrument event)))
;; :for kwctl = (make-keyword ctl)
;; :for res = (multiple-value-list (event-value event kwctl))
;; :for val = (if (eql :gate kwctl)
;; 1
;; (elt res 0))
;; :for has = (elt res 1)
;; :if (and (null has)
;; (not (eql :gate kwctl)))
;; :do (setf use-kw t)
;; :append (when val
;; (if use-kw
;; (list kwctl val)
;; (list val)))))
(defun incudine-note (dsp-name args-list end-time)
(apply (fdefinition dsp-name) args-list)
(when (backend-instrument-has-gate-p 'incudine dsp-name)
(let ((node (slot-value (incudine:node 0) 'last)))
(incudine:at end-time #'incudine:set-control node :gate 0))))
;;; backend functions
(defmethod backend-start ((backend incudine) &key)
(unless *clock*
(warn "cl-patterns' ~S is nil; starting the clock on your behalf with ~S: ~S."
'*clock* 'start-clock-loop (start-clock-loop)))
(incudine:rt-start)
(setf *incudine-start-timestamp* (local-time:now)
*incudine-start-samples* (incudine:now)))
(defmethod backend-stop ((backend incudine))
(incudine:rt-stop)
backend)
(defmethod backend-tempo-change-at ((backend incudine) clock timestamp)
nil)
(defmethod backend-convert-object ((backend incudine) (object incudine:buffer) key)
(declare (ignore key))
object)
(defmethod backend-instrument-controls ((backend incudine) instrument)
(incudine.vug::dsp-properties-arguments (incudine.vug::get-dsp-properties (incudine-dsp-name instrument))))
(defmethod backend-all-instruments ((backend incudine))
(incudine.vug:all-dsp-names))
(defmethod backend-all-nodes ((backend incudine))
;; FIX; perhaps we can parse the output of (let ((stream (make-string-output-stream))) (incudine:dump 0 stream) stream) ?
)
(defmethod backend-node-p ((backend incudine) object)
(incudine:node-p object))
(defmethod backend-panic ((backend incudine))
(incudine:node-free-all))
(defmethod backend-timestamps-for-event ((backend incudine) event task)
(when (and (not *incudine-start-timestamp*)
(not (eql (incudine:rt-status) :started)))
(error "cl-patterns cannot generate a timestamp for Incudine as Incudine is not running. Try (backend-start 'incudine) if you want to use Incudine, or disable its backend."))
(let ((timestamp (or (raw-event-value event :timestamp-at-start)
(local-time:now))))
(mapcar #'timestamp-to-incudine (list timestamp (local-time:timestamp+ timestamp (dur-time (sustain event)) :sec)))))
(defmethod backend-event-timestamps ((backend incudine) event task)
(when (and (not (backend-start-timestamp backend))
(not (eql (incudine:rt-status) :started)))
(error "cl-patterns cannot generate a timestamp for Incudine as Incudine is not running. Try (backend-start 'incudine) if you want to use Incudine, or disable its backend."))
(let ((timestamp (or (raw-event-value event :timestamp-at-start)
(local-time:now))))
(mapcar (rcurry #'timestamp-incudine-samples backend)
(list timestamp (local-time:timestamp+ timestamp (dur-time (sustain event)) :sec)))))
(defmethod backend-proxys-node ((backend incudine) id)
;; FIX?
)
(defmethod backend-control-node-at ((backend incudine) time (node incudine:node) params)
(apply #'incudine:at
time
#'incudine:set-controls
node
params)
node)
;; FIX: maybe use incudine.vug::with-reserved-node for this?
(defmethod backend-control-node-at ((backend incudine) time (node symbol) params)
(if-let ((dsp (find node (incudine.vug:all-dsp-names) :test #'string-equal))) ; FIX: can we just assume it will be in the incudine.scratch package?
(let ((id (or (getf params :id)
(incf incudine::*last-node-id*)))) ; can we reserve a node ID with `incudine.vug::with-reserved-node' or the like?
(apply #'incudine:at time (fdefinition dsp) :id id params)
(incudine:node id)) ; this will show :ID NIL but it does actually point to the correct node.
(error "Unable to find an Incudine DSP with name ~S." node)))
(defmethod play ((node incudine:node))
t)
(defmethod stop ((node incudine:node))
(incudine:free node))
(defmethod end ((node incudine:node))
(incudine:set-control node :gate 0))
(defmethod play ((buffer incudine:buffer))
(let* ((backend (find-backend 'incudine))
(dsp (backend-buffer-preview-synth backend))
(dsp-controls (backend-instrument-controls 'incudine dsp)))
(play (event :backend 'incudine
;; :type :play ; to avoid automatically stopping it ; FIX: implement this note type
:instrument dsp
(find-buffer-symbol dsp-controls) buffer
:dur 16
:quant 0
:latency 0))))
(export '(incudine))