annotate base/methods.lisp @ 259:c4e9a7bb9897

change move-to-first-bar to return a constituent, not a list of events
author Jamie Forth <j.forth@gold.ac.uk>
date Sat, 19 Mar 2011 18:50:24 +0000
parents aac79c0ac1b9
children f5836b2bf334
rev   line source
m@24 1 (cl:in-package #:amuse)
m@24 2
m@89 3 ;;; monody
m@89 4
m@143 5 (defmethod ensure-monody ((m standard-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))
j@187 37 (make-diatonic-pitch #\C 0 4))
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
d@136 69 (defmethod chromatic-pitch ((pitch chromatic-pitch))
d@136 70 pitch)
m@24 71
d@136 72 (defmethod midi-pitch-number ((pitch chromatic-pitch))
d@136 73 (%chromatic-pitch-number pitch))
m@24 74
d@136 75 (defmethod midi-pitch-number ((pitch pitch))
d@136 76 (%chromatic-pitch-number (chromatic-pitch pitch)))
m@24 77
m@113 78 (defmethod print-object ((o chromatic-pitch) stream)
m@113 79 (print-unreadable-object (o stream :type t)
m@113 80 (write (midi-pitch-number o) :stream stream)))
m@113 81
m@113 82 (defmethod print-object ((o chromatic-pitch-interval) stream)
m@113 83 (print-unreadable-object (o stream :type t)
m@113 84 (write (span o) :stream stream)))
m@113 85
m@113 86
d@136 87 (defmethod span ((pitch-interval chromatic-pitch-interval))
d@136 88 (%chromatic-pitch-interval-span pitch-interval))
m@24 89
d@136 90 (defmethod duration ((period standard-period))
d@136 91 (%period-interval period))
m@24 92
d@136 93 (defmethod (setf duration) ((value real) (period standard-period))
d@136 94 (setf (%period-interval period) value))
d@33 95
d@136 96 (defmethod timepoint ((moment standard-moment))
d@136 97 (%moment-time moment))
m@24 98
d@136 99 (defmethod (setf timepoint) ((value real) (moment standard-moment))
d@136 100 (setf (%moment-time moment) value))
d@33 101
d@136 102 (defmethod cut-off ((anchored-period standard-anchored-period))
d@136 103 (make-instance 'standard-moment
d@136 104 :time (+ (%moment-time anchored-period)
d@136 105 (%period-interval anchored-period))))
d@73 106
d@136 107 (defmethod print-object ((o standard-moment) stream)
m@113 108 (print-unreadable-object (o stream :type t)
m@113 109 (write (timepoint o) :stream stream)))
m@113 110
d@136 111 (defmethod print-object ((o standard-period) stream)
m@113 112 (print-unreadable-object (o stream :type t)
m@113 113 (write (duration o) :stream stream)))
m@113 114
d@136 115 (defmethod print-object ((o standard-anchored-period) stream)
m@126 116 (print-unreadable-object (o stream :type t)
m@126 117 (format stream "~A ~A" (timepoint o) (duration o))))
m@113 118
d@136 119 (defmethod beat-units-per-bar ((time-signature standard-time-signature))
m@24 120 (%basic-time-signature-numerator time-signature))
m@24 121
d@136 122 (defmethod beat-units ((time-signature standard-time-signature))
m@24 123 (%basic-time-signature-denominator time-signature))
m@24 124
d@136 125 (defmethod time-signature-equal ((ts1 standard-time-signature)
d@136 126 (ts2 standard-time-signature))
m@67 127 (let ((n1 (time-signature-numerator ts1))
m@67 128 (n2 (time-signature-numerator ts2))
m@67 129 (d1 (time-signature-denominator ts1))
m@67 130 (d2 (time-signature-denominator ts2)))
m@67 131 (and n1 n2 (= n1 n2)
m@67 132 d1 d2 (= d1 d2))))
m@67 133
d@136 134 (defmethod print-object ((sts standard-time-signature) stream)
d@136 135 (print-unreadable-object (sts stream :type t)
d@136 136 (format stream "~A/~A" (beat-units-per-bar sts) (beat-units sts))))
m@113 137
d@136 138 (defmethod key-signature-sharps ((key-signature standard-key-signature))
m@24 139 (%basic-key-signature-sharp-count key-signature))
m@24 140
m@45 141 (defmethod key-signature-mode ((ks midi-key-signature))
m@45 142 (%midi-key-signature-mode ks))
m@45 143
m@113 144 (defmethod print-object ((mks midi-key-signature) stream)
m@113 145 (print-unreadable-object (mks stream :type t)
m@113 146 (format stream "~A ~A"
m@113 147 (%basic-key-signature-sharp-count mks)
m@113 148 (%midi-key-signature-mode mks))))
m@113 149
d@136 150 (defmethod key-signature-equal ((ks1 standard-key-signature)
d@136 151 (ks2 standard-key-signature))
m@67 152 (let ((s1 (key-signature-sharps ks1))
m@67 153 (s2 (key-signature-sharps ks2)))
m@67 154 (and s1 s2 (= s1 s2))))
m@67 155
m@67 156 (defmethod key-signature-equal ((ks1 midi-key-signature)
m@67 157 (ks2 midi-key-signature))
m@67 158 (let ((s1 (key-signature-sharps ks1))
m@67 159 (s2 (key-signature-sharps ks2))
m@67 160 (m1 (key-signature-mode ks1))
m@67 161 (m2 (key-signature-mode ks2)))
m@67 162 (and s1 s2 (= s1 s2)
m@67 163 m1 m2 (= m1 m2))))
m@67 164
d@136 165 (defmethod bpm ((tempo standard-tempo))
m@24 166 (%tempo-bpm tempo))
m@24 167
d@136 168 (defmethod print-object ((tempo standard-tempo) stream)
m@113 169 (print-unreadable-object (tempo stream :type t)
m@113 170 (write (bpm tempo) :stream stream)))
m@113 171
m@67 172 (defmethod tempo-equal ((t1 tempo) (t2 tempo))
m@67 173 (and (bpm t1) (bpm t2) (= t1 t2)))
m@67 174
m@67 175
m@24 176 ;; Time protocol
m@24 177
d@136 178 (defmethod time+ ((moment standard-moment) (period standard-period))
d@136 179 "Returns a <standard-moment>. Implemented as a straightforward
d@121 180 summation."
d@136 181 (make-standard-moment (+ (timepoint moment) (duration period))))
m@24 182
d@136 183 (defmethod time+ ((period standard-period) (moment standard-moment)) ;?
d@136 184 "Returns a <standard-moment>. Implemented as a straightforward
d@136 185 summation and defined by default as (time+ <moment> <period>)."
d@137 186 (time+ moment period))
m@24 187
d@136 188 (defmethod time+ ((period1 standard-period)
d@136 189 (period2 standard-period))
d@136 190 "Returns a <standard-period>. Implemented as a straightforward
d@121 191 summation."
d@136 192 (make-standard-period (+ (duration period1)
d@136 193 (duration period2))))
m@24 194
d@136 195 (defmethod time+ ((moment1 moment) (moment2 moment))
d@121 196 "Returns <condition:undefined-action>. The question makes no
d@121 197 sense."
d@136 198 (error 'undefined-action :operation 'time+
d@137 199 :datatype (list (class-of moment1) (class-of moment2))))
m@24 200
d@136 201 (defmethod time- ((moment1 standard-moment) (moment2 standard-moment))
d@136 202 "Returns <standard-anchored-period> with an onset at moment2 and
d@136 203 extending to moment1"
d@136 204 (make-standard-anchored-period (timepoint moment2)
d@136 205 (- (timepoint moment1)
d@136 206 (timepoint moment2))))
m@24 207
d@136 208 (defmethod time- ((moment standard-moment) (period standard-period))
d@136 209 "Returns <standard-moment>. Simple subtraction."
d@136 210 (make-standard-moment (- (timepoint moment)
d@136 211 (duration period))))
m@24 212
d@136 213 (defmethod time- ((period period) (moment moment)) ;?
d@121 214 "Returns <condition:undefined-action>. The question makes no
d@121 215 sense"
m@24 216 (error 'undefined-action
m@24 217 :operation 'time-
d@137 218 :datatype (list (class-of period) (class-of moment))))
m@24 219
d@136 220 (defmethod time- ((period1 standard-period) (period2 standard-period))
d@136 221 "Returns <standard-period> spanning the difference of the
d@121 222 periods"
d@136 223 (make-standard-period (- (duration period2)
d@136 224 (duration period1))))
m@24 225
m@24 226 ;; these ones are less certain. I've just put them in, but think I
m@24 227 ;; should remove them and force the user to specify what they mean
m@24 228 ;; when they give objects that are both moments *and* periods to these
m@24 229 ;; functions.
m@24 230
m@24 231 (defmethod time- ((object1 anchored-period) (object2 anchored-period)) ;?
m@24 232 (time- (moment object1) (moment object2)))
m@24 233
m@24 234 (defmethod time- (object1 (object2 anchored-period)) ;?
m@24 235 (time- object1 (moment object2)))
m@24 236
m@24 237 (defmethod time- ((object1 anchored-period) object2) ;?
m@24 238 (time- (moment object1) object2))
m@24 239
m@24 240 (defmethod time> ((object1 moment) (object2 moment))
m@24 241 (> (timepoint object1) (timepoint object2)))
m@24 242
d@73 243 (defmethod time< ((object1 moment) (object2 moment))
d@73 244 (< (timepoint object1) (timepoint object2)))
d@73 245
m@24 246 (defmethod time= ((object1 moment) (object2 moment))
m@24 247 (= (timepoint object1) (timepoint object2)))
m@24 248
d@136 249 (defmethod duration> ((period1 standard-period) (period2 standard-period))
d@136 250 (> (duration period1) (duration period2)))
m@24 251
d@136 252 (defmethod duration= ((period1 standard-period) (period2 standard-period))
d@136 253 (= (duration period1) (duration period2)))
m@24 254
d@136 255 (defmethod duration* ((period1 standard-period) (object2 number))
d@137 256 (make-standard-period (* (duration period1) object2)))
m@24 257
d@136 258 (defmethod duration* ((object1 number) (period standard-period))
d@136 259 (duration* period object1))
m@24 260
d@136 261 (defmethod duration/ ((period standard-period) (object2 number))
d@137 262 (make-standard-period (/ (duration period) object2)))
m@24 263
c@111 264 ;;;; Pitch protocol
m@24 265
c@111 266 ;;; Some catch-all methods for undefined operations and cases where we
c@111 267 ;;; don't have enough information:
c@111 268 (macrolet ((def (name class1 class2)
c@111 269 `(defmethod ,name ((object1 ,class1) (object2 ,class2))
c@111 270 (error 'undefined-action :operation ',name
c@111 271 :datatype (list (class-of object1) (class-of object2))))))
d@136 272 (def pitch+ pitch pitch)
d@136 273 (def pitch- pitch-interval pitch))
m@24 274
c@111 275 (macrolet ((def (name class1 class2)
c@111 276 `(defmethod ,name ((object1 ,class1) (object2 ,class2))
c@111 277 (error 'insufficient-information :operation ',name
c@111 278 :datatype (list (class-of object1) (class-of object2))))))
d@136 279 (def pitch+ pitch pitch-interval)
d@136 280 (def pitch+ pitch-interval pitch)
d@136 281 (def pitch+ pitch-interval pitch-interval)
d@136 282 (def pitch- pitch pitch)
d@136 283 (def pitch- pitch pitch-interval)
d@136 284 (def pitch- pitch-interval pitch-interval))
m@24 285
c@111 286 ;;; chromatic pitch intervals
m@24 287
c@111 288 (defmethod pitch+ ((object1 chromatic-pitch)
c@111 289 (object2 chromatic-pitch-interval))
c@111 290 (make-chromatic-pitch (+ (midi-pitch-number object1) (span object2))))
c@111 291
c@111 292 (defmethod pitch+ ((object1 chromatic-pitch-interval)
c@111 293 (object2 chromatic-pitch))
c@111 294 (make-chromatic-pitch (+ (span object1) (midi-pitch-number object2))))
c@111 295
c@111 296 (defmethod pitch+ ((object1 chromatic-pitch-interval)
c@111 297 (object2 chromatic-pitch-interval))
c@105 298 (make-chromatic-pitch-interval (+ (span object1) (span object2))))
m@24 299
c@111 300 (defmethod pitch- ((object1 chromatic-pitch)
c@111 301 (object2 chromatic-pitch))
c@111 302 (make-chromatic-pitch-interval
c@105 303 (- (midi-pitch-number object1) (midi-pitch-number object2))))
m@24 304
c@111 305 (defmethod pitch- ((object1 chromatic-pitch)
c@111 306 (object2 chromatic-pitch-interval))
c@105 307 (make-chromatic-pitch (- (midi-pitch-number object1) (span object2))))
m@24 308
c@111 309 (defmethod pitch- ((object1 chromatic-pitch-interval)
c@111 310 (object2 chromatic-pitch-interval))
c@105 311 (make-chromatic-pitch-interval (- (span object1) (span object2))))
m@24 312
c@111 313 (defmethod pitch> ((object1 chromatic-pitch)
c@111 314 (object2 chromatic-pitch))
c@111 315 (> (midi-pitch-number object1) (midi-pitch-number object2)))
m@24 316
c@111 317 (defmethod pitch= ((object1 chromatic-pitch)
c@111 318 (object2 chromatic-pitch))
c@111 319 (= (midi-pitch-number object1) (midi-pitch-number object2)))
m@24 320
c@111 321 (defmethod interval> ((object1 chromatic-pitch-interval)
c@111 322 (object2 chromatic-pitch-interval))
c@111 323 (> (span object1) (span object2)))
m@24 324
c@111 325 (defmethod interval= ((object1 chromatic-pitch-interval)
c@111 326 (object2 chromatic-pitch-interval))
c@111 327 (= (span object1) (span object2)))
m@24 328
c@111 329 ;;; diatonic pitch intervals
m@24 330
c@111 331 (defmethod pitch+ ((object1 diatonic-pitch) (object2 diatonic-pitch-interval))
c@111 332 (let* ((cp (%p-pc object1))
c@111 333 (mp (%p-pm object1))
c@111 334 (span (span object2))
c@111 335 (cps (first span))
c@111 336 (mps (second span)))
c@111 337 (make-mips-pitch (+ cp cps) (+ mp mps))))
c@111 338
c@111 339 (defmethod pitch+ ((object1 diatonic-pitch-interval) (object2 diatonic-pitch))
c@111 340 (let* ((cp (%p-pc object2))
c@111 341 (mp (%p-pm object2))
c@111 342 (span (span object1))
c@111 343 (cps (first span))
c@111 344 (mps (second span)))
c@111 345 (make-mips-pitch (+ cp cps) (+ mp mps))))
c@111 346
c@111 347 (defmethod pitch+ ((object1 diatonic-pitch-interval)
c@111 348 (object2 diatonic-pitch-interval))
c@111 349 (let* ((span1 (span object1))
c@111 350 (span2 (span object2)))
c@111 351 (make-mips-pitch-interval (+ (first span1) (first span2))
c@111 352 (+ (second span1) (second span2)))))
c@111 353
c@111 354 (defmethod pitch- ((object1 diatonic-pitch) (object2 diatonic-pitch))
c@111 355 (let ((cp1 (%p-pc object1))
c@111 356 (mp1 (%p-pm object1))
c@111 357 (cp2 (%p-pc object2))
c@111 358 (mp2 (%p-pm object2)))
c@111 359 (make-mips-pitch-interval (- cp1 cp2) (- mp1 mp2))))
c@111 360
c@111 361 (defmethod pitch- ((object1 diatonic-pitch) (object2 diatonic-pitch-interval))
c@111 362 (let* ((cp (%p-pc object1))
c@111 363 (mp (%p-pm object1))
c@111 364 (span (span object2))
c@111 365 (cps (first span))
c@111 366 (mps (second span)))
c@111 367 (make-mips-pitch (- cp cps) (- mp mps))))
c@111 368
c@111 369 (defmethod pitch- ((object1 diatonic-pitch-interval)
c@111 370 (object2 diatonic-pitch-interval))
c@111 371 (let ((span1 (span object1))
c@111 372 (span2 (span object2)))
c@111 373 (make-mips-pitch-interval (- (first span1) (first span2))
c@111 374 (- (second span1) (second span2)))))
c@111 375
c@111 376 (defmethod pitch> ((p1 diatonic-pitch) (p2 diatonic-pitch))
c@111 377 (error 'undefined-action :operation 'pitch>
c@111 378 :datatype (list (class-of p1) (class-of p2))))
c@111 379
c@111 380 (defmethod pitch= ((p1 diatonic-pitch) (p2 diatonic-pitch))
c@111 381 (let ((c1 (%p-pc p1)) (m1 (%p-pm p1))
c@111 382 (c2 (%p-pc p2)) (m2 (%p-pm p2)))
c@111 383 (and c1 c2 (= c1 c2)
c@111 384 m1 m2 (= m1 m2))))
m@24 385
m@24 386
m@24 387 ;; Allen
m@24 388
m@24 389 (defmethod meets ((object1 anchored-period)
m@24 390 (object2 anchored-period))
m@24 391 (or (time= (cut-off object1) object2)
m@24 392 (time= (cut-off object2) object1)))
m@24 393
m@24 394 (defmethod before ((object1 anchored-period)
m@24 395 (object2 anchored-period))
m@24 396 (time> object2 (cut-off object1)))
m@24 397
m@24 398 (defmethod overlaps ((object1 anchored-period)
m@24 399 (object2 anchored-period))
m@24 400 ;; FIXME: Is there a tidier method?
m@24 401 (or (and (time> object2 object1) ; object1 starts before object2
m@24 402 (time> (cut-off object1) object2) ; object1 ends after object2 starts
m@24 403 (time> (cut-off object2) (cut-off object1))) ; object1 ends before object2 does
m@24 404 (and (time> object1 object2) ; object1 starts after object2
m@24 405 (time> (cut-off object2) object1) ; object1 starts before object2 ends
m@24 406 (time> (cut-off object1) (cut-off object2))))) ; object1 ends after object2 does
m@24 407
m@24 408 (defmethod during ((object1 anchored-period)
m@24 409 (object2 anchored-period))
j@205 410 (or (and (time> object1 object2)
j@205 411 (time<= (cut-off object1) (cut-off object2)))
j@205 412 (and (time>= object1 object2)
j@205 413 (time< (cut-off object1) (cut-off object2)))))
m@24 414
m@24 415 (defmethod starts ((object1 anchored-period)
m@24 416 (object2 anchored-period))
m@24 417 (time= object1 object2))
m@24 418
m@24 419 (defmethod ends ((object1 anchored-period)
m@24 420 (object2 anchored-period))
m@24 421 (time= (cut-off object1) (cut-off object2)))
m@24 422
m@24 423 ;; ...and
m@24 424
d@33 425 (defmethod period= ((object1 anchored-period)
c@105 426 (object2 anchored-period))
d@33 427 (and (time= object1 object2)
d@33 428 (duration= object1 object2)))
d@136 429 (defmethod period= ((object1 period)
d@136 430 (object2 period))
d@33 431 (duration= object1 object2))
d@33 432
d@136 433 (defmethod period-intersection ((object1 standard-anchored-period)
d@136 434 (object2 standard-anchored-period))
m@24 435 (cond
m@24 436 ((disjoint object1 object2)
m@24 437 ;; if they don't overlap, return nil, not a negative-valued
m@24 438 ;; period
m@24 439 nil)
m@24 440 ((let* ((start (if (time> (onset object2) (onset object1))
m@24 441 (onset object2)
m@24 442 (onset object1)))
m@24 443 (duration (duration (time- (if (time> (cut-off object2) (cut-off object1))
m@24 444 (cut-off object1)
m@24 445 (cut-off object2))
m@24 446 start))))
m@24 447 (make-anchored-period (timepoint start) duration)))))
m@24 448
d@136 449 ;; Time constructors
d@136 450 (defmethod make-moment ((time-value real))
d@136 451 "Returns STANDARD-MOMENT given a real"
d@136 452 (make-standard-moment time-value))
d@136 453 (defmethod make-period ((duration-value real))
d@136 454 "Returns STANDARD-PERIOD given a real"
d@136 455 (make-standard-period duration-value))
d@136 456 (defmethod make-anchored-period ((onset-value real) (duration-value real))
d@136 457 "Returns STANDARD-ANCHORED-PERIOD given a real"
m@143 458 (make-standard-anchored-period onset-value duration-value))
d@151 459
d@151 460 ;; Needed by some sequence functions, notably remove-if.
d@151 461 (defmethod sequence:make-sequence-like :around ((o standard-composition) length
d@151 462 &key (initial-element nil iep)
d@151 463 (initial-contents nil icp))
d@152 464 "Around method for make-sequence-like, only with all slots
d@151 465 preserved from the source sequence (except onset and duration,
d@151 466 which are calculated afresh)."
d@151 467 (declare (ignore length initial-element initial-contents iep icp))
d@178 468 (let ((new-sequence (call-next-method)) (slot-name))
d@151 469 ;; Get timing information
d@153 470 (setf new-sequence (%recompute-standard-composition-period new-sequence))
d@153 471 (dolist (slotd (sb-mop:class-slots (class-of new-sequence)) new-sequence)
d@178 472 (setf slot-name (sb-mop:slot-definition-name slotd))
d@178 473 (unless (or (equal slot-name '%data)
d@178 474 (equal slot-name 'time)
d@178 475 (equal slot-name 'interval)
d@178 476 (not (slot-boundp o slot-name)))
d@153 477 (setf (sb-mop:slot-value-using-class (class-of new-sequence)
d@153 478 new-sequence
d@153 479 slotd)
d@153 480 (sb-mop:slot-value-using-class (class-of new-sequence)
d@153 481 o ;; if this isn't the same, we're lost anyway
d@153 482 slotd))))))
d@153 483
d@153 484 (defun %recompute-standard-composition-period (composition)
d@153 485 "Find onset and duration times for newly-made composition object."
d@153 486 (let ((start) (finish))
d@153 487 (sequence:dosequence (element composition)
d@151 488 ;; Actually, this next bit is pretty stupid - I know this is
d@151 489 ;; ordered, so this bit could be replaced by
d@151 490 ;; (setf (timepoint new-sequence)
d@151 491 ;; (timepoint (elt new-sequence 0)))
d@151 492 ;; outside of the loop.
d@152 493 (when (and element
d@152 494 (or (null start)
d@152 495 (< (timepoint element) start)))
d@152 496 (setf start (timepoint element)))
d@152 497 (when (and element
d@152 498 (or (null finish)
d@152 499 (> (timepoint (cut-off element))
d@152 500 finish)))
d@152 501 (setf finish (timepoint (cut-off element)))))
d@152 502 (unless start
d@152 503 (setf start 0))
d@152 504 (unless finish
d@152 505 (setf finish 0))
d@153 506 (setf (timepoint composition) start
d@153 507 (duration composition) (- finish start))
d@153 508 composition))
d@153 509
d@153 510
d@153 511 (defmethod sequence:adjust-sequence :around ((o standard-composition) length
d@153 512 &key initial-element
d@153 513 (initial-contents nil icp))
d@153 514 (declare (ignore length o initial-element initial-contents icp))
d@175 515 (%recompute-standard-composition-period (call-next-method)))
d@175 516
j@193 517 (defmethod get-constituents ((identifier composition-identifier))
j@193 518 (list (get-composition identifier)))
d@175 519
d@175 520 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
d@175 521 ;;
d@175 522 ;; Experimental:
d@175 523 ;;
d@175 524
d@175 525 ;; Some not obviously correct implementations of the new metre
d@175 526 ;; functions. These are no worse than we're already using (they should
d@175 527 ;; be more or less equivalent)
d@175 528
d@175 529 (defmethod bar-period ((time-signature standard-time-signature)
d@175 530 object)
d@175 531 (make-standard-period (* (duration (crotchet object))
d@175 532 (time-signature-numerator time-signature)
d@175 533 (/ 4 (time-signature-denominator time-signature)))))
d@175 534
d@175 535 (defmethod current-bar ((moment standard-moment) (composition composition))
d@175 536 (let* ((time-sig (car (get-applicable-time-signatures
d@175 537 (make-standard-anchored-period (timepoint moment)
d@175 538 (duration (crotchet composition)))
d@175 539 composition)))
d@175 540 (bar-duration (bar-period time-sig composition)))
d@175 541 (do* ((start (onset time-sig) next-start)
d@175 542 (next-start (time+ start bar-duration) (time+ start bar-duration)))
d@175 543 ((time> next-start moment)
d@175 544 (make-standard-anchored-period (timepoint start)
d@175 545 (duration bar-duration))))))
d@175 546
j@233 547 (defmethod ioi-from-bar ((event event))
j@258 548 "Within-short-bar-p here is for catching anacruses. The correct IOI
j@258 549 of the event(s) from the barline can be calculated by finding the
j@258 550 corresponding position of timepoint 0 relative to the bar, and then
j@258 551 calculating the event IOI relative to that. FIXME: This will almost
j@258 552 certainly not be the desired behaviour if 'short bars' are found
j@258 553 within a piece. Also, what about 'long bars'? Also, more generally,
j@258 554 what should we do if get-app-time-sig gives us multiple
j@258 555 time-signatures? We should at least be checking."
j@258 556 (cond
j@258 557 ((within-short-bar-p event)
j@258 558 (+ (timepoint (onset event))
j@258 559 (- (duration (bar-period (car (get-applicable-time-signatures
j@258 560 event (composition event)))
j@258 561 event))
j@258 562 (duration (current-bar event (composition event))))))
j@258 563 (t
j@258 564 (- (timepoint (onset event))
j@258 565 (timepoint (current-bar event (composition event)))))))
j@233 566
j@233 567 (defmethod ioi-from-bar ((constituent constituent))
j@258 568 "FIXME: Check for short bars, or maybe just use the first event?"
j@233 569 (- (timepoint (onset constituent))
j@233 570 (timepoint (current-bar constituent constituent))))
j@233 571
j@233 572 (defmethod onset-in-bar ((o moment))
j@250 573 "FIXME: Won't actually work for standard-moments because they do not
j@250 574 have a composition slot! So either we allow for 'linked moments', or
j@250 575 change the method to have an optional composition parameter."
j@233 576 (1+ (ioi-from-bar o)))
j@233 577
j@250 578 (defmethod onset-in-bar-relative-to-tactus ((o moment))
j@250 579 (1+ (/ (ioi-from-bar o)
j@250 580 (tactus-duration
j@250 581 (car (get-applicable-time-signatures o (composition o)))))))
j@250 582
j@258 583 (defmethod within-short-bar-p ((event linked-event))
j@258 584 (let ((time-sig (get-applicable-time-signatures event
j@258 585 (composition event))))
j@258 586 (assert (= (length time-sig) 1))
j@258 587 (setf time-sig (car time-sig))
j@258 588 (let ((bar-duration (bar-period time-sig (composition event)))
j@258 589 (current-bar (current-bar event (composition event))))
j@258 590 (duration< current-bar bar-duration))))
j@258 591
d@175 592 (defmethod beat-period ((moment standard-moment)
d@175 593 (time-signature standard-time-signature)
d@175 594 (composition composition))
d@175 595 ;; Simple example - standard-time-signature has constant tactus
d@175 596 (let* ((containing-bar (current-bar moment composition))
d@175 597 (beat-duration (* (duration (crotchet composition))
d@175 598 (tactus-duration time-signature)))
d@175 599 (beat-period (make-standard-anchored-period (timepoint containing-bar)
d@175 600 beat-duration)))
d@175 601 (do ()
d@175 602 ((time> (cut-off beat-period) moment) beat-period)
d@175 603 (setf (timepoint beat-period) (timepoint (cut-off beat-period))))))
d@175 604
d@175 605 (defmethod current-beat ((moment standard-moment) (composition composition))
d@175 606 ;; Assume at most one time signature per bar (otherwise, this is hell)
d@175 607 (let* ((time-sig (car (get-applicable-time-signatures (current-bar moment composition) composition))))
d@175 608 (if time-sig
d@175 609 (beat-period moment time-sig composition)
d@175 610 ;; If no time-sig, there's no way of answering this
d@175 611 ;; directly. There may be sensible defaults, but it's the job
d@175 612 ;; of an implementation's author to solve that.
d@175 613 (error 'insufficient-information :operation 'beat-period :datatype (class-of composition)))))
d@175 614
j@208 615
j@245 616 ;;;=====================================================================
j@230 617 ;;; Copying events in time
j@245 618 ;;;=====================================================================
j@208 619
j@208 620 (defmethod move-to-first-bar ((composition composition))
j@226 621 (let ((offset (floor (timepoint (elt composition 0)))))
j@208 622 (loop
j@209 623 for event in (%list-slot-sequence-data composition)
j@208 624 do (setf event (copy-event event))
j@208 625 do (setf (timepoint event)
j@208 626 (- (timepoint event) offset))
j@208 627 collect event into shifted-events
j@259 628 finally (return
j@259 629 (sequence:make-sequence-like
j@259 630 composition
j@259 631 (length composition)
j@259 632 :initial-contents shifted-events)))))
j@230 633
j@230 634
j@245 635 ;;;=====================================================================
j@230 636 ;;; Searching for events
j@245 637 ;;;=====================================================================
j@230 638
j@230 639 (defmethod find-next-event ((source-event event) &key predicate test
j@230 640 break-test search-list)
j@230 641 "Ideally a sorted search list that begins with the first event after
j@230 642 the source-event should be provided, otherwise, the search will begin
j@230 643 from the beginning."
j@230 644 (unless search-list (setf search-list (composition source-event)))
j@230 645 (cond
j@230 646 ((and test predicate)
j@230 647 (error "Supplied both a test and a predicate."))
j@230 648 (test
j@230 649 (sequence:dosequence (e search-list nil)
j@230 650 (when (and (time> (onset e) (onset source-event))
j@230 651 (funcall test source-event e))
j@230 652 (return e))
j@230 653 (when break-test
j@230 654 (when (funcall break-test source-event e)
j@230 655 (return nil)))))
j@230 656 (predicate
j@230 657 (sequence:dosequence (e search-list nil)
j@230 658 (when (and (time> (onset e) (onset source-event))
j@230 659 (funcall predicate e))
j@230 660 (return e))
j@230 661 (when break-test
j@230 662 (when (funcall break-test source-event e)
j@230 663 (return nil)))))))
j@230 664
j@230 665
j@245 666 ;;;=====================================================================
j@230 667 ;;; Sorting Compositions
j@245 668 ;;;=====================================================================
j@230 669
j@230 670 (defmethod event< ((event1 event) (event2 event) attribute-list)
j@230 671 (dolist (attribute attribute-list nil) ;nil if equal
j@230 672 (if (< (funcall attribute event1) (funcall attribute event2))
j@230 673 (return t)
j@230 674 (if (> (funcall attribute event1) (funcall attribute event2))
j@230 675 (return nil)))))
j@230 676
j@230 677 (defun make-event< (attribute-list)
j@230 678 (lambda (event1 event2)
j@230 679 (funcall #'event< event1 event2 attribute-list)))
j@230 680
j@230 681 (defmethod sort-composition ((composition composition) dimension-spec)
j@230 682 (sequence:make-sequence-like composition
j@230 683 (length composition)
j@230 684 :initial-contents
j@230 685 (stable-sort
j@230 686 (copy-seq composition)
j@230 687 (make-event< dimension-spec))))