mp3-tag.lisp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. ;;; -*- Mode: Lisp; show-trailing-whitespace: t; Base: 10; indent-tabs: nil; Syntax: ANSI-Common-Lisp; Package: MP3-TAG; -*-
  2. ;;; Copyright (c) 2013, Mark VandenBrink. All rights reserved.
  3. (in-package #:mp3-tag)
  4. (defparameter *id3v1-genres*
  5. #("Blues"
  6. "Classic Rock"
  7. "Country"
  8. "Dance"
  9. "Disco"
  10. "Funk"
  11. "Grunge"
  12. "Hip-Hop"
  13. "Jazz"
  14. "Metal"
  15. "New Age"
  16. "Oldies"
  17. "Other"
  18. "Pop"
  19. "R&B"
  20. "Rap"
  21. "Reggae"
  22. "Rock"
  23. "Techno"
  24. "Industrial"
  25. "Alternative"
  26. "Ska"
  27. "Death Metal"
  28. "Pranks"
  29. "Soundtrack"
  30. "Euro-Techno"
  31. "Ambient"
  32. "Trip-Hop"
  33. "Vocal"
  34. "Jazz+Funk"
  35. "Fusion"
  36. "Trance"
  37. "Classical"
  38. "Instrumental"
  39. "Acid"
  40. "House"
  41. "Game"
  42. "Sound Clip"
  43. "Gospel"
  44. "Noise"
  45. "Alternative Rock"
  46. "Bass"
  47. "Soul"
  48. "Punk"
  49. "Space"
  50. "Meditative"
  51. "Instrumental Pop"
  52. "Instrumental Rock"
  53. "Ethnic"
  54. "Gothic"
  55. "Darkwave"
  56. "Techno-Industrial"
  57. "Electronic"
  58. "Pop-Folk"
  59. "Eurodance"
  60. "Dream"
  61. "Southern Rock"
  62. "Comedy"
  63. "Cult"
  64. "Gangsta"
  65. "Top 40"
  66. "Christian Rap"
  67. "Pop/Funk"
  68. "Jungle"
  69. "Native American"
  70. "Cabaret"
  71. "New Wave"
  72. "Psychedelic"
  73. "Rave"
  74. "Showtunes"
  75. "Trailer"
  76. "Lo-Fi"
  77. "Tribal"
  78. "Acid Punk"
  79. "Acid Jazz"
  80. "Polka"
  81. "Retro"
  82. "Musical"
  83. "Rock & Roll"
  84. "Hard Rock"
  85. "Folk"
  86. "Folk/Rock"
  87. "National Folk"
  88. "Swing"
  89. "Fusion"
  90. "Bebob"
  91. "Latin"
  92. "Revival"
  93. "Celtic"
  94. "Bluegrass"
  95. "Avantgarde"
  96. "Gothic Rock"
  97. "Progressive Rock"
  98. "Psychedelic Rock"
  99. "Symphonic Rock"
  100. "Slow Rock"
  101. "Big Band"
  102. "Chorus"
  103. "Easy Listening"
  104. "Acoustic"
  105. "Humour"
  106. "Speech"
  107. "Chanson"
  108. "Opera"
  109. "Chamber Music"
  110. "Sonata"
  111. "Symphony"
  112. "Booty Bass"
  113. "Primus"
  114. "Porn Groove"
  115. "Satire"
  116. "Slow Jam"
  117. "Club"
  118. "Tango"
  119. "Samba"
  120. "Folklore"
  121. "Ballad"
  122. "Power Ballad"
  123. "Rhythmic Soul"
  124. "Freestyle"
  125. "Duet"
  126. "Punk Rock"
  127. "Drum Solo"
  128. "A Cappella"
  129. "Euro-House"
  130. "Dance Hall"
  131. "Goa"
  132. "Drum & Bass"
  133. "Club-House"
  134. "Hardcore"
  135. "Terror"
  136. "Indie"
  137. "BritPop"
  138. "Negerpunk"
  139. "Polsk Punk"
  140. "Beat"
  141. "Christian Gangsta Rap"
  142. "Heavy Metal"
  143. "Black Metal"
  144. "Crossover"
  145. "Contemporary Christian"
  146. "Christian Rock"
  147. "Merengue"
  148. "Salsa"
  149. "Thrash Metal"
  150. "Anime"
  151. "Jpop"
  152. "Synthpop"))
  153. (defun get-id3v1-genre (n)
  154. (let ((idx (- n 1))) ; arrays are zero-based
  155. (if (or (> idx (length *id3v1-genres*))
  156. (< idx 0))
  157. "BAD GENRE!?!?!?"
  158. (aref *id3v1-genres* idx))))
  159. (defun get-frames (stream names)
  160. (let (found-frames)
  161. (mp3-map-frames stream
  162. :func (lambda (f)
  163. (when (member (id f) names :test #'string=)
  164. (push f found-frames))))
  165. found-frames))
  166. (defmethod album ((me mp3-file-stream))
  167. (let ((frames (get-frames me '("TAL" "TALB"))))
  168. (when frames
  169. (assert (= 1 (length frames)) () "There can be only one album tag")
  170. (return-from album (info (first frames)))))
  171. (if (v21-tag-header (mp3-header me))
  172. (album (v21-tag-header (mp3-header me)))
  173. nil))
  174. (defmethod artist ((me mp3-file-stream))
  175. (let ((frames (get-frames me '("TP1" "TPE1"))))
  176. (when frames
  177. (assert (= 1 (length frames)) () "There can be only one artist tag")
  178. (return-from artist (info (first frames)))))
  179. (if (v21-tag-header (mp3-header me))
  180. (artist (v21-tag-header (mp3-header me)))
  181. nil))
  182. (defmethod comment ((me mp3-file-stream))
  183. (let ((frames (get-frames me '("COM" "COMM"))))
  184. (when frames
  185. (let ((new-frames))
  186. (dolist (f frames)
  187. (push (list (encoding f) (lang f) (desc f) (val f)) new-frames))
  188. (return-from comment new-frames))))
  189. (if (v21-tag-header (mp3-header me))
  190. (comment (v21-tag-header (mp3-header me)))
  191. nil))
  192. (defmethod year ((me mp3-file-stream))
  193. (let ((frames (get-frames me '("TRD" "TDRC"))))
  194. (when frames
  195. (assert (= 1 (length frames)) () "There can be only one year tag")
  196. (return-from year (info (first frames)))))
  197. (if (v21-tag-header (mp3-header me))
  198. (year (v21-tag-header (mp3-header me)))
  199. nil))
  200. (defmethod title ((me mp3-file-stream))
  201. (let ((frames (get-frames me '("TT2" "TIT2"))))
  202. (when frames
  203. (assert (= 1 (length frames)) () "There can be only one title tag")
  204. (return-from title (info (first frames)))))
  205. (if (v21-tag-header (mp3-header me))
  206. (title (v21-tag-header (mp3-header me)))
  207. nil))
  208. (defmethod genre ((me mp3-file-stream))
  209. (let ((frames (get-frames me '("TCO" "TCON"))))
  210. (when frames
  211. (assert (= 1 (length frames)) () "There can be only one genre tag")
  212. (let ((count)
  213. (end)
  214. (str (info (first frames))))
  215. (when (and (>= (length str) 1) (eq #\( (aref str 0)))
  216. (setf count (count #\( str))
  217. (when (> count 1) (warn "Don't support genre refinement yet, found ~d genres" count))
  218. (setf end (position #\) str))
  219. (when (null end) (warn "Bad format for genre, ending paren is missing"))
  220. (setf str (get-id3v1-genre (parse-integer (subseq str 1 end)))))
  221. (return-from genre str))))
  222. (if (v21-tag-header (mp3-header me))
  223. (get-id3v1-genre (genre (v21-tag-header (mp3-header me))))
  224. nil))
  225. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; no V2.1 tags for any of these ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  226. (defmethod album-artist ((me mp3-file-stream))
  227. (let ((frames (get-frames me '("TP2" "TPE2"))))
  228. (when frames
  229. (assert (= 1 (length frames)) () "There can be only one album-artist tag")
  230. (return-from album-artist (info (first frames)))))
  231. nil)
  232. (defmethod composer ((me mp3-file-stream))
  233. (let ((frames (get-frames me '("TCM" "TCOM"))))
  234. (when frames
  235. (assert (= 1 (length frames)) () "There can be only one composer tag")
  236. (return-from composer (info (first frames)))))
  237. nil)
  238. (defmethod copyright ((me mp3-file-stream))
  239. (let ((frames (get-frames me '("TCR" "TCOP"))))
  240. (when frames
  241. (assert (= 1 (length frames)) () "There can be only one copyright tag")
  242. (return-from copyright (info (first frames)))))
  243. nil)
  244. (defmethod encoder ((me mp3-file-stream))
  245. (let ((frames (get-frames me '("TEN" "TENC"))))
  246. (when frames
  247. (assert (= 1 (length frames)) () "There can be only one encoder tag")
  248. (return-from encoder (info (first frames)))))
  249. nil)
  250. (defmethod groups ((me mp3-file-stream))
  251. (let ((frames (get-frames me '("TT1" "TTE1"))))
  252. (when frames
  253. (assert (= 1 (length frames)) () "There can be only one group tag")
  254. (return-from groups (info (first frames)))))
  255. nil)
  256. (defmethod lyrics ((me mp3-file-stream))
  257. (let ((frames (get-frames me '("ULT" "USLT"))))
  258. (when frames
  259. (assert (= 1 (length frames)) () "There can be only one lyrics tag")
  260. (return-from lyrics (val (first frames)))))
  261. nil)
  262. (defmethod purchased-date ((me mp3-file-stream)) "NIY")
  263. (defmethod tool ((me mp3-file-stream)) "NIY")
  264. (defmethod writer ((me mp3-file-stream))
  265. (let ((frames (get-frames me '("TCM" "TCOM"))))
  266. (when frames
  267. (assert (= 1 (length frames)) () "There can be only one composer tag")
  268. (return-from writer (info (first frames)))))
  269. nil)
  270. (defmethod compilation ((me mp3-file-stream))
  271. (let ((frames (get-frames me '("TCMP"))))
  272. (when frames
  273. (assert (= 1 (length frames)) () "There can be only one compilation tag")
  274. (let ((str (info (first frames))))
  275. (return-from compilation (if str 1 0)))))
  276. nil)
  277. (defmethod disk ((me mp3-file-stream))
  278. (let ((frames (get-frames me '("TPA" "TPOS"))))
  279. (when frames
  280. (assert (= 1 (length frames)) () "There can be only one disk number tag")
  281. (return-from disk (mk-lst (info (first frames))))))
  282. nil)
  283. (defmethod tempo ((me mp3-file-stream))
  284. (let ((frames (get-frames me '("TBP" "TBPM"))))
  285. (when frames
  286. (assert (= 1 (length frames)) () "There can be only one tempo tag")
  287. (return-from tempo (info (first frames)))))
  288. nil)
  289. (defun mk-lst (str)
  290. (let ((pos (position #\/ str)))
  291. (if (null pos)
  292. (list str)
  293. (list (subseq str 0 pos) (subseq str (+ 1 pos))))))
  294. (defmethod track ((me mp3-file-stream))
  295. (let ((frames (get-frames me '("TRK" "TRCK"))))
  296. (when frames
  297. (assert (= 1 (length frames)) () "There can be only one track number tag")
  298. (return-from track (mk-lst (info (first frames))))))
  299. nil)
  300. (defmethod show-tags ((me mp3-file-stream) &key (raw nil))
  301. "Show the tags for an mp3-file. If RAW is non-nil, dump all the frames; else, print out a subset."
  302. (if raw
  303. (format t "~a:~a~%" (stream-filename me) (with-output-to-string (s) (mp3-frame:vpprint (audio-streams:mp3-header me) s)))
  304. (let ((album (album me))
  305. (album-artist (album-artist me))
  306. (artist (artist me))
  307. (comment (comment me))
  308. (compilation (compilation me))
  309. (composer (composer me))
  310. (copyright (copyright me))
  311. (disk (disk me))
  312. (encoder (encoder me))
  313. (genre (genre me))
  314. (groups (groups me))
  315. (lyrics (lyrics me))
  316. (purchased-date (purchased-date me))
  317. (tempo (tempo me))
  318. (title (title me))
  319. (tool (tool me))
  320. (track (track me))
  321. (writer (writer me))
  322. (year (year me)))
  323. (format t "~a~%" (stream-filename me))
  324. (when album (format t "~4talbum: ~a~%" album))
  325. (when album-artist (format t "~4talbum-artist: ~a~%" album-artist))
  326. (when artist (format t "~4tartist: ~a~%" artist))
  327. (when comment (format t "~4tcomment: ~a~%" comment))
  328. (when compilation (format t "~4tcompilation: ~a~%" compilation))
  329. (when composer (format t "~4tcomposer: ~a~%" composer))
  330. (when copyright (format t "~4tcopyright: ~a~%" copyright))
  331. (when disk (format t "~4tdisk: ~a~%" disk))
  332. (when encoder (format t "~4tencoder: ~a~%" encoder))
  333. (when genre (format t "~4tgenre: ~a~%" genre))
  334. (when groups (format t "~4tgroups: ~a~%" groups))
  335. (when lyrics (format t "~4tlyrics: ~a~%" lyrics))
  336. (when purchased-date (format t "~4tpurchased date: ~a~%" purchased-date))
  337. (when tempo (format t "~4ttempo: ~a~%" tempo))
  338. (when title (format t "~4ttitle: ~a~%" title))
  339. (when tool (format t "~4ttool: ~a~%" tool))
  340. (when track (format t "~4ttrack: ~a~%" track))
  341. (when writer (format t "~4twriter: ~a~%" writer))
  342. (when year (format t "~4tyear: ~a~%" year)))))