annotate implementations/mtp/methods.lisp @ 326:5271a0aa06d6

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