annotate implementations/midi/methods.lisp @ 249:bba5e8571b92

Hack in linked-events for the midi package. Some refactoring of the constructors for this and geerdes might be helpful, since neither lend themselves to using initialize-instance for performing the linking.
author Jamie Forth <j.forth@gold.ac.uk>
date Thu, 24 Feb 2011 11:23:18 +0000
parents 93f8768b12dd
children 6a3adca16910
rev   line source
d@34 1 (cl:in-package #:amuse-midi)
d@34 2
j@228 3 (defgeneric midi-channel (midi-message)
d@134 4 (:documentation "MIDI channel. Also used for midi output"))
d@34 5 (defmethod midi-channel ((midi-message midi-message))
d@34 6 (%midi-message-channel midi-message))
d@34 7
j@243 8 (defgeneric (setf midi-channel) (value event))
j@243 9 (defmethod (setf midi-channel) (value (event midi-message))
j@243 10 (setf (%midi-message-channel event) value) event)
j@243 11
d@134 12 (defgeneric midi-track (midi-message)
d@134 13 (:documentation "MIDI track. Also used for midi output"))
d@34 14 (defmethod midi-track ((midi-message midi-message))
d@34 15 (%midi-message-track midi-message))
d@34 16
j@243 17 (defgeneric (setf midi-track) (value event))
j@243 18 (defmethod (setf midi-track) (value (event midi-message))
j@243 19 (setf (%midi-message-track event) value) event)
j@243 20
d@134 21 (defgeneric midi-velocity (event)
d@134 22 (:documentation "MIDI velocity. Also used for midi output"))
d@34 23 (defmethod midi-velocity ((event midi-pitched-event))
d@34 24 (%midi-pitched-event-velocity event))
d@34 25 (defmethod midi-velocity ((event midi-percussive-event))
d@34 26 (%midi-percussive-event-velocity event))
d@34 27
j@243 28 (defgeneric (setf midi-velocity) (value event))
j@243 29 (defmethod (setf midi-velocity) (value (event midi-pitched-event))
j@243 30 (setf (%midi-pitched-event-velocity event) value) event)
j@243 31 (defmethod (setf midi-velocity) (value (event midi-percussive-event))
j@243 32 (setf (%midi-percussive-event-velocity event) value) event)
j@243 33
d@134 34 (defgeneric midi-patch (event)
d@134 35 (:documentation "MIDI patch (instrumental sound). Also used for
d@134 36 midi output"))
d@34 37 (defmethod midi-patch ((event midi-pitched-event))
d@34 38 (%midi-pitched-event-patch event))
j@243 39 (defmethod midi-patch ((event midi-percussive-event))
j@243 40 (%midi-percussive-event-patch event))
d@34 41
j@243 42 (defgeneric (setf midi-patch) (value event))
j@243 43 (defmethod (setf midi-patch) (value (event midi-pitched-event))
j@243 44 (setf (%midi-pitched-event-patch event) value) event)
j@243 45 (defmethod (setf midi-patch) (value (event midi-percussive-event))
j@243 46 (setf (%midi-percussive-event-patch event) value) event)
j@228 47
d@134 48 (defgeneric midi-drum-sound (event)
d@134 49 (:documentation "MIDI pitch for unpitched events (usually, drum
d@134 50 sound for drum kits on channel 10, but also for semi-pitched
d@134 51 SFX, etc). Also used for midi output"))
d@34 52 (defmethod midi-drum-sound ((event midi-percussive-event))
d@34 53 (%midi-percussive-event-sound event))
d@34 54
j@243 55 (defgeneric (setf midi-drum-sound) (value event))
j@243 56 (defmethod (setf midi-drum-sound) (value (event midi-percussive-event))
j@243 57 (setf (%midi-percussive-event-sound event) value) event)
j@243 58
j@243 59 (defgeneric midi-note-number (event)
j@243 60 (:documentation "Same as get-pitch-for-midi"))
j@243 61 (defmethod midi-note-number ((event midi-pitched-event))
j@243 62 (midi-pitch-number event))
j@243 63 (defmethod midi-note-number ((event midi-percussive-event))
j@243 64 (midi-drum-sound event))
j@243 65
j@228 66 (defgeneric midi-drum-sound= (event1 event2))
j@228 67 (defmethod midi-drum-sound= ((event1 midi-percussive-event)
j@228 68 (event2 midi-percussive-event))
j@228 69 (= (midi-drum-sound event1) (midi-drum-sound event2)))
j@228 70 (defmethod midi-drum-sound= ((event1 event)
j@228 71 (event2 event))
j@228 72 "Return nil for any comparisons involving non-percussive events."
j@228 73 nil)
j@228 74
d@34 75 (defmethod time-signatures ((composition midi-composition))
d@34 76 (%midi-time-signatures composition))
d@34 77 (defmethod (setf time-signatures) (sequence (composition midi-composition))
d@34 78 (setf (%midi-time-signatures composition) sequence))
d@34 79 (defmethod tempi ((composition midi-composition))
d@34 80 (%midi-tempi composition))
d@34 81 (defmethod (setf tempi) (sequence (composition midi-composition))
d@34 82 (setf (%midi-tempi composition) sequence))
d@115 83 (defmethod key-signatures ((composition midi-composition))
d@115 84 (%midi-key-signatures composition))
d@115 85 (defmethod (setf key-signatures) (sequence (composition midi-composition))
d@115 86 (setf (%midi-key-signatures composition) sequence))
d@34 87
j@212 88 (defgeneric midi-timebase (composition))
j@212 89 (defmethod midi-timebase ((composition midi-composition))
j@212 90 (%midi-timebase composition))
j@212 91
d@34 92 ;; FIXME: This ought to call-next-method and operate on the result,
d@34 93 ;; rather than calling internals from the other package
d@34 94 (defmethod copy-event ((event midi-pitched-event))
j@206 95 (with-slots (channel track (number amuse::number)
j@206 96 (time amuse::time) (interval amuse::interval)
j@249 97 velocity patch composition) event
j@206 98 (make-midi-pitched-event number velocity patch channel track time
j@249 99 interval composition)))
j@206 100
d@34 101 (defmethod copy-event ((event midi-percussive-event))
j@206 102 (with-slots (channel track (time amuse::time)
j@206 103 (interval amuse::interval) velocity patch
j@249 104 sound composition) event
j@206 105 (make-midi-percussive-event sound velocity patch channel track
j@249 106 time interval composition)))
j@206 107
d@154 108 (defgeneric copy-time-signature (time-signature))
d@154 109 (defmethod copy-time-signature ((time-signature standard-time-signature))
d@154 110 (make-instance (class-of time-signature)
d@154 111 :numerator (time-signature-numerator time-signature)
d@154 112 :denominator (time-signature-denominator time-signature)))
d@154 113 (defmethod copy-time-signature ((time-signature-period standard-time-signature-period))
d@154 114 (let ((sig (call-next-method)))
d@154 115 (setf (timepoint sig) (timepoint time-signature-period)
d@154 116 (duration sig) (duration time-signature-period))
d@154 117 sig))
d@154 118 (defgeneric copy-tempo (tempo))
d@154 119 (defmethod copy-tempo ((tempo standard-tempo))
d@154 120 (make-instance (class-of tempo)
d@154 121 :bpm (bpm tempo)))
d@154 122 (defmethod copy-tempo ((tempo-period standard-tempo-period))
d@154 123 (let ((tp (call-next-method)))
d@154 124 (setf (timepoint tp) (timepoint tempo-period)
d@154 125 (duration tp) (duration tempo-period))
d@154 126 tp))
d@154 127 (defgeneric copy-key-signature (key-signature))
d@154 128 (defmethod copy-key-signature ((key-signature standard-key-signature))
d@154 129 (make-instance (class-of key-signature)
d@154 130 :sharp-count (key-signature-sharps key-signature)
d@154 131 :mode (key-signature-mode key-signature)))
d@154 132 (defmethod copy-key-signature ((key-signature-period standard-key-signature-period))
d@154 133 (let ((sig (call-next-method)))
d@154 134 (setf (timepoint sig) (timepoint key-signature-period)
d@154 135 (duration sig) (duration key-signature-period))
d@154 136 sig))
d@154 137
d@34 138
d@34 139
d@34 140 ;; Allow derived sequences from remove-if, etc. to preserve other slot
d@34 141 ;; info (timesigs, etc)
d@154 142 #+nil
d@34 143 (defmethod sequence:make-sequence-like :around ((o midi-composition) length
d@34 144 &key (initial-element nil iep)
d@34 145 (initial-contents nil icp))
d@34 146 (declare (ignore length initial-element initial-contents))
d@34 147 (let ((result (call-next-method)))
d@34 148 (cond
d@34 149 ((or iep icp)
d@34 150 (setf (timepoint result) (timepoint (elt result 0))
d@34 151 (duration result) (- (timepoint
d@34 152 (loop for e being the elements of result
d@34 153 maximize (cut-off e)))
d@34 154 (timepoint (elt result 0)))))
d@34 155 (t (setf (timepoint result) 0
d@34 156 (duration result) 0)))
d@34 157 (with-slots (time-signatures tempi misc-controllers)
d@34 158 o
d@34 159 (setf (%midi-time-signatures result) time-signatures
d@34 160 (%midi-tempi result) tempi
d@34 161 (%midi-misc-controllers result) misc-controllers))
d@34 162 result))
d@153 163
d@34 164
d@34 165 ;; useful little function
d@34 166
d@34 167 (defun microsecond-per-crotchet-to-bpm (mu-per-c)
d@34 168 (/ 60000000 mu-per-c))
d@34 169
d@34 170 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
d@34 171 ;;
d@34 172 ;; MIDI playback methods
d@34 173
d@34 174 (defmethod get-patch-for-midi ((event midi-pitched-event))
d@34 175 ;; FIXME
d@34 176 (midi-patch event))
d@34 177
d@34 178 (defmethod get-channel-for-midi ((event midi-message))
d@34 179 ;; FIXME 1- ??? I'm only doing this because of the Geerdes
d@34 180 ;; database. Looks like a recipe for disaster. Think should probably
d@34 181 ;; enforce 0-15.
d@34 182 (1- (midi-channel event)))
d@34 183
d@34 184 (defmethod get-velocity-for-midi ((event midi-message))
d@34 185 ;; FIXME: under-exclusive specialisation. Does this matter?
d@34 186 (midi-velocity event))
d@34 187
d@34 188 (defmethod get-pitch-for-midi ((event midi-percussive-event))
d@34 189 (midi-drum-sound event))
d@34 190
d@34 191 (defmethod get-pitch-for-midi ((event midi-pitched-event))
d@41 192 (midi-pitch-number event))
d@41 193
d@41 194 ;; Have avoided percussion vs pitched, as this is more obviously
d@41 195 ;; meaningless.
d@41 196 (defmethod significantly-louderp ((event1 midi-pitched-event) (event2 midi-pitched-event))
d@41 197 (>= (/ (midi-velocity event1)
d@41 198 (midi-velocity event2))
d@41 199 4/3))
d@41 200 (defmethod significantly-louderp ((event1 midi-percussive-event) (event2 midi-percussive-event))
d@41 201 (>= (/ (midi-velocity event1)
d@41 202 (midi-velocity event2))
d@41 203 4/3))
d@41 204 (defmethod substantially-louderp ((event1 midi-pitched-event) (event2 midi-pitched-event))
d@41 205 (>= (/ (midi-velocity event1)
d@41 206 (midi-velocity event2))
d@41 207 2))
d@41 208 (defmethod substantially-louderp ((event1 midi-percussive-event) (event2 midi-percussive-event))
d@41 209 (>= (/ (midi-velocity event1)
d@41 210 (midi-velocity event2))
d@41 211 2))
d@114 212
d@130 213 (defmethod crotchet ((object midi-object))
d@139 214 (make-standard-period 1))
d@157 215
d@157 216 (defmethod monody ((identifier midifile-identifier))
d@157 217 (monody (get-composition identifier)))
d@157 218
d@157 219 ;; TODO: improve this naive first-cut at MONODY for midi files which
d@157 220 ;; simply selects a track which is both monodic (if any monodic tracks
d@157 221 ;; exist) and contains the highest pitch of any monodic track.
d@157 222 (defmethod monody ((c midi-composition))
d@157 223 (flet ((not-overlapping (track)
d@157 224 (let ((result t)
d@157 225 (track (sort (copy-list track) #'amuse:time<)))
d@157 226 (dotimes (i (1- (length track)) result)
d@157 227 (let ((e1 (elt track i))
d@157 228 (e2 (elt track (1+ i))))
d@157 229 (unless (or (before e1 e2) (meets e1 e2))
d@157 230 (setf result nil)))))))
d@157 231 (let ((tracks (make-hash-table))
d@157 232 (result nil)
d@157 233 (max-pitch 0))
d@157 234 (sequence:dosequence (message c)
d@157 235 (let* ((tracknum (amuse-midi:midi-track message))
d@157 236 (track (gethash tracknum tracks)))
d@157 237 (setf (gethash tracknum tracks) (cons message track))))
d@157 238 (maphash #'(lambda (k v)
d@157 239 (declare (ignore k))
d@157 240 (let ((max (apply #'max (mapcar #'midi-pitch-number v))))
d@157 241 (when (and (not-overlapping v) (> max max-pitch))
d@157 242 (setf result (sort v #'amuse:time<)
d@157 243 max-pitch max))))
d@157 244 tracks)
d@157 245 (when result
d@157 246 (let ((monody (make-instance 'midi-monody
d@157 247 :time (amuse:timepoint c)
d@157 248 :interval (amuse:duration c)
d@157 249 :time-signatures (time-signatures c)
d@157 250 :key-signatures (key-signatures c)
d@157 251 :tempi (tempi c)
d@157 252 :controllers (%midi-misc-controllers c))))
d@157 253 (sequence:adjust-sequence monody (length result)
d@157 254 :initial-contents result)
d@157 255 monody)))))
d@157 256
d@157 257 (defmethod trim-enclosing-silence ((composition midi-composition))
d@157 258 (let ((start (timepoint (bar-before (onset (elt composition 0))
d@157 259 composition)))
d@157 260 (end)
d@157 261 (new-sequence) (new-composition))
d@157 262 ;; First attend to the events themselves - copy and slide
d@157 263 (sequence:dosequence (event composition)
d@157 264 (push (copy-event event) new-sequence)
d@157 265 (setf (timepoint (car new-sequence)) (- (timepoint event)
d@157 266 start))
d@157 267 (when (or (not end)
d@157 268 (> (timepoint (cut-off event)) end))
d@157 269 (setf end (timepoint (cut-off event)))))
d@157 270 ;; Make the new composition with slid events
d@157 271 ;; Should work, but doesn't
d@157 272 #+nil
d@157 273 (setf new-composition (sequence:make-sequence-like composition 0))
d@157 274 (setf new-composition (make-instance 'midi-composition
d@157 275 :time 0
d@157 276 :interval (- end start)))
d@157 277 (setf (amuse::%list-slot-sequence-data new-composition)
d@157 278 (reverse new-sequence))
d@157 279 ;; Time-sigs
d@157 280 (let ((sigs))
d@157 281 (dolist (sig (time-signatures composition))
d@157 282 ;; only include if signature affects window
d@157 283 (when (and (> (timepoint (cut-off sig))
d@157 284 start)
d@157 285 (< (timepoint sig)
d@157 286 end))
d@157 287 ;; copy the signature
d@157 288 (push (copy-time-signature sig)
d@157 289 sigs)
d@157 290 ;; adjust the timing
d@157 291 (setf (timepoint (car sigs))
d@157 292 (max 0 (- (timepoint (car sigs)) start))
d@157 293 (duration (car sigs))
d@157 294 (- (min (timepoint (cut-off (car sigs)))
d@157 295 (- end start))
d@157 296 (timepoint (car sigs))))))
d@157 297 (setf (time-signatures new-composition) (reverse sigs)))
d@157 298 (let ((sigs))
d@157 299 (dolist (sig (key-signatures composition))
d@157 300 ;; only include if signature affects window
d@157 301 (when (and (> (timepoint (cut-off sig))
d@157 302 start)
d@157 303 (< (timepoint sig)
d@157 304 end))
d@157 305 ;; copy the signature
d@157 306 (push (copy-key-signature sig)
d@157 307 sigs)
d@157 308 ;; adjust the timing
d@157 309 (setf (timepoint (car sigs))
d@157 310 (max 0 (- (timepoint (car sigs)) start))
d@157 311 (duration (car sigs))
d@157 312 (- (min (timepoint (cut-off (car sigs)))
d@157 313 (- end start))
d@157 314 (timepoint (car sigs))))))
d@157 315 (setf (key-signatures new-composition) (reverse sigs)))
d@157 316 (let ((tempi))
d@157 317 (dolist (tempo (tempi composition))
d@157 318 ;; only include if signature affects window
d@157 319 (when (and (> (timepoint (cut-off tempo))
d@157 320 start)
d@157 321 (< (timepoint tempo)
d@157 322 end))
d@157 323 ;; copy the signature
d@157 324 (push (copy-tempo tempo)
d@157 325 tempi)
d@157 326 ;; adjust the timing
d@157 327 (setf (timepoint (car tempi))
d@157 328 (max 0 (- (timepoint (car tempi)) start))
d@157 329 (duration (car tempi))
d@157 330 (- (min (timepoint (cut-off (car tempi)))
d@157 331 (- end start))
d@157 332 (timepoint (car tempi))))))
d@157 333 (setf (tempi new-composition) (reverse tempi)))
d@157 334 new-composition))
d@157 335
d@157 336
d@157 337 (defgeneric bar-before (moment composition))
d@157 338
d@157 339 (defmethod bar-before (moment (composition midi-composition))
d@157 340 "Returns the moment at which the containing bar begins"
d@157 341 (do ((time-sigs (time-signatures composition) (cdr time-sigs)))
d@157 342 ((null time-sigs) nil)
d@157 343 (let ((bar-period (make-standard-period
d@157 344 (crotchets-in-a-bar (car time-sigs)))))
d@157 345 (when (time> (cut-off (car time-sigs))
d@157 346 moment)
d@157 347 (do ((bar (time+ (onset (car time-sigs)) bar-period)
d@157 348 (time+ bar bar-period))
d@157 349 (prev-bar (onset (car time-sigs))))
d@157 350 ((time> bar moment) (return-from bar-before prev-bar))
d@165 351 (setf prev-bar bar))))))
d@165 352
d@165 353 (defmethod get-applicable-time-signatures ((anchored-period anchored-period)
d@165 354 (composition midi-composition))
d@165 355 (%find-overlapping anchored-period (time-signatures composition)))
d@165 356 (defmethod get-applicable-tempi ((anchored-period anchored-period)
d@165 357 (composition midi-composition))
d@165 358 (%find-overlapping anchored-period (tempi composition)))
d@165 359 (defmethod get-applicable-key-signatures ((anchored-period anchored-period)
d@165 360 (composition midi-composition))
d@165 361 (%find-overlapping anchored-period (key-signatures composition)))
d@165 362
d@165 363 (defun %find-overlapping (period1 period-list)
d@165 364 (let ((result-list))
d@165 365 (dolist (period2 period-list result-list)
d@165 366 (cond
d@165 367 ((time>= period2 (cut-off period1))
d@165 368 (return-from %find-overlapping (reverse result-list)))
d@165 369 ((time> (cut-off period2) period1)
j@206 370 (push period2 result-list))))))