m@24
|
1 (cl:in-package #:amuse)
|
m@24
|
2
|
m@89
|
3 ;;; monody
|
m@89
|
4
|
m@89
|
5 (defmethod ensure-monody ((m monody))
|
m@89
|
6 (let ((result t))
|
m@89
|
7 (dotimes (i (1- (length m)) result)
|
m@89
|
8 ;; assumes the events are time ordered which (since monody is a
|
m@89
|
9 ;; subclass of time-ordered-constituent) they ought to be.
|
m@89
|
10 (let ((e1 (elt m i))
|
m@89
|
11 (e2 (elt m (1+ i))))
|
m@89
|
12 (unless (or (before e1 e2) (meets e1 e2))
|
m@89
|
13 (setf result nil))))))
|
m@89
|
14
|
c@109
|
15 ;;; diatonic pitch (represented using MIPS)
|
m@81
|
16
|
c@109
|
17 (defmethod asa-pitch-string ((mp diatonic-pitch))
|
c@106
|
18 (mips:p-pn (list (%p-pc mp) (%p-pm mp))))
|
c@106
|
19
|
c@109
|
20 (defmethod diatonic-pitch-octave ((mp diatonic-pitch))
|
c@106
|
21 (let* ((asa-string (asa-pitch-string mp))
|
c@106
|
22 (start (position-if #'digit-char-p asa-string)))
|
c@106
|
23 (values (parse-integer asa-string :start start))))
|
m@86
|
24
|
c@109
|
25 (defmethod diatonic-pitch-accidental ((mp diatonic-pitch))
|
c@106
|
26 (let* ((asa-string (asa-pitch-string mp))
|
c@106
|
27 (start 1)
|
c@106
|
28 (end (position-if #'digit-char-p asa-string))
|
c@106
|
29 (malist '((#\n . 0) (#\s . +1) (#\f . -1)))
|
c@106
|
30 (multiplier (cdr (assoc (char asa-string 1) malist))))
|
c@106
|
31 (* multiplier (- end start))))
|
c@106
|
32
|
c@109
|
33 (defmethod diatonic-pitch-name ((mp diatonic-pitch))
|
c@106
|
34 (elt (asa-pitch-string mp) 0))
|
m@86
|
35
|
c@109
|
36 (defmethod middle-c ((mp diatonic-pitch))
|
c@109
|
37 (make-diatonic-pitch 39 23))
|
m@81
|
38
|
c@109
|
39 (defmethod midi-pitch-number ((mp diatonic-pitch))
|
c@106
|
40 (+ (%p-pc mp) 21))
|
c@106
|
41
|
c@109
|
42 (defmethod octave ((mp diatonic-pitch))
|
c@106
|
43 (1- (floor (midi-pitch-number mp) 12)))
|
c@106
|
44
|
c@109
|
45 (defmethod diatonic-pitch ((mp diatonic-pitch))
|
m@81
|
46 mp)
|
m@81
|
47
|
c@109
|
48 (defmethod print-object ((o diatonic-pitch) stream)
|
c@106
|
49 (print-unreadable-object (o stream :type t)
|
c@106
|
50 (let ((asa-string (asa-pitch-string o)))
|
c@106
|
51 (write asa-string :stream stream))))
|
m@81
|
52
|
c@111
|
53 (defmethod asa-interval-string ((mpi diatonic-pitch-interval))
|
c@111
|
54 (mips:pi-pin (%diatonic-pitch-interval-span mpi)))
|
c@111
|
55
|
c@111
|
56 (defmethod print-object ((o diatonic-pitch-interval) stream)
|
c@111
|
57 (print-unreadable-object (o stream :type t)
|
c@111
|
58 (let ((asa-string (asa-interval-string o)))
|
c@111
|
59 (write asa-string :stream stream))))
|
c@111
|
60
|
m@81
|
61 ;;; Chromatic pitch
|
m@81
|
62
|
m@86
|
63 (defmethod octave ((cp chromatic-pitch))
|
c@106
|
64 (1- (floor (%chromatic-pitch-number cp) 12)))
|
m@86
|
65
|
m@81
|
66 (defmethod middle-c ((cp chromatic-pitch))
|
m@81
|
67 (make-chromatic-pitch 60))
|
m@81
|
68
|
m@24
|
69 (defmethod chromatic-pitch ((pitch-designator chromatic-pitch))
|
m@24
|
70 pitch-designator)
|
m@24
|
71
|
m@24
|
72 (defmethod midi-pitch-number ((pitch-designator chromatic-pitch))
|
m@24
|
73 (%chromatic-pitch-number pitch-designator))
|
m@24
|
74
|
m@24
|
75 (defmethod midi-pitch-number ((pitch-designator pitch))
|
m@24
|
76 (%chromatic-pitch-number (chromatic-pitch pitch-designator)))
|
m@24
|
77
|
c@111
|
78 (defmethod span ((pitch-interval-designator chromatic-pitch-interval))
|
c@111
|
79 (%chromatic-pitch-interval-span pitch-interval-designator))
|
m@24
|
80
|
m@24
|
81 (defmethod duration ((period-designator period))
|
m@24
|
82 (%period-interval period-designator))
|
m@24
|
83
|
d@33
|
84 (defmethod (setf duration) ((value real) (period-designator period))
|
d@33
|
85 (setf (%period-interval period-designator) value))
|
d@33
|
86
|
m@24
|
87 (defmethod timepoint ((moment-designator moment))
|
m@24
|
88 (%moment-time moment-designator))
|
m@24
|
89
|
d@33
|
90 (defmethod (setf timepoint) ((value real) (moment-designator moment))
|
d@33
|
91 (setf (%moment-time moment-designator) value))
|
d@33
|
92
|
d@73
|
93 (defmethod cut-off ((anchored-period-designator anchored-period))
|
d@73
|
94 (make-instance 'moment
|
d@73
|
95 :time (+ (%moment-time anchored-period-designator)
|
d@73
|
96 (%period-interval anchored-period-designator))))
|
d@73
|
97
|
m@24
|
98 (defmethod beat-units-per-bar ((time-signature basic-time-signature))
|
m@24
|
99 (%basic-time-signature-numerator time-signature))
|
m@24
|
100
|
m@24
|
101 (defmethod beat-units ((time-signature basic-time-signature))
|
m@24
|
102 (%basic-time-signature-denominator time-signature))
|
m@24
|
103
|
m@67
|
104 (defmethod time-signature-equal ((ts1 basic-time-signature)
|
m@67
|
105 (ts2 basic-time-signature))
|
m@67
|
106 (let ((n1 (time-signature-numerator ts1))
|
m@67
|
107 (n2 (time-signature-numerator ts2))
|
m@67
|
108 (d1 (time-signature-denominator ts1))
|
m@67
|
109 (d2 (time-signature-denominator ts2)))
|
m@67
|
110 (and n1 n2 (= n1 n2)
|
m@67
|
111 d1 d2 (= d1 d2))))
|
m@67
|
112
|
m@24
|
113 (defmethod key-signature-sharps ((key-signature basic-key-signature))
|
m@24
|
114 (%basic-key-signature-sharp-count key-signature))
|
m@24
|
115
|
m@45
|
116 (defmethod key-signature-mode ((ks midi-key-signature))
|
m@45
|
117 (%midi-key-signature-mode ks))
|
m@45
|
118
|
m@67
|
119 (defmethod key-signature-equal ((ks1 basic-key-signature)
|
m@67
|
120 (ks2 basic-key-signature))
|
m@67
|
121 (let ((s1 (key-signature-sharps ks1))
|
m@67
|
122 (s2 (key-signature-sharps ks2)))
|
m@67
|
123 (and s1 s2 (= s1 s2))))
|
m@67
|
124
|
m@67
|
125 (defmethod key-signature-equal ((ks1 midi-key-signature)
|
m@67
|
126 (ks2 midi-key-signature))
|
m@67
|
127 (let ((s1 (key-signature-sharps ks1))
|
m@67
|
128 (s2 (key-signature-sharps ks2))
|
m@67
|
129 (m1 (key-signature-mode ks1))
|
m@67
|
130 (m2 (key-signature-mode ks2)))
|
m@67
|
131 (and s1 s2 (= s1 s2)
|
m@67
|
132 m1 m2 (= m1 m2))))
|
m@67
|
133
|
m@24
|
134 (defmethod bpm ((tempo tempo))
|
m@24
|
135 (%tempo-bpm tempo))
|
m@24
|
136
|
m@67
|
137 (defmethod tempo-equal ((t1 tempo) (t2 tempo))
|
m@67
|
138 (and (bpm t1) (bpm t2) (= t1 t2)))
|
m@67
|
139
|
m@67
|
140
|
m@24
|
141 ;; Time protocol
|
m@24
|
142
|
m@24
|
143 (defmethod time+ ((object1 moment) (object2 period))
|
d@100
|
144 "(time+ <moment> <period>) -> <moment> Implemented as a
|
d@100
|
145 straightforward summation."
|
m@24
|
146 (make-moment (+ (timepoint object1) (duration object2))))
|
m@24
|
147
|
m@24
|
148 (defmethod time+ ((object1 period) (object2 moment)) ;?
|
d@100
|
149 "(time+ <period> <moment>) -> <moment> Implemented as a
|
d@100
|
150 straightforward summation."
|
m@24
|
151 (time+ object2 object1))
|
m@24
|
152
|
m@24
|
153 (defmethod time+ ((object1 period) (object2 period))
|
d@100
|
154 "(time+ <period> <period>) -> <period> Implemented as a
|
d@100
|
155 straightforward summation."
|
m@24
|
156 (make-floating-period (+ (duration object1)
|
m@24
|
157 (duration object2))))
|
m@24
|
158
|
m@24
|
159 (defmethod time+ ((object1 moment) (object2 moment))
|
d@100
|
160 "(time+ <moment> <moment>) -> <condition:undefined-action> The
|
d@100
|
161 question makes no sense."
|
m@24
|
162 (error 'undefined-action :operation 'time+ :datatype (list (class-of object1) (class-of object2))))
|
m@24
|
163
|
m@24
|
164 (defmethod time- ((object1 moment) (object2 moment))
|
m@24
|
165 (make-anchored-period (timepoint object2)
|
m@24
|
166 (- (timepoint object1)
|
m@24
|
167 (timepoint object2))))
|
m@24
|
168
|
m@24
|
169 (defmethod time- ((object1 moment) (object2 period))
|
m@24
|
170 (make-moment (- (timepoint object1) (duration object2))))
|
m@24
|
171
|
m@24
|
172 (defmethod time- ((object1 period) (object2 moment)) ;?
|
m@24
|
173 (error 'undefined-action
|
m@24
|
174 :operation 'time-
|
m@24
|
175 :datatype (list (class-of object1) (class-of object2))))
|
m@24
|
176
|
m@24
|
177 (defmethod time- ((object1 period) (object2 period))
|
m@24
|
178 (make-floating-period (- (duration object2)
|
m@24
|
179 (duration object1))))
|
m@24
|
180
|
m@24
|
181 ;; these ones are less certain. I've just put them in, but think I
|
m@24
|
182 ;; should remove them and force the user to specify what they mean
|
m@24
|
183 ;; when they give objects that are both moments *and* periods to these
|
m@24
|
184 ;; functions.
|
m@24
|
185
|
m@24
|
186 (defmethod time- ((object1 anchored-period) (object2 anchored-period)) ;?
|
m@24
|
187 (time- (moment object1) (moment object2)))
|
m@24
|
188
|
m@24
|
189 (defmethod time- (object1 (object2 anchored-period)) ;?
|
m@24
|
190 (time- object1 (moment object2)))
|
m@24
|
191
|
m@24
|
192 (defmethod time- ((object1 anchored-period) object2) ;?
|
m@24
|
193 (time- (moment object1) object2))
|
m@24
|
194
|
m@24
|
195 (defmethod time> ((object1 moment) (object2 moment))
|
m@24
|
196 (> (timepoint object1) (timepoint object2)))
|
m@24
|
197
|
d@73
|
198 (defmethod time< ((object1 moment) (object2 moment))
|
d@73
|
199 (< (timepoint object1) (timepoint object2)))
|
d@73
|
200
|
m@24
|
201 (defmethod time= ((object1 moment) (object2 moment))
|
m@24
|
202 (= (timepoint object1) (timepoint object2)))
|
m@24
|
203
|
m@24
|
204 (defmethod duration> ((object1 period) (object2 period))
|
m@24
|
205 (> (duration object1) (duration object2)))
|
m@24
|
206
|
m@24
|
207 (defmethod duration= ((object1 period) (object2 period))
|
m@24
|
208 (= (duration object1) (duration object2)))
|
m@24
|
209
|
m@24
|
210 (defmethod duration* ((object1 period) (object2 number))
|
m@24
|
211 (make-floating-period (* (duration object1) object2)))
|
m@24
|
212
|
m@24
|
213 (defmethod duration* ((object1 number) (object2 period))
|
m@24
|
214 (duration* object2 object1))
|
m@24
|
215
|
m@24
|
216 (defmethod duration/ ((object1 period) (object2 number))
|
m@24
|
217 (make-floating-period (/ (duration object1) object2)))
|
m@24
|
218
|
c@111
|
219 ;;;; Pitch protocol
|
m@24
|
220
|
c@111
|
221 ;;; Some catch-all methods for undefined operations and cases where we
|
c@111
|
222 ;;; don't have enough information:
|
c@111
|
223 (macrolet ((def (name class1 class2)
|
c@111
|
224 `(defmethod ,name ((object1 ,class1) (object2 ,class2))
|
c@111
|
225 (error 'undefined-action :operation ',name
|
c@111
|
226 :datatype (list (class-of object1) (class-of object2))))))
|
c@111
|
227 (def pitch+ pitch-designator pitch-designator)
|
c@111
|
228 (def pitch- pitch-interval-designator pitch-designator))
|
m@24
|
229
|
c@111
|
230 (macrolet ((def (name class1 class2)
|
c@111
|
231 `(defmethod ,name ((object1 ,class1) (object2 ,class2))
|
c@111
|
232 (error 'insufficient-information :operation ',name
|
c@111
|
233 :datatype (list (class-of object1) (class-of object2))))))
|
c@111
|
234 (def pitch+ pitch-designator pitch-interval-designator)
|
c@111
|
235 (def pitch+ pitch-interval-designator pitch-designator)
|
c@111
|
236 (def pitch+ pitch-interval-designator pitch-interval-designator)
|
c@111
|
237 (def pitch- pitch-designator pitch-designator)
|
c@111
|
238 (def pitch- pitch-designator pitch-interval-designator)
|
c@111
|
239 (def pitch- pitch-interval-designator pitch-interval-designator))
|
m@24
|
240
|
c@111
|
241 ;;; chromatic pitch intervals
|
m@24
|
242
|
c@111
|
243 (defmethod pitch+ ((object1 chromatic-pitch)
|
c@111
|
244 (object2 chromatic-pitch-interval))
|
c@111
|
245 (make-chromatic-pitch (+ (midi-pitch-number object1) (span object2))))
|
c@111
|
246
|
c@111
|
247 (defmethod pitch+ ((object1 chromatic-pitch-interval)
|
c@111
|
248 (object2 chromatic-pitch))
|
c@111
|
249 (make-chromatic-pitch (+ (span object1) (midi-pitch-number object2))))
|
c@111
|
250
|
c@111
|
251 (defmethod pitch+ ((object1 chromatic-pitch-interval)
|
c@111
|
252 (object2 chromatic-pitch-interval))
|
c@105
|
253 (make-chromatic-pitch-interval (+ (span object1) (span object2))))
|
m@24
|
254
|
c@111
|
255 (defmethod pitch- ((object1 chromatic-pitch)
|
c@111
|
256 (object2 chromatic-pitch))
|
c@111
|
257 (make-chromatic-pitch-interval
|
c@105
|
258 (- (midi-pitch-number object1) (midi-pitch-number object2))))
|
m@24
|
259
|
c@111
|
260 (defmethod pitch- ((object1 chromatic-pitch)
|
c@111
|
261 (object2 chromatic-pitch-interval))
|
c@105
|
262 (make-chromatic-pitch (- (midi-pitch-number object1) (span object2))))
|
m@24
|
263
|
c@111
|
264 (defmethod pitch- ((object1 chromatic-pitch-interval)
|
c@111
|
265 (object2 chromatic-pitch-interval))
|
c@105
|
266 (make-chromatic-pitch-interval (- (span object1) (span object2))))
|
m@24
|
267
|
c@111
|
268 (defmethod pitch> ((object1 chromatic-pitch)
|
c@111
|
269 (object2 chromatic-pitch))
|
c@111
|
270 (> (midi-pitch-number object1) (midi-pitch-number object2)))
|
m@24
|
271
|
c@111
|
272 (defmethod pitch= ((object1 chromatic-pitch)
|
c@111
|
273 (object2 chromatic-pitch))
|
c@111
|
274 (= (midi-pitch-number object1) (midi-pitch-number object2)))
|
m@24
|
275
|
c@111
|
276 (defmethod interval> ((object1 chromatic-pitch-interval)
|
c@111
|
277 (object2 chromatic-pitch-interval))
|
c@111
|
278 (> (span object1) (span object2)))
|
m@24
|
279
|
c@111
|
280 (defmethod interval= ((object1 chromatic-pitch-interval)
|
c@111
|
281 (object2 chromatic-pitch-interval))
|
c@111
|
282 (= (span object1) (span object2)))
|
m@24
|
283
|
c@111
|
284 ;;; diatonic pitch intervals
|
m@24
|
285
|
c@111
|
286 (defmethod pitch+ ((object1 diatonic-pitch) (object2 diatonic-pitch-interval))
|
c@111
|
287 (let* ((cp (%p-pc object1))
|
c@111
|
288 (mp (%p-pm object1))
|
c@111
|
289 (span (span object2))
|
c@111
|
290 (cps (first span))
|
c@111
|
291 (mps (second span)))
|
c@111
|
292 (make-mips-pitch (+ cp cps) (+ mp mps))))
|
c@111
|
293
|
c@111
|
294 (defmethod pitch+ ((object1 diatonic-pitch-interval) (object2 diatonic-pitch))
|
c@111
|
295 (let* ((cp (%p-pc object2))
|
c@111
|
296 (mp (%p-pm object2))
|
c@111
|
297 (span (span object1))
|
c@111
|
298 (cps (first span))
|
c@111
|
299 (mps (second span)))
|
c@111
|
300 (make-mips-pitch (+ cp cps) (+ mp mps))))
|
c@111
|
301
|
c@111
|
302 (defmethod pitch+ ((object1 diatonic-pitch-interval)
|
c@111
|
303 (object2 diatonic-pitch-interval))
|
c@111
|
304 (let* ((span1 (span object1))
|
c@111
|
305 (span2 (span object2)))
|
c@111
|
306 (make-mips-pitch-interval (+ (first span1) (first span2))
|
c@111
|
307 (+ (second span1) (second span2)))))
|
c@111
|
308
|
c@111
|
309 (defmethod pitch- ((object1 diatonic-pitch) (object2 diatonic-pitch))
|
c@111
|
310 (let ((cp1 (%p-pc object1))
|
c@111
|
311 (mp1 (%p-pm object1))
|
c@111
|
312 (cp2 (%p-pc object2))
|
c@111
|
313 (mp2 (%p-pm object2)))
|
c@111
|
314 (make-mips-pitch-interval (- cp1 cp2) (- mp1 mp2))))
|
c@111
|
315
|
c@111
|
316 (defmethod pitch- ((object1 diatonic-pitch) (object2 diatonic-pitch-interval))
|
c@111
|
317 (let* ((cp (%p-pc object1))
|
c@111
|
318 (mp (%p-pm object1))
|
c@111
|
319 (span (span object2))
|
c@111
|
320 (cps (first span))
|
c@111
|
321 (mps (second span)))
|
c@111
|
322 (make-mips-pitch (- cp cps) (- mp mps))))
|
c@111
|
323
|
c@111
|
324 (defmethod pitch- ((object1 diatonic-pitch-interval)
|
c@111
|
325 (object2 diatonic-pitch-interval))
|
c@111
|
326 (let ((span1 (span object1))
|
c@111
|
327 (span2 (span object2)))
|
c@111
|
328 (make-mips-pitch-interval (- (first span1) (first span2))
|
c@111
|
329 (- (second span1) (second span2)))))
|
c@111
|
330
|
c@111
|
331 (defmethod pitch> ((p1 diatonic-pitch) (p2 diatonic-pitch))
|
c@111
|
332 (error 'undefined-action :operation 'pitch>
|
c@111
|
333 :datatype (list (class-of p1) (class-of p2))))
|
c@111
|
334
|
c@111
|
335 (defmethod pitch= ((p1 diatonic-pitch) (p2 diatonic-pitch))
|
c@111
|
336 (let ((c1 (%p-pc p1)) (m1 (%p-pm p1))
|
c@111
|
337 (c2 (%p-pc p2)) (m2 (%p-pm p2)))
|
c@111
|
338 (and c1 c2 (= c1 c2)
|
c@111
|
339 m1 m2 (= m1 m2))))
|
m@24
|
340
|
m@24
|
341
|
m@24
|
342 ;; Allen
|
m@24
|
343
|
m@24
|
344 (defmethod meets ((object1 anchored-period)
|
m@24
|
345 (object2 anchored-period))
|
m@24
|
346 (or (time= (cut-off object1) object2)
|
m@24
|
347 (time= (cut-off object2) object1)))
|
m@24
|
348
|
m@24
|
349 (defmethod before ((object1 anchored-period)
|
m@24
|
350 (object2 anchored-period))
|
m@24
|
351 (time> object2 (cut-off object1)))
|
m@24
|
352
|
m@24
|
353 (defmethod overlaps ((object1 anchored-period)
|
m@24
|
354 (object2 anchored-period))
|
m@24
|
355 ;; FIXME: Is there a tidier method?
|
m@24
|
356 (or (and (time> object2 object1) ; object1 starts before object2
|
m@24
|
357 (time> (cut-off object1) object2) ; object1 ends after object2 starts
|
m@24
|
358 (time> (cut-off object2) (cut-off object1))) ; object1 ends before object2 does
|
m@24
|
359 (and (time> object1 object2) ; object1 starts after object2
|
m@24
|
360 (time> (cut-off object2) object1) ; object1 starts before object2 ends
|
m@24
|
361 (time> (cut-off object1) (cut-off object2))))) ; object1 ends after object2 does
|
m@24
|
362
|
m@24
|
363 (defmethod during ((object1 anchored-period)
|
m@24
|
364 (object2 anchored-period))
|
m@24
|
365 (and (time> object1 object2)
|
m@24
|
366 (time< (cut-off object2) (cut-off object2))))
|
m@24
|
367
|
m@24
|
368 (defmethod starts ((object1 anchored-period)
|
m@24
|
369 (object2 anchored-period))
|
m@24
|
370 (time= object1 object2))
|
m@24
|
371
|
m@24
|
372 (defmethod ends ((object1 anchored-period)
|
m@24
|
373 (object2 anchored-period))
|
m@24
|
374 (time= (cut-off object1) (cut-off object2)))
|
m@24
|
375
|
m@24
|
376 ;; ...and
|
m@24
|
377
|
d@33
|
378 (defmethod period= ((object1 anchored-period)
|
c@105
|
379 (object2 anchored-period))
|
d@33
|
380 (and (time= object1 object2)
|
d@33
|
381 (duration= object1 object2)))
|
d@33
|
382 (defmethod period= ((object1 floating-period)
|
d@33
|
383 (object2 floating-period))
|
d@33
|
384 (duration= object1 object2))
|
d@33
|
385
|
m@24
|
386 (defmethod period-intersection ((object1 anchored-period)
|
m@24
|
387 (object2 anchored-period))
|
m@24
|
388 (cond
|
m@24
|
389 ((disjoint object1 object2)
|
m@24
|
390 ;; if they don't overlap, return nil, not a negative-valued
|
m@24
|
391 ;; period
|
m@24
|
392 nil)
|
m@24
|
393 ((let* ((start (if (time> (onset object2) (onset object1))
|
m@24
|
394 (onset object2)
|
m@24
|
395 (onset object1)))
|
m@24
|
396 (duration (duration (time- (if (time> (cut-off object2) (cut-off object1))
|
m@24
|
397 (cut-off object1)
|
m@24
|
398 (cut-off object2))
|
m@24
|
399 start))))
|
m@24
|
400 (make-anchored-period (timepoint start) duration)))))
|
m@24
|
401
|
m@24
|
402
|