1
0

db.lisp 15 KB

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