1
0

db.lisp 16 KB

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