comparison implementations/midi-db/db-select-functions.lisp @ 236:a5d065905f6d

Add midi-db. Ignore-this: c6f4fc32efa4453ddbdc478793eedd52 A basic implementation for working with MIDI files stored in the database. It is a test case for `versioned' data, but only partially implemented at the moment. darcs-hash:20100223152703-16a00-4388d2720907d777a1c6c6b3a010885ce0fe06a7.gz committer: Jamie Forth <j.forth@gold.ac.uk>
author j.forth <j.forth@gold.ac.uk>
date Thu, 24 Feb 2011 11:23:18 +0000
parents
children 2138ea478adb
comparison
equal deleted inserted replaced
235:ea45a3d0730c 236:a5d065905f6d
1 (cl:in-package #:amuse-midi-db)
2
3 (defmethod get-composition ((identifier midi-db-composition-identifier))
4 (let ((database *amuse-database*)) ; Shaddow this to use a different
5 ; database (for debugging).
6 (destructuring-bind (collection-id filename timebase start
7 duration owner version
8 creation-timestamp
9 deletion-timestamp)
10 (%get-midi-db-composition-header identifier database)
11 (let* ((collection-identifier (make-midi-db-collection-identifier
12 collection-id))
13 (events (%get-midi-db-events identifier
14 collection-identifier
15 version timebase database))
16 (tempi (%get-midi-db-tempi identifier
17 collection-identifier version
18 timebase database))
19 (timesigs (%get-midi-db-timesigs identifier
20 collection-identifier
21 version timebase database))
22 (keysigs (%get-midi-db-keysigs identifier
23 collection-identifier
24 version timebase database)))
25 (make-midi-db-composition events
26 (/ start timebase)
27 (/ duration timebase)
28 tempi
29 timesigs
30 keysigs
31 identifier
32 collection-identifier
33 timebase
34 filename
35 owner
36 version
37 creation-timestamp
38 deletion-timestamp)))))
39
40
41 (defun %get-all-collection-headers ()
42 #.(clsql:locally-enable-sql-reader-syntax)
43 (let ((collection-rows (clsql:select [collection-id]
44 [collection-name] [description]
45 :from "midi_db_collections"
46 :flatp t
47 :database *amuse-database*)))
48 #.(clsql:locally-disable-sql-reader-syntax) collection-rows))
49
50 (defun %get-midi-db-composition-header (identifier database)
51 "Basic low-level retrieval of constituents. Just takes an identifier
52 and returns a header without any checking of version or deletion
53 fields."
54 #.(clsql:locally-enable-sql-reader-syntax)
55 (let ((header-row (car (clsql:select
56 [collection-id]
57 [filename]
58 [timebase]
59 [start]
60 [duration]
61 [owner]
62 [version]
63 [creation-timestamp]
64 [deletion-timestamp]
65 :from "midi_db_compositions"
66 :where [= [composition-id]
67 (composition-id identifier)]
68 :flatp t
69 :database database))))
70 #.(clsql:locally-disable-sql-reader-syntax)
71 header-row))
72
73 (defun %get-all-collection-composition-headers (collection-identifier)
74 #.(clsql:locally-enable-sql-reader-syntax)
75 (let ((header-rows (clsql:select
76 [collection-id]
77 [filename]
78 [timebase]
79 [start]
80 [duration]
81 [owner]
82 [version]
83 [creation-timestamp]
84 [deletion-timestamp]
85 :from "midi_db_compositions"
86 :where [= [collection-id] (collection-id
87 collection-identifier)]
88 :flatp t
89 :database *amuse-database*)))
90 #.(clsql:locally-disable-sql-reader-syntax)
91 header-rows))
92
93 (defun %get-all-composition-headers ()
94 #.(clsql:locally-enable-sql-reader-syntax)
95 (let ((header-rows (clsql:select
96 [collection-id]
97 [filename]
98 [timebase]
99 [start]
100 [duration]
101 [owner]
102 [version]
103 [creation-timestamp]
104 [deletion-timestamp]
105 :from "midi_db_compositions"
106 :flatp t
107 :database *amuse-database*)))
108 #.(clsql:locally-disable-sql-reader-syntax)
109 header-rows))
110
111 (defun %get-midi-db-events (identifier collection-identifier version
112 timebase database)
113 (let ((event-rows (clsql:query (concatenate 'string "
114 SELECT event_id, track, channel, patch, pitch, velocity, start, duration,
115 version
116 FROM midi_db_events
117 WHERE collection_id = " (princ-to-string (collection-id collection-identifier)) "
118 AND composition_id= " (princ-to-string (composition-id identifier)) "
119 AND version = " (princ-to-string version))
120 :flatp t
121 :database database)))
122 (%init-events identifier collection-identifier event-rows timebase)))
123
124 (defun %init-events (identifier collection-identifier event-rows timebase)
125 (loop for event-row in event-rows
126 collecting (init-midi-db-event identifier collection-identifier event-row timebase)
127 into events
128 finally (return events)))
129
130 (defun init-midi-db-event (identifier collection-identifier event-row timebase)
131 (destructuring-bind (event-id track channel patch pitch velocity
132 start duration version)
133 event-row
134 (if (= channel 10)
135 (make-midi-db-percussive-event (collection-id collection-identifier)
136 (composition-id identifier)
137 event-id track channel patch
138 pitch velocity
139 (/ start timebase)
140 (/ duration timebase) version)
141 (make-midi-db-pitched-event (collection-id collection-identifier)
142 (composition-id identifier)
143 event-id track channel patch pitch
144 velocity (/ start timebase)
145 (/ duration timebase) version))))
146
147 (defun %get-midi-db-tempi (identifier collection-identifier version
148 timebase database)
149 (declare (ignore version))
150 (let ((tempo-rows (clsql:query (concatenate 'string "
151 SELECT start, duration, microsecs_per_crotchet, version
152 FROM midi_db_tempi
153 WHERE collection_id = " (princ-to-string (collection-id collection-identifier)) "
154 AND composition_id = " (princ-to-string (composition-id identifier)))
155 ;AND version = " (princ-to-string version))
156 :flatp t
157 :database database)))
158 (%init-midi-db-tempi tempo-rows timebase)))
159
160 (defun %init-midi-db-tempi (tempo-rows timebase)
161 (loop for tempo-row in tempo-rows
162 collecting (init-midi-db-tempo tempo-row timebase)
163 into tempi
164 finally (return tempi)))
165
166 (defun init-midi-db-tempo (tempo-row timebase)
167 (destructuring-bind (start duration microsecs-per-crotchet version)
168 tempo-row
169 (make-midi-db-tempo (/ start timebase) (/ duration timebase)
170 microsecs-per-crotchet version)))
171
172 (defun %get-midi-db-timesigs (identifier collection-identifier version
173 timebase database)
174 (declare (ignore version))
175 (let ((timesig-rows (clsql:query (concatenate 'string "
176 SELECT start, duration, numerator, denominator, version
177 FROM midi_db_timesigs
178 WHERE collection_id = " (princ-to-string (collection-id collection-identifier)) "
179 AND composition_id = " (princ-to-string (composition-id identifier)))
180 ;AND version = " (princ-to-string version))
181 :flatp t
182 :database database)))
183 (%init-midi-db-timesigs timesig-rows timebase)))
184
185 (defun %init-midi-db-timesigs (timesig-rows timebase)
186 (loop for timesig-row in timesig-rows
187 collecting (init-midi-db-timesig timesig-row timebase)
188 into timesigs
189 finally (return timesigs)))
190
191 (defun init-midi-db-timesig (timesig-row timebase)
192 (destructuring-bind (start duration numerator denominator version)
193 timesig-row
194 (make-midi-db-timesig (/ start timebase) (/ duration timebase)
195 numerator denominator version)))
196
197 (defun %get-midi-db-keysigs (identifier collection-identifier version
198 timebase database)
199 (declare (ignore version))
200 (let ((keysig-rows (clsql:query (concatenate 'string "
201 SELECT start, duration, mode, sharp_count, version
202 FROM midi_db_keysigs
203 WHERE collection_id = " (princ-to-string (collection-id collection-identifier)) "
204 AND composition_id = " (princ-to-string (composition-id identifier)))
205 ;AND version = " (princ-to-string version))
206 :flatp t
207 :database database)))
208 (%init-midi-db-keysigs keysig-rows timebase)))
209
210 (defun %init-midi-db-keysigs (keysig-rows timebase)
211 (loop for keysig-row in keysig-rows
212 collecting (init-midi-db-keysig keysig-row timebase)
213 into keysigs
214 finally (return keysigs)))
215
216 (defun init-midi-db-keysig (keysig-row timebase)
217 (destructuring-bind (start duration mode sharp-count version)
218 keysig-row
219 (make-midi-db-keysig (/ start timebase) (/ duration timebase)
220 mode sharp-count version)))