db.lisp 14 KB

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