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@35
|
22 (notes) (time-sigs) (tempi) (misses 0) (track-no -1) (last-time 0))
|
d@35
|
23 (dolist (track tracks)
|
d@35
|
24 (incf track-no)
|
d@35
|
25 (setf track (sort (copy-seq track)
|
d@35
|
26 #'(lambda (x y)
|
d@35
|
27 (or (< (midi:message-time x)
|
d@35
|
28 (midi:message-time y))
|
d@35
|
29 (and (= (midi:message-time x)
|
d@35
|
30 (midi:message-time y))
|
d@35
|
31 (typep x 'midi:note-off-message))))))
|
d@35
|
32 (let ((ons (make-array '(17 128) :initial-element nil))
|
d@35
|
33 (offs)
|
d@35
|
34 (patches (make-array 17 :initial-element 0)))
|
d@35
|
35 (dolist (event track)
|
d@35
|
36 (when (> (midi:message-time event) last-time)
|
d@35
|
37 (setf last-time (midi:message-time event)))
|
d@35
|
38 (cond
|
d@35
|
39 ((or (typep event 'midi:note-off-message)
|
d@35
|
40 (and (typep event 'midi:note-on-message)
|
d@35
|
41 (= (midi:message-velocity event) 0)))
|
d@35
|
42 (let ((pitch (midi:message-key event))
|
d@35
|
43 (channel (1+ (midi:message-channel event)))
|
d@35
|
44 (t-off (midi:message-time event)))
|
d@35
|
45 (if (aref ons channel pitch)
|
d@35
|
46 (push (make-event-from-on-off-pair (aref ons channel pitch)
|
d@35
|
47 t-off
|
d@35
|
48 division
|
d@35
|
49 track-no
|
d@35
|
50 (aref patches channel))
|
d@35
|
51 notes)
|
d@35
|
52 ;; if there's no matching on, wait until the beat
|
d@35
|
53 ;; is done.
|
d@35
|
54 (push event offs))))
|
d@35
|
55 ((typep event 'midi:note-on-message)
|
d@35
|
56 (let ((pitch (midi:message-key event))
|
d@35
|
57 (channel (1+ (midi:message-channel event)))
|
d@35
|
58 (t-off (midi:message-time event)))
|
d@35
|
59 (when (aref ons channel pitch)
|
d@35
|
60 ;; there's a note already sounding. End it.
|
d@35
|
61 (push (make-event-from-on-off-pair (aref ons channel pitch)
|
d@35
|
62 t-off
|
d@35
|
63 division
|
d@35
|
64 track-no
|
d@35
|
65 (aref patches channel))
|
d@35
|
66 notes))
|
d@35
|
67 (setf (aref ons channel pitch) event)))
|
d@35
|
68 ((typep event 'midi:time-signature-message)
|
d@35
|
69 ;; FIXME: Should I make a midi version of this object,
|
d@35
|
70 ;; with track/channel?
|
d@35
|
71 (when time-sigs
|
d@35
|
72 (setf (duration (car time-sigs))
|
d@35
|
73 (- (/ (midi:message-time event)
|
d@35
|
74 division)
|
d@35
|
75 (timepoint (car time-sigs)))))
|
d@35
|
76 (push (make-instance 'basic-time-signature
|
d@35
|
77 :time (/ (midi:message-time event)
|
d@35
|
78 division)
|
d@35
|
79 :numerator (midi:message-numerator event)
|
d@35
|
80 :denominator (midi:message-denominator event))
|
d@35
|
81 time-sigs))
|
d@35
|
82 ((typep event 'midi:tempo-message)
|
d@35
|
83 (when tempi
|
d@35
|
84 (setf (duration (car tempi))
|
d@35
|
85 (- (/ (midi:message-time event)
|
d@35
|
86 division)
|
d@35
|
87 (timepoint (car tempi)))))
|
d@35
|
88 (push (make-instance 'tempo
|
d@35
|
89 :time (/ (midi:message-time event)
|
d@35
|
90 division)
|
d@35
|
91 :bpm (microsecond-per-crotchet-to-bpm (midi:message-tempo event)))
|
d@35
|
92 tempi))
|
d@35
|
93 ((typep event 'midi:program-change-message)
|
d@35
|
94 (setf (aref patches (1+ (midi:message-channel event)))
|
d@35
|
95 (midi:message-program event)))
|
d@35
|
96 (t (incf misses))))))
|
d@35
|
97 (when tempi
|
d@35
|
98 (setf (duration (car tempi)) (- (/ last-time division) (timepoint (car tempi)))))
|
d@35
|
99 (when time-sigs
|
d@35
|
100 (setf (duration (car time-sigs)) (- (/ last-time division) (timepoint (car time-sigs)))))
|
d@35
|
101 ;; make a midi object from notes, etc.
|
d@35
|
102 (let ((composition (make-instance 'midi-composition
|
d@35
|
103 :time 0
|
d@35
|
104 :interval (/ last-time division)
|
d@35
|
105 :time-signatures (sort time-sigs #'time<)
|
d@35
|
106 :tempi (sort tempi #'time<))))
|
d@35
|
107 (sequence:adjust-sequence composition
|
d@35
|
108 (length notes)
|
d@35
|
109 :initial-contents (sort notes #'time<)))))
|
d@35
|
110
|
d@35
|
111 (defparameter *short* nil)
|
d@35
|
112
|
d@35
|
113 (defun make-event-from-on-off-pair (note-on cut-off divisions track patch)
|
d@35
|
114 (when (< (/ (- cut-off (midi:message-time note-on)) divisions) 1/8)
|
d@35
|
115 (push (cons note-on cut-off) *short*))
|
d@35
|
116 (cond
|
d@35
|
117 ((or (= (midi:message-channel note-on) 9)
|
d@35
|
118 (> patch 111))
|
d@35
|
119 ;; percussive
|
d@35
|
120 (make-instance 'midi-percussive-event
|
d@35
|
121 :channel (1+ (midi:message-channel note-on))
|
d@35
|
122 :track track
|
d@35
|
123 :time (/ (midi:message-time note-on) divisions)
|
d@35
|
124 :interval (/ (- cut-off (midi:message-time note-on))
|
d@35
|
125 divisions)
|
d@35
|
126 :velocity (midi:message-velocity note-on)
|
d@35
|
127 :patch patch
|
d@35
|
128 :sound (midi:message-key note-on)))
|
d@35
|
129 (t
|
d@35
|
130 ;; pitched
|
d@35
|
131 (make-instance 'midi-pitched-event
|
d@35
|
132 :channel (1+ (midi:message-channel note-on))
|
d@35
|
133 :track track
|
d@35
|
134 :time (/ (midi:message-time note-on) divisions)
|
d@35
|
135 :interval (/ (- cut-off (midi:message-time note-on))
|
d@35
|
136 divisions)
|
d@35
|
137 :velocity (midi:message-velocity note-on)
|
d@35
|
138 :patch patch
|
d@35
|
139 :number (midi:message-key note-on))))) |