db.lisp 11 KB

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