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