annotate base/generics.lisp @ 81:4e1538df0d10

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