This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
/
sound.clj
executable file
·242 lines (202 loc) · 8.37 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
(ns alda.sound
(:require [alda.sound.midi :as midi]
[alda.util :refer (parse-time
pdoseq-block
parse-position)]
[taoensso.timbre :as log]))
(defn new-audio-context
[]
(atom
{:audio-types #{}}))
(defn set-up?
[{:keys [audio-context] :as score} audio-type]
(contains? (:audio-types @audio-context) audio-type))
(defmulti set-up-audio-type!
(fn [score audio-type] audio-type))
(defmethod set-up-audio-type! :default
[score audio-type]
(log/errorf "No implementation of set-up-audio-type! defined for type %s"
audio-type))
(defmethod set-up-audio-type! :midi
[{:keys [audio-context] :as score} _]
(log/debug "Setting up MIDI...")
(midi/get-midi-synth! audio-context)
(midi/get-midi-sequencer! audio-context))
(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."
([score]
(set-up! score (determine-audio-types score)))
([{:keys [audio-context] :as score} audio-type]
(if (coll? audio-type)
(pdoseq-block [a-t audio-type]
(set-up! score a-t))
(when-not (set-up? score audio-type)
(swap! audio-context update :audio-types conj audio-type)
(set-up-audio-type! score audio-type)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmulti tear-down-audio-type!
(fn [score audio-type] audio-type))
(defmethod tear-down-audio-type! :default
[score audio-type]
(log/errorf "No implementation of tear-down! defined for type %s" audio-type))
(defmethod tear-down-audio-type! :midi
[{:keys [audio-context] :as score} _]
(log/debug "Closing MIDI...")
(midi/close-midi-sequencer! audio-context)
(midi/close-midi-synth! audio-context))
(defn tear-down!
"Completely clean up after a score.
Playback may not necessarily be resumed after doing this."
([{:keys [audio-context] :as score}]
;; Do any necessary clean-up for each audio type.
;; e.g. for MIDI, close the MidiSynthesizer.
(tear-down! score (determine-audio-types score)))
([{:keys [audio-context] :as score} audio-type]
(if (coll? audio-type)
(pdoseq-block [a-t audio-type]
(tear-down! score a-t))
(when (set-up? score audio-type)
(swap! audio-context update :audio-types disj audio-type)
(tear-down-audio-type! score audio-type)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmulti stop-playback-for-audio-type!
(fn [score audio-type] audio-type))
(defmethod stop-playback-for-audio-type! :default
[score audio-type]
(log/errorf
"No implementation of stop-playback-for-audiotype! defined for type %s"
audio-type))
(defmethod stop-playback-for-audio-type! :midi
[{:keys [audio-context] :as score} _]
(log/debug "Stopping MIDI playback...")
(midi/all-sound-off! audio-context))
(defn stop-playback!
"Stop playback, but leave the score in a state where playback can be resumed."
([{:keys [audio-context] :as score}]
(stop-playback! score (determine-audio-types score)))
([{:keys [audio-context] :as score} audio-type]
(if (coll? audio-type)
(pdoseq-block [a-t audio-type]
(stop-playback! score a-t))
(when (set-up? score audio-type)
(stop-playback-for-audio-type! score audio-type)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(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]
(let [[start end] (map (partial lookup-time markers) [from to])]
(log/debugf "from: %s => %s to: %s => %s" from start to end)
[start end]))
(defn earliest-offset
[event-set]
(->> (map :offset event-set)
(apply min Long/MAX_VALUE)
(max 0)))
(defn shift-events
[events offset cut-off]
(log/debugf "Keeping events between %s and %s." 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 create-sequence!
[score & [event-set]]
(let [score (update score :audio-context #(or % (new-audio-context)))
_ (log/debug "Setting up audio types...")
_ (set-up! score)
_ (log/debug "Determining events to schedule...")
_ (log/debug (str "*play-opts*: " *play-opts*))
[start end] (start-finish-times *play-opts* (:markers score))
start' (cond
;; If a "from" offset is explicitly provided, use the
;; calculated start offset derived from it.
(:from *play-opts*)
start
;; If an event-set is provided, use the earliest event's
;; offset.
event-set
(earliest-offset event-set)
:else
start)
events (-> (or event-set (:events score))
(shift-events start' end))]
(midi/load-sequencer! events score)))
(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 result map containing the following values:
:score The full score being played.
:stop! A function that, when called mid-playback, will stop any further
events from playing.
:wait A function that will sleep for the duration of the score. This is
useful if you want to playback asynchronously, perform some
actions, then wait until playback is complete before proceeding."
[score & args]
(let [{:keys [one-off? async?]} *play-opts*
score (update score :audio-context #(or % (new-audio-context)))
wait (promise)]
(log/debug "Creating sequence...")
(apply create-sequence! score args)
(log/debug "Playing sequence...")
(midi/play-sequence! (:audio-context score) #(deliver wait :done))
(cond
(and one-off? async?) (future @wait (tear-down! score))
(and one-off? (not async?)) (do @wait (tear-down! score))
(not async?) @wait)
{:score score
:stop! #(if one-off?
(tear-down! score)
(stop-playback! score))
:wait #(deref wait)}))
(defn export!
"Exports an Alda score to a MIDI file."
[score output-filename]
(let [{:keys [audio-context] :as score}
(update score :audio-context #(or % (new-audio-context)))]
(log/debug "Creating sequence...")
(create-sequence! score)
(let [sqnc (.getSequence (:midi-sequencer @audio-context))]
(log/debugf "Exporting score to MIDI file: %s" output-filename)
(midi/export-midi-file! sqnc output-filename)
(log/debug "Score MIDI export done."))))