comparison base/methods.lisp @ 24:8d2b1662f658

base/*.lisp: move files in amuse-base to subdirectory. darcs-hash:20061215161617-aa3d6-1b63bd555b02ec02aa2db12d335e8b726e2108cd.gz
author m.pearce <m.pearce@gold.ac.uk>
date Fri, 15 Dec 2006 16:16:17 +0000
parents
children d1010755f507
comparison
equal deleted inserted replaced
23:e2e19baba730 24:8d2b1662f658
1 (cl:in-package #:amuse)
2
3 (defmethod chromatic-pitch ((pitch-designator chromatic-pitch))
4 pitch-designator)
5
6 (defmethod midi-pitch-number ((pitch-designator chromatic-pitch))
7 (%chromatic-pitch-number pitch-designator))
8
9 (defmethod midi-pitch-number ((pitch-designator pitch))
10 (%chromatic-pitch-number (chromatic-pitch pitch-designator)))
11
12 (defmethod span ((pitch-interval-designator pitch-interval))
13 (%pitch-interval-span pitch-interval-designator))
14
15 (defmethod duration ((period-designator period))
16 (%period-interval period-designator))
17
18 (defmethod timepoint ((moment-designator moment))
19 (%moment-time moment-designator))
20
21 (defmethod beat-units-per-bar ((time-signature basic-time-signature))
22 (%basic-time-signature-numerator time-signature))
23
24 (defmethod beat-units ((time-signature basic-time-signature))
25 (%basic-time-signature-denominator time-signature))
26
27 (defmethod key-signature-sharps ((key-signature basic-key-signature))
28 (%basic-key-signature-sharp-count key-signature))
29
30 (defmethod bpm ((tempo tempo))
31 (%tempo-bpm tempo))
32
33 ;; Time protocol
34
35 (defmethod time+ ((object1 moment) (object2 period))
36 (make-moment (+ (timepoint object1) (duration object2))))
37
38 (defmethod time+ ((object1 period) (object2 moment)) ;?
39 (time+ object2 object1))
40
41 (defmethod time+ ((object1 period) (object2 period))
42 (make-floating-period (+ (duration object1)
43 (duration object2))))
44
45 (defmethod time+ ((object1 moment) (object2 moment))
46 (error 'undefined-action :operation 'time+ :datatype (list (class-of object1) (class-of object2))))
47
48 (defmethod time- ((object1 moment) (object2 moment))
49 (make-anchored-period (timepoint object2)
50 (- (timepoint object1)
51 (timepoint object2))))
52
53 (defmethod time- ((object1 moment) (object2 period))
54 (make-moment (- (timepoint object1) (duration object2))))
55
56 (defmethod time- ((object1 period) (object2 moment)) ;?
57 (error 'undefined-action
58 :operation 'time-
59 :datatype (list (class-of object1) (class-of object2))))
60
61 (defmethod time- ((object1 period) (object2 period))
62 (make-floating-period (- (duration object2)
63 (duration object1))))
64
65 ;; these ones are less certain. I've just put them in, but think I
66 ;; should remove them and force the user to specify what they mean
67 ;; when they give objects that are both moments *and* periods to these
68 ;; functions.
69
70 (defmethod time- ((object1 anchored-period) (object2 anchored-period)) ;?
71 (time- (moment object1) (moment object2)))
72
73 (defmethod time- (object1 (object2 anchored-period)) ;?
74 (time- object1 (moment object2)))
75
76 (defmethod time- ((object1 anchored-period) object2) ;?
77 (time- (moment object1) object2))
78
79 (defmethod time> ((object1 moment) (object2 moment))
80 (> (timepoint object1) (timepoint object2)))
81
82 (defmethod time= ((object1 moment) (object2 moment))
83 (= (timepoint object1) (timepoint object2)))
84
85 (defmethod duration> ((object1 period) (object2 period))
86 (> (duration object1) (duration object2)))
87
88 (defmethod duration= ((object1 period) (object2 period))
89 (= (duration object1) (duration object2)))
90
91 (defmethod duration* ((object1 period) (object2 number))
92 (make-floating-period (* (duration object1) object2)))
93
94 (defmethod duration* ((object1 number) (object2 period))
95 (duration* object2 object1))
96
97 (defmethod duration/ ((object1 period) (object2 number))
98 (make-floating-period (/ (duration object1) object2)))
99
100 ;; Pitch protocol
101
102 (defmethod pitch+ ((object1 pitch-designator)
103 (object2 pitch-designator))
104 (error 'undefined-action :operation 'pitch+
105 :datatype (list (class-of object1) (class-of object2))))
106
107 (defmethod pitch+ ((object1 pitch-designator)
108 (object2 pitch-interval)) ; or should I check the
109 ; pitch/interval types?
110 (make-chromatic-pitch (+ (chromatic-pitch object1)
111 (span object2))))
112
113 (defmethod pitch+ ((object1 pitch-interval)
114 (object2 pitch-designator)) ;?
115 (pitch+ object2 object1))
116
117 (defmethod pitch+ ((object1 pitch-interval)
118 (object2 pitch-interval))
119 (make-pitch-interval (+ (span object1)
120 (span object2))))
121
122 (defmethod pitch- ((object1 pitch-designator)
123 (object2 pitch-designator))
124 (make-pitch-interval (- (chromatic-pitch object1)
125 (chromatic-pitch object2))))
126
127 (defmethod pitch- ((object1 pitch-designator)
128 (object2 pitch-interval))
129 (make-chromatic-pitch (- (chromatic-pitch object1)
130 (span object2))))
131
132 (defmethod pitch- ((object1 pitch-interval)
133 (object2 pitch-interval))
134 (make-pitch-interval (- (span object1)
135 (span object2))))
136
137 (defmethod pitch- ((object1 pitch-interval)
138 (object2 pitch-designator))
139 (error 'undefined-action :operation 'pitch-
140 :datatype (list (class-of object1) (class-of object2))))
141
142 (defmethod pitch> ((object1 pitch-designator)
143 (object2 pitch-designator))
144 (> (chromatic-pitch object1)
145 (chromatic-pitch object2)))
146
147 (defmethod pitch= ((object1 pitch-designator)
148 (object2 pitch-designator))
149 (= (chromatic-pitch object1)
150 (chromatic-pitch object2)))
151
152 (defmethod interval> ((object1 pitch-interval)
153 (object2 pitch-interval))
154 (> (span object1)
155 (span object2)))
156
157 (defmethod interval= ((object1 pitch-interval)
158 (object2 pitch-interval))
159 (= (span object1)
160 (span object2)))
161
162
163
164 ;; Allen
165
166 (defmethod meets ((object1 anchored-period)
167 (object2 anchored-period))
168 (or (time= (cut-off object1) object2)
169 (time= (cut-off object2) object1)))
170
171 (defmethod before ((object1 anchored-period)
172 (object2 anchored-period))
173 (time> object2 (cut-off object1)))
174
175 (defmethod overlaps ((object1 anchored-period)
176 (object2 anchored-period))
177 ;; FIXME: Is there a tidier method?
178 (or (and (time> object2 object1) ; object1 starts before object2
179 (time> (cut-off object1) object2) ; object1 ends after object2 starts
180 (time> (cut-off object2) (cut-off object1))) ; object1 ends before object2 does
181 (and (time> object1 object2) ; object1 starts after object2
182 (time> (cut-off object2) object1) ; object1 starts before object2 ends
183 (time> (cut-off object1) (cut-off object2))))) ; object1 ends after object2 does
184
185 (defmethod during ((object1 anchored-period)
186 (object2 anchored-period))
187 (and (time> object1 object2)
188 (time< (cut-off object2) (cut-off object2))))
189
190 (defmethod starts ((object1 anchored-period)
191 (object2 anchored-period))
192 (time= object1 object2))
193
194 (defmethod ends ((object1 anchored-period)
195 (object2 anchored-period))
196 (time= (cut-off object1) (cut-off object2)))
197
198 ;; ...and
199
200 (defmethod period-intersection ((object1 anchored-period)
201 (object2 anchored-period))
202 (cond
203 ((disjoint object1 object2)
204 ;; if they don't overlap, return nil, not a negative-valued
205 ;; period
206 nil)
207 ((let* ((start (if (time> (onset object2) (onset object1))
208 (onset object2)
209 (onset object1)))
210 (duration (duration (time- (if (time> (cut-off object2) (cut-off object1))
211 (cut-off object1)
212 (cut-off object2))
213 start))))
214 (make-anchored-period (timepoint start) duration)))))
215
216
217
218