annotate base/generics.lisp @ 160:136ec5516cc4

patch on my computer. Not sure what it does darcs-hash:20071211145912-f76cc-cb69a7a78367366f9731284888db1c558fa07989.gz
author David Lewis <d.lewis@gold.ac.uk>
date Tue, 11 Dec 2007 14:59:12 +0000
parents cc7724248043
children 4a0e15e2829a
rev   line source
m@24 1 (cl:in-package #:amuse)
m@24 2
m@24 3 ;;; Pulling compositions from the database
m@24 4
d@123 5 (defgeneric get-composition (identifier)
d@123 6 (:documentation "Returns a composition of type dependant on
d@123 7 identifier"))
m@24 8
m@89 9 (defgeneric monody (object)
m@89 10 (:documentation "Returns a monody."))
m@89 11 (defgeneric ensure-monody (object)
m@89 12 (:documentation "Returns a generalised boolean."))
m@89 13
d@33 14 ;;; Getting constituents from compositions
d@33 15 ;; IS this the mechanism we want to use
d@72 16 (defgeneric time-signatures (composition)
d@72 17 (:documentation "Returns all time-signatures in a composition
d@72 18 Probably shouldn't be exported - can be replaced
d@72 19 by (get-applicable-time-signature commposition composition)"))
d@72 20 (defgeneric (setf time-signatures) (sequence composition)
d@72 21 (:documentation "Sets all time-signatures in a composition.
d@72 22 Is this wanted here?"))
d@72 23 (defgeneric tempi (composition)
d@72 24 (:documentation "Returns all tempi in a composition Probably
d@72 25 shouldn't be exported - can be replaced
d@72 26 by (get-applicable-tempi commposition composition)"))
d@72 27 (defgeneric (setf tempi) (sequence composition)
d@72 28 (:documentation "Sets all tempi in a composition.
d@72 29 Is this wanted here?"))
d@72 30 (defgeneric key-signatures (composition)
d@72 31 (:documentation "Returns all key-signatures in a composition
d@72 32 Probably shouldn't be exported - can be replaced
d@72 33 by (get-applicable-key-signature commposition composition)"))
d@72 34 (defgeneric (setf key-signatures) (sequence composition)
d@72 35 (:documentation "Sets all key sigs in a composition.
d@72 36 Is this wanted here?"))
d@33 37
m@24 38 ;;; Simple Accessors
m@24 39
m@24 40 ;; pitch-based
m@24 41
m@24 42 (defgeneric pitch (object &key kind)) ; ? Maybe this returns the pitch
m@24 43 ; in its ur form?
d@136 44 (defgeneric chromatic-pitch (pitch))
d@136 45 (defgeneric diatonic-pitch (pitch))
m@24 46 (defgeneric frequency (object)) ;?
m@81 47
d@136 48 (defgeneric octave (pitch)
m@86 49 (:documentation "Return an integer representing the octave of
d@136 50 pitch where middle c is defined to be the lowest pitch in
m@86 51 octave 4."))
m@86 52
d@136 53 (defgeneric diatonic-pitch-octave (pitch)
c@106 54 (:documentation "Return an integer representing the diatonic octave
d@136 55 of pitch."))
c@106 56
d@136 57 (defgeneric diatonic-pitch-accidental (pitch)
m@86 58 (:documentation "Return an integer representing the inflection of a
m@86 59 diatonic pitch where where negative values indicate numbers of flats,
m@86 60 0 indicates natural and positive values indicate numbers of sharps."))
m@86 61
d@136 62 (defgeneric diatonic-pitch-mp (pitch)
c@108 63 (:documentation "Return an integer representing the morphetic pitch
c@108 64 \(in MIPS terms) of a diatonic pitch."))
d@136 65 (defgeneric diatonic-pitch-cp (pitch)
c@108 66 (:documentation "Return an integer representing the chromatic pitch
c@108 67 \(in MIPS terms) of a diatonic pitch."))
c@108 68
d@136 69 (defgeneric middle-c (pitch)
m@81 70 (:documentation "Returns the value of middle C in the particular
d@136 71 representation of pitch used by PITCH."))
d@136 72 (defgeneric midi-pitch-number (pitch)
d@136 73 (:documentation "Takes a pitch (usually a pitched event) and
d@136 74 returns an integer between 0 and 127 representing the chromatic
d@136 75 pitch represented (60=middle C, 48 the C below that, etc.)"))
d@136 76 (defgeneric asa-pitch-string (pitch)
m@81 77 (:documentation "Returns a string representing the designated ASA
m@81 78 pitch name which has three parts: a letter name in the set
m@81 79 {A,B,C,D,E,F,G}, an inflection in the set {n,f,s,ff,ss,fff,sss,...}
m@81 80 and an octave number. E.g., Cn4 = Middle C."))
d@136 81 (defgeneric asa-interval-string (pitch)
c@112 82 (:documentation "Returns a string representing the designated ASA
c@112 83 interval name which has two or three parts: a direction in the set
c@112 84 {r,f} (absent for unisons/primes), a type in the set
c@112 85 {p,ma,mi,a,d,aa,dd,aaa,ddd,...}, and a size number. E.g. rma2 =
c@112 86 rising major second."))
d@136 87 (defgeneric diatonic-pitch-name (pitch)
m@81 88 (:documentation "Returns a char in the set
m@81 89 {#\A,#\B,#\C,#\D,#\E,#\F,#\G}, representing the pitch name of
d@136 90 PITCH."))
d@136 91 (defgeneric pitch-class (pitch)
d@136 92 (:documentation "Takes a pitch (usually a pitched event) and
d@136 93 returns an integer between 0 and 12 representing the
d@136 94 octave-independant pitch, with c=0, c#=1, etc.")
m@24 95 (:method (p) (mod (midi-pitch-number p) 12)))
d@136 96 (defgeneric span (pitch-interval))
m@24 97
m@24 98 ;; time
m@24 99
d@136 100 (defgeneric duration (period)
d@136 101 (:documentation "Returns a real. Probably should only apply do
d@136 102 standard-periods (rather than periods? or should it return
d@136 103 something other than a value in other cases)"))
d@136 104 (defgeneric (setf duration) (value period)
d@136 105 (:documentation "As with duration, perhaps this should work only
d@136 106 with standard-periods"))
d@136 107 (defgeneric timepoint (moment)
d@136 108 (:documentation "Returns a number for a moment. Does this make
d@136 109 any sense on an abstrace class? Should it just apply to
d@136 110 standard-moment?"))
d@136 111 (defgeneric (setf timepoint) (value moment)
d@136 112 (:documentation "Sets timepoint. What does this mean for an
d@136 113 abstract class? Should it just apply to standard-moment"))
d@136 114 (defgeneric cut-off (anchored-period) ; name?
d@72 115 (:documentation "Returns a <moment> representing the point at
d@121 116 which the anchored period has ended. By default, is calculated
d@121 117 as the result of running time+ on the onset and period of the
d@121 118 object.")
d@136 119 (:method (apd) (time+ (moment apd) (period apd))))
d@136 120 (defgeneric crotchet (object)
m@95 121 (:documentation "Returns a period, the duration of which represents
m@95 122 a crotchet in the time representation used by object."))
m@24 123
m@24 124 ;; others
m@24 125
m@24 126 ;; I've given the time-sig accessors general names because it allows
m@24 127 ;; for symbols in time-signatures as well as numbers - numerator is an
m@24 128 ;; odd accessor if the time sig is C (even in common practice) but
d@100 129 ;; its meaning is clear. beat-units-per-bar is clearer, though, I
m@24 130 ;; think.
m@24 131
d@123 132 (defgeneric beat-units-per-bar (time-signature)
d@123 133 (:documentation "In a standard, fraction-like time-signature or
d@123 134 a symbolic equivalent, this is the numerator."))
m@24 135 (defgeneric time-signature-numerator (time-signature)
d@123 136 (:method (ts) (beat-units-per-bar ts))
d@123 137 (:documentation "Not obviously meaningful for non fraction-like
d@123 138 time signatures"))
d@123 139 (defgeneric beat-units (time-signature)
d@123 140 (:documentation "In a standard, fraction-like time-signature or
d@134 141 a symbolic equivalent, this is the denominator (n.b. the
d@134 142 difference between this and @code{tactus-duration} is only
d@134 143 visible for compound time, where this returns the lower portion
d@134 144 of the time signature, but @code{tactus-duration} returns the
d@134 145 higher-level unit)."))
m@24 146 (defgeneric time-signature-denominator (time-signature)
d@123 147 (:method (ts) (beat-units ts))
d@123 148 (:documentation "Not obviously meaningful for non fraction-like
d@123 149 time signatures"))
d@33 150 (defgeneric tactus-duration (time-signature)
d@33 151 ;; basic, but should do?
d@33 152 (:method (ts)
d@33 153 (cond
d@33 154 ((and (not (= (beat-units-per-bar ts) 3))
d@33 155 (= (rem (beat-units-per-bar ts) 3) 0))
d@33 156 ;; compound time
d@33 157 (* (/ 4 (beat-units ts))
d@33 158 3))
d@123 159 (t (/ 4 (beat-units ts)))))
d@123 160 (:documentation "Returns a number of crotchets to represent the
d@123 161 tactus, based on some idea of time signature patterns. Should,
d@123 162 in future, return a duration rather than a number."))
m@24 163
d@123 164 (defgeneric key-signature-sharps (key-signature)
d@123 165 (:documentation "Simple query for normal key-signatures."))
d@123 166 (defgeneric key-signature-mode (ks)
d@123 167 (:documentation "Query that only makes sense for midi-like key
d@123 168 signatures"))
m@24 169
d@123 170 (defgeneric bpm (tempo)
d@123 171 (:documentation "Basic tempo query")) ;; in bpm
m@24 172 (defgeneric microseconds-per-crotchet (tempo)
m@24 173 ;; As used (when rounded) in MIDI
d@123 174 (:method (tp) (/ 60000000 (bpm tp)))
d@123 175 (:documentation "Basic tempo query for MIDI. N.B. This will be
d@123 176 a fraction and must be rounded before being used for output."))
m@24 177
m@24 178 ;;; Coerce-type accessors
m@24 179
m@24 180 ;; Should I be including these default methods? Should the accessors
m@24 181 ;; be direct slot accessors or the generics I'm using? Should we
m@24 182 ;; return the object itself if it already is in the target class?
m@24 183
d@136 184 (defgeneric anchored-period (anchored-period)
d@123 185 (:method (apd) (make-anchored-period (onset apd) (duration apd)))
d@123 186 (:documentation "Coerce any anchored period to a plain anchored
d@123 187 period"))
m@24 188
d@136 189 (defgeneric period (period)
d@136 190 (:method (pd) (make-period (duration pd)))
d@123 191 (:documentation "Coerce any period to a floating period"))
m@24 192
d@136 193 (defgeneric moment (moment)
d@123 194 (:method (md) (make-moment (timepoint md)))
d@136 195 (:documentation "Coerce any moment, including an
d@123 196 anchored-period to a moment"))
m@24 197
d@136 198 (defgeneric onset (anchored-period)
d@139 199 (:method (ap) (moment ap))
d@123 200 (:documentation "Return a moment for the start of an anchored period"))
d@136 201 (defgeneric (setf onset) (value anchored-period))
m@24 202
m@24 203 ;;; Time Protocol (or moments?)
m@24 204
m@24 205 ;; negative times/durations -> ERROR?
m@24 206
m@24 207 ;; time+: <time> <duration> -> <time>
m@24 208 ;; <duration> <time> -> <time> (same as previous?)
m@24 209 ;; <duration> <duration> -> <duration> (or a distinct duration+?)
m@24 210 ;; <time> <time> -> ERROR?
m@24 211 ;;
m@24 212 ;; time-: <time> <time> -> <duration>
m@24 213 ;; <time> <duration> -> <time>
m@24 214 ;; <duration> <duration> -> <duration> (or a distinct duration-?)
m@24 215 ;; <duration> <time> -> ERROR?
m@24 216 ;; <anchored> <anchored> -> (time- (moment o1) (moment o2)) ? or error?
m@24 217
d@102 218 (defgeneric time+ (object1 object2)
d@136 219 (:documentation "Addition for time objects"))
d@102 220 (defgeneric time- (object1 object2)
d@136 221 (:documentation "Subtraction for time objects"))
m@24 222
d@102 223 (defgeneric time> (object1 object2)
d@136 224 (:documentation "> operator for moments"))
m@24 225 (defgeneric time< (object1 object2)
d@136 226 (:documentation "< operator for moments")
m@24 227 (:method (o1 o2) (time> o2 o1)))
d@102 228 (defgeneric time= (object1 object2)
d@136 229 (:documentation "= operator for moments"))
m@24 230 (defgeneric time>= (object1 object2)
d@136 231 (:documentation ">= operator for moments")
m@24 232 (:method (o1 o2) (or (time> o1 o2) (time= o1 o2))))
m@24 233 (defgeneric time<= (object1 object2)
d@136 234 (:documentation "<= operator for moments")
m@24 235 (:method (o1 o2) (or (time< o1 o2) (time= o1 o2))))
m@24 236 (defgeneric time/= (object1 object2)
d@136 237 (:documentation "not = operator for moments")
m@24 238 (:method (o1 o2) (not (time= o1 o2))))
m@24 239
m@24 240 ;;; Duration protocol
m@24 241
d@118 242 (defgeneric duration> (object1 object2)
d@136 243 (:documentation "> operator for periods"))
m@24 244 (defgeneric duration< (object1 object2)
d@136 245 (:documentation "< operator for periods")
m@24 246 (:method (o1 o2) (duration> o2 o1)))
d@118 247 (defgeneric duration= (object1 object2)
d@136 248 (:documentation "= operator for periods"))
m@24 249 (defgeneric duration>= (object1 object2)
d@136 250 (:documentation ">= operator for periods")
m@24 251 (:method (o1 o2) (or (duration> o1 o2) (duration= o1 o2))))
m@24 252 (defgeneric duration<= (object1 object2)
d@136 253 (:documentation "<= operator for periods")
m@24 254 (:method (o1 o2) (or (duration< o1 o2) (duration= o1 o2))))
m@24 255 (defgeneric duration/= (object1 object2)
d@136 256 (:documentation "not = operator for periods")
m@24 257 (:method (o1 o2) (not (duration= o1 o2))))
m@24 258
m@24 259 ;; for linear scaling:
d@118 260 (defgeneric duration* (object1 object2)
d@136 261 (:documentation "Multiplication operator for
d@136 262 periods. Intuitively, this makes sense, but it may cause us
d@118 263 trouble with some implementations in the future."))
d@118 264 (defgeneric duration/ (object1 number)
d@136 265 (:documentation "Division operator for periods. This may turn
d@136 266 out not to mean much. Division is probably useful, but we may
d@136 267 need to define what we mean with care."))
m@24 268
m@24 269 ;;; Pitch protocol
m@24 270
m@24 271 ;; pitch+: <pitch> <pitch> -> ERROR
m@24 272 ;; <pitch> <interval> -> <pitch>
m@24 273 ;; <interval> <pitch> -> <pitch> (same as previous?)
m@24 274 ;; <interval> <interval> -> <interval> (or a distinct interval+?)
m@24 275 ;;
m@24 276 ;; pitch-: <pitch> <pitch> -> <interval>
m@24 277 ;; <pitch> <interval> -> <pitch>
m@24 278 ;; <interval> <interval> -> <interval>
m@24 279 ;; <interval> <pitch> -> ERROR
m@24 280
m@24 281 (defgeneric pitch+ (object1 object2))
m@24 282 (defgeneric pitch- (object1 object2))
m@24 283
m@24 284 (defgeneric pitch> (object1 object2))
m@24 285 (defgeneric pitch< (object1 object2)
m@24 286 (:method (o1 o2) (pitch> o2 o1)))
m@24 287 (defgeneric pitch= (object1 object2))
m@24 288 (defgeneric pitch>= (object1 object2)
m@24 289 (:method (o1 o2) (or (pitch> o1 o2) (pitch= o1 o2))))
m@24 290 (defgeneric pitch<= (object1 object2)
m@24 291 (:method (o1 o2) (or (pitch< o1 o2) (pitch= o1 o2))))
m@24 292 (defgeneric pitch/= (object1 object2)
m@24 293 (:method (o1 o2) (not (pitch= o1 o2))))
m@24 294
m@24 295 ;;; Interval protocol (emphasise _pitch_ not _time_ interval?)
m@24 296
m@24 297 (defgeneric interval> (object1 object2))
m@24 298 (defgeneric interval< (object1 object2)
m@24 299 (:method (o1 o2) (interval> o2 o1)))
m@24 300 (defgeneric interval= (object1 object2))
m@24 301 (defgeneric interval>= (object1 object2)
m@24 302 (:method (o1 o2) (or (interval> o1 o2) (interval= o1 o2))))
m@24 303 (defgeneric interval<= (object1 object2)
m@24 304 (:method (o1 o2) (or (interval< o1 o2) (interval= o1 o2))))
m@24 305 (defgeneric interval/= (object1 object2)
m@24 306 (:method (o1 o2) (not (interval= o1 o2))))
m@24 307
m@24 308 ;;; Allen's (1984) interval relations
m@24 309 ;;; . equals already defined as INTERVAL= above
m@24 310 ;;; . inverses ommitted for now (just use CL:NOT)
m@24 311 ;;; . can all be defined in terms of MEETS (apparently)
m@24 312
m@24 313 (defgeneric meets (object1 object2))
m@24 314 (defgeneric before (object1 object2))
m@24 315 (defgeneric overlaps (object1 object2))
m@24 316 (defgeneric during (object1 object2))
m@24 317 (defgeneric starts (object1 object2))
m@24 318 (defgeneric ends (object1 object2))
m@24 319
m@24 320 ;;; and extensions thereof ...
m@24 321
m@24 322 (defgeneric subinterval (object1 object2)
m@24 323 (:method (o1 o2) (or (starts o1 o2) (during o1 o2) (ends o1 o2))))
m@24 324
m@24 325 (defgeneric disjoint (object1 object2)
m@24 326 (:method (o1 o2)
m@24 327 (or (before o1 o2) (meets o1 o2) (meets o2 o1) (before o2 o1))))
m@24 328
m@24 329 ;;; More time-based functions
d@33 330
d@33 331 (defgeneric period= (object1 object2)
d@33 332 (:method (x y) nil))
d@33 333
d@33 334 (defgeneric find-overlapping (anchored-period sequence)
d@33 335 ;; Returns all members of a sequence of period signifiers that overlap
d@33 336 ;; with the supplied period
d@33 337 (:method (ap s) (remove-if #'(lambda (x) (amuse:disjoint ap x)) s)))
d@33 338
m@24 339 ;; Return the anchored-period representing the intersection of two
d@136 340 ;; anchored-period.
d@136 341 (defgeneric period-intersection (anchored-period1
d@136 342 anchored-period2))
m@24 343
d@136 344 (defgeneric inter-onset-interval (moment1 moment2)
d@136 345 (:method (moment1 moment2) (time- (moment moment2) (moment moment1))))
m@24 346
m@24 347
m@24 348 ;;; Time Signature
m@24 349
d@33 350 (defgeneric get-applicable-time-signatures (anchored-period composition)
d@123 351 (:method (ap c) (find-overlapping ap (time-signatures c)))
d@136 352 (:documentation "Return a list of TIME-SIGNATURE-PERIODs that are
d@123 353 relevant to <anchored-period>. The period may contain
d@123 354 information such as staff position and voicing, and the method
d@123 355 may use this to filter its response"))
m@24 356
d@123 357 (defgeneric time-signature-equal (ts1 ts2)
d@123 358 (:documentation "Comparison operator. The definition of
d@123 359 equality is left open for implementers"))
m@67 360
m@24 361 ;;; Tempo
m@24 362
d@33 363 (defgeneric get-applicable-tempi (anchored-period composition)
d@123 364 (:method (ap c) (find-overlapping ap (tempi c)))
d@136 365 (:documentation "Return a list of TEMPO-PERIODs that are
d@136 366 relevant to <anchored-period>. The period may contain
d@136 367 information such as staff position and voicing, and the method
d@136 368 may use this to filter its response"))
m@24 369
d@123 370 (defgeneric tempo-equal (t1 t2)
d@123 371 (:documentation "Comparison operator. The definition of
d@123 372 equality is left open for implementers"))
m@67 373
m@24 374 ;;; Tonality (Key Signature / Mode)
m@24 375
d@123 376 (defgeneric get-applicable-key-signatures (object1 object2)
d@136 377 (:documentation "Return a list of KEY-SIGNATURE-PERIODs that are
d@123 378 relevant to <anchored-period>. The period may contain
d@123 379 information such as staff position and voicing, and the method
d@123 380 may use this to filter its response"))
m@24 381
d@123 382 (defgeneric key-signature-equal (ks1 ks2)
d@123 383 (:documentation "Comparison operator. The definition of
d@123 384 equality is left open to implementers"))
m@67 385
d@136 386 ;;; Some generic constructors - are these useful? (DL 31/8/07)
d@136 387 (defgeneric make-moment (value)
d@136 388 (:documentation "Returns MOMENT of subclass appropriate to the
d@136 389 class of value. Probably guessed."))
d@136 390 (defgeneric make-period (value)
d@136 391 (:documentation "Returns PERIOD of subclass appropriate to the
d@136 392 class of value. Probably guessed."))
d@136 393 (defgeneric make-anchored-period (start-value duration-value)
d@136 394 (:documentation "Returns ANCHORED-PERIOD of subclass
d@136 395 appropriate to the class of value. Probably guessed."))
d@136 396
d@160 397 (defgeneric trim-enclosing-silence (composition)
d@156 398 (:documentation "Returns a composition of the same type as
d@156 399 composition provided, with leading and following silences/rests
d@156 400 removed, but preserving all relevant information. Where
d@156 401 relevant, any silence in a bar containing musical material may
d@160 402 be preserved."))
d@136 403
m@24 404 ;;; Dynamics
m@24 405 ;;; Voice
m@81 406 ;;; Boundary Strength (phrasing)