m@46
|
1 (cl:in-package #:amuse-mtp)
|
m@46
|
2
|
m@46
|
3 ;;; Compositions
|
m@46
|
4
|
m@46
|
5 #.(clsql:locally-enable-sql-reader-syntax)
|
m@46
|
6
|
m@87
|
7 (defvar *event-attributes*
|
m@87
|
8 (list [dataset-id] [composition-id] [event-id]
|
jeremy@328
|
9 [onset] [dur] [deltast] [cpitch] [cpitch-adj] [cents]
|
jeremy@328
|
10 [mpitch] [accidental] [keysig] [mode]
|
marcus@326
|
11 [barlength] [pulses] [phrase] [tempo] [dyn] [voice] [bioi]
|
marcus@326
|
12 [ornament] [comma] [articulation]))
|
m@87
|
13
|
m@53
|
14 (defgeneric get-dataset (identifer))
|
m@53
|
15
|
m@53
|
16 (defmethod get-dataset ((identifier mtp-dataset-identifier))
|
m@53
|
17 (let* ((dataset-id (dataset-id identifier))
|
m@53
|
18 (where-clause [= [dataset-id] dataset-id])
|
m@149
|
19 (db-dataset (car (clsql:select [*] :from [mtp-dataset] :where where-clause)))
|
marcus@326
|
20 (db-compositions (clsql:select [composition-id][description][timebase]
|
m@87
|
21 :from [mtp-composition]
|
m@87
|
22 :order-by '(([composition-id] :asc))
|
m@87
|
23 :where where-clause))
|
m@87
|
24 (db-events (apply #'clsql:select
|
m@87
|
25 (append *event-attributes*
|
m@87
|
26 (list :from [mtp-event]
|
m@87
|
27 :order-by '(([composition-id] :asc)
|
m@87
|
28 ([event-id] :asc))
|
m@87
|
29 :where where-clause))))
|
m@87
|
30 (dataset (make-mtp-dataset :dataset-id (first db-dataset)
|
m@87
|
31 :description (second db-dataset)
|
m@87
|
32 :timebase (third db-dataset)
|
m@87
|
33 :midc (fourth db-dataset)))
|
m@53
|
34 (compositions nil)
|
m@87
|
35 (events nil))
|
m@87
|
36 ;; for each db-composition
|
m@87
|
37 (dolist (dbc db-compositions)
|
marcus@326
|
38 (let ((composition-id (first dbc))
|
marcus@326
|
39 (description (second dbc))
|
marcus@326
|
40 (timebase (third dbc)))
|
m@87
|
41 ;; for each db-event
|
m@87
|
42 (do* ((dbes db-events (cdr dbes))
|
m@87
|
43 (dbe (car dbes) (car dbes))
|
m@87
|
44 (cid (second dbe) (second dbe)))
|
m@87
|
45 ((or (null dbes) (not (= cid composition-id)))
|
m@87
|
46 (setf db-events dbes))
|
m@87
|
47 (when dbe
|
marcus@326
|
48 (push (db-event->mtp-event dbe timebase) events)))
|
m@149
|
49 (when events
|
m@149
|
50 (let* ((interval (+ (timepoint (car events)) (duration (car events))))
|
m@149
|
51 (composition
|
m@149
|
52 (make-mtp-composition :dataset-id dataset-id
|
m@149
|
53 :composition-id composition-id
|
m@149
|
54 :description description
|
marcus@326
|
55 :timebase timebase
|
m@149
|
56 :time 0
|
m@149
|
57 :interval interval)))
|
m@149
|
58 (sequence:adjust-sequence composition (length events)
|
m@149
|
59 :initial-contents (nreverse events))
|
m@149
|
60 (setf events nil)
|
m@149
|
61 (push composition compositions)))))
|
m@53
|
62 (sequence:adjust-sequence dataset (length compositions)
|
m@53
|
63 :initial-contents (nreverse compositions))
|
m@53
|
64 dataset))
|
m@53
|
65
|
m@46
|
66 (defmethod get-composition ((identifier mtp-composition-identifier))
|
m@46
|
67 (let* ((dataset-id (dataset-id identifier))
|
m@46
|
68 (composition-id (composition-id identifier))
|
m@46
|
69 (where-clause [and [= [dataset-id] dataset-id]
|
m@46
|
70 [= [composition-id] composition-id]])
|
m@46
|
71 (description
|
m@51
|
72 (car (clsql:select [description] :from [mtp-composition]
|
m@46
|
73 :where where-clause :flatp t :field-names nil)))
|
marcus@326
|
74 (timebase
|
marcus@326
|
75 (car (clsql:select [timebase] :from [mtp-composition]
|
marcus@326
|
76 :where where-clause :flatp t :field-names nil)))
|
m@87
|
77 (db-events (apply #'clsql:select
|
m@87
|
78 (append *event-attributes*
|
m@87
|
79 (list :from [mtp-event]
|
m@87
|
80 :order-by '(([event-id] :asc))
|
m@87
|
81 :where where-clause))))
|
m@46
|
82 (events nil))
|
m@87
|
83 (dolist (e db-events)
|
marcus@326
|
84 (push (db-event->mtp-event e timebase) events))
|
m@46
|
85 (let* ((interval (+ (timepoint (car events)) (duration (car events))))
|
m@46
|
86 (composition
|
m@46
|
87 (make-mtp-composition :dataset-id dataset-id
|
m@46
|
88 :composition-id composition-id
|
m@46
|
89 :description description
|
marcus@326
|
90 :timebase timebase
|
m@46
|
91 :time 0
|
m@46
|
92 :interval interval)))
|
m@46
|
93 (sequence:adjust-sequence composition (length events)
|
m@46
|
94 :initial-contents (nreverse events))
|
m@46
|
95 composition)))
|
m@46
|
96
|
m@90
|
97 #.(clsql:restore-sql-reader-syntax-state)
|
m@90
|
98
|
marcus@326
|
99 (defun db-event->mtp-event (db-event timebase)
|
m@87
|
100 (let* ((slots ; the order must match *event-attributes*
|
jeremy@328
|
101 '(amuse::time amuse::interval deltast cpitch cpitch-adj cents mpitch accidental
|
marcus@326
|
102 keysig mode barlength pulses phrase tempo dyn voice bioi
|
marcus@326
|
103 ornament comma articulation))
|
marcus@326
|
104 (time-slots '(amuse::time amuse::interval deltast barlength bioi))
|
m@46
|
105 (mtp-event
|
m@87
|
106 (make-mtp-event :dataset-id (first db-event)
|
m@87
|
107 :composition-id (second db-event)
|
m@87
|
108 :event-id (third db-event))))
|
m@87
|
109 (do* ((slts slots (cdr slts))
|
m@87
|
110 (db-atts (nthcdr 3 db-event) (cdr db-atts)))
|
m@87
|
111 ((null slts) mtp-event)
|
marcus@326
|
112 (if (member (car slts) time-slots :test #'eql)
|
marcus@326
|
113 (setf (slot-value mtp-event (car slts)) (convert-time-slot (car db-atts) timebase))
|
marcus@326
|
114 (setf (slot-value mtp-event (car slts)) (car db-atts))))))
|
marcus@326
|
115
|
marcus@326
|
116 (defun convert-time-slot (value timebase)
|
marcus@326
|
117 "Convert native representation of time into a representation where
|
marcus@326
|
118 a crotchet has a value of 96."
|
marcus@326
|
119 (if (or (null value) (null timebase))
|
marcus@326
|
120 nil
|
marcus@326
|
121 (let ((multiplier (/ 96 timebase)))
|
marcus@326
|
122 (* value multiplier))))
|
m@90
|
123
|
m@90
|
124 ;;; Monodies
|
m@90
|
125
|
m@90
|
126 (defmethod monody ((identifier mtp-composition-identifier))
|
m@90
|
127 (monody (get-composition identifier)))
|
m@90
|
128
|
m@90
|
129 (defmethod monody ((c mtp-composition))
|
m@292
|
130 ;; using the voice of the first event in the piece
|
m@90
|
131 (let ((monody (make-instance 'mtp-monody
|
m@90
|
132 :dataset-id (dataset-id c)
|
m@90
|
133 :composition-id (composition-id c)
|
m@90
|
134 :description (description c)
|
marcus@326
|
135 :timebase (composition-timebase c)
|
m@90
|
136 :time 0
|
m@90
|
137 :interval (duration c)))
|
m@90
|
138 (events nil)
|
m@292
|
139 (monody-voice nil))
|
m@90
|
140 (sequence:dosequence (event c)
|
m@292
|
141 (when (null monody-voice)
|
m@292
|
142 (setf monody-voice (%mtp-voice event)))
|
m@90
|
143 (when (= (%mtp-voice event) monody-voice)
|
m@90
|
144 (push event events)))
|
m@90
|
145 (sequence:adjust-sequence
|
m@90
|
146 monody (length events)
|
m@90
|
147 :initial-contents (sort events #'< :key #'amuse:timepoint))
|
m@90
|
148 monody))
|
m@46
|
149
|
marcus@326
|
150
|
m@46
|
151 ;;; Constituents from compositions: time-signatures
|
m@46
|
152
|
m@96
|
153 (defmethod crotchet ((dataset mtp-dataset))
|
d@136
|
154 (amuse:make-standard-period
|
m@96
|
155 (/ (dataset-timebase dataset) 4)))
|
m@87
|
156
|
marcus@326
|
157 (defmethod crotchet ((composition mtp-composition))
|
marcus@326
|
158 (amuse:make-standard-period
|
marcus@326
|
159 (/ (composition-timebase composition) 4)))
|
marcus@326
|
160
|
m@46
|
161 #.(clsql:locally-enable-sql-reader-syntax)
|
m@96
|
162 (defmethod crotchet ((event mtp-event))
|
m@96
|
163 (let ((timebase
|
marcus@326
|
164 (car (clsql:select [timebase] :from [mtp-composition]
|
marcus@326
|
165 :where [and [= [dataset-id] (dataset-id event)] [= [composition-id] (composition-id event)]]
|
m@96
|
166 :flatp t
|
m@96
|
167 :field-names nil))))
|
d@136
|
168 (amuse:make-standard-period (/ timebase 4))))
|
m@46
|
169 #.(clsql:restore-sql-reader-syntax-state)
|
m@46
|
170
|
m@69
|
171 (defmethod get-applicable-time-signatures ((e mtp-event) c)
|
m@69
|
172 (declare (ignore c))
|
m@291
|
173 ;(format t "~&GATS ~A ~A ~A: pulses = ~A; barlength = ~A.~%" (dataset-id e) (composition-id e) (event-id e) (%mtp-pulses e) (%mtp-barlength e))
|
m@291
|
174 (let* ((pulses (%mtp-pulses e))
|
m@291
|
175 (barlength (%mtp-barlength e))
|
m@291
|
176 (timebase (* 4 (duration (crotchet e))))
|
m@291
|
177 (numerator (if (null pulses) 0 pulses))
|
m@291
|
178 (denominator (if (null barlength)
|
m@291
|
179 1
|
m@291
|
180 (/ timebase (/ barlength pulses)))))
|
m@291
|
181 (list
|
m@291
|
182 (amuse:make-standard-time-signature-period numerator
|
m@291
|
183 denominator
|
d@136
|
184 (timepoint e)
|
d@136
|
185 (duration e)))))
|
m@69
|
186
|
m@46
|
187 (defmethod time-signatures ((c mtp-composition))
|
m@46
|
188 (let ((results nil)
|
m@46
|
189 (interval 0)
|
m@46
|
190 (current nil))
|
m@46
|
191 (sequence:dosequence (event c)
|
m@70
|
192 (let ((ts (car (get-applicable-time-signatures event c))))
|
m@46
|
193 (when (and (%mtp-barlength event)
|
m@46
|
194 (%mtp-pulses event)
|
m@46
|
195 (or (null current)
|
m@46
|
196 (not (time-signature-equal ts current))))
|
m@46
|
197 (unless (null current)
|
m@46
|
198 (setf (duration current) interval)
|
m@46
|
199 (push current results))
|
m@46
|
200 (setf interval 0
|
m@46
|
201 current ts)))
|
m@46
|
202 (incf interval (%mtp-deltast event))
|
m@46
|
203 (incf interval (duration event)))
|
m@46
|
204 (when current
|
m@46
|
205 (setf (duration current) interval)
|
m@46
|
206 (push current results))
|
m@46
|
207 (nreverse results)))
|
m@46
|
208
|
m@46
|
209 ;;; Constituents from compositions: key-signatures
|
m@46
|
210
|
m@69
|
211 (defmethod get-applicable-key-signatures ((e mtp-event) c)
|
m@69
|
212 (declare (ignore c))
|
m@69
|
213 (let* ((sharps (%mtp-keysig e))
|
m@68
|
214 (mode (%mtp-mode e))
|
m@69
|
215 (midi-mode (and mode (if (= mode 0) 0 1))))
|
d@136
|
216 (list (amuse:make-midi-key-signature-period sharps midi-mode
|
d@136
|
217 (timepoint e)
|
d@136
|
218 (duration e)))))
|
m@46
|
219
|
m@68
|
220 (defmethod key-signatures ((c mtp-composition))
|
m@46
|
221 (let ((results nil)
|
m@46
|
222 (interval 0)
|
m@46
|
223 (current nil))
|
m@46
|
224 (sequence:dosequence (event c)
|
m@69
|
225 (let ((ks (car (get-applicable-key-signatures event c))))
|
m@46
|
226 (when (and (%mtp-keysig event)
|
m@46
|
227 (%mtp-mode event)
|
m@46
|
228 (or (null current)
|
m@46
|
229 (not (key-signature-equal ks current))))
|
m@46
|
230 (unless (null current)
|
m@46
|
231 (setf (duration current) interval)
|
m@46
|
232 (push current results))
|
m@46
|
233 (setf interval 0
|
m@46
|
234 current ks)))
|
m@46
|
235 (incf interval (%mtp-deltast event))
|
m@46
|
236 (incf interval (duration event)))
|
m@46
|
237 (when current
|
m@46
|
238 (setf (duration current) interval)
|
m@46
|
239 (push current results))
|
m@46
|
240 (nreverse results)))
|
m@46
|
241
|
m@46
|
242 ;;; Constituents from compositions: tempi
|
m@46
|
243
|
m@69
|
244 (defmethod get-applicable-tempi ((e mtp-event) c)
|
m@69
|
245 (declare (ignore c))
|
d@136
|
246 (list (amuse:make-standard-tempo-period (%mtp-tempo e)
|
d@136
|
247 (timepoint e)
|
d@136
|
248 (duration e))))
|
m@69
|
249
|
m@46
|
250 (defmethod tempi ((c mtp-composition))
|
m@46
|
251 (let ((results nil)
|
m@46
|
252 (interval 0)
|
m@46
|
253 (current nil))
|
m@46
|
254 (sequence:dosequence (event c)
|
m@46
|
255 (when (and (%mtp-tempo event)
|
m@46
|
256 (or (null current)
|
m@46
|
257 (not (= (bpm current) (%mtp-tempo event)))))
|
m@46
|
258 (unless (null current)
|
m@46
|
259 (setf (duration current) interval)
|
m@46
|
260 (push current results))
|
m@69
|
261 (let ((new (car (get-applicable-tempi event c))))
|
m@46
|
262 (setf interval 0
|
m@46
|
263 current new)))
|
m@46
|
264 (incf interval (%mtp-deltast event))
|
m@46
|
265 (incf interval (duration event)))
|
m@46
|
266 (when current
|
m@46
|
267 (setf (duration current) interval)
|
m@46
|
268 (push current results))
|
m@46
|
269 (nreverse results)))
|
m@46
|
270
|
m@46
|
271 ;;; Events: Pitch
|
m@46
|
272
|
m@46
|
273 (defmethod chromatic-pitch ((e mtp-event))
|
m@46
|
274 (make-chromatic-pitch (%mtp-cpitch e)))
|
m@46
|
275
|
m@46
|
276 (defmethod midi-pitch-number ((e mtp-event))
|
m@46
|
277 (%mtp-cpitch e))
|
m@46
|
278
|
m@110
|
279 (defmethod diatonic-pitch-cp ((e mtp-event))
|
m@110
|
280 ;; MIPS morphetic pitch is relative to An0 while cpitch is relative to Cn2
|
m@110
|
281 (- (%mtp-cpitch e) 21))
|
m@110
|
282
|
m@110
|
283 (defmethod diatonic-pitch-mp ((e mtp-event))
|
m@82
|
284 ;; MIPS morphetic pitch is relative to An0 while mpitch is relative to Cn2
|
m@82
|
285 (- (%mtp-mpitch e) 12))
|
m@82
|
286
|
m@46
|
287 (defmethod diatonic-pitch ((e mtp-event))
|
m@110
|
288 (make-mips-pitch (diatonic-pitch-cp e)
|
m@110
|
289 (diatonic-pitch-mp e)))
|
m@82
|
290
|
m@84
|
291 (defmethod asa-pitch-string ((e mtp-event))
|
m@110
|
292 (asa-pitch-string (diatonic-pitch e)))
|
m@84
|
293
|
m@82
|
294 #.(clsql:locally-enable-sql-reader-syntax)
|
m@82
|
295 (defmethod middle-c ((e mtp-event))
|
m@82
|
296 (let ((cpitch (car (clsql:select [midc] :from [dataset]
|
m@82
|
297 :where [= [dataset-id] (dataset-id e)]
|
m@82
|
298 :flatp t :field-names nil))))
|
m@82
|
299 (make-mtp-event :cpitch cpitch :mpitch (* (/ cpitch 12) 7))))
|
m@82
|
300 #.(clsql:restore-sql-reader-syntax-state)
|
m@79
|
301
|
m@79
|
302 ;;; Phrase boundaries
|
m@79
|
303
|
m@98
|
304 (defmethod ground-truth-segmenter-before ((c mtp-composition))
|
m@98
|
305 (declare (ignore c))
|
m@98
|
306 (make-instance 'mtp-before-segmenter))
|
m@98
|
307
|
m@98
|
308 (defmethod ground-truth-segmenter-after ((c mtp-composition))
|
m@98
|
309 (declare (ignore c))
|
m@99
|
310 (make-instance 'mtp-after-segmenter))
|
m@98
|
311
|
m@98
|
312 (defmethod ground-truth-segmenter-before ((e mtp-event))
|
m@98
|
313 (declare (ignore e))
|
m@98
|
314 (make-instance 'mtp-before-segmenter))
|
m@98
|
315
|
m@98
|
316 (defmethod ground-truth-segmenter-after ((e mtp-event))
|
m@98
|
317 (declare (ignore e))
|
m@99
|
318 (make-instance 'mtp-after-segmenter))
|
m@98
|
319
|
m@79
|
320 (defmethod boundary-strength ((s mtp-before-segmenter) (e mtp-event) c)
|
m@79
|
321 (declare (ignore s c))
|
m@79
|
322 (let ((phrase (%mtp-phrase e)))
|
m@79
|
323 (case phrase
|
m@79
|
324 (-1 1)
|
m@79
|
325 (t 0))))
|
m@79
|
326
|
m@79
|
327 (defmethod boundary-strength ((s mtp-after-segmenter) (e mtp-event) c)
|
m@79
|
328 (declare (ignore s c))
|
m@79
|
329 (let ((phrase (%mtp-phrase e)))
|
m@79
|
330 (case phrase
|
m@79
|
331 (1 1)
|
m@79
|
332 (t 0))))
|