id3-frame.lisp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. ;;; -*- Mode: Lisp; show-trailing-whitespace: t; Base: 10; indent-tabs: nil; Syntax: ANSI-Common-Lisp; Package: ID3-FRAME; -*-
  2. ;;; Copyright (c) 2013, Mark VandenBrink. All rights reserved.
  3. (in-package #:id3-frame)
  4. (log5:defcategory cat-log-id3-frame)
  5. (defmacro log-id3-frame (&rest log-stuff) `(log5:log-for (cat-log-id3-frame) ,@log-stuff))
  6. (define-condition id3-frame-condition ()
  7. ((location :initarg :location :reader location :initform nil)
  8. (object :initarg :object :reader object :initform nil)
  9. (messsage :initarg :message :reader message :initform "Undefined Condition"))
  10. (:report (lambda (condition stream)
  11. (format stream "id3-frame condition at location: <~a> with object: <~a>: message: <~a>"
  12. (location condition) (object condition) (message condition)))))
  13. (defmethod print-object ((me id3-frame-condition) stream)
  14. (format stream "location: <~a>, object: <~a>, message: <~a>" (location me) (object me) (message me)))
  15. (defclass id3-header ()
  16. ((version :accessor version :initarg :version :initform 0)
  17. (revision :accessor revision :initarg :revision :initform 0)
  18. (flags :accessor flags :initarg :flags :initform 0)
  19. (size :accessor size :initarg :size :initform 0)
  20. (ext-header :accessor ext-header :initarg :ext-header :initform nil)
  21. (frames :accessor frames :initarg :frames :initform nil)
  22. (v21-tag-header :accessor v21-tag-header :initarg :v21-tag-header :initform nil))
  23. (:documentation "The ID3 header, found at start of file"))
  24. (defun is-valid-mp3-file (mp3-file)
  25. "Make sure this is an MP3 file. Look for frames at begining and/or end"
  26. (log5:with-context "is-valid-mp3-file"
  27. (stream-seek mp3-file 0 :start)
  28. (let* ((id3 (stream-read-string-with-len mp3-file 3))
  29. (version (stream-read-u8 mp3-file))
  30. (tag))
  31. (stream-seek mp3-file 128 :end)
  32. (setf tag (stream-read-string-with-len mp3-file 3))
  33. (stream-seek mp3-file 0 :start)
  34. (log-id3-frame "id3 = ~a, version = ~d" id3 version)
  35. (or (and (string= "ID3" id3)
  36. (or (= 2 version) (= 3 version) (= 4 version)))
  37. (string= tag "TAG")))))
  38. (defclass v21-tag-header ()
  39. ((title :accessor title :initarg :title :initform nil)
  40. (artist :accessor artist :initarg :artist :initform nil)
  41. (album :accessor album :initarg :album :initform nil)
  42. (year :accessor year :initarg :year :initform nil)
  43. (comment :accessor comment :initarg :comment :initform nil)
  44. (genre :accessor genre :initarg :genre :initform nil))
  45. (:documentation "ID3 V2.1 old-style tag. If present, found in last 128 bytes of file."))
  46. (defmethod vpprint ((me v21-tag-header) stream)
  47. (with-slots (title artist album year comment genre) me
  48. (format stream "title = <~a>, artist = <~a>, album = <~a>, year = <~a>, comment = <~a>, genre = ~d (~a)"
  49. title artist album year comment genre (mp3-tag::get-id3v1-genre genre))))
  50. (defmethod initialize-instance ((me v21-tag-header) &key instream)
  51. "Read in a V2.1 tag. Caller will have stream-seek'ed file to correct location and ensured that TAG was present"
  52. (log5:with-context "v21-frame-initializer"
  53. (log-id3-frame "reading v2.1 tag")
  54. (with-slots (title artist album year comment genre) me
  55. (setf title (trim-string (stream-read-string-with-len instream 30)))
  56. (setf artist (trim-string (stream-read-string-with-len instream 30)))
  57. (setf album (trim-string (stream-read-string-with-len instream 30)))
  58. (setf year (trim-string (stream-read-string-with-len instream 4)))
  59. (setf comment (trim-string (stream-read-string-with-len instream 30)))
  60. (setf genre (stream-read-u8 instream))
  61. (log-id3-frame "v21 tag: ~a" (vpprint me nil)))))
  62. (defclass id3-ext-header ()
  63. ((size :accessor size :initarg :size :initform 0)
  64. (flags :accessor flags :initarg :flags :initform 0)
  65. (padding :accessor padding :initarg :padding :initform 0)
  66. (crc :accessor crc :initarg :crc :initform nil))
  67. (:documentation "class representing a V2.3/4 extended header"))
  68. ;;; XXX I almost certainly don't have handling of extended headers complete/correct
  69. (defmacro ext-header-crc-p (flags) `(logbitp 14 ,flags))
  70. (defmethod initialize-instance ((me id3-ext-header) &key instream)
  71. "Read in the extended header. Caller will have stream-seek'ed to correct location in file.
  72. Note: extended headers are subject to unsynchronization, so make sure that INSTREAM has been made sync-safe."
  73. (with-slots (size flags padding crc) me
  74. (setf size (stream-read-u32 instream))
  75. (setf flags (stream-read-u16 instream))
  76. (setf padding (stream-read-u32 instream))
  77. (when (not (zerop flags))
  78. (error "non-zero extended header flags = ~x, check validity" flags))
  79. (when (ext-header-crc-p flags)
  80. (setf crc (stream-read-u32 instream)))))
  81. (defmethod vpprint ((me id3-ext-header) stream)
  82. (with-slots (size flags padding crc) me
  83. (format stream "extended header: size: ~d, flags: ~x, padding ~:d, crc = ~x~%"
  84. size flags padding crc)))
  85. (defmacro header-unsynchronized-p (flags) `(logbitp 7 ,flags))
  86. (defmacro header-extended-p (flags) `(logbitp 6 ,flags))
  87. (defmacro header-experimental-p (flags) `(logbitp 5 ,flags))
  88. (defmacro header-footer-p (flags) `(logbitp 4 ,flags)) ;; N.B. *NOT* defined for 2.3 tags
  89. (defmacro print-header-flags (stream flags)
  90. `(format ,stream "0x~2,'0x: ~:[0/~;unsynchronized-frames/~]~:[0/~;extended-header/~]~:[0/~;expermental-tag/~]~:[0~;footer-present~]"
  91. ,flags
  92. (header-unsynchronized-p ,flags)
  93. (header-extended-p ,flags)
  94. (header-experimental-p ,flags)
  95. (header-footer-p ,flags)))
  96. (defmethod vpprint ((me id3-header) stream)
  97. (with-slots (version revision flags v21-tag-header size ext-header frames) me
  98. (format stream "~a"
  99. (with-output-to-string (s)
  100. (format s "Header: version/revision: ~d/~d, flags: ~a, size = ~:d bytes; ~a; ~a"
  101. version revision (print-header-flags nil flags) size
  102. (if (header-extended-p flags)
  103. (concatenate 'string "Extended header: " (vpprint ext-header nil))
  104. "No extended header")
  105. (if v21-tag-header
  106. (concatenate 'string "V21 tag: " (vpprint v21-tag-header nil))
  107. "No v21 tag"))
  108. (when frames
  109. (format s "~&~4tFrames[~d]:~%" (length frames))
  110. (dolist (f frames)
  111. (format s "~8t~a~%" (vpprint f nil))))))))
  112. (defmethod initialize-instance :after ((me id3-header) &key instream &allow-other-keys)
  113. "Fill in an mp3-header from INSTREAM."
  114. (log5:with-context "id3-header-initializer"
  115. (with-slots (version revision flags size ext-header frames v21-tag-header) me
  116. (stream-seek instream 128 :end)
  117. (when (string= "TAG" (stream-read-string-with-len instream 3))
  118. (log-id3-frame "looking at last 128 bytes at ~:d to try to read id3v21 header" (stream-seek instream 0 :current))
  119. (handler-case
  120. (setf v21-tag-header (make-instance 'v21-tag-header :instream instream))
  121. (id3-frame-condition (c)
  122. (log-id3-frame "reading v21 got condition: ~a" c))))
  123. (stream-seek instream 0 :start)
  124. (when (string= "ID3" (stream-read-string-with-len instream 3))
  125. (setf version (stream-read-u8 instream))
  126. (setf revision (stream-read-u8 instream))
  127. (setf flags (stream-read-u8 instream))
  128. (setf size (stream-read-u32 instream :bits-per-byte 7))
  129. (when (header-unsynchronized-p flags)
  130. (log-id3-frame "header flags indicate unsync"))
  131. (assert (not (header-footer-p flags)) () "Can't decode ID3 footer's yet")
  132. (log-id3-frame "id3 header = ~a" (vpprint me nil))))))
  133. (defclass id3-frame ()
  134. ((pos :accessor pos :initarg :pos)
  135. (id :accessor id :initarg :id)
  136. (len :accessor len :initarg :len)
  137. (version :accessor version :initarg :version)
  138. (flags :accessor flags :initarg :flags :initform nil))
  139. (:documentation "Base class for an ID3 frame"))
  140. (defmacro frame-23-altertag-p (frame-flags) `(logbitp 15 ,frame-flags))
  141. (defmacro frame-23-alterfile-p (frame-flags) `(logbitp 14 ,frame-flags))
  142. (defmacro frame-23-readonly-p (frame-flags) `(logbitp 13 ,frame-flags))
  143. (defmacro frame-23-compress-p (frame-flags) `(logbitp 7 ,frame-flags))
  144. (defmacro frame-23-encrypt-p (frame-flags) `(logbitp 6 ,frame-flags))
  145. (defmacro frame-23-group-p (frame-flags) `(logbitp 5 ,frame-flags))
  146. (defmacro frame-24-altertag-p (frame-flags) `(logbitp 14 ,frame-flags))
  147. (defmacro frame-24-alterfile-p (frame-flags) `(logbitp 13 ,frame-flags))
  148. (defmacro frame-24-readonly-p (frame-flags) `(logbitp 12 ,frame-flags))
  149. (defmacro frame-24-groupid-p (frame-flags) `(logbitp 6 ,frame-flags))
  150. (defmacro frame-24-compress-p (frame-flags) `(logbitp 3 ,frame-flags))
  151. (defmacro frame-24-encrypt-p (frame-flags) `(logbitp 2 ,frame-flags))
  152. (defmacro frame-24-unsynch-p (frame-flags) `(logbitp 1 ,frame-flags))
  153. (defmacro frame-24-datalen-p (frame-flags) `(logbitp 0 ,frame-flags))
  154. (defun valid-frame-flags (header-version frame-flags)
  155. (ecase header-version
  156. (3 (zerop (logand #b0001111100011111 frame-flags)))
  157. (4 (zerop (logand #b1000111110110000 frame-flags)))))
  158. (defun print-frame-flags (version flags stream)
  159. (ecase version
  160. (3 (format stream "flags: 0x~4,'0x: ~:[0/~;tag-alter-preservation/~]~:[0/~;file-alter-preservation/~]~:[0/~;read-only/~]~:[0/~;compress/~]~:[0/~;encypt/~]~:[0~;group~]"
  161. flags
  162. (frame-23-altertag-p flags)
  163. (frame-23-alterfile-p flags)
  164. (frame-23-readonly-p flags)
  165. (frame-23-compress-p flags)
  166. (frame-23-encrypt-p flags)
  167. (frame-23-group-p flags)))
  168. (4 (format stream "flags: 0x~4,'0x: ~:[0/~;tag-alter-preservation/~]~:[0/~;file-alter-preservation/~]~:[0/~;read-only/~]~:[0/~;group-id/~]~:[0/~;compress/~]~:[0/~;encypt/~]~:[0/~;unsynch/~]~:[0~;datalen~], "
  169. flags
  170. (frame-24-altertag-p flags)
  171. (frame-24-alterfile-p flags)
  172. (frame-24-readonly-p flags)
  173. (frame-24-groupid-p flags)
  174. (frame-24-compress-p flags)
  175. (frame-24-encrypt-p flags)
  176. (frame-24-unsynch-p flags)
  177. (frame-24-datalen-p flags)))))
  178. (defun vpprint-frame-header (id3-frame)
  179. (with-output-to-string (stream)
  180. (with-slots (pos version id len flags) id3-frame
  181. (format stream "offset: ~:d, version = ~d, id: ~a, len: ~:d, " pos version id len)
  182. (if flags (print-frame-flags version flags stream)))))
  183. (defclass frame-raw (id3-frame)
  184. ((octets :accessor octets :initform nil))
  185. (:documentation "Frame class that slurps in frame contents"))
  186. (defmethod initialize-instance :after ((me frame-raw) &key instream)
  187. (log5:with-context "frame-raw"
  188. (with-slots (pos len octets) me
  189. (log-id3-frame "reading ~:d bytes from position ~:d" len pos)
  190. (setf octets (stream-read-sequence instream len))
  191. (log-id3-frame "frame: ~a" (vpprint me nil)))))
  192. (defparameter *max-raw-bytes-print-len* 10)
  193. (defun printable-array (array)
  194. "given an array, return a string of the first *MAX-RAW-BYTES-PRINT-LEN* bytes"
  195. (let* ((len (length array))
  196. (print-len (min len *max-raw-bytes-print-len*))
  197. (printable-array (make-array print-len :displaced-to array)))
  198. (format nil "[~:d of ~:d bytes] <~x>" print-len len printable-array)))
  199. (defun upto-null (string)
  200. (subseq string 0 (position #\Null string)))
  201. (defmethod vpprint ((me frame-raw) stream)
  202. (with-slots (octets) me
  203. (format stream "frame-raw: ~a, ~a" (vpprint-frame-header me) (printable-array octets))))
  204. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  205. ;; V22 frames
  206. ;;
  207. ;;; frame I haven't parsed (or don't need to parse)
  208. (defclass frame-buf (frame-raw) ())
  209. (defclass frame-cnt (frame-raw) ())
  210. (defclass frame-cra (frame-raw) ())
  211. (defclass frame-crm (frame-raw) ())
  212. (defclass frame-equ (frame-raw) ())
  213. (defclass frame-etc (frame-raw) ())
  214. (defclass frame-geo (frame-raw) ())
  215. (defclass frame-ipl (frame-raw) ())
  216. (defclass frame-lnk (frame-raw) ())
  217. (defclass frame-mci (frame-raw) ())
  218. (defclass frame-mll (frame-raw) ())
  219. (defclass frame-pop (frame-raw) ())
  220. (defclass frame-rev (frame-raw) ())
  221. (defclass frame-rva (frame-raw) ())
  222. (defclass frame-slt (frame-raw) ())
  223. (defclass frame-waf (frame-raw) ())
  224. (defclass frame-war (frame-raw) ())
  225. (defclass frame-was (frame-raw) ())
  226. (defclass frame-wcm (frame-raw) ())
  227. (defclass frame-wcp (frame-raw) ())
  228. (defclass frame-wpb (frame-raw) ())
  229. (defclass frame-wxx (frame-raw) ())
  230. (defclass frame-stc (frame-raw) ())
  231. ;; COM frames
  232. ;; Comment "COM"
  233. ;; Frame size $xx xx xx
  234. ;; Text encoding $xx
  235. ;; Language $xx xx xx
  236. ;; Short content description <textstring> $00 (00)
  237. ;; The actual text <textstring>
  238. (defclass frame-com (id3-frame)
  239. ((encoding :accessor encoding)
  240. (lang :accessor lang)
  241. (desc :accessor desc)
  242. (val :accessor val)))
  243. (defmethod initialize-instance :after ((me frame-com) &key instream)
  244. (log5:with-context "frame-com"
  245. (with-slots (len encoding lang desc val) me
  246. (setf encoding (stream-read-u8 instream))
  247. (setf lang (stream-read-iso-string-with-len instream 3))
  248. (multiple-value-bind (n v) (get-name-value-pair instream (- len 1 3) encoding encoding)
  249. (setf desc n)
  250. (let ((len (1- (length v))))
  251. (if (and (> len 0) (eq #\Null (aref v len))) ; iTunes broken-ness... maybe this should be done on rendering the comment instead of here?
  252. (setf val (make-array len :displaced-to v))
  253. (setf val v))))
  254. (log-id3-frame "encoding = ~d, lang = <~a>, desc = <~a>, text = <~a>" encoding lang desc val))))
  255. (defmethod vpprint ((me frame-com) stream)
  256. (with-slots (len encoding lang desc val) me
  257. (format stream "frame-com: ~a, encoding = ~d, lang = <~a>, desc = <~a>, val = <~a>" (vpprint-frame-header me) encoding lang desc val)))
  258. ;; v22 PIC
  259. ;; Attached picture "PIC"
  260. ;; Frame size $xx xx xx
  261. ;; Text encoding $xx
  262. ;; Image format $xx xx xx
  263. ;; Picture type $xx
  264. ;; Description <textstring> $00 (00)
  265. ;; Picture data <binary data>
  266. (defclass frame-pic (id3-frame)
  267. ((encoding :accessor encoding)
  268. (img-format :accessor img-format)
  269. (type :accessor type)
  270. (desc :accessor desc)
  271. (data :accessor data)))
  272. (defmethod initialize-instance :after ((me frame-pic) &key instream)
  273. (log5:with-context "frame-pic"
  274. (with-slots (id len encoding img-format type desc data) me
  275. (setf encoding (stream-read-u8 instream))
  276. (setf img-format (stream-read-iso-string-with-len instream 3))
  277. (setf type (stream-read-u8 instream))
  278. (multiple-value-bind (n v) (get-name-value-pair instream (- len 5) encoding -1)
  279. (setf desc n)
  280. (setf data v)
  281. (log-id3-frame "encoding: ~d, img-format = <~a>, type = ~d, desc = <~a>, value = ~a"
  282. encoding img-format type desc (printable-array data))))))
  283. (defmethod vpprint ((me frame-pic) stream)
  284. (with-slots (encoding img-format type desc data) me
  285. (format stream "frame-pic: ~a, encoding ~d, img-format type: <~a>, picture type: ~d, description <~a>, data: ~a"
  286. (vpprint-frame-header me) encoding img-format type desc (printable-array data))))
  287. ;; Generic text-info frames
  288. ;; Text information identifier "T00" - "TZZ" , excluding "TXX", or "T000 - TZZZ", excluding "TXXX"
  289. ;; Text encoding $xx
  290. ;; Information <textstring>
  291. (defclass frame-text-info (id3-frame)
  292. ((encoding :accessor encoding)
  293. (info :accessor info)))
  294. (defmethod initialize-instance :after ((me frame-text-info) &key instream)
  295. (log5:with-context "frame-text-info"
  296. (with-slots (version flags len encoding info) me
  297. (let ((read-len len))
  298. (when (and (= version 4) (frame-24-unsynch-p flags))
  299. (if (frame-24-datalen-p flags)
  300. (setf read-len (stream-read-u32 instream :bits-per-byte 7))))
  301. (setf encoding (stream-read-u8 instream))
  302. (setf info (stream-read-string-with-len instream (1- read-len) :encoding encoding)))
  303. ;; a null is ok, but according to the "spec", you're supposed to ignore anything after a 'Null'
  304. (log-id3-frame "made text-info-frame: ~a" (vpprint me nil))
  305. (setf info (upto-null info))
  306. (log-id3-frame "encoding = ~d, info = <~a>" encoding info))))
  307. (defmethod vpprint ((me frame-text-info) stream)
  308. (with-slots (len encoding info) me
  309. (format stream "frame-text-info: ~a, encoding = ~d, info = <~a>" (vpprint-frame-header me) encoding info)))
  310. ;; v22 User defined... "TXX" frames
  311. ;; Frame size $xx xx xx
  312. ;; Text encoding $xx
  313. ;; Description <textstring> $00 (00)
  314. ;; Value <textstring>
  315. (defclass frame-txx (id3-frame)
  316. ((encoding :accessor encoding)
  317. (desc :accessor desc)
  318. (val :accessor val)))
  319. (defmethod initialize-instance :after ((me frame-txx) &key instream)
  320. (log5:with-context "frame-txx"
  321. (with-slots (len encoding desc val) me
  322. (setf encoding (stream-read-u8 instream))
  323. (multiple-value-bind (n v) (get-name-value-pair instream (1- len) encoding encoding)
  324. (setf desc n)
  325. (setf val v)
  326. (log-id3-frame "encoding = ~d, desc = <~a>, val = <~a>" encoding desc val)))))
  327. (defmethod vpprint ((me frame-txx) stream)
  328. (with-slots (len encoding desc val) me
  329. (format stream "frame-txx: ~a, encoding = ~d, desc = <~a>, val = <~a>" (vpprint-frame-header me) encoding desc val)))
  330. (defclass frame-ufi (id3-frame)
  331. ((name :accessor name)
  332. (value :accessor value)))
  333. (defmethod initialize-instance :after ((me frame-ufi) &key instream)
  334. (log5:with-context "frame-ufi"
  335. (with-slots (id len name value) me
  336. (multiple-value-bind (n v) (get-name-value-pair instream len 0 -1)
  337. (setf name n)
  338. (setf value v))
  339. (log-id3-frame "name = <~a>, value = ~a" name (printable-array value)))))
  340. (defmethod vpprint ((me frame-ufi) stream)
  341. (with-slots (id len name value) me
  342. (format stream "frame-ufi: ~a, name: <~a>, value: ~a" (vpprint-frame-header me) name (printable-array value))))
  343. (defclass frame-tal (frame-text-info) ())
  344. (defclass frame-tbp (frame-text-info) ())
  345. (defclass frame-tcm (frame-text-info) ())
  346. (defclass frame-tco (frame-text-info) ())
  347. (defclass frame-tcp (frame-text-info) ())
  348. (defclass frame-tcr (frame-text-info) ())
  349. (defclass frame-tda (frame-text-info) ())
  350. (defclass frame-tdy (frame-text-info) ())
  351. (defclass frame-ten (frame-text-info) ())
  352. (defclass frame-tft (frame-text-info) ())
  353. (defclass frame-tim (frame-text-info) ())
  354. (defclass frame-tke (frame-text-info) ())
  355. (defclass frame-tla (frame-text-info) ())
  356. (defclass frame-tle (frame-text-info) ())
  357. (defclass frame-tmt (frame-text-info) ())
  358. (defclass frame-toa (frame-text-info) ())
  359. (defclass frame-tof (frame-text-info) ())
  360. (defclass frame-tol (frame-text-info) ())
  361. (defclass frame-tor (frame-text-info) ())
  362. (defclass frame-tot (frame-text-info) ())
  363. (defclass frame-tp1 (frame-text-info) ())
  364. (defclass frame-tp2 (frame-text-info) ())
  365. (defclass frame-tp3 (frame-text-info) ())
  366. (defclass frame-tp4 (frame-text-info) ())
  367. (defclass frame-tpa (frame-text-info) ())
  368. (defclass frame-tpb (frame-text-info) ())
  369. (defclass frame-trc (frame-text-info) ())
  370. (defclass frame-trd (frame-text-info) ())
  371. (defclass frame-trk (frame-text-info) ())
  372. (defclass frame-tsi (frame-text-info) ())
  373. (defclass frame-tss (frame-text-info) ())
  374. (defclass frame-tt1 (frame-text-info) ())
  375. (defclass frame-tt2 (frame-text-info) ())
  376. (defclass frame-tt3 (frame-text-info) ())
  377. (defclass frame-txt (frame-text-info) ())
  378. (defclass frame-tye (frame-text-info) ())
  379. (defclass frame-ult (frame-com) ())
  380. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  381. ;; V2.3/4 frames
  382. ;;
  383. ;; <Header for 'Audio encryption', ID: "AENC">
  384. ;; Owner identifier <text string> $00
  385. ;; Preview start $xx xx
  386. ;; Preview length $xx xx
  387. ;; Encryption info <binary data>
  388. (defclass frame-aenc (frame-raw) ())
  389. (defclass frame-aspi (frame-raw) ())
  390. (defclass frame-comr (frame-raw) ())
  391. (defclass frame-encr (frame-raw) ())
  392. (defclass frame-equ2 (frame-raw) ())
  393. (defclass frame-equa (frame-raw) ())
  394. (defclass frame-etco (frame-raw) ())
  395. (defclass frame-geob (frame-raw) ())
  396. (defclass frame-grid (frame-raw) ())
  397. (defclass frame-ipls (frame-raw) ())
  398. (defclass frame-link (frame-raw) ())
  399. (defclass frame-mcdi (frame-raw) ())
  400. (defclass frame-mllt (frame-raw) ())
  401. (defclass frame-ncon (frame-raw) ())
  402. (defclass frame-owne (frame-raw) ())
  403. (defclass frame-popm (frame-raw) ())
  404. (defclass frame-poss (frame-raw) ())
  405. (defclass frame-rbuf (frame-raw) ())
  406. (defclass frame-rva2 (frame-raw) ())
  407. (defclass frame-rvad (frame-raw) ())
  408. (defclass frame-rvrb (frame-raw) ())
  409. (defclass frame-seek (frame-raw) ())
  410. (defclass frame-sign (frame-raw) ())
  411. (defclass frame-sylt (frame-raw) ())
  412. (defclass frame-sytc (frame-raw) ())
  413. (defclass frame-user (frame-raw) ())
  414. ;; APIC
  415. ;; <Header for 'Attached picture', ID: "APIC">
  416. ;; Text encoding $xx
  417. ;; MIME type <text string> $00
  418. ;; Picture type $xx
  419. ;; Description <text string according to encoding> $00 (00)
  420. ;; Picture data <binary data>
  421. (defclass frame-apic (id3-frame)
  422. ((encoding :accessor encoding)
  423. (mime :accessor mime)
  424. (type :accessor type)
  425. (desc :accessor desc)
  426. (data :accessor data)))
  427. (defmethod initialize-instance :after ((me frame-apic) &key instream)
  428. (log5:with-context "frame-apic"
  429. (with-slots (id len encoding mime type desc data) me
  430. (setf encoding (stream-read-u8 instream))
  431. (setf mime (stream-read-iso-string instream))
  432. (setf type (stream-read-u8 instream))
  433. (multiple-value-bind (n v) (get-name-value-pair instream (- len 1 (length mime) 1 1) encoding -1)
  434. (setf desc n)
  435. (setf data v)
  436. (log-id3-frame "enoding = ~d, mime = <~a>, type = ~d, descx = <~a>, data = ~a" encoding mime type desc (printable-array data))))))
  437. (defmethod vpprint ((me frame-apic) stream)
  438. (with-slots (encoding mime type desc data) me
  439. (format stream "frame-apic: ~a, encoding ~d, mime type: ~a, picture type: ~d, description <~a>, data: ~a"
  440. (vpprint-frame-header me) encoding mime type desc (printable-array data))))
  441. ;; COMM frames
  442. ;; <Header for 'Comment', ID: "COMM">
  443. ;; Text encoding $xx
  444. ;; Language $xx xx xx
  445. ;; Short content descrip. <text string according to encoding> $00 (00)
  446. ;; The actual text <full text string according to encoding>
  447. (defclass frame-comm (id3-frame)
  448. ((encoding :accessor encoding)
  449. (lang :accessor lang)
  450. (desc :accessor desc)
  451. (val :accessor val)))
  452. (defmethod initialize-instance :after ((me frame-comm) &key instream)
  453. (log5:with-context "frame-comm"
  454. (with-slots (encoding lang len desc val) me
  455. (setf encoding (stream-read-u8 instream))
  456. (setf lang (stream-read-iso-string-with-len instream 3))
  457. (multiple-value-bind (n v) (get-name-value-pair instream (- len 1 3) encoding encoding)
  458. (setf desc n)
  459. (let ((len (1- (length v))))
  460. (if (and (> len 0) (eq #\Null (aref v len))) ; iTunes broken-ness... maybe this should be done on rendering the comment instead of here?
  461. (setf val (make-array len :displaced-to v))
  462. (setf val v))))
  463. (log-id3-frame "encoding = ~d, lang = <~a>, desc = <~a>, val = <~a>" encoding lang desc val))))
  464. (defmethod vpprint ((me frame-comm) stream)
  465. (with-slots (encoding lang desc val) me
  466. (format stream "frame-comm: ~a, encoding: ~d, lang: ~x, desc: ~a, val ~a"
  467. (vpprint-frame-header me) encoding lang desc val)))
  468. (defclass frame-uslt (frame-comm) ())
  469. ;; PCNT frames
  470. ;; <Header for 'Play counter', ID: "PCNT">
  471. ;; Counter $xx xx xx xx (xx ...)
  472. (defclass frame-pcnt (id3-frame)
  473. ((play-count :accessor play-count)))
  474. (defmethod initialize-instance :after ((me frame-pcnt) &key instream)
  475. (log5:with-context "frame-pcnt"
  476. (with-slots (play-count len) me
  477. (assert (= 4 len) () "Ran into a play count with ~d bytes" len)
  478. (setf play-count (stream-read-u32 instream)) ; probably safe---play count *can* be longer than 4 bytes, but...
  479. (log-id3-frame "play count = <~d>" play-count))))
  480. (defmethod vpprint ((me frame-pcnt) stream)
  481. (with-slots (play-count) me
  482. (format stream "frame-pcnt: ~a, count = ~d" (vpprint-frame-header me) play-count)))
  483. ;; PRIV frames
  484. ;; <Header for 'Private frame', ID: "PRIV">
  485. ;; Owner identifier <text string> $00
  486. ;; The private data <binary data>
  487. (defclass frame-priv (id3-frame)
  488. ((name :accessor name)
  489. (value :accessor value)))
  490. (defmethod initialize-instance :after ((me frame-priv) &key instream)
  491. (log5:with-context "frame-priv"
  492. (with-slots (id len name value) me
  493. (multiple-value-bind (n v) (get-name-value-pair instream len 0 -1)
  494. (setf name n)
  495. (setf value v)
  496. (log-id3-frame "name = <~a>, value = <~a>" name value)))))
  497. (defmethod vpprint ((me frame-priv) stream)
  498. (with-slots (id len name value) me
  499. (format stream "frame-priv: ~a, name: <~a>, data: ~a" (vpprint-frame-header me) name (printable-array value))))
  500. ;; TXXX frames
  501. ;; <Header for 'User defined text information frame', ID: "TXXX">
  502. ;; Text encoding $xx
  503. ;; Description <text string according to encoding> $00 (00)
  504. ;; Value <text string according to encoding>
  505. (defclass frame-txxx (id3-frame)
  506. ((encoding :accessor encoding)
  507. (desc :accessor desc)
  508. (val :accessor val)))
  509. (defmethod initialize-instance :after ((me frame-txxx) &key instream)
  510. (log5:with-context "frame-txxx"
  511. (with-slots (encoding len desc val) me
  512. (setf encoding (stream-read-u8 instream))
  513. (multiple-value-bind (n v) (get-name-value-pair instream
  514. (- len 1)
  515. encoding
  516. encoding)
  517. (setf desc n)
  518. (setf val v))
  519. (log-id3-frame "encoding = ~d, desc = <~a>, value = <~a>" encoding desc val))))
  520. (defmethod vpprint ((me frame-txxx) stream)
  521. (format stream "frame-txxx: ~a, <~a/~a>" (vpprint-frame-header me) (desc me) (val me)))
  522. ;; UFID frames
  523. ;; <Header for 'Unique file identifier', ID: "UFID">
  524. ;; Owner identifier <text string> $00
  525. ;; Identifier <up to 64 bytes binary data>
  526. (defclass frame-ufid (id3-frame)
  527. ((name :accessor name)
  528. (value :accessor value)))
  529. (defmethod initialize-instance :after ((me frame-ufid) &key instream)
  530. (log5:with-context "frame-ufid"
  531. (with-slots (id len name value) me
  532. (multiple-value-bind (n v) (get-name-value-pair instream len 0 -1)
  533. (setf name n)
  534. (setf value v))
  535. (log-id3-frame "name = <~a>, value = ~a" name (printable-array value)))))
  536. (defmethod vpprint ((me frame-ufid) stream)
  537. (with-slots (id len name value) me
  538. (format stream "frame-ufid: ~a, name: <~a>, value: ~a" (vpprint-frame-header me) name (printable-array value))))
  539. ;; URL frame
  540. ;; <Header for 'URL link frame', ID: "W000" - "WZZZ", excluding "WXXX" described in 4.3.2.>
  541. ;; URL <text string>
  542. (defclass frame-url-link (id3-frame)
  543. ((url :accessor url)))
  544. (defmethod initialize-instance :after ((me frame-url-link) &key instream)
  545. (with-slots (id len url) me
  546. (log5:with-context "url"
  547. (setf url (stream-read-iso-string-with-len instream len))
  548. (log-id3-frame "url = <~a>" url))))
  549. (defmethod vpprint ((me frame-url-link) stream)
  550. (with-slots (url) me
  551. (format stream "frame-url-link: ~a, url: ~a" (vpprint-frame-header me) url)))
  552. (defclass frame-talb (frame-text-info) ())
  553. (defclass frame-tbpm (frame-text-info) ())
  554. (defclass frame-tcmp (frame-text-info) ())
  555. (defclass frame-tcom (frame-text-info) ())
  556. (defclass frame-tcon (frame-text-info) ())
  557. (defclass frame-tcop (frame-text-info) ())
  558. (defclass frame-tdat (frame-text-info) ())
  559. (defclass frame-tden (frame-text-info) ())
  560. (defclass frame-tdly (frame-text-info) ())
  561. (defclass frame-tdor (frame-text-info) ())
  562. (defclass frame-tdrc (frame-text-info) ())
  563. (defclass frame-tdrl (frame-text-info) ())
  564. (defclass frame-tdtg (frame-text-info) ())
  565. (defclass frame-tenc (frame-text-info) ())
  566. (defclass frame-text (frame-text-info) ())
  567. (defclass frame-tflt (frame-text-info) ())
  568. (defclass frame-time (frame-text-info) ())
  569. (defclass frame-tipl (frame-text-info) ())
  570. (defclass frame-tit1 (frame-text-info) ())
  571. (defclass frame-tit2 (frame-text-info) ())
  572. (defclass frame-tit3 (frame-text-info) ())
  573. (defclass frame-tkey (frame-text-info) ())
  574. (defclass frame-tlan (frame-text-info) ())
  575. (defclass frame-tlen (frame-text-info) ())
  576. (defclass frame-tmcl (frame-text-info) ())
  577. (defclass frame-tmed (frame-text-info) ())
  578. (defclass frame-tmoo (frame-text-info) ())
  579. (defclass frame-toal (frame-text-info) ())
  580. (defclass frame-tofn (frame-text-info) ())
  581. (defclass frame-toly (frame-text-info) ())
  582. (defclass frame-tope (frame-text-info) ())
  583. (defclass frame-tory (frame-text-info) ())
  584. (defclass frame-town (frame-text-info) ())
  585. (defclass frame-tpe1 (frame-text-info) ())
  586. (defclass frame-tpe2 (frame-text-info) ())
  587. (defclass frame-tpe3 (frame-text-info) ())
  588. (defclass frame-tpe4 (frame-text-info) ())
  589. (defclass frame-tpos (frame-text-info) ())
  590. (defclass frame-tpro (frame-text-info) ())
  591. (defclass frame-tpub (frame-text-info) ())
  592. (defclass frame-trda (frame-text-info) ())
  593. (defclass frame-trsn (frame-text-info) ())
  594. (defclass frame-trso (frame-text-info) ())
  595. (defclass frame-tsoa (frame-text-info) ())
  596. (defclass frame-tsop (frame-text-info) ())
  597. (defclass frame-tsot (frame-text-info) ())
  598. (defclass frame-tsst (frame-text-info) ())
  599. (defclass frame-tsse (frame-text-info) ())
  600. (defclass frame-tsrc (frame-text-info) ())
  601. (defclass frame-tsiz (frame-text-info) ())
  602. (defclass frame-tyer (frame-text-info) ())
  603. (defclass frame-trck (frame-text-info) ())
  604. (defclass frame-wcom (frame-url-link) ())
  605. (defclass frame-wcop (frame-url-link) ())
  606. (defclass frame-woaf (frame-url-link) ())
  607. (defclass frame-woar (frame-url-link) ())
  608. (defclass frame-woas (frame-url-link) ())
  609. (defclass frame-wors (frame-url-link) ())
  610. (defclass frame-wpay (frame-url-link) ())
  611. (defclass frame-wpub (frame-url-link) ())
  612. (defclass frame-wxxx (frame-url-link) ())
  613. ;;
  614. ;; many id3 tags are name/value pairs, with the name/value encoded in various ways
  615. ;; this routine assumes that the name is always a string with a "normal" encoding (i.e. 0, 1, 2, or 3).
  616. ;; a value, however, accepts any negative number, which means read
  617. ;; the bytes an raw octets.
  618. (defun get-name-value-pair (instream len name-encoding value-encoding)
  619. (log5:with-context "get-name-value-pair"
  620. (log-id3-frame "reading from ~:d, len ~:d, name-encoding = ~d, value-encoding = ~d" (stream-seek instream 0 :current) len name-encoding value-encoding)
  621. (let* ((old-pos (stream-seek instream 0 :current))
  622. (name (stream-read-string instream :encoding name-encoding))
  623. (name-len (- (stream-seek instream 0 :current) old-pos))
  624. (value))
  625. (log-id3-frame "name = <~a>, name-len = ~d" name name-len)
  626. (setf value (if (>= value-encoding 0)
  627. (stream-read-string-with-len instream (- len name-len) :encoding value-encoding)
  628. (stream-read-sequence instream (- len name-len)))) ; if < 0, then just read as octets
  629. (values name value))))
  630. ;;
  631. ;; test to see if a string is a potentially valid frame id
  632. (defun possibly-valid-frame-id? (frame-id)
  633. (labels ((numeric-char-p (c)
  634. (let ((code (char-code c)))
  635. (and (>= code (char-code #\0))
  636. (<= code (char-code #\9))))))
  637. (dotimes (i (length frame-id))
  638. (let ((c (aref frame-id i)))
  639. (when (not (or (numeric-char-p c)
  640. (and (alpha-char-p c) (upper-case-p c))))
  641. (return-from possibly-valid-frame-id? nil))))
  642. t))
  643. ;;; Search by frame-id for a class, returning a class that can be used as arg to
  644. ;;; make-instance.
  645. (defun find-frame-class (id)
  646. (log5:with-context "find-frame-class"
  647. (log-id3-frame "looking for class <~a>" id)
  648. (let ((found-class-symbol (find-symbol (string-upcase (concatenate 'string "frame-" id)) :ID3-FRAME))
  649. found-class)
  650. (when found-class-symbol
  651. (setf found-class (find-class found-class-symbol))
  652. (log-id3-frame "found class: ~a" found-class)
  653. (return-from find-frame-class found-class))
  654. (log-id3-frame "didn't find class, checking general cases")
  655. ;; if not a "normal" frame-id, look at general cases of
  656. ;; starting with a 'T' or a 'W'
  657. (setf found-class (case (aref id 0)
  658. (#\T (log-id3-frame "assuming text-info") (find-class (find-symbol "FRAME-TEXT-INFO" :ID3-FRAME)))
  659. (#\W (log-id3-frame "assuming url-link") (find-class (find-symbol "FRAME-URL-LINK" :ID3-FRAME)))
  660. (t
  661. ;; we don't recognize the frame name. if it could possibly be a real frame name,
  662. ;; then just read it raw
  663. (when (possibly-valid-frame-id? id)
  664. (log-id3-frame "just reading raw")
  665. (find-class (find-symbol "FRAME-RAW" :ID3-FRAME))))))
  666. (log-id3-frame "general case for id <~a> is ~a" id found-class)
  667. found-class)))
  668. (defun make-frame (version instream)
  669. "Create an appropriate mp3 frame by reading data from INSTREAM."
  670. (log5:with-context "find-id3-frames"
  671. (let* ((pos (stream-seek instream 0 :current))
  672. (byte (stream-read-u8 instream))
  673. frame-name frame-len frame-flags frame-class)
  674. (log-id3-frame "reading from position ~:d (size of stream = ~:d" pos (stream-size instream))
  675. (when (zerop byte)
  676. (log-id3-frame "hit padding")
  677. (return-from make-frame nil)) ; hit padding
  678. (setf frame-name
  679. (concatenate 'string (string (code-char byte)) (stream-read-string-with-len instream (ecase version (2 2) (3 3) (4 3)))))
  680. (setf frame-len (ecase version
  681. (2 (stream-read-u24 instream))
  682. (3 (stream-read-u32 instream))
  683. (4 (stream-read-u32 instream :bits-per-byte 7))))
  684. (when (or (= version 3) (= version 4))
  685. (setf frame-flags (stream-read-u16 instream))
  686. (if (not (valid-frame-flags version frame-flags))
  687. (warn "Invalid frame flags found ~a" (print-frame-flags version frame-flags nil))))
  688. (log-id3-frame "making frame: id:~a, version: ~d, len: ~:d, flags: ~a"
  689. frame-name version frame-len
  690. (print-frame-flags version frame-flags nil))
  691. (setf frame-class (find-frame-class frame-name))
  692. (when (or (> (+ (stream-seek instream 0 :current) frame-len) (stream-size instream))
  693. (null frame-class))
  694. (error 'id3-frame-condition :message "bad frame found" :object frame-name :location pos))
  695. (make-instance frame-class :pos pos :version version :id frame-name :len frame-len :flags frame-flags :instream instream))))
  696. (defun find-id3-frames (mp3-file)
  697. "With an open mp3-file, make sure it is in fact an MP3 file, then read it's header and frames"
  698. (labels ((read-loop (version stream)
  699. (log-id3-frame "Starting loop through ~:d bytes" (stream-size stream))
  700. (let (frames this-frame)
  701. (do ()
  702. ((>= (stream-seek stream 0 :current) (stream-size stream)))
  703. (handler-case
  704. (progn
  705. (setf this-frame (make-frame version stream))
  706. (when (null this-frame)
  707. (log-id3-frame "hit padding: returning ~d frames" (length frames))
  708. (return-from read-loop (values t (nreverse frames))))
  709. (log-id3-frame "bottom of read-loop: pos = ~:d, size = ~:d" (stream-seek stream 0 :current) (stream-size stream))
  710. (push this-frame frames))
  711. (condition (c)
  712. (log-id3-frame "got condition ~a when making frame" c)
  713. (return-from read-loop (values nil (nreverse frames))))))
  714. (log-id3-frame "hit end: returning ~d frames" (length frames))
  715. (values t (nreverse frames)))))
  716. (log5:with-context "find-id3-frames"
  717. (when (not (is-valid-mp3-file mp3-file))
  718. (log-id3-frame "~a is not an mp3 file" (stream-filename mp3-file))
  719. (error 'id3-frame-condition :location "find-id3-frames" :object (stream-filename mp3-file) :message "is not an mp3 file"))
  720. (log-id3-frame "~a is a valid mp3 file" (stream-filename mp3-file))
  721. (setf (id3-header mp3-file) (make-instance 'id3-header :instream mp3-file))
  722. (with-slots (size ext-header frames flags version) (id3-header mp3-file)
  723. (when (not (zerop size))
  724. (let ((mem-stream (make-mem-stream (stream-read-sequence mp3-file size
  725. :bits-per-byte (if (header-unsynchronized-p flags) 7 8)))))
  726. ;; must make extended header here since it is subject to unsynchronization.
  727. (when (header-extended-p flags)
  728. (setf ext-header (make-instance 'id3-extended-header :instream mem-stream)))
  729. (multiple-value-bind (_ok _frames) (read-loop version mem-stream)
  730. (if (not _ok)
  731. (warn "File ~a had errors finding mp3 frames. potentially missed frames!" (stream-filename mp3-file)))
  732. (log-id3-frame "ok = ~a, returning ~d frames" _ok (length _frames))
  733. (setf frames _frames)
  734. _ok)))))))
  735. (defun get-frame-info (mp3-file frame-id)
  736. (with-slots (frames) (id3-header mp3-file)
  737. (dolist (f frames)
  738. (if (string= frame-id (id f))
  739. (return-from get-frame-info f)))))
  740. (defun map-id3-frames (mp3-file &key (func (constantly t)))
  741. (mapcar func (frames (id3-header mp3-file))))
  742. #|
  743. Random ideas for rewrite:
  744. -might be simplest to read in frame payloads (sync'ed appropriately) for all frames and then move parsing into
  745. accessor methods? This might be easier to handle sync/compression/etc.
  746. |#