annotate base/generics.lisp @ 33:d1010755f507

Large upload of local changes. Many additions, such as harmony and piece-level objects darcs-hash:20070413100909-f76cc-a8aa8dfc07f438dc0c1a7c45cee7ace2ecc1e6a5.gz
author David Lewis <d.lewis@gold.ac.uk>
date Fri, 13 Apr 2007 11:09:09 +0100
parents 8d2b1662f658
children 0f31919a855d
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@33 9 (defgeneric time-signatures (composition))
d@33 10 (defgeneric (setf time-signatures) (sequence composition))
d@33 11 (defgeneric tempi (composition))
d@33 12 (defgeneric (setf tempi) (sequence composition))
d@33 13 (defgeneric key-signatures (composition))
d@33 14 (defgeneric (setf key-signatures) (sequence composition))
d@33 15
m@24 16 ;;; Simple Accessors
m@24 17
m@24 18 ;; pitch-based
m@24 19
m@24 20 (defgeneric pitch (object &key kind)) ; ? Maybe this returns the pitch
m@24 21 ; in its ur form?
m@24 22 (defgeneric chromatic-pitch (pitch-designator)) ; How simple are these
m@24 23 (defgeneric diatonic-pitch (pitch-designator)) ; if has to be computed?
m@24 24 (defgeneric frequency (object)) ;?
m@24 25 (defgeneric midi-pitch-number (pitch-designator))
m@24 26 (defgeneric meredith-chromatic-pitch-number (pitch-designator)
m@24 27 ;; David Meredith's PhD and ps13 code
m@24 28 (:method (p) (- (midi-pitch-number p) 21)))
m@24 29 (defgeneric pitch-class (pitch-designator)
m@24 30 (:method (p) (mod (midi-pitch-number p) 12)))
m@24 31 (defgeneric span (pitch-interval-designator))
m@24 32
m@24 33 ;; time
m@24 34
m@24 35 (defgeneric duration (period-designator))
m@24 36 (defgeneric (setf duration) (value period-designator))
m@24 37 (defgeneric timepoint (moment-designator))
m@24 38 (defgeneric (setf timepoint) (value moment-designator))
m@24 39 (defgeneric cut-off (anchored-period-designator) ; name?
m@24 40 (:method (apd) (time+ (moment apd) (floating-period apd))))
m@24 41
m@24 42 ;; others
m@24 43
m@24 44 ;; I've given the time-sig accessors general names because it allows
m@24 45 ;; for symbols in time-signatures as well as numbers - numerator is an
m@24 46 ;; odd accessor if the time sig is C (even in common practice) but
m@24 47 ;; it's meaning is clear. beat-units-per-bar is clearer, though, I
m@24 48 ;; think.
m@24 49
m@24 50 (defgeneric beat-units-per-bar (time-signature))
m@24 51 (defgeneric time-signature-numerator (time-signature)
m@24 52 (:method (ts) (beat-units-per-bar ts)))
m@24 53 (defgeneric beat-units (time-signature))
m@24 54 (defgeneric time-signature-denominator (time-signature)
m@24 55 (:method (ts) (beat-units ts)))
d@33 56 (defgeneric tactus-duration (time-signature)
d@33 57 ;; basic, but should do?
d@33 58 (:method (ts)
d@33 59 (cond
d@33 60 ((and (not (= (beat-units-per-bar ts) 3))
d@33 61 (= (rem (beat-units-per-bar ts) 3) 0))
d@33 62 ;; compound time
d@33 63 (* (/ 4 (beat-units ts))
d@33 64 3))
d@33 65 (t (/ 4 (beat-units ts))))))
m@24 66
m@24 67 (defgeneric key-signature-sharps (key-signature))
m@24 68
m@24 69 (defgeneric bpm (tempo)) ;; in bpm
m@24 70 (defgeneric microseconds-per-crotchet (tempo)
m@24 71 ;; As used (when rounded) in MIDI
m@24 72 (:method (tp) (/ 60000000 (bpm tp))))
m@24 73
m@24 74 ;;; Coerce-type accessors
m@24 75
m@24 76 ;; Should I be including these default methods? Should the accessors
m@24 77 ;; be direct slot accessors or the generics I'm using? Should we
m@24 78 ;; return the object itself if it already is in the target class?
m@24 79
m@24 80 (defgeneric anchored-period (anchored-period-designator)
m@24 81 (:method (apd) (make-anchored-period (onset apd) (duration apd))))
m@24 82
m@24 83 (defgeneric floating-period (period-designator)
m@24 84 (:method (pd) (make-floating-period (duration pd))))
m@24 85
m@24 86 (defgeneric moment (moment-designator)
m@24 87 (:method (md) (make-moment (timepoint md))))
m@24 88
m@24 89 (defgeneric onset (anchored-period-designator)
m@24 90 (:method (apd) (moment apd)))
m@24 91 (defgeneric (setf onset) (value anchored-period-designator))
m@24 92
m@24 93 ;;; Time Protocol (or moments?)
m@24 94
m@24 95 ;; negative times/durations -> ERROR?
m@24 96
m@24 97 ;; time+: <time> <duration> -> <time>
m@24 98 ;; <duration> <time> -> <time> (same as previous?)
m@24 99 ;; <duration> <duration> -> <duration> (or a distinct duration+?)
m@24 100 ;; <time> <time> -> ERROR?
m@24 101 ;;
m@24 102 ;; time-: <time> <time> -> <duration>
m@24 103 ;; <time> <duration> -> <time>
m@24 104 ;; <duration> <duration> -> <duration> (or a distinct duration-?)
m@24 105 ;; <duration> <time> -> ERROR?
m@24 106 ;; <anchored> <anchored> -> (time- (moment o1) (moment o2)) ? or error?
m@24 107
m@24 108 (defgeneric time+ (object1 object2))
m@24 109 (defgeneric time- (object1 object2))
m@24 110
m@24 111 (defgeneric time> (object1 object2))
m@24 112 (defgeneric time< (object1 object2)
m@24 113 (:method (o1 o2) (time> o2 o1)))
m@24 114 (defgeneric time= (object1 object2))
m@24 115 (defgeneric time>= (object1 object2)
m@24 116 (:method (o1 o2) (or (time> o1 o2) (time= o1 o2))))
m@24 117 (defgeneric time<= (object1 object2)
m@24 118 (:method (o1 o2) (or (time< o1 o2) (time= o1 o2))))
m@24 119 (defgeneric time/= (object1 object2)
m@24 120 (:method (o1 o2) (not (time= o1 o2))))
m@24 121
m@24 122 ;;; Duration protocol
m@24 123
m@24 124 (defgeneric duration> (object1 object2))
m@24 125 (defgeneric duration< (object1 object2)
m@24 126 (:method (o1 o2) (duration> o2 o1)))
m@24 127 (defgeneric duration= (object1 object2))
m@24 128 (defgeneric duration>= (object1 object2)
m@24 129 (:method (o1 o2) (or (duration> o1 o2) (duration= o1 o2))))
m@24 130 (defgeneric duration<= (object1 object2)
m@24 131 (:method (o1 o2) (or (duration< o1 o2) (duration= o1 o2))))
m@24 132 (defgeneric duration/= (object1 object2)
m@24 133 (:method (o1 o2) (not (duration= o1 o2))))
m@24 134
m@24 135 ;; for linear scaling:
m@24 136 (defgeneric duration* (object1 object2))
m@24 137 (defgeneric duration/ (object1 number))
m@24 138
m@24 139 ;;; Pitch protocol
m@24 140
m@24 141 ;; pitch+: <pitch> <pitch> -> ERROR
m@24 142 ;; <pitch> <interval> -> <pitch>
m@24 143 ;; <interval> <pitch> -> <pitch> (same as previous?)
m@24 144 ;; <interval> <interval> -> <interval> (or a distinct interval+?)
m@24 145 ;;
m@24 146 ;; pitch-: <pitch> <pitch> -> <interval>
m@24 147 ;; <pitch> <interval> -> <pitch>
m@24 148 ;; <interval> <interval> -> <interval>
m@24 149 ;; <interval> <pitch> -> ERROR
m@24 150
m@24 151 (defgeneric pitch+ (object1 object2))
m@24 152 (defgeneric pitch- (object1 object2))
m@24 153
m@24 154 (defgeneric pitch> (object1 object2))
m@24 155 (defgeneric pitch< (object1 object2)
m@24 156 (:method (o1 o2) (pitch> o2 o1)))
m@24 157 (defgeneric pitch= (object1 object2))
m@24 158 (defgeneric pitch>= (object1 object2)
m@24 159 (:method (o1 o2) (or (pitch> o1 o2) (pitch= o1 o2))))
m@24 160 (defgeneric pitch<= (object1 object2)
m@24 161 (:method (o1 o2) (or (pitch< o1 o2) (pitch= o1 o2))))
m@24 162 (defgeneric pitch/= (object1 object2)
m@24 163 (:method (o1 o2) (not (pitch= o1 o2))))
m@24 164
m@24 165 ;;; Interval protocol (emphasise _pitch_ not _time_ interval?)
m@24 166
m@24 167 (defgeneric interval> (object1 object2))
m@24 168 (defgeneric interval< (object1 object2)
m@24 169 (:method (o1 o2) (interval> o2 o1)))
m@24 170 (defgeneric interval= (object1 object2))
m@24 171 (defgeneric interval>= (object1 object2)
m@24 172 (:method (o1 o2) (or (interval> o1 o2) (interval= o1 o2))))
m@24 173 (defgeneric interval<= (object1 object2)
m@24 174 (:method (o1 o2) (or (interval< o1 o2) (interval= o1 o2))))
m@24 175 (defgeneric interval/= (object1 object2)
m@24 176 (:method (o1 o2) (not (interval= o1 o2))))
m@24 177
m@24 178 ;;; Allen's (1984) interval relations
m@24 179 ;;; . equals already defined as INTERVAL= above
m@24 180 ;;; . inverses ommitted for now (just use CL:NOT)
m@24 181 ;;; . can all be defined in terms of MEETS (apparently)
m@24 182
m@24 183 (defgeneric meets (object1 object2))
m@24 184 (defgeneric before (object1 object2))
m@24 185 (defgeneric overlaps (object1 object2))
m@24 186 (defgeneric during (object1 object2))
m@24 187 (defgeneric starts (object1 object2))
m@24 188 (defgeneric ends (object1 object2))
m@24 189
m@24 190 ;;; and extensions thereof ...
m@24 191
m@24 192 (defgeneric subinterval (object1 object2)
m@24 193 (:method (o1 o2) (or (starts o1 o2) (during o1 o2) (ends o1 o2))))
m@24 194
m@24 195 (defgeneric disjoint (object1 object2)
m@24 196 (:method (o1 o2)
m@24 197 (or (before o1 o2) (meets o1 o2) (meets o2 o1) (before o2 o1))))
m@24 198
m@24 199 ;;; More time-based functions
d@33 200
d@33 201 (defgeneric period= (object1 object2)
d@33 202 (:method (x y) nil))
d@33 203
d@33 204 (defgeneric find-overlapping (anchored-period sequence)
d@33 205 ;; Returns all members of a sequence of period signifiers that overlap
d@33 206 ;; with the supplied period
d@33 207 (:method (ap s) (remove-if #'(lambda (x) (amuse:disjoint ap x)) s)))
d@33 208
m@24 209 ;; Return the anchored-period representing the intersection of two
m@24 210 ;; anchored-period-specifiers.
m@24 211 (defgeneric period-intersection (anchored-period-specifier1
m@24 212 anchored-period-specifier2))
m@24 213
m@24 214 (defgeneric inter-onset-interval (moment-designator1 moment-designator2)
m@24 215 (:method (md1 md2) (time- (moment md2) (moment md1))))
m@24 216
m@24 217
m@24 218 ;;; Time Signature
m@24 219
d@33 220 (defgeneric get-applicable-time-signatures (anchored-period composition)
d@33 221 (:method (ap c) (find-overlapping ap (time-signatures c))))
m@24 222
m@24 223 ;;; Tempo
m@24 224
d@33 225 (defgeneric get-applicable-tempi (anchored-period composition)
d@33 226 (:method (ap c) (find-overlapping ap (tempi c))))
m@24 227
m@24 228 ;;; Tonality (Key Signature / Mode)
m@24 229
m@24 230 (defgeneric get-applicable-key-signatures (object1 object2))
m@24 231
m@24 232 ;;; Dynamics
m@24 233 ;;; Voice
m@24 234 ;;; Boundary Strength (phrasing)
m@24 235