db.lisp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. (in-package :cl-user)
  2. (defpackage chad-music.db
  3. (:use :cl #:audio-streams #:alexandria #:chad-music.utils)
  4. (:export #:*standard-optimize-settings*
  5. #:*db*
  6. #:album #:id #:artist #:year #:album #:original-date #:publisher #:country #:genre #:type #:status #:mb-id #:track-count #:total-duration #:cover
  7. #:make-album #:album-id #:album-artist #:album-year #:album-album #:album-original-date #:album-publisher #:album-country
  8. #:album-genre #:album-type #:album-status #:album-mb-id #:album-track-count #:album-total-duration #:album-cover
  9. #:track #:no #:title #:part-of-set #:bit-rate #:is-vbr #:duration #:path
  10. #:make-track #:track-album #:track-artist #:track-no #:track-title #:track-part-of-set
  11. #:track-bit-rate #:track-is-vbr #:track-duration #:track-path
  12. #:entry #:track #:added #:modified #:present
  13. #:make-entry #:entry-track #:entry-added #:entry-modified #:entry-present
  14. #:get-album-id #:get-file-id #:rescan
  15. #:clear-track-no
  16. #:save-db #:load-db
  17. #:query-category #:query-tracks #:album-tracks))
  18. (in-package :chad-music.db)
  19. (defstruct db
  20. (albums (make-hash-table :test 'equal) :type hash-table)
  21. (tracks (make-hash-table :test 'equal) :type hash-table)
  22. (album-tracks (make-hash-table :test 'equal) :type hash-table))
  23. (defvar *db* (make-db) "Metadata database")
  24. (defstruct album
  25. id artist year album original-date publisher country
  26. genre type status mb-id track-count total-duration cover)
  27. (defstruct track
  28. id album-id artist no title part-of-set
  29. bit-rate is-vbr duration path)
  30. (defstruct entry track added modified present)
  31. (defun get-file-id (file)
  32. (crypto:byte-array-to-hex-string
  33. (crypto:digest-sequence :md5 (flex:string-to-octets
  34. (namestring file)
  35. :external-format :utf-8))))
  36. (defun get-album-id (artist year title)
  37. (crypto:byte-array-to-hex-string
  38. (crypto:digest-sequence :md5 (flex:string-to-octets
  39. (format nil "~A-~A-~A" artist year title)
  40. :external-format :utf-8))))
  41. (defun parse-file (file track-id)
  42. (declare #.*standard-optimize-settings*)
  43. (let ((it (open-audio-file file)))
  44. (when it
  45. (let* ((albums-db (db-albums *db*))
  46. (album-artist (or (abstract-tag:album-artist it)
  47. (abstract-tag:artist it)))
  48. (album-year (utils:awhen (or (abstract-tag::year it)
  49. (abstract-tag:original-date it))
  50. (parse-integer utils:it :junk-allowed t)))
  51. (album-title (abstract-tag:album it))
  52. (album-id (get-album-id album-artist album-year album-title)))
  53. (multiple-value-bind (album foundp)
  54. (gethash album-id albums-db)
  55. (unless foundp
  56. (setf album (make-album
  57. :id album-id
  58. :artist album-artist
  59. :year album-year
  60. :album album-title
  61. :original-date (abstract-tag:original-date it)
  62. :publisher (publisher it)
  63. :country (country it)
  64. :genre (abstract-tag::genre it)
  65. :type (utils:awhen (text-tag it "MusicBrainz Album Type") (string-downcase utils:it))
  66. :status (utils:awhen (text-tag it "MusicBrainz Album Status") (string-downcase utils:it))
  67. :mb-id (text-tag it "MusicBrainz Album Id"))
  68. (gethash album-id albums-db) album))
  69. (make-track
  70. :id track-id
  71. :album-id album-id
  72. :artist (abstract-tag:artist it)
  73. :no (abstract-tag::track it)
  74. :title (abstract-tag:title it)
  75. :part-of-set (abstract-tag::disk it)
  76. :bit-rate (bit-rate it)
  77. :is-vbr (is-vbr it)
  78. :duration (round (or (duration it) 0))
  79. :path file))))))
  80. (defun track-album (track)
  81. (gethash (track-album-id track)
  82. (db-albums *db*)))
  83. (defun rescan (paths)
  84. (declare #.*standard-optimize-settings*)
  85. (let ((added 0) (updated 0) (removed 0)
  86. (albums-db (db-albums *db*))
  87. (tracks-db (db-tracks *db*)))
  88. (declare (fixnum added updated removed))
  89. (loop for value being the hash-values in tracks-db
  90. do (setf (entry-present value) nil))
  91. (unless (listp paths)
  92. (setf paths (list paths)))
  93. (labels ((scan-file (file)
  94. (let ((file-id (get-file-id file)))
  95. (multiple-value-bind (entry foundp)
  96. (gethash file-id tracks-db)
  97. (let ((modified (file-write-date file)))
  98. (unless foundp
  99. (incf added)
  100. (setf entry (make-entry :added (get-universal-time))
  101. (gethash file-id tracks-db) entry))
  102. (unless (and foundp (= (the fixnum (entry-modified entry)) modified))
  103. (when foundp (incf updated))
  104. (setf (entry-track entry) (parse-file file file-id)
  105. (entry-modified entry) modified))
  106. (setf (entry-present entry) t))))))
  107. (dolist (dir paths)
  108. (cl-fad:walk-directory dir #'scan-file :follow-symlinks nil))
  109. (let ((album-stats (make-hash-table)))
  110. (loop for file being the hash-keys in tracks-db using (hash-value entry)
  111. do (if (entry-present entry)
  112. (when-let (track (entry-track entry))
  113. (let ((album (track-album track)))
  114. (multiple-value-bind (stats foundp) (gethash album album-stats)
  115. (unless foundp
  116. (setf stats (cons 0 0)
  117. (gethash album album-stats) stats)
  118. (unless (album-cover album)
  119. (setf (album-cover album)
  120. (probe-file (cl-fad:merge-pathnames-as-file
  121. (cl-fad:pathname-directory-pathname (track-path track))
  122. "cover.jpg")))))
  123. (incf (the fixnum (car stats))) ;; track-count
  124. (incf (the fixnum (cdr stats)) ;; total-duration
  125. (the fixnum (or (track-duration track) 0))))))
  126. (progn
  127. (incf removed)
  128. (remhash file tracks-db))))
  129. (loop for album-id being the hash-keys in albums-db using (hash-value album)
  130. for stats = (gethash album album-stats)
  131. do (if stats
  132. (setf (album-track-count album) (car stats)
  133. (album-total-duration album) (cdr stats))
  134. (remhash album-id albums-db))))
  135. (reindex-db)
  136. (values added updated removed))))
  137. (defun reindex-db ()
  138. (declare #.*standard-optimize-settings*)
  139. (let ((tracks-db (db-tracks *db*))
  140. (album-tracks-db (db-album-tracks *db*)))
  141. (clrhash album-tracks-db)
  142. (loop for entry being the hash-values in tracks-db
  143. for track = (entry-track entry)
  144. when track
  145. do (push track (gethash (track-album-id track) album-tracks-db)))
  146. (loop for album-id being the hash-keys in album-tracks-db using (hash-value tracks)
  147. do (setf (gethash album-id album-tracks-db)
  148. (sort tracks 'track<)))))
  149. (defun clear-track-no (track-no)
  150. (when track-no
  151. (parse-integer (if (consp track-no)
  152. (car track-no)
  153. track-no)
  154. :junk-allowed t)))
  155. (defparameter +album-type-order+ '("album" "lp" "ep" "single" "compilation" "live" "soundtrack"
  156. "spokenword" "remix" "mixed" "dj-mix" "mixtape" "broadcast")
  157. "Half-arbitrary album type order")
  158. (defun gen-comparator (slots)
  159. (named-lambda info<> (a b)
  160. (declare #.*standard-optimize-settings*)
  161. (dolist (slot slots 0)
  162. (let ((slot-a (slot-value a slot))
  163. (slot-b (slot-value b slot)))
  164. (when (xor (null slot-a) (null slot-b))
  165. (return-from info<> (if (null slot-b) 1 -1)))
  166. (case slot
  167. (type
  168. (setf slot-a (or (position slot-a (the list +album-type-order+) :test 'string-equal) 0)
  169. slot-b (or (position slot-b (the list +album-type-order+) :test 'string-equal) 0)))
  170. (no
  171. (setf slot-a (clear-track-no slot-a)
  172. slot-b (clear-track-no slot-b))))
  173. (unless (or (and (null slot-a) (null slot-b))
  174. (case slot
  175. ((type year no) (= (the fixnum slot-a) (the fixnum slot-b)))
  176. (t (string-equal slot-a slot-b))))
  177. (return-from info<> (case slot
  178. ((type year no) (- (the fixnum slot-a) (the fixnum slot-b)))
  179. (t (if (string< slot-a slot-b) -1 1)))))))))
  180. (defparameter +album<>+ (gen-comparator '(artist type original-date year album)))
  181. (defun album< (a b)
  182. (< (funcall +album<>+ a b) 0))
  183. (defparameter +track<>+ (gen-comparator '(no title)))
  184. (defun track< (a b)
  185. (let ((albs (funcall +album<>+ (track-album a) (track-album b))))
  186. (if (zerop albs)
  187. (< (funcall +track<>+ a b) 0)
  188. albs)))
  189. (defun match-filter (data category filter)
  190. (declare #.*standard-optimize-settings*)
  191. (or (null filter)
  192. (let ((words (split-sequence:split-sequence #\Space filter)))
  193. (every #'(lambda (word)
  194. (declare (simple-string word))
  195. (case category
  196. (album (or (search word (the simple-string (album-album data)) :test 'char-equal)
  197. (search word (the simple-string (album-artist data)) :test 'char-equal)
  198. (and (album-publisher data)
  199. (search word (the simple-string (album-publisher data)) :test 'char-equal))
  200. (and (album-country data)
  201. (search word (the simple-string (album-country data)) :test 'char-equal))))
  202. (year (or (and (album-year data)
  203. (search word (princ-to-string (the fixnum (album-year data))) :test 'char-equal))
  204. (and (album-original-date data)
  205. (search word (the simple-string (album-original-date data)) :test 'char-equal))))
  206. (t (and (slot-value data category)
  207. (search word (the simple-string (slot-value data category)) :test 'char-equal)))))
  208. words))))
  209. (defun match-restrictions (data restrictions)
  210. (declare #.*standard-optimize-settings*)
  211. (every #'(lambda (r)
  212. (equal (slot-value data (car r)) (cdr r)))
  213. restrictions))
  214. (defun query-category (category &key filter restrictions limit (offset 0) count-only)
  215. (declare #.*standard-optimize-settings*
  216. (type (or null fixnum) limit offset))
  217. (let ((albums-db (db-albums *db*))
  218. (results (make-hash-table :test (case category
  219. (album 'eq)
  220. (t 'equal)))))
  221. (loop for data being the hash-value of albums-db
  222. for result = (case category
  223. (album data)
  224. (t (slot-value data category)))
  225. when (and
  226. result
  227. (match-restrictions data restrictions)
  228. (match-filter data category filter))
  229. do (incf (the fixnum (gethash result results 0))))
  230. (let* ((total (hash-table-count results))
  231. (start (min total offset))
  232. (end (min total (+ offset limit))))
  233. (if count-only total
  234. (subseq (the list (sort (case category
  235. (album (hash-table-keys results))
  236. (t (hash-table-alist results)))
  237. (case category
  238. (album #'album<)
  239. (year #'<)
  240. (t #'string<))
  241. :key (case category
  242. (album nil)
  243. (t #'car))))
  244. start end)))))
  245. (defun query-tracks (&key filter restrictions limit (offset 0))
  246. (declare #.*standard-optimize-settings*
  247. (type (or null fixnum) limit offset))
  248. (let ((tracks-db (db-tracks *db*)) results)
  249. (loop for entry being the hash-value of tracks-db
  250. for track = (entry-track entry)
  251. when (and track
  252. (match-restrictions (track-album track) restrictions)
  253. (match-filter track 'title filter))
  254. do (push track results))
  255. (let* ((total (length results))
  256. (start (min total offset))
  257. (end (min total (+ offset limit))))
  258. (subseq (sort results
  259. #'track<)
  260. start end))))
  261. (defun album-tracks (album-id)
  262. (gethash album-id (db-album-tracks *db*)))
  263. (defun save-db (file-name)
  264. (declare #.*standard-optimize-settings*)
  265. (with-output-to-file (s file-name :if-exists :supersede)
  266. (labels ((print-values (table)
  267. (loop for value being the hash-value of table
  268. do (print value s))))
  269. (print-values (db-albums *db*))
  270. (print-values (db-tracks *db*)))))
  271. (defun load-db (file-name)
  272. (declare #.*standard-optimize-settings*)
  273. (with-input-from-file (s file-name)
  274. (let ((albums (make-hash-table :test 'equal))
  275. (entries (make-hash-table :test 'equal)))
  276. (loop for value = (read s nil)
  277. while value
  278. do (etypecase value
  279. (album (setf (gethash (album-id value) albums) value))
  280. (entry (when (entry-track value)
  281. (setf (gethash (track-id (entry-track value)) entries) value)))))
  282. (let ((*db* (make-db :albums albums :tracks entries)))
  283. (reindex-db)
  284. *db*))))