d@34
|
1 (cl:in-package #:amuse-midi)
|
d@34
|
2
|
d@134
|
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
|
d@134
|
8 (defgeneric midi-track (midi-message)
|
d@134
|
9 (:documentation "MIDI track. Also used for midi output"))
|
d@34
|
10 (defmethod midi-track ((midi-message midi-message))
|
d@34
|
11 (%midi-message-track midi-message))
|
d@34
|
12
|
d@134
|
13 (defgeneric midi-velocity (event)
|
d@134
|
14 (:documentation "MIDI velocity. Also used for midi output"))
|
d@34
|
15 (defmethod midi-velocity ((event midi-pitched-event))
|
d@34
|
16 (%midi-pitched-event-velocity event))
|
d@34
|
17 (defmethod midi-velocity ((event midi-percussive-event))
|
d@34
|
18 (%midi-percussive-event-velocity event))
|
d@34
|
19
|
d@134
|
20 (defgeneric midi-patch (event)
|
d@134
|
21 (:documentation "MIDI patch (instrumental sound). Also used for
|
d@134
|
22 midi output"))
|
d@34
|
23 (defmethod midi-patch ((event midi-pitched-event))
|
d@34
|
24 (%midi-pitched-event-patch event))
|
d@34
|
25
|
d@134
|
26 (defgeneric midi-drum-sound (event)
|
d@134
|
27 (:documentation "MIDI pitch for unpitched events (usually, drum
|
d@134
|
28 sound for drum kits on channel 10, but also for semi-pitched
|
d@134
|
29 SFX, etc). Also used for midi output"))
|
d@34
|
30 (defmethod midi-drum-sound ((event midi-percussive-event))
|
d@34
|
31 (%midi-percussive-event-sound event))
|
d@34
|
32
|
d@34
|
33 (defmethod time-signatures ((composition midi-composition))
|
d@34
|
34 (%midi-time-signatures composition))
|
d@34
|
35 (defmethod (setf time-signatures) (sequence (composition midi-composition))
|
d@34
|
36 (setf (%midi-time-signatures composition) sequence))
|
d@34
|
37 (defmethod tempi ((composition midi-composition))
|
d@34
|
38 (%midi-tempi composition))
|
d@34
|
39 (defmethod (setf tempi) (sequence (composition midi-composition))
|
d@34
|
40 (setf (%midi-tempi composition) sequence))
|
d@115
|
41 (defmethod key-signatures ((composition midi-composition))
|
d@115
|
42 (%midi-key-signatures composition))
|
d@115
|
43 (defmethod (setf key-signatures) (sequence (composition midi-composition))
|
d@115
|
44 (setf (%midi-key-signatures composition) sequence))
|
d@34
|
45
|
d@34
|
46 (defgeneric copy-event (event))
|
d@34
|
47 ;; FIXME: This ought to call-next-method and operate on the result,
|
d@34
|
48 ;; rather than calling internals from the other package
|
d@34
|
49 (defmethod copy-event ((event midi-pitched-event))
|
d@34
|
50 (with-slots (channel track (number amuse::number) (time amuse::time) (interval amuse::interval) velocity patch)
|
d@34
|
51 event
|
d@34
|
52 (make-instance 'midi-pitched-event
|
d@34
|
53 :channel channel
|
d@34
|
54 :track track
|
d@34
|
55 :number number
|
d@34
|
56 :time time
|
d@34
|
57 :interval interval
|
d@34
|
58 :velocity velocity
|
d@34
|
59 :patch patch)))
|
d@34
|
60 (defmethod copy-event ((event midi-percussive-event))
|
d@34
|
61 (with-slots (channel track (time amuse::time) (interval amuse::interval) velocity patch sound)
|
d@34
|
62 event
|
d@34
|
63 (make-instance 'midi-percussive-event
|
d@34
|
64 :channel channel
|
d@34
|
65 :track track
|
d@34
|
66 :time time
|
d@34
|
67 :interval interval
|
d@34
|
68 :velocity velocity
|
d@34
|
69 :patch patch
|
d@34
|
70 :sound sound)))
|
d@34
|
71
|
d@34
|
72
|
d@34
|
73 ;; Allow derived sequences from remove-if, etc. to preserve other slot
|
d@34
|
74 ;; info (timesigs, etc)
|
d@34
|
75 (defmethod sequence:make-sequence-like :around ((o midi-composition) length
|
d@34
|
76 &key (initial-element nil iep)
|
d@34
|
77 (initial-contents nil icp))
|
d@34
|
78 (declare (ignore length initial-element initial-contents))
|
d@34
|
79 (let ((result (call-next-method)))
|
d@34
|
80 (cond
|
d@34
|
81 ((or iep icp)
|
d@34
|
82 (setf (timepoint result) (timepoint (elt result 0))
|
d@34
|
83 (duration result) (- (timepoint
|
d@34
|
84 (loop for e being the elements of result
|
d@34
|
85 maximize (cut-off e)))
|
d@34
|
86 (timepoint (elt result 0)))))
|
d@34
|
87 (t (setf (timepoint result) 0
|
d@34
|
88 (duration result) 0)))
|
d@34
|
89 (with-slots (time-signatures tempi misc-controllers)
|
d@34
|
90 o
|
d@34
|
91 (setf (%midi-time-signatures result) time-signatures
|
d@34
|
92 (%midi-tempi result) tempi
|
d@34
|
93 (%midi-misc-controllers result) misc-controllers))
|
d@34
|
94 result))
|
d@34
|
95
|
d@34
|
96
|
d@34
|
97 ;; useful little function
|
d@34
|
98
|
d@34
|
99 (defun microsecond-per-crotchet-to-bpm (mu-per-c)
|
d@34
|
100 (/ 60000000 mu-per-c))
|
d@34
|
101
|
d@34
|
102 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
d@34
|
103 ;;
|
d@34
|
104 ;; MIDI playback methods
|
d@34
|
105
|
d@34
|
106 (defmethod get-patch-for-midi ((event midi-pitched-event))
|
d@34
|
107 ;; FIXME
|
d@34
|
108 (midi-patch event))
|
d@34
|
109
|
d@34
|
110 (defmethod get-channel-for-midi ((event midi-message))
|
d@34
|
111 ;; FIXME 1- ??? I'm only doing this because of the Geerdes
|
d@34
|
112 ;; database. Looks like a recipe for disaster. Think should probably
|
d@34
|
113 ;; enforce 0-15.
|
d@34
|
114 (1- (midi-channel event)))
|
d@34
|
115
|
d@34
|
116 (defmethod get-velocity-for-midi ((event midi-message))
|
d@34
|
117 ;; FIXME: under-exclusive specialisation. Does this matter?
|
d@34
|
118 (midi-velocity event))
|
d@34
|
119
|
d@34
|
120 (defmethod get-pitch-for-midi ((event midi-percussive-event))
|
d@34
|
121 (midi-drum-sound event))
|
d@34
|
122
|
d@34
|
123 (defmethod get-pitch-for-midi ((event midi-pitched-event))
|
d@41
|
124 (midi-pitch-number event))
|
d@41
|
125
|
d@41
|
126 ;; Have avoided percussion vs pitched, as this is more obviously
|
d@41
|
127 ;; meaningless.
|
d@41
|
128 (defmethod significantly-louderp ((event1 midi-pitched-event) (event2 midi-pitched-event))
|
d@41
|
129 (>= (/ (midi-velocity event1)
|
d@41
|
130 (midi-velocity event2))
|
d@41
|
131 4/3))
|
d@41
|
132 (defmethod significantly-louderp ((event1 midi-percussive-event) (event2 midi-percussive-event))
|
d@41
|
133 (>= (/ (midi-velocity event1)
|
d@41
|
134 (midi-velocity event2))
|
d@41
|
135 4/3))
|
d@41
|
136 (defmethod substantially-louderp ((event1 midi-pitched-event) (event2 midi-pitched-event))
|
d@41
|
137 (>= (/ (midi-velocity event1)
|
d@41
|
138 (midi-velocity event2))
|
d@41
|
139 2))
|
d@41
|
140 (defmethod substantially-louderp ((event1 midi-percussive-event) (event2 midi-percussive-event))
|
d@41
|
141 (>= (/ (midi-velocity event1)
|
d@41
|
142 (midi-velocity event2))
|
d@41
|
143 2))
|
d@114
|
144
|
d@130
|
145 (defmethod crotchet ((object midi-object))
|
d@139
|
146 (make-standard-period 1))
|