annotate base/generics.lisp @ 106:8528c316fbcc

delete diatonic-pitch (as was) Delete the diatonic-pitch class, in preparation for MIPS pitch being our single representation. Add a DIATONIC-PITCH-OCTAVE generic, and make OCTAVE return the octave based on a MIDI-PITCH-NUMBER (and make sure that it returns an integer, too). darcs-hash:20070726121235-dc3a5-4cbd1058145163eecbfc84384234904e2353fead.gz
author c.rhodes <c.rhodes@gold.ac.uk>
date Thu, 26 Jul 2007 13:12:35 +0100
parents 59e69a32b8d9
children b445959f4cc1
rev   line source
m@24 1 (cl:in-package #:amuse)
m@24 2
m@24 3 ;;; Pulling compositions from the database
m@24 4
m@24 5 (defgeneric get-composition (identifier))
m@24 6
m@89 7 (defgeneric monody (object)
m@89 8 (:documentation "Returns a monody."))
m@89 9 (defgeneric ensure-monody (object)
m@89 10 (:documentation "Returns a generalised boolean."))
m@89 11
d@33 12 ;;; Getting constituents from compositions
d@33 13 ;; IS this the mechanism we want to use
d@72 14 (defgeneric time-signatures (composition)
d@72 15 (:documentation "Returns all time-signatures in a composition
d@72 16 Probably shouldn't be exported - can be replaced
d@72 17 by (get-applicable-time-signature commposition composition)"))
d@72 18 (defgeneric (setf time-signatures) (sequence composition)
d@72 19 (:documentation "Sets all time-signatures in a composition.
d@72 20 Is this wanted here?"))
d@72 21 (defgeneric tempi (composition)
d@72 22 (:documentation "Returns all tempi in a composition Probably
d@72 23 shouldn't be exported - can be replaced
d@72 24 by (get-applicable-tempi commposition composition)"))
d@72 25 (defgeneric (setf tempi) (sequence composition)
d@72 26 (:documentation "Sets all tempi in a composition.
d@72 27 Is this wanted here?"))
d@72 28 (defgeneric key-signatures (composition)
d@72 29 (:documentation "Returns all key-signatures in a composition
d@72 30 Probably shouldn't be exported - can be replaced
d@72 31 by (get-applicable-key-signature commposition composition)"))
d@72 32 (defgeneric (setf key-signatures) (sequence composition)
d@72 33 (:documentation "Sets all key sigs in a composition.
d@72 34 Is this wanted here?"))
d@33 35
m@24 36 ;;; Simple Accessors
m@24 37
m@24 38 ;; pitch-based
m@24 39
m@24 40 (defgeneric pitch (object &key kind)) ; ? Maybe this returns the pitch
m@24 41 ; in its ur form?
c@106 42 (defgeneric chromatic-pitch (pitch-designator))
c@106 43 (defgeneric diatonic-pitch (pitch-designator))
m@24 44 (defgeneric frequency (object)) ;?
m@81 45
m@86 46 (defgeneric octave (pitch-designator)
m@86 47 (:documentation "Return an integer representing the octave of
m@86 48 pitch-designator where middle c is defined to be the lowest pitch in
m@86 49 octave 4."))
m@86 50
c@106 51 (defgeneric diatonic-pitch-octave (pitch-designator)
c@106 52 (:documentation "Return an integer representing the diatonic octave
c@106 53 of PITCH-DESIGNATOR."))
c@106 54
m@86 55 (defgeneric diatonic-pitch-accidental (pitch-designator)
m@86 56 (:documentation "Return an integer representing the inflection of a
m@86 57 diatonic pitch where where negative values indicate numbers of flats,
m@86 58 0 indicates natural and positive values indicate numbers of sharps."))
m@86 59
m@81 60 (defgeneric middle-c (pitch-designator)
m@81 61 (:documentation "Returns the value of middle C in the particular
m@81 62 representation of pitch used by PITCH-DESIGNATOR."))
d@72 63 (defgeneric midi-pitch-number (pitch-designator)
d@72 64 (:documentation "Takes a pitch-designator (usually a pitched
d@72 65 event) and returns an integer between 0 and 127 representing
d@72 66 the chromatic pitch designated (60=middle C, 48 the C below
d@72 67 that, etc.)"))
m@81 68 (defgeneric asa-pitch-string (pitch-designator)
m@81 69 (:documentation "Returns a string representing the designated ASA
m@81 70 pitch name which has three parts: a letter name in the set
m@81 71 {A,B,C,D,E,F,G}, an inflection in the set {n,f,s,ff,ss,fff,sss,...}
m@81 72 and an octave number. E.g., Cn4 = Middle C."))
m@81 73 (defgeneric diatonic-pitch-name (pitch-designator)
m@81 74 (:documentation "Returns a char in the set
m@81 75 {#\A,#\B,#\C,#\D,#\E,#\F,#\G}, representing the pitch name of
m@81 76 PITCH-DESIGNATOR."))
m@24 77 (defgeneric pitch-class (pitch-designator)
d@72 78 (:documentation "Takes a pitch-designator (usually a pitched
d@72 79 event) and returns an integer between 0 and 12 representing
d@72 80 the octave-independant pitch, with c=0, c#=1, etc.")
m@24 81 (:method (p) (mod (midi-pitch-number p) 12)))
m@24 82 (defgeneric span (pitch-interval-designator))
m@24 83
m@24 84 ;; time
m@24 85
m@24 86 (defgeneric duration (period-designator))
m@24 87 (defgeneric (setf duration) (value period-designator))
m@24 88 (defgeneric timepoint (moment-designator))
m@24 89 (defgeneric (setf timepoint) (value moment-designator))
m@24 90 (defgeneric cut-off (anchored-period-designator) ; name?
d@72 91 (:documentation "Returns a <moment> representing the point at
d@72 92 which the anchored period has ended.")
m@24 93 (:method (apd) (time+ (moment apd) (floating-period apd))))
m@95 94 (defgeneric crotchet (object)
m@95 95 (:documentation "Returns a period, the duration of which represents
m@95 96 a crotchet in the time representation used by object."))
m@24 97
m@24 98 ;; others
m@24 99
m@24 100 ;; I've given the time-sig accessors general names because it allows
m@24 101 ;; for symbols in time-signatures as well as numbers - numerator is an
m@24 102 ;; odd accessor if the time sig is C (even in common practice) but
d@100 103 ;; its meaning is clear. beat-units-per-bar is clearer, though, I
m@24 104 ;; think.
m@24 105
m@24 106 (defgeneric beat-units-per-bar (time-signature))
m@24 107 (defgeneric time-signature-numerator (time-signature)
m@24 108 (:method (ts) (beat-units-per-bar ts)))
m@24 109 (defgeneric beat-units (time-signature))
m@24 110 (defgeneric time-signature-denominator (time-signature)
m@24 111 (:method (ts) (beat-units ts)))
d@33 112 (defgeneric tactus-duration (time-signature)
d@33 113 ;; basic, but should do?
d@33 114 (:method (ts)
d@33 115 (cond
d@33 116 ((and (not (= (beat-units-per-bar ts) 3))
d@33 117 (= (rem (beat-units-per-bar ts) 3) 0))
d@33 118 ;; compound time
d@33 119 (* (/ 4 (beat-units ts))
d@33 120 3))
d@33 121 (t (/ 4 (beat-units ts))))))
m@24 122
m@24 123 (defgeneric key-signature-sharps (key-signature))
m@45 124 (defgeneric key-signature-mode (ks))
m@24 125
m@24 126 (defgeneric bpm (tempo)) ;; in bpm
m@24 127 (defgeneric microseconds-per-crotchet (tempo)
m@24 128 ;; As used (when rounded) in MIDI
m@24 129 (:method (tp) (/ 60000000 (bpm tp))))
m@24 130
m@24 131 ;;; Coerce-type accessors
m@24 132
m@24 133 ;; Should I be including these default methods? Should the accessors
m@24 134 ;; be direct slot accessors or the generics I'm using? Should we
m@24 135 ;; return the object itself if it already is in the target class?
m@24 136
m@24 137 (defgeneric anchored-period (anchored-period-designator)
m@24 138 (:method (apd) (make-anchored-period (onset apd) (duration apd))))
m@24 139
m@24 140 (defgeneric floating-period (period-designator)
m@24 141 (:method (pd) (make-floating-period (duration pd))))
m@24 142
m@24 143 (defgeneric moment (moment-designator)
m@24 144 (:method (md) (make-moment (timepoint md))))
m@24 145
m@24 146 (defgeneric onset (anchored-period-designator)
m@24 147 (:method (apd) (moment apd)))
m@24 148 (defgeneric (setf onset) (value anchored-period-designator))
m@24 149
m@24 150 ;;; Time Protocol (or moments?)
m@24 151
m@24 152 ;; negative times/durations -> ERROR?
m@24 153
m@24 154 ;; time+: <time> <duration> -> <time>
m@24 155 ;; <duration> <time> -> <time> (same as previous?)
m@24 156 ;; <duration> <duration> -> <duration> (or a distinct duration+?)
m@24 157 ;; <time> <time> -> ERROR?
m@24 158 ;;
m@24 159 ;; time-: <time> <time> -> <duration>
m@24 160 ;; <time> <duration> -> <time>
m@24 161 ;; <duration> <duration> -> <duration> (or a distinct duration-?)
m@24 162 ;; <duration> <time> -> ERROR?
m@24 163 ;; <anchored> <anchored> -> (time- (moment o1) (moment o2)) ? or error?
m@24 164
d@102 165 (defgeneric time+ (object1 object2)
d@102 166 (:documentation "Addition for time designators"))
d@102 167 (defgeneric time- (object1 object2)
d@102 168 (:documentation "Subtraction for time designators"))
m@24 169
d@102 170 (defgeneric time> (object1 object2)
d@102 171 (:documentation "> operator for time designators"))
m@24 172 (defgeneric time< (object1 object2)
d@102 173 (:documentation "< operator for time designators")
m@24 174 (:method (o1 o2) (time> o2 o1)))
d@102 175 (defgeneric time= (object1 object2)
d@102 176 (:documentation "= operator for time designators"))
m@24 177 (defgeneric time>= (object1 object2)
d@102 178 (:documentation ">= operator for time designators")
m@24 179 (:method (o1 o2) (or (time> o1 o2) (time= o1 o2))))
m@24 180 (defgeneric time<= (object1 object2)
d@102 181 (:documentation "<= operator for time designators")
m@24 182 (:method (o1 o2) (or (time< o1 o2) (time= o1 o2))))
m@24 183 (defgeneric time/= (object1 object2)
d@102 184 (:documentation "not = operator for time designators")
m@24 185 (:method (o1 o2) (not (time= o1 o2))))
m@24 186
m@24 187 ;;; Duration protocol
m@24 188
m@24 189 (defgeneric duration> (object1 object2))
m@24 190 (defgeneric duration< (object1 object2)
m@24 191 (:method (o1 o2) (duration> o2 o1)))
m@24 192 (defgeneric duration= (object1 object2))
m@24 193 (defgeneric duration>= (object1 object2)
m@24 194 (:method (o1 o2) (or (duration> o1 o2) (duration= o1 o2))))
m@24 195 (defgeneric duration<= (object1 object2)
m@24 196 (:method (o1 o2) (or (duration< o1 o2) (duration= o1 o2))))
m@24 197 (defgeneric duration/= (object1 object2)
m@24 198 (:method (o1 o2) (not (duration= o1 o2))))
m@24 199
m@24 200 ;; for linear scaling:
m@24 201 (defgeneric duration* (object1 object2))
m@24 202 (defgeneric duration/ (object1 number))
m@24 203
m@24 204 ;;; Pitch protocol
m@24 205
m@24 206 ;; pitch+: <pitch> <pitch> -> ERROR
m@24 207 ;; <pitch> <interval> -> <pitch>
m@24 208 ;; <interval> <pitch> -> <pitch> (same as previous?)
m@24 209 ;; <interval> <interval> -> <interval> (or a distinct interval+?)
m@24 210 ;;
m@24 211 ;; pitch-: <pitch> <pitch> -> <interval>
m@24 212 ;; <pitch> <interval> -> <pitch>
m@24 213 ;; <interval> <interval> -> <interval>
m@24 214 ;; <interval> <pitch> -> ERROR
m@24 215
m@24 216 (defgeneric pitch+ (object1 object2))
m@24 217 (defgeneric pitch- (object1 object2))
m@24 218
m@24 219 (defgeneric pitch> (object1 object2))
m@24 220 (defgeneric pitch< (object1 object2)
m@24 221 (:method (o1 o2) (pitch> o2 o1)))
m@24 222 (defgeneric pitch= (object1 object2))
m@24 223 (defgeneric pitch>= (object1 object2)
m@24 224 (:method (o1 o2) (or (pitch> o1 o2) (pitch= o1 o2))))
m@24 225 (defgeneric pitch<= (object1 object2)
m@24 226 (:method (o1 o2) (or (pitch< o1 o2) (pitch= o1 o2))))
m@24 227 (defgeneric pitch/= (object1 object2)
m@24 228 (:method (o1 o2) (not (pitch= o1 o2))))
m@24 229
m@24 230 ;;; Interval protocol (emphasise _pitch_ not _time_ interval?)
m@24 231
m@24 232 (defgeneric interval> (object1 object2))
m@24 233 (defgeneric interval< (object1 object2)
m@24 234 (:method (o1 o2) (interval> o2 o1)))
m@24 235 (defgeneric interval= (object1 object2))
m@24 236 (defgeneric interval>= (object1 object2)
m@24 237 (:method (o1 o2) (or (interval> o1 o2) (interval= o1 o2))))
m@24 238 (defgeneric interval<= (object1 object2)
m@24 239 (:method (o1 o2) (or (interval< o1 o2) (interval= o1 o2))))
m@24 240 (defgeneric interval/= (object1 object2)
m@24 241 (:method (o1 o2) (not (interval= o1 o2))))
m@24 242
m@24 243 ;;; Allen's (1984) interval relations
m@24 244 ;;; . equals already defined as INTERVAL= above
m@24 245 ;;; . inverses ommitted for now (just use CL:NOT)
m@24 246 ;;; . can all be defined in terms of MEETS (apparently)
m@24 247
m@24 248 (defgeneric meets (object1 object2))
m@24 249 (defgeneric before (object1 object2))
m@24 250 (defgeneric overlaps (object1 object2))
m@24 251 (defgeneric during (object1 object2))
m@24 252 (defgeneric starts (object1 object2))
m@24 253 (defgeneric ends (object1 object2))
m@24 254
m@24 255 ;;; and extensions thereof ...
m@24 256
m@24 257 (defgeneric subinterval (object1 object2)
m@24 258 (:method (o1 o2) (or (starts o1 o2) (during o1 o2) (ends o1 o2))))
m@24 259
m@24 260 (defgeneric disjoint (object1 object2)
m@24 261 (:method (o1 o2)
m@24 262 (or (before o1 o2) (meets o1 o2) (meets o2 o1) (before o2 o1))))
m@24 263
m@24 264 ;;; More time-based functions
d@33 265
d@33 266 (defgeneric period= (object1 object2)
d@33 267 (:method (x y) nil))
d@33 268
d@33 269 (defgeneric find-overlapping (anchored-period sequence)
d@33 270 ;; Returns all members of a sequence of period signifiers that overlap
d@33 271 ;; with the supplied period
d@33 272 (:method (ap s) (remove-if #'(lambda (x) (amuse:disjoint ap x)) s)))
d@33 273
m@24 274 ;; Return the anchored-period representing the intersection of two
m@24 275 ;; anchored-period-specifiers.
m@24 276 (defgeneric period-intersection (anchored-period-specifier1
m@24 277 anchored-period-specifier2))
m@24 278
m@24 279 (defgeneric inter-onset-interval (moment-designator1 moment-designator2)
m@24 280 (:method (md1 md2) (time- (moment md2) (moment md1))))
m@24 281
m@24 282
m@24 283 ;;; Time Signature
m@24 284
d@33 285 (defgeneric get-applicable-time-signatures (anchored-period composition)
d@33 286 (:method (ap c) (find-overlapping ap (time-signatures c))))
m@24 287
m@67 288 (defgeneric time-signature-equal (ts1 ts2))
m@67 289
m@24 290 ;;; Tempo
m@24 291
d@33 292 (defgeneric get-applicable-tempi (anchored-period composition)
d@33 293 (:method (ap c) (find-overlapping ap (tempi c))))
m@24 294
m@67 295 (defgeneric tempo-equal (t1 t2))
m@67 296
m@24 297 ;;; Tonality (Key Signature / Mode)
m@24 298
m@24 299 (defgeneric get-applicable-key-signatures (object1 object2))
m@24 300
m@67 301 (defgeneric key-signature-equal (ks1 ks2))
m@67 302
m@24 303 ;;; Dynamics
m@24 304 ;;; Voice
m@81 305 ;;; Boundary Strength (phrasing)