annotate implementations/midi-db/db-select-functions.lisp @ 310:f99fd6a7bbfc

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