db.lisp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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-id #: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 (paths)
  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 (let ((track (entry-track value)))
  97. (when (and track
  98. (some (lambda (dir)
  99. (starts-with-subseq (namestring dir)
  100. (namestring (track-path track))))
  101. paths))
  102. (setf (entry-present value) nil))))
  103. (unless (listp paths)
  104. (setf paths (list paths)))
  105. (labels ((scan-file (file)
  106. (let ((file-id (get-file-id file)))
  107. (multiple-value-bind (entry foundp)
  108. (gethash file-id tracks-db)
  109. (let ((modified (file-write-date file)))
  110. (unless foundp
  111. (incf added)
  112. (setf entry (make-entry :added (get-universal-time))
  113. (gethash file-id tracks-db) entry))
  114. (unless (and foundp (= (the fixnum (entry-modified entry)) modified))
  115. (when foundp (incf updated))
  116. (setf (entry-track entry) (parse-file file file-id)
  117. (entry-modified entry) modified))
  118. (setf (entry-present entry) t))))))
  119. (dolist (dir paths)
  120. (cl-fad:walk-directory dir #'scan-file :follow-symlinks nil))
  121. (let ((album-stats (make-hash-table)))
  122. (loop for file being the hash-keys in tracks-db using (hash-value entry)
  123. do (if (entry-present entry)
  124. (when-let (track (entry-track entry))
  125. (let ((album (track-album track)))
  126. (multiple-value-bind (stats foundp) (gethash album album-stats)
  127. (unless foundp
  128. (setf stats (cons 0 0)
  129. (gethash album album-stats) stats)
  130. (unless (album-cover album)
  131. (setf (album-cover album)
  132. (probe-file (cl-fad:merge-pathnames-as-file
  133. (cl-fad:pathname-directory-pathname (track-path track))
  134. "cover.jpg")))))
  135. (incf (the fixnum (car stats))) ;; track-count
  136. (incf (the fixnum (cdr stats)) ;; total-duration
  137. (the fixnum (or (track-duration track) 0))))))
  138. (progn
  139. (incf removed)
  140. (remhash file tracks-db))))
  141. (loop for album-id being the hash-keys in albums-db using (hash-value album)
  142. for stats = (gethash album album-stats)
  143. do (if stats
  144. (setf (album-track-count album) (car stats)
  145. (album-total-duration album) (cdr stats))
  146. (remhash album-id albums-db))))
  147. (reindex-db)
  148. (values added updated removed))))
  149. (defun reindex-db ()
  150. (declare #.*standard-optimize-settings*)
  151. (let ((tracks-db (db-tracks *db*))
  152. (album-tracks-db (db-album-tracks *db*))
  153. (album-added-db (db-album-added *db*)))
  154. (clrhash album-tracks-db)
  155. (clrhash album-added-db)
  156. (loop for entry being the hash-values in tracks-db
  157. for track = (entry-track entry)
  158. when track
  159. do (setf (gethash (track-album-id track) album-added-db) (entry-added entry))
  160. and do (push track (gethash (track-album-id track) album-tracks-db)))
  161. (loop for album-id being the hash-keys in album-tracks-db using (hash-value tracks)
  162. do (setf (gethash album-id album-tracks-db)
  163. (sort tracks 'track<)))))
  164. (defun clear-track-no (track-no)
  165. (when track-no
  166. (let ((track (if (consp track-no)
  167. (car track-no)
  168. track-no)))
  169. (etypecase track
  170. (null nil)
  171. (integer track)
  172. (string (parse-integer track :junk-allowed t))))))
  173. (defun slot< (a b)
  174. (declare #.*standard-optimize-settings*
  175. (type simple-string a b))
  176. (when (xor (emptyp a) (emptyp b))
  177. (return-from slot< (when (emptyp b) 0)))
  178. (let ((aa (alpha-char-p (elt a 0)))
  179. (ba (alpha-char-p (elt b 0))))
  180. (cond
  181. ((and aa ba) (string< a b))
  182. (aa 0)
  183. (ba)
  184. (t (string< a b)))))
  185. (defparameter +album-type-order+ '("album" "lp" "ep" "single" "compilation" "live" "soundtrack"
  186. "spokenword" "remix" "mixed" "dj-mix" "mixtape" "broadcast")
  187. "Half-arbitrary album type order")
  188. (defun gen-comparator (slots)
  189. (named-lambda info<> (a b)
  190. (declare #.*standard-optimize-settings*)
  191. (dolist (slot slots 0)
  192. (let ((slot-a (slot-value a slot))
  193. (slot-b (slot-value b slot)))
  194. (when (xor (null slot-a) (null slot-b))
  195. (return-from info<> (if (null slot-b) -1 1)))
  196. (unless (and (null slot-a) (null slot-b))
  197. (case slot
  198. (type
  199. (let ((cmp (if (slot< slot-a slot-b) -1 1)))
  200. (setf slot-a (or (position slot-a (the list +album-type-order+) :test 'string-equal)
  201. (+ 100 cmp))
  202. slot-b (or (position slot-b (the list +album-type-order+) :test 'string-equal)
  203. (- 100 cmp)))))
  204. (no
  205. (setf slot-a (clear-track-no slot-a)
  206. slot-b (clear-track-no slot-b))))
  207. (unless (case slot
  208. ((type year no) (= (the fixnum slot-a) (the fixnum slot-b)))
  209. (t (string-equal slot-a slot-b)))
  210. (return-from info<> (case slot
  211. ((type year no) (- (the fixnum slot-a) (the fixnum slot-b)))
  212. (t (if (slot< slot-a slot-b) -1 1))))))))))
  213. (defparameter +album<>+ (gen-comparator '(artist type year album)))
  214. (defun album< (a b)
  215. (declare #.*standard-optimize-settings*
  216. (type function +album<>+))
  217. (< (the fixnum (funcall +album<>+ a b)) 0))
  218. (defun album-added> (a b)
  219. (declare #.*standard-optimize-settings*
  220. (type album a b))
  221. (let ((album-added-db (db-album-added *db*)))
  222. (> (the fixnum (gethash (album-id a) album-added-db))
  223. (the fixnum (gethash (album-id b) album-added-db)))))
  224. (defparameter +track<>+ (gen-comparator '(no title)))
  225. (defun track< (a b)
  226. (declare #.*standard-optimize-settings*
  227. (type function +album<>+ +track<>+))
  228. (let ((albs (the fixnum (funcall +album<>+ (track-album a) (track-album b)))))
  229. (if (zerop albs)
  230. (< (the fixnum (funcall +track<>+ a b)) 0)
  231. albs)))
  232. (defun match-filter (data category filter)
  233. (declare #.*standard-optimize-settings*)
  234. (or (null filter)
  235. (let ((words (split-sequence:split-sequence #\Space filter)))
  236. (every #'(lambda (word)
  237. (case category
  238. (album (or (search word (album-album data) :test 'char-equal)
  239. (search word (album-artist data) :test 'char-equal)
  240. (and (album-publisher data)
  241. (search word (album-publisher data) :test 'char-equal))
  242. (and (album-country data)
  243. (search word (album-country data) :test 'char-equal))))
  244. (year (or (and (album-year data)
  245. (search word (princ-to-string (the fixnum (album-year data))) :test 'char-equal))
  246. (and (album-original-date data)
  247. (search word (album-original-date data) :test 'char-equal))))
  248. (t (and (slot-value data category)
  249. (search word (slot-value data category) :test 'char-equal)))))
  250. words))))
  251. (defun match-restrictions (data restrictions)
  252. (declare #.*standard-optimize-settings*)
  253. (every #'(lambda (r)
  254. (equal (slot-value data (car r)) (cdr r)))
  255. restrictions))
  256. (defun query-category (category &key filter restrictions limit (offset 0) count-only latest)
  257. (declare #.*standard-optimize-settings*
  258. (type (or null fixnum) limit offset))
  259. (let ((albums-db (db-albums *db*))
  260. (results (make-hash-table :test (case category
  261. (album 'eq)
  262. (t 'equal)))))
  263. (loop for data being the hash-value of albums-db
  264. for result = (case category
  265. (album data)
  266. (t (slot-value data category)))
  267. when (and
  268. result
  269. (match-restrictions data restrictions)
  270. (match-filter data category filter))
  271. do (incf (the fixnum (gethash result results 0))))
  272. (let* ((total (hash-table-count results))
  273. (start (min total offset))
  274. (end (min total (+ offset limit))))
  275. (if count-only total
  276. (subseq (the list (sort (case category
  277. (album (hash-table-keys results))
  278. (t (hash-table-alist results)))
  279. (case category
  280. (album (if latest #'album-added> #'album<))
  281. (year #'<)
  282. (t #'slot<))
  283. :key (case category
  284. (album nil)
  285. (t #'car))))
  286. start end)))))
  287. (defun query-tracks (&key filter restrictions limit (offset 0))
  288. (declare #.*standard-optimize-settings*
  289. (type (or null fixnum) limit offset))
  290. (let ((tracks-db (db-tracks *db*)) results)
  291. (loop for entry being the hash-value of tracks-db
  292. for track = (entry-track entry)
  293. when (and track
  294. (match-restrictions (track-album track) restrictions)
  295. (match-filter track 'title filter))
  296. do (push track results))
  297. (let* ((total (length results))
  298. (start (min total offset))
  299. (end (min total (+ offset limit))))
  300. (subseq (sort results
  301. #'track<)
  302. start end))))
  303. (defun album-tracks (album-id)
  304. (gethash album-id (db-album-tracks *db*)))
  305. (defun db-stats ()
  306. (declare #.*standard-optimize-settings*)
  307. (let ((albums (db-albums *db*))
  308. (artists (make-hash-table :test 'equal)))
  309. (loop for entry being the hash-values in albums
  310. summing (album-total-duration entry) into total-duration
  311. summing (album-track-count entry) into total-tracks
  312. counting t into total-albums
  313. do (setf (gethash (album-artist entry) artists) t)
  314. finally (return (list :|artists| (hash-table-count artists)
  315. :|albums| total-albums
  316. :|tracks| total-tracks
  317. :|duration| total-duration)))))
  318. (defun save-db (&optional (file-name *db-path*))
  319. (declare #.*standard-optimize-settings*)
  320. (with-output-to-file (s file-name :if-exists :supersede)
  321. (labels ((print-values (table)
  322. (loop for value being the hash-value of table
  323. do (print value s))))
  324. (print-values (db-albums *db*))
  325. (print-values (db-tracks *db*)))))
  326. (defun load-db (&optional (file-name *db-path*))
  327. (declare #.*standard-optimize-settings*)
  328. (with-input-from-file (s file-name)
  329. (let ((albums (make-hash-table :test 'equal))
  330. (entries (make-hash-table :test 'equal)))
  331. (loop for value = (read s nil)
  332. while value
  333. do (etypecase value
  334. (album (setf (gethash (album-id value) albums) value))
  335. (entry (when (entry-track value)
  336. (setf (gethash (track-id (entry-track value)) entries) value)))))
  337. (let ((*db* (make-db :albums albums :tracks entries)))
  338. (reindex-db)
  339. *db*))))