annotate implementations/midi/methods.lisp @ 228:16b99fc989aa

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