/
sound.clj
282 lines (235 loc) · 9.86 KB
/
sound.clj
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
(ns alda.sound
(:require [alda.sound.midi :as midi]
[alda.util :refer (parse-time
pdoseq-block
parse-position)]
[taoensso.timbre :as log])
(:import [com.softsynth.shared.time TimeStamp ScheduledCommand]
[com.jsyn.engine SynthesisEngine]))
(def ^:dynamic *synthesis-engine* nil)
(defn new-synthesis-engine
[]
(doto (SynthesisEngine.) .start))
(defn start-synthesis-engine!
[]
(alter-var-root #'*synthesis-engine* (constantly (new-synthesis-engine))))
(defn new-audio-context
[]
(atom
{:audio-types #{}
:synthesis-engine (or *synthesis-engine* (new-synthesis-engine))}))
(defn set-up?
[audio-ctx audio-type]
(contains? (:audio-types @audio-ctx) audio-type))
(defmulti set-up-audio-type!
(fn [audio-ctx audio-type & [score]] audio-type))
(defmethod set-up-audio-type! :default
[_ audio-type & [score]]
(log/errorf "No implementation of set-up-audio-type! defined for type %s"
audio-type))
(defmethod set-up-audio-type! :midi
[audio-ctx _ & [score]]
(log/debug "Setting up MIDI...")
(midi/get-midi-synth! audio-ctx))
(declare determine-audio-types)
(defn set-up!
"Does any necessary setup for one or more audio types.
e.g. for MIDI, create and open a MIDI synth."
([{:keys [audio-context] :as score}]
(let [audio-types (determine-audio-types score)]
(set-up! audio-context audio-types score)))
([audio-ctx audio-type & [score]]
(if (coll? audio-type)
(pdoseq-block [a-t audio-type]
(set-up! audio-ctx a-t score))
(when-not (set-up? audio-ctx audio-type)
(set-up-audio-type! audio-ctx audio-type score)
(swap! audio-ctx update :audio-types conj audio-type)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmulti refresh-audio-type!
(fn [audio-ctx audio-type & [score]] audio-type))
(defmethod refresh-audio-type! :default
[_ audio-type & [score]]
(log/errorf "No implementation of refresh-audio-type! defined for type %s"
audio-type))
(defmethod refresh-audio-type! :midi
[audio-ctx _ & [score]]
(midi/load-instruments! audio-ctx score))
(defn refresh!
"Performs any actions that may be needed each time the `play!` function is
called. e.g. for MIDI, load instruments into channels (this needs to be
done every time `play!` is called because new instruments may have been
added to the score between calls to `play!`, when using Alda live.)"
[audio-ctx audio-type & [score]]
(if (coll? audio-type)
(pdoseq-block [a-t audio-type]
(refresh! audio-ctx a-t score))
(when (set-up? audio-ctx audio-type)
(refresh-audio-type! audio-ctx audio-type score))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmulti tear-down-audio-type!
(fn [audio-ctx audio-type & [score]] audio-type))
(defmethod tear-down-audio-type! :default
[_ audio-type & [score]]
(log/errorf "No implementation of tear-down! defined for type %s" audio-type))
(defmethod tear-down-audio-type! :midi
[audio-ctx _ & [score]]
(midi/close-midi-synth! audio-ctx))
(defn tear-down!
"Does any necessary clean-up at the end.
e.g. for MIDI, close the MIDI synth."
([{:keys [audio-context] :as score}]
(let [audio-types (determine-audio-types score)]
(tear-down! audio-context audio-types score)))
([audio-ctx audio-type & [score]]
(if (coll? audio-type)
(pdoseq-block [a-t audio-type]
(tear-down! audio-ctx a-t score))
(when (set-up? audio-ctx audio-type)
(tear-down-audio-type! audio-ctx audio-type score)
(swap! audio-ctx update :audio-types disj audio-type)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmulti start-event!
"Kicks off a note/event, using the appropriate method based on the type of the
instrument."
(fn [audio-ctx event instrument]
(-> instrument :config :type)))
(defmethod start-event! :default
[_ _ instrument]
(log/errorf "No implementation of start-event! defined for type %s"
(-> instrument :config :type)))
(defmethod start-event! nil
[_ _ _]
:do-nothing)
(defmethod start-event! :midi
[audio-ctx note _]
(midi/play-note! audio-ctx note))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmulti stop-event!
"Ends a note/event, using the appropriate method based on the type of the
instrument."
(fn [audio-ctx event instrument]
(-> instrument :config :type)))
(defmethod stop-event! :default
[_ _ instrument]
(log/errorf "No implementation of start-event! defined for type %s"
(-> instrument :config :type)))
(defmethod stop-event! nil
[_ _ _]
:do-nothing)
(defmethod stop-event! :midi
[audio-ctx note _]
(midi/stop-note! audio-ctx note))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- score-length
"Given an event set from a score, calculates the length of the score in ms."
[event-set]
(let [events (filter :duration event-set)
note-end (fn [{:keys [offset duration] :as note}]
(+ offset duration))]
(if (and events (not (empty? events)))
(apply max (map note-end events))
0)))
(defn determine-audio-types
[{:keys [instruments] :as score}]
(set (for [[id {:keys [config]}] instruments]
(:type config))))
(def ^:dynamic *play-opts* {})
(defmacro with-play-opts
"Apply `opts` as overrides to *play-opts* when executing `body`"
[opts & body]
`(binding [*play-opts* (merge *play-opts* ~opts)]
~@body))
(defn- lookup-time [markers pos]
(let [pos (if (string? pos)
(parse-position pos)
pos)]
(cond (keyword? pos)
(or (markers (name pos))
(throw (Exception. (str "Marker " pos " not found."))))
(or (number? pos) (nil? pos))
pos
:else
(throw (Exception.
(str "Do not support " (type pos) " as a play time."))))))
(defn start-finish-times [{:keys [from to]} markers]
(map (partial lookup-time markers) [from to]))
(defn shift-events
[events offset cut-off]
(let [offset (or offset 0)
cut-off (when cut-off (- cut-off offset))
keep? (if cut-off
#(and (<= 0 %) (> cut-off %))
#(<= 0 %))]
(->> (sequence (comp (map #(update-in % [:offset] - offset))
(filter (comp keep? :offset)))
events)
(sort-by :offset))))
(defn schedule-event!
[^SynthesisEngine engine offset f]
(let [ts (TimeStamp. offset)
cmd (proxy [ScheduledCommand] [] (run [] (f)))]
(.scheduleCommand engine ts cmd)))
(defn schedule-events!
[events score audio-ctx playing? wait]
(let [{:keys [instruments]} score
engine (:synthesis-engine @audio-ctx)
begin (.getCurrentTime ^SynthesisEngine engine)
; bug? this could cause infinite blocking if @playing? is false
end! #(when @playing? (deliver wait :done))]
(pdoseq-block [{:keys [offset instrument duration] :as event} events]
(let [inst (-> instrument instruments)
start! #(when @playing?
(if-let [f (:function event)]
(future (f))
(start-event! audio-ctx event inst)))
stop! #(when-not (:function event)
(stop-event! audio-ctx event inst))]
(schedule-event! engine (+ begin
(/ offset 1000.0)) start!)
(when-not (:function event)
(schedule-event! engine (+ begin
(/ offset 1000.0)
(/ duration 1000.0)) stop!))))
(schedule-event! engine (+ begin
(/ (score-length events) 1000.0)
1) end!)))
(defn play!
"Plays an Alda score, optionally from given start/end marks determined by
*play-opts*.
Optionally takes as a second argument a set of events to play (which could
be pre-filtered, e.g. for playing only a portion of the score).
In either case, the offsets of the events to be played are shifted back such
that the earliest event's offset is 0 -- this is so that playback will start
immediately.
Returns a function that, when called mid-playback, will stop any further
events from playing."
[score & [event-set]]
(let [{:keys [one-off? async?]} *play-opts*
_ (log/debug "Determining audio types...")
audio-types (determine-audio-types score)
audio-ctx (or (:audio-context score) (new-audio-context))
_ (log/debug "Setting up audio types...")
_ (set-up! audio-ctx audio-types score)
_ (refresh! audio-ctx audio-types score)
playing? (atom true)
wait (promise)
_ (log/debug "Determining events to schedule...")
events (if event-set
(let [earliest (->> (map :offset event-set)
(apply min Long/MAX_VALUE)
(max 0))]
(shift-events event-set earliest nil))
(let [event-set (:events score)
markers (:markers score)
[start end] (start-finish-times *play-opts*
markers)]
(shift-events event-set start end)))
clean-up #(tear-down! audio-ctx audio-types score)]
(log/debug "Scheduling events...")
(schedule-events! events score audio-ctx playing? wait)
(cond
(and one-off? async?) (future @wait (clean-up))
(and one-off? (not async?)) (do @wait (clean-up))
(not async?) @wait)
#(reset! playing? false)))