annotate base/methods.lisp @ 330:2fbff655ba47 tip

Removed cpitch-adj and cents SQL columns
author Jeremy Gow <jeremy.gow@gmail.com>
date Mon, 21 Jan 2013 11:08:11 +0000
parents 6c57b16a7829
children
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))
m@24 410 (and (time> object1 object2)
m@24 411 (time< (cut-off object2) (cut-off object2))))
m@24 412
m@24 413 (defmethod starts ((object1 anchored-period)
m@24 414 (object2 anchored-period))
m@24 415 (time= object1 object2))
m@24 416
m@24 417 (defmethod ends ((object1 anchored-period)
m@24 418 (object2 anchored-period))
m@24 419 (time= (cut-off object1) (cut-off object2)))
m@24 420
m@24 421 ;; ...and
m@24 422
d@33 423 (defmethod period= ((object1 anchored-period)
c@105 424 (object2 anchored-period))
d@33 425 (and (time= object1 object2)
d@33 426 (duration= object1 object2)))
d@136 427 (defmethod period= ((object1 period)
d@136 428 (object2 period))
d@33 429 (duration= object1 object2))
d@33 430
d@136 431 (defmethod period-intersection ((object1 standard-anchored-period)
d@136 432 (object2 standard-anchored-period))
m@24 433 (cond
m@24 434 ((disjoint object1 object2)
m@24 435 ;; if they don't overlap, return nil, not a negative-valued
m@24 436 ;; period
m@24 437 nil)
m@24 438 ((let* ((start (if (time> (onset object2) (onset object1))
m@24 439 (onset object2)
m@24 440 (onset object1)))
m@24 441 (duration (duration (time- (if (time> (cut-off object2) (cut-off object1))
m@24 442 (cut-off object1)
m@24 443 (cut-off object2))
m@24 444 start))))
m@24 445 (make-anchored-period (timepoint start) duration)))))
m@24 446
d@136 447 ;; Time constructors
d@136 448 (defmethod make-moment ((time-value real))
d@136 449 "Returns STANDARD-MOMENT given a real"
d@136 450 (make-standard-moment time-value))
d@136 451 (defmethod make-period ((duration-value real))
d@136 452 "Returns STANDARD-PERIOD given a real"
d@136 453 (make-standard-period duration-value))
d@136 454 (defmethod make-anchored-period ((onset-value real) (duration-value real))
d@136 455 "Returns STANDARD-ANCHORED-PERIOD given a real"
m@143 456 (make-standard-anchored-period onset-value duration-value))
d@151 457
d@151 458 ;; Needed by some sequence functions, notably remove-if.
d@151 459 (defmethod sequence:make-sequence-like :around ((o standard-composition) length
d@151 460 &key (initial-element nil iep)
d@151 461 (initial-contents nil icp))
d@152 462 "Around method for make-sequence-like, only with all slots
d@151 463 preserved from the source sequence (except onset and duration,
d@151 464 which are calculated afresh)."
d@151 465 (declare (ignore length initial-element initial-contents iep icp))
d@178 466 (let ((new-sequence (call-next-method)) (slot-name))
d@151 467 ;; Get timing information
d@153 468 (setf new-sequence (%recompute-standard-composition-period new-sequence))
d@153 469 (dolist (slotd (sb-mop:class-slots (class-of new-sequence)) new-sequence)
d@178 470 (setf slot-name (sb-mop:slot-definition-name slotd))
d@178 471 (unless (or (equal slot-name '%data)
d@178 472 (equal slot-name 'time)
d@178 473 (equal slot-name 'interval)
d@178 474 (not (slot-boundp o slot-name)))
d@153 475 (setf (sb-mop:slot-value-using-class (class-of new-sequence)
d@153 476 new-sequence
d@153 477 slotd)
d@153 478 (sb-mop:slot-value-using-class (class-of new-sequence)
d@153 479 o ;; if this isn't the same, we're lost anyway
d@153 480 slotd))))))
d@153 481
d@153 482 (defun %recompute-standard-composition-period (composition)
d@153 483 "Find onset and duration times for newly-made composition object."
d@153 484 (let ((start) (finish))
d@153 485 (sequence:dosequence (element composition)
d@151 486 ;; Actually, this next bit is pretty stupid - I know this is
d@151 487 ;; ordered, so this bit could be replaced by
d@151 488 ;; (setf (timepoint new-sequence)
d@151 489 ;; (timepoint (elt new-sequence 0)))
d@151 490 ;; outside of the loop.
d@152 491 (when (and element
d@152 492 (or (null start)
d@152 493 (< (timepoint element) start)))
d@152 494 (setf start (timepoint element)))
d@152 495 (when (and element
d@152 496 (or (null finish)
d@152 497 (> (timepoint (cut-off element))
d@152 498 finish)))
d@152 499 (setf finish (timepoint (cut-off element)))))
d@152 500 (unless start
d@152 501 (setf start 0))
d@152 502 (unless finish
d@152 503 (setf finish 0))
d@153 504 (setf (timepoint composition) start
d@153 505 (duration composition) (- finish start))
d@153 506 composition))
d@153 507
d@153 508
d@153 509 (defmethod sequence:adjust-sequence :around ((o standard-composition) length
d@153 510 &key initial-element
d@153 511 (initial-contents nil icp))
d@153 512 (declare (ignore length o initial-element initial-contents icp))
d@175 513 (%recompute-standard-composition-period (call-next-method)))
d@175 514
j@284 515 (defmethod get-constituents ((identifier composition-identifier))
j@284 516 (list (get-composition identifier)))
d@175 517
d@175 518 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
d@175 519 ;;
d@175 520 ;; Experimental:
d@175 521 ;;
d@175 522
d@175 523 ;; Some not obviously correct implementations of the new metre
d@175 524 ;; functions. These are no worse than we're already using (they should
d@175 525 ;; be more or less equivalent)
d@175 526
d@175 527 (defmethod bar-period ((time-signature standard-time-signature)
d@175 528 object)
d@175 529 (make-standard-period (* (duration (crotchet object))
d@175 530 (time-signature-numerator time-signature)
d@175 531 (/ 4 (time-signature-denominator time-signature)))))
d@175 532
d@175 533 (defmethod current-bar ((moment standard-moment) (composition composition))
d@175 534 (let* ((time-sig (car (get-applicable-time-signatures
d@175 535 (make-standard-anchored-period (timepoint moment)
d@175 536 (duration (crotchet composition)))
d@175 537 composition)))
d@175 538 (bar-duration (bar-period time-sig composition)))
d@175 539 (do* ((start (onset time-sig) next-start)
d@175 540 (next-start (time+ start bar-duration) (time+ start bar-duration)))
d@175 541 ((time> next-start moment)
d@175 542 (make-standard-anchored-period (timepoint start)
d@175 543 (duration bar-duration))))))
d@175 544
j@307 545 (defmethod ioi-from-bar ((event event))
j@307 546 (- (timepoint (onset event))
j@307 547 (timepoint (current-bar event (composition event)))))
j@307 548
j@307 549 (defmethod ioi-from-bar ((constituent constituent))
j@307 550 (- (timepoint (onset constituent))
j@307 551 (timepoint (current-bar constituent constituent))))
j@307 552
j@307 553 (defmethod onset-in-bar ((o moment))
j@307 554 (1+ (ioi-from-bar o)))
j@307 555
d@175 556 (defmethod beat-period ((moment standard-moment)
d@175 557 (time-signature standard-time-signature)
d@175 558 (composition composition))
d@175 559 ;; Simple example - standard-time-signature has constant tactus
d@175 560 (let* ((containing-bar (current-bar moment composition))
d@175 561 (beat-duration (* (duration (crotchet composition))
d@175 562 (tactus-duration time-signature)))
d@175 563 (beat-period (make-standard-anchored-period (timepoint containing-bar)
d@175 564 beat-duration)))
d@175 565 (do ()
d@175 566 ((time> (cut-off beat-period) moment) beat-period)
d@175 567 (setf (timepoint beat-period) (timepoint (cut-off beat-period))))))
d@175 568
d@175 569 (defmethod current-beat ((moment standard-moment) (composition composition))
d@175 570 ;; Assume at most one time signature per bar (otherwise, this is hell)
d@175 571 (let* ((time-sig (car (get-applicable-time-signatures (current-bar moment composition) composition))))
d@175 572 (if time-sig
d@175 573 (beat-period moment time-sig composition)
d@175 574 ;; If no time-sig, there's no way of answering this
d@175 575 ;; directly. There may be sensible defaults, but it's the job
d@175 576 ;; of an implementation's author to solve that.
d@175 577 (error 'insufficient-information :operation 'beat-period :datatype (class-of composition)))))
d@175 578
j@276 579
j@320 580 ;;;=====================================================================
j@304 581 ;;; Copying events in time
j@320 582 ;;;=====================================================================
j@276 583
j@276 584 (defmethod move-to-first-bar ((composition composition))
j@300 585 (let ((offset (floor (timepoint (elt composition 0)))))
j@276 586 (loop
j@277 587 for event in (%list-slot-sequence-data composition)
j@276 588 do (setf event (copy-event event))
j@276 589 do (setf (timepoint event)
j@276 590 (- (timepoint event) offset))
j@276 591 collect event into shifted-events
j@276 592 finally (return shifted-events))))
j@304 593
j@304 594
j@320 595 ;;;=====================================================================
j@304 596 ;;; Searching for events
j@320 597 ;;;=====================================================================
j@304 598
j@304 599 (defmethod find-next-event ((source-event event) &key predicate test
j@304 600 break-test search-list)
j@304 601 "Ideally a sorted search list that begins with the first event after
j@304 602 the source-event should be provided, otherwise, the search will begin
j@304 603 from the beginning."
j@304 604 (unless search-list (setf search-list (composition source-event)))
j@304 605 (cond
j@304 606 ((and test predicate)
j@304 607 (error "Supplied both a test and a predicate."))
j@304 608 (test
j@304 609 (sequence:dosequence (e search-list nil)
j@304 610 (when (and (time> (onset e) (onset source-event))
j@304 611 (funcall test source-event e))
j@304 612 (return e))
j@304 613 (when break-test
j@304 614 (when (funcall break-test source-event e)
j@304 615 (return nil)))))
j@304 616 (predicate
j@304 617 (sequence:dosequence (e search-list nil)
j@304 618 (when (and (time> (onset e) (onset source-event))
j@304 619 (funcall predicate e))
j@304 620 (return e))
j@304 621 (when break-test
j@304 622 (when (funcall break-test source-event e)
j@304 623 (return nil)))))))
j@304 624
j@304 625
j@320 626 ;;;=====================================================================
j@304 627 ;;; Sorting Compositions
j@320 628 ;;;=====================================================================
j@304 629
j@304 630 (defmethod event< ((event1 event) (event2 event) attribute-list)
j@304 631 (dolist (attribute attribute-list nil) ;nil if equal
j@304 632 (if (< (funcall attribute event1) (funcall attribute event2))
j@304 633 (return t)
j@304 634 (if (> (funcall attribute event1) (funcall attribute event2))
j@304 635 (return nil)))))
j@304 636
j@304 637 (defun make-event< (attribute-list)
j@304 638 (lambda (event1 event2)
j@304 639 (funcall #'event< event1 event2 attribute-list)))
j@304 640
j@304 641 (defmethod sort-composition ((composition composition) dimension-spec)
j@304 642 (sequence:make-sequence-like composition
j@304 643 (length composition)
j@304 644 :initial-contents
j@304 645 (stable-sort
j@304 646 (copy-seq composition)
j@304 647 (make-event< dimension-spec))))