annotate base/methods.lisp @ 83:ae06767e84c5

base/: pitch darcs-hash:20070717153457-c0ce4-cd5350c65ee8414bda17d40eb0e149063f2c22d7.gz
author Marcus Pearce <m.pearce@gold.ac.uk>
date Tue, 17 Jul 2007 16:34:57 +0100
parents 4e1538df0d10
children 7a0ee88f1edb
rev   line source
m@24 1 (cl:in-package #:amuse)
m@24 2
m@81 3 ;;; diatonic pitch
m@81 4
m@83 5 (defmethod pitch= ((p1 diatonic-pitch) (p2 diatonic-pitch))
m@83 6 (let ((n1 (%diatonic-pitch-name p1))
m@83 7 (a1 (%diatonic-pitch-accidental p1))
m@83 8 (o1 (%diatonic-pitch-accidental p1))
m@83 9 (n2 (%diatonic-pitch-name p2))
m@83 10 (a2 (%diatonic-pitch-accidental p2))
m@83 11 (o2 (%diatonic-pitch-accidental p2)))
m@83 12 (and n1 n2 (= n1 n2)
m@83 13 a1 a2 (= a1 a2)
m@83 14 o1 o2 (= o1 o2))))
m@83 15
m@81 16 (defmethod middle-c ((dp diatonic-pitch))
m@81 17 (make-diatonic-pitch 2 0 4))
m@81 18
m@81 19 (defmethod diatonic-pitch ((dp diatonic-pitch))
m@81 20 dp)
m@81 21
m@81 22 (defmethod diatonic-pitch-name ((dp diatonic-pitch))
m@81 23 (elt "ABCDEFG" (%diatonic-pitch-name dp)))
m@81 24
m@81 25 (defmethod asa-pitch-string ((dp diatonic-pitch))
m@81 26 (concatenate 'string
m@81 27 (diatonic-pitch-name dp)
m@81 28 (let ((a (%diatonic-pitch-accidental dp)))
m@81 29 (cond ((plusp a)
m@81 30 (make-sequence 'string a :initial-element "s"))
m@81 31 ((minusp a)
m@81 32 (make-sequence 'string (abs a) :initial-element "f"))
m@81 33 (t "n")))
m@81 34 (%diatonic-pitch-octave dp)))
m@81 35
m@81 36 (defmethod mips-pitch ((dp diatonic-pitch))
m@81 37 (let ((mips-pitch (mips:pn-p (asa-pitch-string dp))))
m@81 38 (make-mips-pitch (first mips-pitch) (second mips-pitch))))
m@81 39 (defmethod midi-pitch-number ((dp diatonic-pitch))
m@81 40 (midi-pitch-number (mips-pitch dp)))
m@81 41 (defmethod chromatic-pitch ((dp diatonic-pitch))
m@81 42 (make-chromatic-pitch (midi-pitch-number dp)))
m@81 43 (defmethod meredith-chromatic-pitch-number ((dp diatonic-pitch))
m@81 44 (meredith-chromatic-pitch-number (mips-pitch dp)))
m@81 45 (defmethod meredith-morphetic-pitch-number ((dp diatonic-pitch))
m@81 46 (meredith-morphetic-pitch-number (mips-pitch dp)))
m@81 47
m@81 48 ;;; MIPS pitch
m@81 49
m@83 50 (defmethod pitch= ((p1 mips-pitch) (p2 mips-pitch))
m@83 51 (let ((c1 (meredith-chromatic-pitch-number p1))
m@83 52 (m1 (meredith-morphetic-pitch-number p1))
m@83 53 (c2 (meredith-chromatic-pitch-number p2))
m@83 54 (m2 (meredith-morphetic-pitch-number p2)))
m@83 55 (and c1 c2 (= c1 c2)
m@83 56 m1 m2 (= m1 m2))))
m@83 57
m@81 58 (defmethod middle-c ((mp mips-pitch))
m@81 59 (make-mips-pitch 39 23))
m@81 60
m@81 61 (defmethod mips-pitch ((mp mips-pitch))
m@81 62 mp)
m@81 63
m@81 64 (defmethod diatonic-pitch ((mp mips-pitch))
m@81 65 (let ((asa-pitch (mips:p-pn (list (%p-pc mp) (%p-pm mp))))
m@81 66 (accidental-count nil))
m@81 67 (make-diatonic-pitch
m@81 68 (position (elt asa-pitch 0) "ABCDEFG")
m@81 69 (ecase (elt asa-pitch 1)
m@81 70 (#\n 0)
m@81 71 (#\s
m@81 72 (let ((c (count #\s asa-pitch)))
m@81 73 (setf accidental-count c)
m@81 74 c))
m@81 75 (#\f
m@81 76 (let ((c (count #\f asa-pitch)))
m@81 77 (setf accidental-count c)
m@81 78 (- c))))
m@81 79 (parse-integer
m@81 80 asa-pitch :start (if accidental-count (1+ accidental-count) 2)))))
m@81 81
m@81 82 (defmethod meredith-chromatic-pitch-number ((mp mips-pitch))
m@81 83 (%p-pc mp))
m@81 84 (defmethod meredith-morphetic-pitch-number ((mp mips-pitch))
m@81 85 (%p-pm mp))
m@81 86 (defmethod midi-pitch-number ((mp mips-pitch))
m@81 87 (+ (meredith-chromatic-pitch-number mp) 21))
m@81 88 (defmethod chromatic-pitch ((mp mips-pitch))
m@81 89 (make-chromatic-pitch (midi-pitch-number mp)))
m@81 90 (defmethod asa-pitch-string ((mp mips-pitch))
m@81 91 (mips:p-pn (list (meredith-chromatic-pitch-number mp)
m@81 92 (meredith-morphetic-pitch-number mp))))
m@81 93 (defmethod diatonic-pitch-name ((mp mips-pitch))
m@81 94 (elt (asa-pitch-string mp) 0))
m@81 95
m@81 96 ;;; Chromatic pitch
m@81 97
m@81 98 (defmethod middle-c ((cp chromatic-pitch))
m@81 99 (make-chromatic-pitch 60))
m@81 100
m@24 101 (defmethod chromatic-pitch ((pitch-designator chromatic-pitch))
m@24 102 pitch-designator)
m@24 103
m@24 104 (defmethod midi-pitch-number ((pitch-designator chromatic-pitch))
m@24 105 (%chromatic-pitch-number pitch-designator))
m@24 106
m@24 107 (defmethod midi-pitch-number ((pitch-designator pitch))
m@24 108 (%chromatic-pitch-number (chromatic-pitch pitch-designator)))
m@24 109
m@24 110 (defmethod span ((pitch-interval-designator pitch-interval))
m@24 111 (%pitch-interval-span pitch-interval-designator))
m@24 112
m@24 113 (defmethod duration ((period-designator period))
m@24 114 (%period-interval period-designator))
m@24 115
d@33 116 (defmethod (setf duration) ((value real) (period-designator period))
d@33 117 (setf (%period-interval period-designator) value))
d@33 118
m@24 119 (defmethod timepoint ((moment-designator moment))
m@24 120 (%moment-time moment-designator))
m@24 121
d@33 122 (defmethod (setf timepoint) ((value real) (moment-designator moment))
d@33 123 (setf (%moment-time moment-designator) value))
d@33 124
d@73 125 (defmethod cut-off ((anchored-period-designator anchored-period))
d@73 126 (make-instance 'moment
d@73 127 :time (+ (%moment-time anchored-period-designator)
d@73 128 (%period-interval anchored-period-designator))))
d@73 129
m@24 130 (defmethod beat-units-per-bar ((time-signature basic-time-signature))
m@24 131 (%basic-time-signature-numerator time-signature))
m@24 132
m@24 133 (defmethod beat-units ((time-signature basic-time-signature))
m@24 134 (%basic-time-signature-denominator time-signature))
m@24 135
m@67 136 (defmethod time-signature-equal ((ts1 basic-time-signature)
m@67 137 (ts2 basic-time-signature))
m@67 138 (let ((n1 (time-signature-numerator ts1))
m@67 139 (n2 (time-signature-numerator ts2))
m@67 140 (d1 (time-signature-denominator ts1))
m@67 141 (d2 (time-signature-denominator ts2)))
m@67 142 (and n1 n2 (= n1 n2)
m@67 143 d1 d2 (= d1 d2))))
m@67 144
m@24 145 (defmethod key-signature-sharps ((key-signature basic-key-signature))
m@24 146 (%basic-key-signature-sharp-count key-signature))
m@24 147
m@45 148 (defmethod key-signature-mode ((ks midi-key-signature))
m@45 149 (%midi-key-signature-mode ks))
m@45 150
m@67 151 (defmethod key-signature-equal ((ks1 basic-key-signature)
m@67 152 (ks2 basic-key-signature))
m@67 153 (let ((s1 (key-signature-sharps ks1))
m@67 154 (s2 (key-signature-sharps ks2)))
m@67 155 (and s1 s2 (= s1 s2))))
m@67 156
m@67 157 (defmethod key-signature-equal ((ks1 midi-key-signature)
m@67 158 (ks2 midi-key-signature))
m@67 159 (let ((s1 (key-signature-sharps ks1))
m@67 160 (s2 (key-signature-sharps ks2))
m@67 161 (m1 (key-signature-mode ks1))
m@67 162 (m2 (key-signature-mode ks2)))
m@67 163 (and s1 s2 (= s1 s2)
m@67 164 m1 m2 (= m1 m2))))
m@67 165
m@24 166 (defmethod bpm ((tempo tempo))
m@24 167 (%tempo-bpm tempo))
m@24 168
m@67 169 (defmethod tempo-equal ((t1 tempo) (t2 tempo))
m@67 170 (and (bpm t1) (bpm t2) (= t1 t2)))
m@67 171
m@67 172
m@24 173 ;; Time protocol
m@24 174
m@24 175 (defmethod time+ ((object1 moment) (object2 period))
m@24 176 (make-moment (+ (timepoint object1) (duration object2))))
m@24 177
m@24 178 (defmethod time+ ((object1 period) (object2 moment)) ;?
m@24 179 (time+ object2 object1))
m@24 180
m@24 181 (defmethod time+ ((object1 period) (object2 period))
m@24 182 (make-floating-period (+ (duration object1)
m@24 183 (duration object2))))
m@24 184
m@24 185 (defmethod time+ ((object1 moment) (object2 moment))
m@24 186 (error 'undefined-action :operation 'time+ :datatype (list (class-of object1) (class-of object2))))
m@24 187
m@24 188 (defmethod time- ((object1 moment) (object2 moment))
m@24 189 (make-anchored-period (timepoint object2)
m@24 190 (- (timepoint object1)
m@24 191 (timepoint object2))))
m@24 192
m@24 193 (defmethod time- ((object1 moment) (object2 period))
m@24 194 (make-moment (- (timepoint object1) (duration object2))))
m@24 195
m@24 196 (defmethod time- ((object1 period) (object2 moment)) ;?
m@24 197 (error 'undefined-action
m@24 198 :operation 'time-
m@24 199 :datatype (list (class-of object1) (class-of object2))))
m@24 200
m@24 201 (defmethod time- ((object1 period) (object2 period))
m@24 202 (make-floating-period (- (duration object2)
m@24 203 (duration object1))))
m@24 204
m@24 205 ;; these ones are less certain. I've just put them in, but think I
m@24 206 ;; should remove them and force the user to specify what they mean
m@24 207 ;; when they give objects that are both moments *and* periods to these
m@24 208 ;; functions.
m@24 209
m@24 210 (defmethod time- ((object1 anchored-period) (object2 anchored-period)) ;?
m@24 211 (time- (moment object1) (moment object2)))
m@24 212
m@24 213 (defmethod time- (object1 (object2 anchored-period)) ;?
m@24 214 (time- object1 (moment object2)))
m@24 215
m@24 216 (defmethod time- ((object1 anchored-period) object2) ;?
m@24 217 (time- (moment object1) object2))
m@24 218
m@24 219 (defmethod time> ((object1 moment) (object2 moment))
m@24 220 (> (timepoint object1) (timepoint object2)))
m@24 221
d@73 222 (defmethod time< ((object1 moment) (object2 moment))
d@73 223 (< (timepoint object1) (timepoint object2)))
d@73 224
m@24 225 (defmethod time= ((object1 moment) (object2 moment))
m@24 226 (= (timepoint object1) (timepoint object2)))
m@24 227
m@24 228 (defmethod duration> ((object1 period) (object2 period))
m@24 229 (> (duration object1) (duration object2)))
m@24 230
m@24 231 (defmethod duration= ((object1 period) (object2 period))
m@24 232 (= (duration object1) (duration object2)))
m@24 233
m@24 234 (defmethod duration* ((object1 period) (object2 number))
m@24 235 (make-floating-period (* (duration object1) object2)))
m@24 236
m@24 237 (defmethod duration* ((object1 number) (object2 period))
m@24 238 (duration* object2 object1))
m@24 239
m@24 240 (defmethod duration/ ((object1 period) (object2 number))
m@24 241 (make-floating-period (/ (duration object1) object2)))
m@24 242
m@24 243 ;; Pitch protocol
m@24 244
m@24 245 (defmethod pitch+ ((object1 pitch-designator)
m@24 246 (object2 pitch-designator))
m@24 247 (error 'undefined-action :operation 'pitch+
m@24 248 :datatype (list (class-of object1) (class-of object2))))
m@24 249
m@24 250 (defmethod pitch+ ((object1 pitch-designator)
m@24 251 (object2 pitch-interval)) ; or should I check the
m@24 252 ; pitch/interval types?
d@34 253 (make-chromatic-pitch (+ (midi-pitch-number object1)
m@24 254 (span object2))))
m@24 255
m@24 256 (defmethod pitch+ ((object1 pitch-interval)
m@24 257 (object2 pitch-designator)) ;?
m@24 258 (pitch+ object2 object1))
m@24 259
m@24 260 (defmethod pitch+ ((object1 pitch-interval)
m@24 261 (object2 pitch-interval))
m@24 262 (make-pitch-interval (+ (span object1)
m@24 263 (span object2))))
m@24 264
m@24 265 (defmethod pitch- ((object1 pitch-designator)
m@24 266 (object2 pitch-designator))
d@34 267 (make-pitch-interval (- (midi-pitch-number object1)
d@34 268 (midi-pitch-number object2))))
m@24 269
m@24 270 (defmethod pitch- ((object1 pitch-designator)
m@24 271 (object2 pitch-interval))
d@34 272 (make-chromatic-pitch (- (midi-pitch-number object1)
m@24 273 (span object2))))
m@24 274
m@24 275 (defmethod pitch- ((object1 pitch-interval)
m@24 276 (object2 pitch-interval))
m@24 277 (make-pitch-interval (- (span object1)
m@24 278 (span object2))))
m@24 279
m@24 280 (defmethod pitch- ((object1 pitch-interval)
m@24 281 (object2 pitch-designator))
m@24 282 (error 'undefined-action :operation 'pitch-
m@24 283 :datatype (list (class-of object1) (class-of object2))))
m@24 284
m@24 285 (defmethod pitch> ((object1 pitch-designator)
m@24 286 (object2 pitch-designator))
d@34 287 (> (midi-pitch-number object1)
d@34 288 (midi-pitch-number object2)))
m@24 289
m@24 290 (defmethod pitch= ((object1 pitch-designator)
m@24 291 (object2 pitch-designator))
d@34 292 (= (midi-pitch-number object1)
d@34 293 (midi-pitch-number object2)))
m@24 294
m@24 295 (defmethod interval> ((object1 pitch-interval)
m@24 296 (object2 pitch-interval))
m@24 297 (> (span object1)
m@24 298 (span object2)))
m@24 299
m@24 300 (defmethod interval= ((object1 pitch-interval)
m@24 301 (object2 pitch-interval))
m@24 302 (= (span object1)
m@24 303 (span object2)))
m@24 304
m@24 305
m@24 306
m@24 307 ;; Allen
m@24 308
m@24 309 (defmethod meets ((object1 anchored-period)
m@24 310 (object2 anchored-period))
m@24 311 (or (time= (cut-off object1) object2)
m@24 312 (time= (cut-off object2) object1)))
m@24 313
m@24 314 (defmethod before ((object1 anchored-period)
m@24 315 (object2 anchored-period))
m@24 316 (time> object2 (cut-off object1)))
m@24 317
m@24 318 (defmethod overlaps ((object1 anchored-period)
m@24 319 (object2 anchored-period))
m@24 320 ;; FIXME: Is there a tidier method?
m@24 321 (or (and (time> object2 object1) ; object1 starts before object2
m@24 322 (time> (cut-off object1) object2) ; object1 ends after object2 starts
m@24 323 (time> (cut-off object2) (cut-off object1))) ; object1 ends before object2 does
m@24 324 (and (time> object1 object2) ; object1 starts after object2
m@24 325 (time> (cut-off object2) object1) ; object1 starts before object2 ends
m@24 326 (time> (cut-off object1) (cut-off object2))))) ; object1 ends after object2 does
m@24 327
m@24 328 (defmethod during ((object1 anchored-period)
m@24 329 (object2 anchored-period))
m@24 330 (and (time> object1 object2)
m@24 331 (time< (cut-off object2) (cut-off object2))))
m@24 332
m@24 333 (defmethod starts ((object1 anchored-period)
m@24 334 (object2 anchored-period))
m@24 335 (time= object1 object2))
m@24 336
m@24 337 (defmethod ends ((object1 anchored-period)
m@24 338 (object2 anchored-period))
m@24 339 (time= (cut-off object1) (cut-off object2)))
m@24 340
m@24 341 ;; ...and
m@24 342
d@33 343 (defmethod period= ((object1 anchored-period)
d@33 344 (object2 anchored-period))
d@33 345 (and (time= object1 object2)
d@33 346 (duration= object1 object2)))
d@33 347 (defmethod period= ((object1 floating-period)
d@33 348 (object2 floating-period))
d@33 349 (duration= object1 object2))
d@33 350
m@24 351 (defmethod period-intersection ((object1 anchored-period)
m@24 352 (object2 anchored-period))
m@24 353 (cond
m@24 354 ((disjoint object1 object2)
m@24 355 ;; if they don't overlap, return nil, not a negative-valued
m@24 356 ;; period
m@24 357 nil)
m@24 358 ((let* ((start (if (time> (onset object2) (onset object1))
m@24 359 (onset object2)
m@24 360 (onset object1)))
m@24 361 (duration (duration (time- (if (time> (cut-off object2) (cut-off object1))
m@24 362 (cut-off object1)
m@24 363 (cut-off object2))
m@24 364 start))))
m@24 365 (make-anchored-period (timepoint start) duration)))))
m@24 366
m@24 367