annotate base/methods.lisp @ 73:c9b0739d8dd6

A few small changes, mostly for speed darcs-hash:20070710100856-f76cc-7977785c9dbc9e1edf86ebdbfe6e82b9176d7372.gz
author David Lewis <d.lewis@gold.ac.uk>
date Tue, 10 Jul 2007 11:08:56 +0100
parents 8b31d54c95be
children 4e1538df0d10
rev   line source
m@24 1 (cl:in-package #:amuse)
m@24 2
m@24 3 (defmethod chromatic-pitch ((pitch-designator chromatic-pitch))
m@24 4 pitch-designator)
m@24 5
m@24 6 (defmethod midi-pitch-number ((pitch-designator chromatic-pitch))
m@24 7 (%chromatic-pitch-number pitch-designator))
m@24 8
m@24 9 (defmethod midi-pitch-number ((pitch-designator pitch))
m@24 10 (%chromatic-pitch-number (chromatic-pitch pitch-designator)))
m@24 11
m@24 12 (defmethod span ((pitch-interval-designator pitch-interval))
m@24 13 (%pitch-interval-span pitch-interval-designator))
m@24 14
m@24 15 (defmethod duration ((period-designator period))
m@24 16 (%period-interval period-designator))
m@24 17
d@33 18 (defmethod (setf duration) ((value real) (period-designator period))
d@33 19 (setf (%period-interval period-designator) value))
d@33 20
m@24 21 (defmethod timepoint ((moment-designator moment))
m@24 22 (%moment-time moment-designator))
m@24 23
d@33 24 (defmethod (setf timepoint) ((value real) (moment-designator moment))
d@33 25 (setf (%moment-time moment-designator) value))
d@33 26
d@73 27 (defmethod cut-off ((anchored-period-designator anchored-period))
d@73 28 (make-instance 'moment
d@73 29 :time (+ (%moment-time anchored-period-designator)
d@73 30 (%period-interval anchored-period-designator))))
d@73 31
m@24 32 (defmethod beat-units-per-bar ((time-signature basic-time-signature))
m@24 33 (%basic-time-signature-numerator time-signature))
m@24 34
m@24 35 (defmethod beat-units ((time-signature basic-time-signature))
m@24 36 (%basic-time-signature-denominator time-signature))
m@24 37
m@67 38 (defmethod time-signature-equal ((ts1 basic-time-signature)
m@67 39 (ts2 basic-time-signature))
m@67 40 (let ((n1 (time-signature-numerator ts1))
m@67 41 (n2 (time-signature-numerator ts2))
m@67 42 (d1 (time-signature-denominator ts1))
m@67 43 (d2 (time-signature-denominator ts2)))
m@67 44 (and n1 n2 (= n1 n2)
m@67 45 d1 d2 (= d1 d2))))
m@67 46
m@24 47 (defmethod key-signature-sharps ((key-signature basic-key-signature))
m@24 48 (%basic-key-signature-sharp-count key-signature))
m@24 49
m@45 50 (defmethod key-signature-mode ((ks midi-key-signature))
m@45 51 (%midi-key-signature-mode ks))
m@45 52
m@67 53 (defmethod key-signature-equal ((ks1 basic-key-signature)
m@67 54 (ks2 basic-key-signature))
m@67 55 (let ((s1 (key-signature-sharps ks1))
m@67 56 (s2 (key-signature-sharps ks2)))
m@67 57 (and s1 s2 (= s1 s2))))
m@67 58
m@67 59 (defmethod key-signature-equal ((ks1 midi-key-signature)
m@67 60 (ks2 midi-key-signature))
m@67 61 (let ((s1 (key-signature-sharps ks1))
m@67 62 (s2 (key-signature-sharps ks2))
m@67 63 (m1 (key-signature-mode ks1))
m@67 64 (m2 (key-signature-mode ks2)))
m@67 65 (and s1 s2 (= s1 s2)
m@67 66 m1 m2 (= m1 m2))))
m@67 67
m@24 68 (defmethod bpm ((tempo tempo))
m@24 69 (%tempo-bpm tempo))
m@24 70
m@67 71 (defmethod tempo-equal ((t1 tempo) (t2 tempo))
m@67 72 (and (bpm t1) (bpm t2) (= t1 t2)))
m@67 73
m@67 74
m@24 75 ;; Time protocol
m@24 76
m@24 77 (defmethod time+ ((object1 moment) (object2 period))
m@24 78 (make-moment (+ (timepoint object1) (duration object2))))
m@24 79
m@24 80 (defmethod time+ ((object1 period) (object2 moment)) ;?
m@24 81 (time+ object2 object1))
m@24 82
m@24 83 (defmethod time+ ((object1 period) (object2 period))
m@24 84 (make-floating-period (+ (duration object1)
m@24 85 (duration object2))))
m@24 86
m@24 87 (defmethod time+ ((object1 moment) (object2 moment))
m@24 88 (error 'undefined-action :operation 'time+ :datatype (list (class-of object1) (class-of object2))))
m@24 89
m@24 90 (defmethod time- ((object1 moment) (object2 moment))
m@24 91 (make-anchored-period (timepoint object2)
m@24 92 (- (timepoint object1)
m@24 93 (timepoint object2))))
m@24 94
m@24 95 (defmethod time- ((object1 moment) (object2 period))
m@24 96 (make-moment (- (timepoint object1) (duration object2))))
m@24 97
m@24 98 (defmethod time- ((object1 period) (object2 moment)) ;?
m@24 99 (error 'undefined-action
m@24 100 :operation 'time-
m@24 101 :datatype (list (class-of object1) (class-of object2))))
m@24 102
m@24 103 (defmethod time- ((object1 period) (object2 period))
m@24 104 (make-floating-period (- (duration object2)
m@24 105 (duration object1))))
m@24 106
m@24 107 ;; these ones are less certain. I've just put them in, but think I
m@24 108 ;; should remove them and force the user to specify what they mean
m@24 109 ;; when they give objects that are both moments *and* periods to these
m@24 110 ;; functions.
m@24 111
m@24 112 (defmethod time- ((object1 anchored-period) (object2 anchored-period)) ;?
m@24 113 (time- (moment object1) (moment object2)))
m@24 114
m@24 115 (defmethod time- (object1 (object2 anchored-period)) ;?
m@24 116 (time- object1 (moment object2)))
m@24 117
m@24 118 (defmethod time- ((object1 anchored-period) object2) ;?
m@24 119 (time- (moment object1) object2))
m@24 120
m@24 121 (defmethod time> ((object1 moment) (object2 moment))
m@24 122 (> (timepoint object1) (timepoint object2)))
m@24 123
d@73 124 (defmethod time< ((object1 moment) (object2 moment))
d@73 125 (< (timepoint object1) (timepoint object2)))
d@73 126
m@24 127 (defmethod time= ((object1 moment) (object2 moment))
m@24 128 (= (timepoint object1) (timepoint object2)))
m@24 129
m@24 130 (defmethod duration> ((object1 period) (object2 period))
m@24 131 (> (duration object1) (duration object2)))
m@24 132
m@24 133 (defmethod duration= ((object1 period) (object2 period))
m@24 134 (= (duration object1) (duration object2)))
m@24 135
m@24 136 (defmethod duration* ((object1 period) (object2 number))
m@24 137 (make-floating-period (* (duration object1) object2)))
m@24 138
m@24 139 (defmethod duration* ((object1 number) (object2 period))
m@24 140 (duration* object2 object1))
m@24 141
m@24 142 (defmethod duration/ ((object1 period) (object2 number))
m@24 143 (make-floating-period (/ (duration object1) object2)))
m@24 144
m@24 145 ;; Pitch protocol
m@24 146
m@24 147 (defmethod pitch+ ((object1 pitch-designator)
m@24 148 (object2 pitch-designator))
m@24 149 (error 'undefined-action :operation 'pitch+
m@24 150 :datatype (list (class-of object1) (class-of object2))))
m@24 151
m@24 152 (defmethod pitch+ ((object1 pitch-designator)
m@24 153 (object2 pitch-interval)) ; or should I check the
m@24 154 ; pitch/interval types?
d@34 155 (make-chromatic-pitch (+ (midi-pitch-number object1)
m@24 156 (span object2))))
m@24 157
m@24 158 (defmethod pitch+ ((object1 pitch-interval)
m@24 159 (object2 pitch-designator)) ;?
m@24 160 (pitch+ object2 object1))
m@24 161
m@24 162 (defmethod pitch+ ((object1 pitch-interval)
m@24 163 (object2 pitch-interval))
m@24 164 (make-pitch-interval (+ (span object1)
m@24 165 (span object2))))
m@24 166
m@24 167 (defmethod pitch- ((object1 pitch-designator)
m@24 168 (object2 pitch-designator))
d@34 169 (make-pitch-interval (- (midi-pitch-number object1)
d@34 170 (midi-pitch-number object2))))
m@24 171
m@24 172 (defmethod pitch- ((object1 pitch-designator)
m@24 173 (object2 pitch-interval))
d@34 174 (make-chromatic-pitch (- (midi-pitch-number object1)
m@24 175 (span object2))))
m@24 176
m@24 177 (defmethod pitch- ((object1 pitch-interval)
m@24 178 (object2 pitch-interval))
m@24 179 (make-pitch-interval (- (span object1)
m@24 180 (span object2))))
m@24 181
m@24 182 (defmethod pitch- ((object1 pitch-interval)
m@24 183 (object2 pitch-designator))
m@24 184 (error 'undefined-action :operation 'pitch-
m@24 185 :datatype (list (class-of object1) (class-of object2))))
m@24 186
m@24 187 (defmethod pitch> ((object1 pitch-designator)
m@24 188 (object2 pitch-designator))
d@34 189 (> (midi-pitch-number object1)
d@34 190 (midi-pitch-number object2)))
m@24 191
m@24 192 (defmethod pitch= ((object1 pitch-designator)
m@24 193 (object2 pitch-designator))
d@34 194 (= (midi-pitch-number object1)
d@34 195 (midi-pitch-number object2)))
m@24 196
m@24 197 (defmethod interval> ((object1 pitch-interval)
m@24 198 (object2 pitch-interval))
m@24 199 (> (span object1)
m@24 200 (span object2)))
m@24 201
m@24 202 (defmethod interval= ((object1 pitch-interval)
m@24 203 (object2 pitch-interval))
m@24 204 (= (span object1)
m@24 205 (span object2)))
m@24 206
m@24 207
m@24 208
m@24 209 ;; Allen
m@24 210
m@24 211 (defmethod meets ((object1 anchored-period)
m@24 212 (object2 anchored-period))
m@24 213 (or (time= (cut-off object1) object2)
m@24 214 (time= (cut-off object2) object1)))
m@24 215
m@24 216 (defmethod before ((object1 anchored-period)
m@24 217 (object2 anchored-period))
m@24 218 (time> object2 (cut-off object1)))
m@24 219
m@24 220 (defmethod overlaps ((object1 anchored-period)
m@24 221 (object2 anchored-period))
m@24 222 ;; FIXME: Is there a tidier method?
m@24 223 (or (and (time> object2 object1) ; object1 starts before object2
m@24 224 (time> (cut-off object1) object2) ; object1 ends after object2 starts
m@24 225 (time> (cut-off object2) (cut-off object1))) ; object1 ends before object2 does
m@24 226 (and (time> object1 object2) ; object1 starts after object2
m@24 227 (time> (cut-off object2) object1) ; object1 starts before object2 ends
m@24 228 (time> (cut-off object1) (cut-off object2))))) ; object1 ends after object2 does
m@24 229
m@24 230 (defmethod during ((object1 anchored-period)
m@24 231 (object2 anchored-period))
m@24 232 (and (time> object1 object2)
m@24 233 (time< (cut-off object2) (cut-off object2))))
m@24 234
m@24 235 (defmethod starts ((object1 anchored-period)
m@24 236 (object2 anchored-period))
m@24 237 (time= object1 object2))
m@24 238
m@24 239 (defmethod ends ((object1 anchored-period)
m@24 240 (object2 anchored-period))
m@24 241 (time= (cut-off object1) (cut-off object2)))
m@24 242
m@24 243 ;; ...and
m@24 244
d@33 245 (defmethod period= ((object1 anchored-period)
d@33 246 (object2 anchored-period))
d@33 247 (and (time= object1 object2)
d@33 248 (duration= object1 object2)))
d@33 249 (defmethod period= ((object1 floating-period)
d@33 250 (object2 floating-period))
d@33 251 (duration= object1 object2))
d@33 252
m@24 253 (defmethod period-intersection ((object1 anchored-period)
m@24 254 (object2 anchored-period))
m@24 255 (cond
m@24 256 ((disjoint object1 object2)
m@24 257 ;; if they don't overlap, return nil, not a negative-valued
m@24 258 ;; period
m@24 259 nil)
m@24 260 ((let* ((start (if (time> (onset object2) (onset object1))
m@24 261 (onset object2)
m@24 262 (onset object1)))
m@24 263 (duration (duration (time- (if (time> (cut-off object2) (cut-off object1))
m@24 264 (cut-off object1)
m@24 265 (cut-off object2))
m@24 266 start))))
m@24 267 (make-anchored-period (timepoint start) duration)))))
m@24 268
m@24 269