annotate implementations/midi/midifile-import.lisp @ 119:f07d4596290b

Key-sig support in midi library darcs-hash:20070727112429-f76cc-0e47bea65bc3f0eb90749a023442e992104a88b2.gz
author David Lewis <d.lewis@gold.ac.uk>
date Fri, 27 Jul 2007 12:24:29 +0100
parents cf198383852d
children b849c4fc4c26
rev   line source
d@35 1 (cl:in-package #:amuse-midi)
d@35 2
d@35 3 (defclass midifile-identifier (identifier)
d@35 4 ((pathname :initarg :path
d@35 5 :reader midifile-identifier-pathname
d@35 6 :initform 'nil)))
d@35 7
d@35 8 (defun midifile-id (pathname)
d@35 9 (make-instance 'midifile-identifier :path pathname))
d@35 10
d@35 11 (defmethod get-composition ((identifier midifile-identifier))
d@35 12 (%initialise-midifile-composition (midi:read-midi-file
d@35 13 (midifile-identifier-pathname identifier))))
d@35 14
d@35 15 (defun %initialise-midifile-composition (midifile)
d@35 16 ;; Takes a midifile object (from the "MIDI" package)
d@35 17 ;; and returns an amuse midi object
d@35 18 ;; FIXME: gets it wrong if patch changes in mid-note
d@35 19 ;; FIXME: assumes controllers are global in scope and location
d@35 20 (let ((tracks (midi:midifile-tracks midifile))
d@35 21 (division (midi:midifile-division midifile))
d@115 22 (notes) (time-sigs) (key-sigs) (tempi) (misses 0)
d@115 23 (track-no -1) (last-time 0))
d@35 24 (dolist (track tracks)
d@35 25 (incf track-no)
d@35 26 (setf track (sort (copy-seq track)
d@35 27 #'(lambda (x y)
d@35 28 (or (< (midi:message-time x)
d@35 29 (midi:message-time y))
d@35 30 (and (= (midi:message-time x)
d@35 31 (midi:message-time y))
d@35 32 (typep x 'midi:note-off-message))))))
d@36 33 (let ((ons (make-array '(16 128) :initial-element nil))
d@35 34 (offs)
d@36 35 (patches (make-array 16 :initial-element 0)))
d@35 36 (dolist (event track)
d@35 37 (when (> (midi:message-time event) last-time)
d@35 38 (setf last-time (midi:message-time event)))
d@35 39 (cond
d@35 40 ((or (typep event 'midi:note-off-message)
d@35 41 (and (typep event 'midi:note-on-message)
d@35 42 (= (midi:message-velocity event) 0)))
d@35 43 (let ((pitch (midi:message-key event))
d@36 44 (channel (midi:message-channel event))
d@35 45 (t-off (midi:message-time event)))
d@35 46 (if (aref ons channel pitch)
d@35 47 (push (make-event-from-on-off-pair (aref ons channel pitch)
d@35 48 t-off
d@35 49 division
d@35 50 track-no
d@35 51 (aref patches channel))
d@35 52 notes)
d@35 53 ;; if there's no matching on, wait until the beat
d@35 54 ;; is done.
d@35 55 (push event offs))))
d@35 56 ((typep event 'midi:note-on-message)
d@35 57 (let ((pitch (midi:message-key event))
d@36 58 (channel (midi:message-channel event))
d@35 59 (t-off (midi:message-time event)))
d@35 60 (when (aref ons channel pitch)
d@35 61 ;; there's a note already sounding. End it.
d@35 62 (push (make-event-from-on-off-pair (aref ons channel pitch)
d@35 63 t-off
d@35 64 division
d@35 65 track-no
d@35 66 (aref patches channel))
d@35 67 notes))
d@35 68 (setf (aref ons channel pitch) event)))
d@35 69 ((typep event 'midi:time-signature-message)
d@35 70 ;; FIXME: Should I make a midi version of this object,
d@35 71 ;; with track/channel?
d@35 72 (when time-sigs
d@35 73 (setf (duration (car time-sigs))
d@35 74 (- (/ (midi:message-time event)
d@35 75 division)
d@35 76 (timepoint (car time-sigs)))))
d@35 77 (push (make-instance 'basic-time-signature
d@35 78 :time (/ (midi:message-time event)
d@35 79 division)
d@35 80 :numerator (midi:message-numerator event)
d@36 81 :denominator (expt 2 (midi:message-denominator event)))
d@35 82 time-sigs))
d@115 83 ((typep event 'midi:key-signature-message)
d@115 84 ;; FIXME: Should I make a midi version of this object,
d@115 85 ;; with track/channel? [probably, yes]
d@115 86 (when key-sigs
d@115 87 (setf (duration (car time-sigs))
d@115 88 (- (/ (midi:message-time event)
d@115 89 division)
d@115 90 (timepoint (car time-sigs)))))
d@115 91 (push (make-instance 'midi-key-signature
d@115 92 :time (/ (midi:message-time event)
d@115 93 division)
d@119 94 :sharp-count (midi:message-sf event)
d@119 95 :mode (midi:message-mi event))
d@115 96 key-sigs))
d@35 97 ((typep event 'midi:tempo-message)
d@35 98 (when tempi
d@35 99 (setf (duration (car tempi))
d@35 100 (- (/ (midi:message-time event)
d@35 101 division)
d@35 102 (timepoint (car tempi)))))
d@35 103 (push (make-instance 'tempo
d@35 104 :time (/ (midi:message-time event)
d@35 105 division)
d@35 106 :bpm (microsecond-per-crotchet-to-bpm (midi:message-tempo event)))
d@35 107 tempi))
d@35 108 ((typep event 'midi:program-change-message)
d@36 109 (setf (aref patches (midi:message-channel event))
d@35 110 (midi:message-program event)))
d@35 111 (t (incf misses))))))
d@35 112 (when tempi
d@35 113 (setf (duration (car tempi)) (- (/ last-time division) (timepoint (car tempi)))))
d@35 114 (when time-sigs
d@35 115 (setf (duration (car time-sigs)) (- (/ last-time division) (timepoint (car time-sigs)))))
d@115 116 (when key-sigs
d@115 117 (setf (duration (car key-sigs)) (- (/ last-time division) (timepoint (car key-sigs)))))
d@35 118 ;; make a midi object from notes, etc.
d@35 119 (let ((composition (make-instance 'midi-composition
d@35 120 :time 0
d@35 121 :interval (/ last-time division)
d@36 122 :time-signatures (if time-sigs
d@36 123 (sort time-sigs #'time<)
d@36 124 (list (make-instance 'basic-time-signature
d@36 125 :time 0
d@36 126 :interval (/ last-time division)
d@36 127 :numerator 4
d@36 128 :denominator 4)))
d@115 129 :tempi (sort tempi #'time<)
d@119 130 :key-signatures (sort key-sigs #'time<))))
d@35 131 (sequence:adjust-sequence composition
d@35 132 (length notes)
d@35 133 :initial-contents (sort notes #'time<)))))
d@35 134
d@35 135 (defun make-event-from-on-off-pair (note-on cut-off divisions track patch)
d@35 136 (cond
d@35 137 ((or (= (midi:message-channel note-on) 9)
d@35 138 (> patch 111))
d@35 139 ;; percussive
d@35 140 (make-instance 'midi-percussive-event
d@35 141 :channel (1+ (midi:message-channel note-on))
d@35 142 :track track
d@35 143 :time (/ (midi:message-time note-on) divisions)
d@35 144 :interval (/ (- cut-off (midi:message-time note-on))
d@35 145 divisions)
d@35 146 :velocity (midi:message-velocity note-on)
d@35 147 :patch patch
d@35 148 :sound (midi:message-key note-on)))
d@35 149 (t
d@35 150 ;; pitched
d@35 151 (make-instance 'midi-pitched-event
d@35 152 :channel (1+ (midi:message-channel note-on))
d@35 153 :track track
d@35 154 :time (/ (midi:message-time note-on) divisions)
d@35 155 :interval (/ (- cut-off (midi:message-time note-on))
d@35 156 divisions)
d@35 157 :velocity (midi:message-velocity note-on)
d@35 158 :patch patch
d@35 159 :number (midi:message-key note-on)))))