id3-frame.lisp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ID3 header/extended header/v2.1 header ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16. (defclass id3-header ()
  17. ((version :accessor version :initarg :version :initform 0 :documentation "ID3 version: 2, 3, or 4")
  18. (revision :accessor revision :initarg :revision :initform 0 :documentation "ID3 revision---is this ever non-zero?")
  19. (flags :accessor flags :initarg :flags :initform 0 :documentation "ID3 header flags")
  20. (size :accessor size :initarg :size :initform 0 :documentation "size of ID3 info")
  21. (ext-header :accessor ext-header :initarg :ext-header :initform nil :documentation "holds v2.3/4 extended header")
  22. (frames :accessor frames :initarg :frames :initform nil :documentation "holds ID3 frames")
  23. (v21-tag-header :accessor v21-tag-header :initarg :v21-tag-header :initform nil :documentation "old-style v2.1 header (if present)"))
  24. (:documentation "The ID3 header, found at start of file"))
  25. (defun is-valid-mp3-file (mp3-file)
  26. "Make sure this is an MP3 file. Look for ID3 header at begining (versions 2, 3, 4) and/or end (version 2.1)
  27. Written in this fashion so as to be 'crash-proof' when passed an arbitrary file."
  28. (log5:with-context "is-valid-mp3-file"
  29. (let ((id3)
  30. (valid)
  31. (version)
  32. (tag))
  33. (unwind-protect
  34. (handler-case
  35. (progn
  36. (stream-seek mp3-file 0 :start)
  37. (setf id3 (stream-read-string-with-len mp3-file 3))
  38. (setf version (stream-read-u8 mp3-file))
  39. (stream-seek mp3-file 128 :end)
  40. (setf tag (stream-read-string-with-len mp3-file 3))
  41. (log-id3-frame "id3 = ~a, version = ~d" id3 version)
  42. (setf valid (or (and (string= "ID3" id3)
  43. (or (= 2 version) (= 3 version) (= 4 version)))
  44. (string= tag "TAG"))))
  45. (condition (c)
  46. (declare (ignore c))))
  47. (stream-seek mp3-file 0 :start))
  48. valid)))
  49. (defclass v21-tag-header ()
  50. ((title :accessor title :initarg :title :initform nil)
  51. (artist :accessor artist :initarg :artist :initform nil)
  52. (album :accessor album :initarg :album :initform nil)
  53. (year :accessor year :initarg :year :initform nil)
  54. (comment :accessor comment :initarg :comment :initform nil)
  55. (track :accessor track :initarg :track :initform nil :documentation "some taggers allow the last 2 bytes of comment to be used as track number")
  56. (genre :accessor genre :initarg :genre :initform nil))
  57. (:documentation "ID3 V2.1 old-style tag. If present, found in last 128 bytes of file."))
  58. (defmethod vpprint ((me v21-tag-header) stream)
  59. (with-slots (title artist album year comment track genre) me
  60. (format stream "title = <~a>, artist = <~a>, album = <~a>, year = <~a>, comment = <~a>, track = <~d>, genre = ~d (~a)"
  61. title artist album year comment track genre (mp3-tag:get-id3v1-genre genre))))
  62. (defmethod initialize-instance ((me v21-tag-header) &key instream)
  63. "Read in a V2.1 tag. Caller will have stream-seek'ed file to correct location and ensured that TAG was present"
  64. (log5:with-context "v21-frame-initializer"
  65. (log-id3-frame "reading v2.1 tag")
  66. (with-slots (title artist album year comment genre track) me
  67. (setf track nil)
  68. (setf title (upto-null (stream-read-string-with-len instream 30)))
  69. (setf artist (upto-null (stream-read-string-with-len instream 30)))
  70. (setf album (upto-null (stream-read-string-with-len instream 30)))
  71. (setf year (upto-null (stream-read-string-with-len instream 4)))
  72. (setf comment (stream-read-string-with-len instream 30))
  73. ;; In V21, a comment can be split into comment and track #
  74. ;; find the first #\Null then check to see if that index < 28. If so, the check the last two bytes being
  75. ;; non-zero---if so, then track can be set to integer value of last two bytes
  76. (let ((trimmed-comment (upto-null comment))
  77. (trck 0))
  78. (when (<= (length trimmed-comment) 28)
  79. (setf (ldb (byte 8 8) trck) (char-code (aref comment 28)))
  80. (setf (ldb (byte 8 0) trck) (char-code (aref comment 29)))
  81. (setf comment trimmed-comment)
  82. (if (> trck 0)
  83. (setf track trck)
  84. (setf track nil))))
  85. (setf genre (stream-read-u8 instream))
  86. (log-id3-frame "v21 tag: ~a" (vpprint me nil)))))
  87. (defclass id3-ext-header ()
  88. ((size :accessor size :initarg :size :initform 0)
  89. (flags :accessor flags :initarg :flags :initform 0)
  90. (padding :accessor padding :initarg :padding :initform 0)
  91. (crc :accessor crc :initarg :crc :initform nil))
  92. (:documentation "Class representing a V2.3/4 extended header"))
  93. (defmacro ext-header-crc-p (flags) `(logbitp 14 ,flags))
  94. ;;; XXX I almost certainly don't have handling of extended headers complete/correct wrt FLAGS
  95. (defmethod initialize-instance ((me id3-ext-header) &key instream)
  96. "Read in the extended header. Caller will have stream-seek'ed to correct location in file.
  97. Note: extended headers are subject to unsynchronization, so make sure that INSTREAM has been made sync-safe."
  98. (with-slots (size flags padding crc) me
  99. (setf size (stream-read-u32 instream))
  100. (setf flags (stream-read-u16 instream))
  101. (setf padding (stream-read-u32 instream))
  102. (when (not (zerop flags))
  103. ;; at this point, we have to potentially read in other fields depending on flags.
  104. ;; for now, just error out...
  105. (assert (zerop flags) () "non-zero extended header flags = ~x, check validity")
  106. ;;(when (ext-header-crc-p flags)
  107. ;; (setf crc (stream-read-u32 instream)))))
  108. )))
  109. (defmethod vpprint ((me id3-ext-header) stream)
  110. (with-slots (size flags padding crc) me
  111. (format stream "extended header: size: ~d, flags: ~x, padding ~:d, crc = ~x~%"
  112. size flags padding crc)))
  113. ;;; NB: v2.2 only really defines bit-7. It does document bit-6 as being the compression flag, but then states
  114. ;;; that if it is set, the software should "ignore the entire tag if this (bit-6) is set"
  115. (defmacro header-unsynchronized-p (flags) `(logbitp 7 ,flags)) ; all share this flag
  116. (defmacro header-extended-p (flags) `(logbitp 6 ,flags)) ; 2.3/2.4
  117. (defmacro header-experimental-p (flags) `(logbitp 5 ,flags)) ; 2.3/2.4
  118. (defmacro header-footer-p (flags) `(logbitp 4 ,flags)) ; 2.4 only
  119. (defmacro print-header-flags (stream flags)
  120. `(format ,stream "0x~2,'0x: ~:[0/~;unsynchronized-frames/~]~:[0/~;extended-header/~]~:[0/~;expermental-tag/~]~:[0~;footer-present~]"
  121. ,flags
  122. (header-unsynchronized-p ,flags)
  123. (header-extended-p ,flags)
  124. (header-experimental-p ,flags)
  125. (header-footer-p ,flags)))
  126. (defmethod vpprint ((me id3-header) stream)
  127. (with-slots (version revision flags v21-tag-header size ext-header frames) me
  128. (format stream "~a"
  129. (with-output-to-string (s)
  130. (format s "Header: version/revision: ~d/~d, flags: ~a, size = ~:d bytes; ~a; ~a"
  131. version revision (print-header-flags nil flags) size
  132. (if (header-extended-p flags)
  133. (concatenate 'string "Extended header: " (vpprint ext-header nil))
  134. "No extended header")
  135. (if v21-tag-header
  136. (concatenate 'string "V21 tag: " (vpprint v21-tag-header nil))
  137. "No V21 tag"))
  138. (when frames
  139. (format s "~&~4tFrames[~d]:~%" (length frames))
  140. (dolist (f frames)
  141. (format s "~8t~a~%" (vpprint f nil))))))))
  142. (defmethod initialize-instance :after ((me id3-header) &key instream &allow-other-keys)
  143. "Fill in an mp3-header from INSTREAM."
  144. (log5:with-context "id3-header-initializer"
  145. (with-slots (version revision flags size ext-header frames v21-tag-header) me
  146. (stream-seek instream 128 :end)
  147. (when (string= "TAG" (stream-read-string-with-len instream 3))
  148. (log-id3-frame "looking at last 128 bytes at ~:d to try to read id3v21 header" (stream-seek instream))
  149. (handler-case
  150. (setf v21-tag-header (make-instance 'v21-tag-header :instream instream))
  151. (id3-frame-condition (c)
  152. (log-id3-frame "reading v21 got condition: ~a" c))))
  153. (stream-seek instream 0 :start)
  154. (when (string= "ID3" (stream-read-string-with-len instream 3))
  155. (setf version (stream-read-u8 instream))
  156. (setf revision (stream-read-u8 instream))
  157. (setf flags (stream-read-u8 instream))
  158. (setf size (stream-read-u32 instream :bits-per-byte 7))
  159. (when (header-unsynchronized-p flags)
  160. (log-id3-frame "header flags indicate unsync"))
  161. (assert (not (header-footer-p flags)) () "Can't decode ID3 footer's yet")
  162. (log-id3-frame "id3 header = ~a" (vpprint me nil))))))
  163. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; frames ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  164. ;;;
  165. ;;; General plan: for each frame type we are interested in, DEFCLASS a class with
  166. ;;; specfic naming convention: frame-xxx/frame-xxxx, where xxx is valid ID3V2.2 frame name
  167. ;;; and xxxx is a valid ID3V2.[34] frame name. Upon finding a frame name in an MP3 file,
  168. ;;; we can then do a FIND-CLASS on the "frame-xxx", and a MAKE-INSTANCE on the found class
  169. ;;; to read in that class (each defined class is assumed to have an INITIALIZE-INSTANCE method
  170. ;;; that reads in data to build class.
  171. ;;;
  172. ;;; Each frame class assumes that the STREAM being passed has been made sync-safe.
  173. ;;;
  174. ;;; For any class we don't want to parse (eg, haven't gotten around to it yet, etc), we create
  175. ;;; a RAW-FRAME class that can be subclassed. RAW-FRAME simply reads in the frame header, and then
  176. ;;; the frame "payload" as raw OCTETS.
  177. ;;;
  178. ;;; many ID3 tags are name/value pairs, with the name/value encoded in various ways
  179. ;;; this routine assumes that the "name" is always a string with a "normal" encoding (i.e. 0, 1, 2, or 3).
  180. ;;; The "value" field accepts "normal" encoding, but also accepts any negative number, which means read
  181. ;;; the bytes an raw octets.
  182. (defun get-name-value-pair (instream len name-encoding value-encoding)
  183. (log5:with-context "get-name-value-pair"
  184. (log-id3-frame "reading from ~:d, len ~:d, name-encoding = ~d, value-encoding = ~d" (stream-seek instream) len name-encoding value-encoding)
  185. (let* ((old-pos (stream-seek instream))
  186. (name (stream-read-string instream :encoding name-encoding))
  187. (name-len (- (stream-seek instream) old-pos))
  188. (value))
  189. (log-id3-frame "name = <~a>, name-len = ~d" name name-len)
  190. (setf value (if (>= value-encoding 0)
  191. (stream-read-string-with-len instream (- len name-len) :encoding value-encoding)
  192. (stream-read-sequence instream (- len name-len)))) ; if < 0, then just read as octets
  193. (values name value))))
  194. (defclass id3-frame ()
  195. ((pos :accessor pos :initarg :pos :documentation "the offset in the buffer were this frame was found")
  196. (id :accessor id :initarg :id :documentation "the 3-4 character name of this frame")
  197. (len :accessor len :initarg :len :documentation "the length of this frame")
  198. (version :accessor version :initarg :version :documentation "the ID3-HEADER version number stored here for convenience")
  199. (flags :accessor flags :initarg :flags :initform nil :documentation "the frame's flags"))
  200. (:documentation "Base class for an ID3 frame. Used for versions 2.2, 2.3, and 2.4"))
  201. ;;; The frame flags are the same for V22/V23
  202. (defmacro frame-23-altertag-p (frame-flags) `(logbitp 15 ,frame-flags))
  203. (defmacro frame-23-alterfile-p (frame-flags) `(logbitp 14 ,frame-flags))
  204. (defmacro frame-23-readonly-p (frame-flags) `(logbitp 13 ,frame-flags))
  205. (defmacro frame-23-compress-p (frame-flags) `(logbitp 7 ,frame-flags))
  206. (defmacro frame-23-encrypt-p (frame-flags) `(logbitp 6 ,frame-flags))
  207. (defmacro frame-23-group-p (frame-flags) `(logbitp 5 ,frame-flags))
  208. ;;; frame flags are different for 2.4. Also note, that some flags indicate that additional data
  209. ;;; follows the frame header and these must be read in the order of the flags
  210. (defmacro frame-24-altertag-p (frame-flags) `(logbitp 14 ,frame-flags)) ; no additional data
  211. (defmacro frame-24-alterfile-p (frame-flags) `(logbitp 13 ,frame-flags)) ; no additional data
  212. (defmacro frame-24-readonly-p (frame-flags) `(logbitp 12 ,frame-flags)) ; no additional data
  213. (defmacro frame-24-groupid-p (frame-flags) `(logbitp 6 ,frame-flags)) ; one byte added to frame
  214. (defmacro frame-24-compress-p (frame-flags) `(logbitp 3 ,frame-flags)) ; one byte added to frame
  215. (defmacro frame-24-encrypt-p (frame-flags) `(logbitp 2 ,frame-flags)) ; wonky case, may or may not be set, dependin on encryption type
  216. (defmacro frame-24-unsynch-p (frame-flags) `(logbitp 1 ,frame-flags)) ; *may* have a 4-byte field after header, iff datalen is set
  217. (defmacro frame-24-datalen-p (frame-flags) `(logbitp 0 ,frame-flags)) ; if unsynch is set and this too, 4-bytes are added to frame
  218. ;; NB version 2.2 does NOT have FLAGS field in a frame; hence, the ECASE
  219. (defun valid-frame-flags (header-version frame-flags)
  220. (ecase header-version
  221. (3 (zerop (logand #b0001111100011111 frame-flags)))
  222. (4 (zerop (logand #b1000111110110000 frame-flags)))))
  223. (defun print-frame-flags (version flags stream)
  224. (ecase version
  225. (2 (format stream "None, "))
  226. (3 (format stream
  227. "flags: 0x~4,'0x: ~:[0/~;tag-alter-preservation/~]~:[0/~;file-alter-preservation/~]~:[0/~;read-only/~]~:[0/~;compress/~]~:[0/~;encypt/~]~:[0~;group~], "
  228. flags
  229. (frame-23-altertag-p flags)
  230. (frame-23-alterfile-p flags)
  231. (frame-23-readonly-p flags)
  232. (frame-23-compress-p flags)
  233. (frame-23-encrypt-p flags)
  234. (frame-23-group-p flags)))
  235. (4 (format stream
  236. "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~], "
  237. flags
  238. (frame-24-altertag-p flags)
  239. (frame-24-alterfile-p flags)
  240. (frame-24-readonly-p flags)
  241. (frame-24-groupid-p flags)
  242. (frame-24-compress-p flags)
  243. (frame-24-encrypt-p flags)
  244. (frame-24-unsynch-p flags)
  245. (frame-24-datalen-p flags)))))
  246. (defun vpprint-frame-header (id3-frame)
  247. (with-output-to-string (stream)
  248. (with-slots (pos version id len flags) id3-frame
  249. (format stream "offset: ~:d, version = ~d, id: ~a, len: ~:d, ~a" pos version id len
  250. (if flags
  251. (print-frame-flags version flags stream)
  252. "flags: none")))))
  253. (defclass frame-raw (id3-frame)
  254. ((octets :accessor octets :initform nil))
  255. (:documentation "Frame class that slurps in frame contents w/no attempt to grok them"))
  256. (defmethod initialize-instance :after ((me frame-raw) &key instream)
  257. (log5:with-context "frame-raw"
  258. (with-slots (pos len octets) me
  259. (log-id3-frame "reading ~:d bytes from position ~:d" len pos)
  260. (setf octets (stream-read-sequence instream len))
  261. (log-id3-frame "frame: ~a" (vpprint me nil)))))
  262. (defmethod vpprint ((me frame-raw) stream)
  263. (with-slots (octets) me
  264. (format stream "frame-raw: ~a, ~a" (vpprint-frame-header me) (printable-array octets))))
  265. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; V2.2 frames ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  266. (defclass frame-buf (frame-raw) ())
  267. (defclass frame-cnt (frame-raw) ())
  268. (defclass frame-cra (frame-raw) ())
  269. (defclass frame-crm (frame-raw) ())
  270. (defclass frame-equ (frame-raw) ())
  271. (defclass frame-etc (frame-raw) ())
  272. (defclass frame-geo (frame-raw) ())
  273. (defclass frame-ipl (frame-raw) ())
  274. (defclass frame-lnk (frame-raw) ())
  275. (defclass frame-mci (frame-raw) ())
  276. (defclass frame-mll (frame-raw) ())
  277. (defclass frame-pop (frame-raw) ())
  278. (defclass frame-rev (frame-raw) ())
  279. (defclass frame-rva (frame-raw) ())
  280. (defclass frame-slt (frame-raw) ())
  281. (defclass frame-waf (frame-raw) ())
  282. (defclass frame-war (frame-raw) ())
  283. (defclass frame-was (frame-raw) ())
  284. (defclass frame-wcm (frame-raw) ())
  285. (defclass frame-wcp (frame-raw) ())
  286. (defclass frame-wpb (frame-raw) ())
  287. (defclass frame-stc (frame-raw) ())
  288. ;;; V22 User defined... "WXX"
  289. ;;; Text encoding $xx
  290. ;;; Description <textstring> $00 (00)
  291. ;;; URL <textstring>
  292. ;;; Identical to TXX
  293. (defclass frame-wxx (frame-txx) ())
  294. ;; V22 COM frames
  295. ;; Comment "COM"
  296. ;; Text encoding $xx
  297. ;; Language $xx xx xx
  298. ;; Short content description <textstring> $00 (00)
  299. ;; The actual text <textstring>
  300. (defclass frame-com (id3-frame)
  301. ((encoding :accessor encoding)
  302. (lang :accessor lang)
  303. (desc :accessor desc)
  304. (val :accessor val)))
  305. (defmethod initialize-instance :after ((me frame-com) &key instream)
  306. (log5:with-context "frame-com"
  307. (with-slots (len encoding lang desc val) me
  308. (setf encoding (stream-read-u8 instream))
  309. (setf lang (stream-read-iso-string-with-len instream 3))
  310. (multiple-value-bind (n v) (get-name-value-pair instream (- len 1 3) encoding encoding)
  311. (setf desc n)
  312. ;; iTunes broken-ness... for frame-coms, there can be an additional null or two at the end
  313. (setf val (upto-null v)))
  314. (log-id3-frame "encoding = ~d, lang = <~a>, desc = <~a>, text = <~a>" encoding lang desc val))))
  315. (defmethod vpprint ((me frame-com) stream)
  316. (with-slots (len encoding lang desc val) me
  317. (format stream "frame-com: ~a, encoding = ~d, lang = <~a> (~a), desc = <~a>, val = <~a>"
  318. (vpprint-frame-header me) encoding lang (get-iso-639-2-language lang) desc val)))
  319. ;;; ULT's are same format as COM's... XXX rewrite this as suggested in comment at bottom of this file
  320. ;;; V22 unsynced lyrics/text "ULT"
  321. ;;; Text encoding $xx
  322. ;;; Language $xx xx xx
  323. ;;; Content descriptor <textstring> $00 (00)
  324. ;;; Lyrics/text <textstring>
  325. (defclass frame-ult (frame-com) ())
  326. ;; V22 PIC frames
  327. ;; Attached picture "PIC"
  328. ;; Text encoding $xx
  329. ;; Image format $xx xx xx
  330. ;; Picture type $xx
  331. ;; Description <textstring> $00 (00)
  332. ;; Picture data <binary data>
  333. (defclass frame-pic (id3-frame)
  334. ((encoding :accessor encoding)
  335. (img-format :accessor img-format)
  336. (type :accessor type)
  337. (desc :accessor desc)
  338. (data :accessor data)))
  339. (defmethod initialize-instance :after ((me frame-pic) &key instream)
  340. (log5:with-context "frame-pic"
  341. (with-slots (id len encoding img-format type desc data) me
  342. (setf encoding (stream-read-u8 instream))
  343. (setf img-format (stream-read-iso-string-with-len instream 3))
  344. (setf type (stream-read-u8 instream))
  345. (multiple-value-bind (n v) (get-name-value-pair instream (- len 5) encoding -1)
  346. (setf desc n)
  347. (setf data v)
  348. (log-id3-frame "encoding: ~d, img-format = <~a>, type = ~d (~a), desc = <~a>, value = ~a"
  349. encoding img-format type (get-picture-type type) desc (printable-array data))))))
  350. (defmethod vpprint ((me frame-pic) stream)
  351. (with-slots (encoding img-format type desc data) me
  352. (format stream "frame-pic: ~a, encoding ~d, img-format type: <~a>, picture type: ~d (~a), description <~a>, data: ~a"
  353. (vpprint-frame-header me) encoding img-format type (get-picture-type type) desc (printable-array data))))
  354. ;; Version 2, 3, or 4 generic text-info frames
  355. ;; Text information identifier "T00" - "TZZ", excluding "TXX", or "T000 - TZZZ", excluding "TXXX"
  356. ;; Text encoding $xx
  357. ;; Information <textstring>
  358. (defclass frame-text-info (id3-frame)
  359. ((encoding :accessor encoding)
  360. (info :accessor info))
  361. (:documentation "V2/V3/V4 T00-TZZ and T000-TZZZ frames, but not TXX or TXXX"))
  362. (defmethod initialize-instance :after ((me frame-text-info) &key instream)
  363. (log5:with-context "frame-text-info"
  364. (with-slots (version flags len encoding info) me
  365. (let ((read-len len))
  366. ;; In version 4 frames, each frame may also have an unsync flag. since we have unsynced already
  367. ;; the only thing we need to do here is check for the optional DATALEN field. If it is present
  368. ;; then it has the actual number of octets to read
  369. (when (and (= version 4) (frame-24-unsynch-p flags))
  370. (if (frame-24-datalen-p flags)
  371. (setf read-len (stream-read-u32 instream :bits-per-byte 7))))
  372. (setf encoding (stream-read-u8 instream))
  373. (setf info (stream-read-string-with-len instream (1- read-len) :encoding encoding)))
  374. ;; A null is ok, but according to the "spec", you're supposed to ignore anything after a 'Null'
  375. (log-id3-frame "made text-info-frame: ~a" (vpprint me nil))
  376. (setf info (upto-null info))
  377. (log-id3-frame "encoding = ~d, info = <~a>" encoding info))))
  378. (defmethod vpprint ((me frame-text-info) stream)
  379. (with-slots (len encoding info) me
  380. (format stream "frame-text-info: ~a, encoding = ~d, info = <~a>" (vpprint-frame-header me) encoding info)))
  381. (defclass frame-tal (frame-text-info) ())
  382. (defclass frame-tbp (frame-text-info) ())
  383. (defclass frame-tcm (frame-text-info) ())
  384. (defclass frame-tco (frame-text-info) ())
  385. (defclass frame-tcp (frame-text-info) ())
  386. (defclass frame-tcr (frame-text-info) ())
  387. (defclass frame-tda (frame-text-info) ())
  388. (defclass frame-tdy (frame-text-info) ())
  389. (defclass frame-ten (frame-text-info) ())
  390. (defclass frame-tft (frame-text-info) ())
  391. (defclass frame-tim (frame-text-info) ())
  392. (defclass frame-tke (frame-text-info) ())
  393. (defclass frame-tla (frame-text-info) ())
  394. (defclass frame-tle (frame-text-info) ())
  395. (defclass frame-tmt (frame-text-info) ())
  396. (defclass frame-toa (frame-text-info) ())
  397. (defclass frame-tof (frame-text-info) ())
  398. (defclass frame-tol (frame-text-info) ())
  399. (defclass frame-tor (frame-text-info) ())
  400. (defclass frame-tot (frame-text-info) ())
  401. (defclass frame-tp1 (frame-text-info) ())
  402. (defclass frame-tp2 (frame-text-info) ())
  403. (defclass frame-tp3 (frame-text-info) ())
  404. (defclass frame-tp4 (frame-text-info) ())
  405. (defclass frame-tpa (frame-text-info) ())
  406. (defclass frame-tpb (frame-text-info) ())
  407. (defclass frame-trc (frame-text-info) ())
  408. (defclass frame-trd (frame-text-info) ())
  409. (defclass frame-trk (frame-text-info) ())
  410. (defclass frame-tsi (frame-text-info) ())
  411. (defclass frame-tss (frame-text-info) ())
  412. (defclass frame-tt1 (frame-text-info) ())
  413. (defclass frame-tt2 (frame-text-info) ())
  414. (defclass frame-tt3 (frame-text-info) ())
  415. (defclass frame-txt (frame-text-info) ())
  416. (defclass frame-tye (frame-text-info) ())
  417. ;; V22 User defined "TXX" frames
  418. ;; Text encoding $xx
  419. ;; Description <textstring> $00 (00)
  420. ;; Value <textstring>
  421. (defclass frame-txx (id3-frame)
  422. ((encoding :accessor encoding)
  423. (desc :accessor desc)
  424. (val :accessor val))
  425. (:documentation "TXX is the only frame starting with a 'T' that has a different format"))
  426. (defmethod initialize-instance :after ((me frame-txx) &key instream)
  427. (log5:with-context "frame-txx"
  428. (with-slots (len encoding desc val) me
  429. (setf encoding (stream-read-u8 instream))
  430. (multiple-value-bind (n v) (get-name-value-pair instream (1- len) encoding encoding)
  431. (setf desc n)
  432. (setf val v)
  433. (log-id3-frame "encoding = ~d, desc = <~a>, val = <~a>" encoding desc val)))))
  434. (defmethod vpprint ((me frame-txx) stream)
  435. (with-slots (len encoding desc val) me
  436. (format stream "frame-txx: ~a, encoding = ~d, desc = <~a>, val = <~a>" (vpprint-frame-header me) encoding desc val)))
  437. ;;; V22 unique file identifier "UFI"
  438. ;;; Owner identifier <textstring> $00
  439. ;;; Identifier <up to 64 bytes binary data>
  440. (defclass frame-ufi (id3-frame)
  441. ((name :accessor name)
  442. (value :accessor value))
  443. (:documentation "Unique File Identifier"))
  444. (defmethod initialize-instance :after ((me frame-ufi) &key instream)
  445. (log5:with-context "frame-ufi"
  446. (with-slots (id len name value) me
  447. (multiple-value-bind (n v) (get-name-value-pair instream len 0 -1)
  448. (setf name n)
  449. (setf value v))
  450. (log-id3-frame "name = <~a>, value = ~a" name (printable-array value)))))
  451. (defmethod vpprint ((me frame-ufi) stream)
  452. (with-slots (id len name value) me
  453. (format stream "frame-ufi: ~a, name: <~a>, value: ~a" (vpprint-frame-header me) name (printable-array value))))
  454. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; V23/V24 frames ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  455. (defclass frame-aenc (frame-raw) ())
  456. (defclass frame-aspi (frame-raw) ())
  457. (defclass frame-comr (frame-raw) ())
  458. (defclass frame-encr (frame-raw) ())
  459. (defclass frame-equ2 (frame-raw) ())
  460. (defclass frame-equa (frame-raw) ())
  461. (defclass frame-etco (frame-raw) ())
  462. (defclass frame-geob (frame-raw) ())
  463. (defclass frame-grid (frame-raw) ())
  464. (defclass frame-ipls (frame-raw) ())
  465. (defclass frame-link (frame-raw) ())
  466. (defclass frame-mcdi (frame-raw) ())
  467. (defclass frame-mllt (frame-raw) ())
  468. (defclass frame-ncon (frame-raw) ())
  469. (defclass frame-owne (frame-raw) ())
  470. (defclass frame-popm (frame-raw) ())
  471. (defclass frame-poss (frame-raw) ())
  472. (defclass frame-rbuf (frame-raw) ())
  473. (defclass frame-rva2 (frame-raw) ())
  474. (defclass frame-rvad (frame-raw) ())
  475. (defclass frame-rvrb (frame-raw) ())
  476. (defclass frame-seek (frame-raw) ())
  477. (defclass frame-sign (frame-raw) ())
  478. (defclass frame-sylt (frame-raw) ())
  479. (defclass frame-sytc (frame-raw) ())
  480. (defclass frame-user (frame-raw) ())
  481. ;;; V23/V24 text-info frames
  482. (defclass frame-talb (frame-text-info) ())
  483. (defclass frame-tbpm (frame-text-info) ())
  484. (defclass frame-tcmp (frame-text-info) ())
  485. (defclass frame-tcom (frame-text-info) ())
  486. (defclass frame-tcon (frame-text-info) ())
  487. (defclass frame-tcop (frame-text-info) ())
  488. (defclass frame-tdat (frame-text-info) ())
  489. (defclass frame-tden (frame-text-info) ())
  490. (defclass frame-tdly (frame-text-info) ())
  491. (defclass frame-tdor (frame-text-info) ())
  492. (defclass frame-tdrc (frame-text-info) ())
  493. (defclass frame-tdrl (frame-text-info) ())
  494. (defclass frame-tdtg (frame-text-info) ())
  495. (defclass frame-tenc (frame-text-info) ())
  496. (defclass frame-text (frame-text-info) ())
  497. (defclass frame-tflt (frame-text-info) ())
  498. (defclass frame-time (frame-text-info) ())
  499. (defclass frame-tipl (frame-text-info) ())
  500. (defclass frame-tit1 (frame-text-info) ())
  501. (defclass frame-tit2 (frame-text-info) ())
  502. (defclass frame-tit3 (frame-text-info) ())
  503. (defclass frame-tkey (frame-text-info) ())
  504. (defclass frame-tlan (frame-text-info) ())
  505. (defclass frame-tlen (frame-text-info) ())
  506. (defclass frame-tmcl (frame-text-info) ())
  507. (defclass frame-tmed (frame-text-info) ())
  508. (defclass frame-tmoo (frame-text-info) ())
  509. (defclass frame-toal (frame-text-info) ())
  510. (defclass frame-tofn (frame-text-info) ())
  511. (defclass frame-toly (frame-text-info) ())
  512. (defclass frame-tope (frame-text-info) ())
  513. (defclass frame-tory (frame-text-info) ())
  514. (defclass frame-town (frame-text-info) ())
  515. (defclass frame-tpe1 (frame-text-info) ())
  516. (defclass frame-tpe2 (frame-text-info) ())
  517. (defclass frame-tpe3 (frame-text-info) ())
  518. (defclass frame-tpe4 (frame-text-info) ())
  519. (defclass frame-tpos (frame-text-info) ())
  520. (defclass frame-tpro (frame-text-info) ())
  521. (defclass frame-tpub (frame-text-info) ())
  522. (defclass frame-trda (frame-text-info) ())
  523. (defclass frame-trsn (frame-text-info) ())
  524. (defclass frame-trso (frame-text-info) ())
  525. (defclass frame-tsoa (frame-text-info) ())
  526. (defclass frame-tsop (frame-text-info) ())
  527. (defclass frame-tsot (frame-text-info) ())
  528. (defclass frame-tsst (frame-text-info) ())
  529. (defclass frame-tsse (frame-text-info) ())
  530. (defclass frame-tsrc (frame-text-info) ())
  531. (defclass frame-tsiz (frame-text-info) ())
  532. (defclass frame-tyer (frame-text-info) ())
  533. (defclass frame-trck (frame-text-info) ())
  534. (defparameter *picture-type*
  535. '("Other"
  536. "32x32 pixels 'file icon' (PNG only)"
  537. "Other file icon"
  538. "Cover (front)"
  539. "Cover (back)"
  540. "Leaflet page"
  541. "Media (e.g. lable side of CD)"
  542. "Lead artist/lead performer/soloist"
  543. "Artist/performer"
  544. "Conductor"
  545. "Band/Orchestra"
  546. "Composer"
  547. "Lyricist/text writer"
  548. "Recording Location"
  549. "During recording"
  550. "During performance"
  551. "Movie/video screen capture"
  552. "A bright coloured fish" ; how do you know the fish is intelligent? :)
  553. "Illustration"
  554. "Band/artist logotype"
  555. "Publisher/Studio logotype"))
  556. (defun get-picture-type (n)
  557. "Function to return picture types for APIC frames"
  558. (if (and (>= n 0) (< n (length *picture-type*)))
  559. (nth n *picture-type*)
  560. "Unknown"))
  561. ;; V23/V24 APIC frames
  562. ;; <Header for 'Attached picture', ID: "APIC">
  563. ;; Text encoding $xx
  564. ;; MIME type <text string> $00
  565. ;; Picture type $xx
  566. ;; Description <text string according to encoding> $00 (00)
  567. ;; Picture data <binary data>
  568. (defclass frame-apic (id3-frame)
  569. ((encoding :accessor encoding)
  570. (mime :accessor mime)
  571. (type :accessor type)
  572. (desc :accessor desc)
  573. (data :accessor data))
  574. (:documentation "Holds an attached picture (cover art)"))
  575. (defmethod initialize-instance :after ((me frame-apic) &key instream)
  576. (log5:with-context "frame-apic"
  577. (with-slots (id len encoding mime type desc data) me
  578. (setf encoding (stream-read-u8 instream))
  579. (setf mime (stream-read-iso-string instream))
  580. (setf type (stream-read-u8 instream))
  581. (multiple-value-bind (n v) (get-name-value-pair instream (- len 1 (length mime) 1 1) encoding -1)
  582. (setf desc n)
  583. (setf data v)
  584. (log-id3-frame "enoding = ~d, mime = <~a>, type = ~d (~a), desc = <~a>, data = ~a" encoding mime type (get-picture-type type) desc (printable-array data))))))
  585. (defmethod vpprint ((me frame-apic) stream)
  586. (with-slots (encoding mime type desc data) me
  587. (format stream "frame-apic: ~a, encoding ~d, mime type: ~a, picture type: ~d (~a), description <~a>, data: ~a"
  588. (vpprint-frame-header me) encoding mime type (get-picture-type type) desc (printable-array data))))
  589. ;;; V23/V24 COMM frames
  590. ;;; <Header for 'Comment', ID: "COMM">
  591. ;;; Text encoding $xx
  592. ;;; Language $xx xx xx
  593. ;;; Short content descrip. <text string according to encoding> $00 (00)
  594. ;;; The actual text <full text string according to encoding>
  595. (defclass frame-comm (id3-frame)
  596. ((encoding :accessor encoding)
  597. (lang :accessor lang)
  598. (desc :accessor desc)
  599. (val :accessor val))
  600. (:documentation "V23/4 Comment frame"))
  601. (defmethod initialize-instance :after ((me frame-comm) &key instream)
  602. (log5:with-context "frame-comm"
  603. (with-slots (encoding lang len desc val) me
  604. (setf encoding (stream-read-u8 instream))
  605. (setf lang (stream-read-iso-string-with-len instream 3))
  606. (multiple-value-bind (n v) (get-name-value-pair instream (- len 1 3) encoding encoding)
  607. (setf desc n)
  608. ;; iTunes broken-ness... for frame-coms, there can be an additional null or two at the end
  609. (setf val (upto-null v)))
  610. (log-id3-frame "encoding = ~d, lang = <~a>, desc = <~a>, val = <~a>" encoding lang desc val))))
  611. (defmethod vpprint ((me frame-comm) stream)
  612. (with-slots (encoding lang desc val) me
  613. (format stream "frame-comm: ~a, encoding: ~d, lang: <~a> (~a), desc = <~a>, val = <~a>"
  614. (vpprint-frame-header me) encoding lang (get-iso-639-2-language lang) desc val)))
  615. ;;; Unsynchronized lyrics frames look very much like comment frames...
  616. (defclass frame-uslt (frame-comm) ())
  617. ;;; V23/24 PCNT frames
  618. ;;; <Header for 'Play counter', ID: "PCNT">
  619. ;;; Counter $xx xx xx xx (xx ...)
  620. (defclass frame-pcnt (id3-frame)
  621. ((play-count :accessor play-count))
  622. (:documentation "Play count frame"))
  623. (defmethod initialize-instance :after ((me frame-pcnt) &key instream)
  624. (log5:with-context "frame-pcnt"
  625. (with-slots (play-count len) me
  626. (assert (= 4 len) () "Ran into a play count with ~d bytes" len)
  627. (setf play-count (stream-read-u32 instream)) ; probably safe---play count *can* be longer than 4 bytes, but...
  628. (log-id3-frame "play count = <~d>" play-count))))
  629. (defmethod vpprint ((me frame-pcnt) stream)
  630. (with-slots (play-count) me
  631. (format stream "frame-pcnt: ~a, count = ~d" (vpprint-frame-header me) play-count)))
  632. ;;; V23/V24 PRIV frames
  633. ;;; <Header for 'Private frame', ID: "PRIV">
  634. ;;; Owner identifier <text string> $00
  635. ;;; The private data <binary data>
  636. (defclass frame-priv (id3-frame)
  637. ((name :accessor name)
  638. (value :accessor value))
  639. (:documentation "Private frame"))
  640. (defmethod initialize-instance :after ((me frame-priv) &key instream)
  641. (log5:with-context "frame-priv"
  642. (with-slots (id len name value) me
  643. (multiple-value-bind (n v) (get-name-value-pair instream len 0 -1)
  644. (setf name n)
  645. (setf value v)
  646. (log-id3-frame "name = <~a>, value = <~a>" name value)))))
  647. (defmethod vpprint ((me frame-priv) stream)
  648. (with-slots (id len name value) me
  649. (format stream "frame-priv: ~a, name: <~a>, data: ~a" (vpprint-frame-header me) name (printable-array value))))
  650. ;; V23/V24 TXXX frames
  651. ;; <Header for 'User defined text information frame', ID: "TXXX">
  652. ;; Text encoding $xx
  653. ;; Description <text string according to encoding> $00 (00)
  654. ;; Value <text string according to encoding>
  655. (defclass frame-txxx (id3-frame)
  656. ((encoding :accessor encoding)
  657. (desc :accessor desc)
  658. (val :accessor val))
  659. (:documentation "TXXX frame"))
  660. (defmethod initialize-instance :after ((me frame-txxx) &key instream)
  661. (log5:with-context "frame-txxx"
  662. (with-slots (encoding len desc val) me
  663. (setf encoding (stream-read-u8 instream))
  664. (multiple-value-bind (n v) (get-name-value-pair instream
  665. (- len 1)
  666. encoding
  667. encoding)
  668. (setf desc n)
  669. (setf val v))
  670. (log-id3-frame "encoding = ~d, desc = <~a>, value = <~a>" encoding desc val))))
  671. (defmethod vpprint ((me frame-txxx) stream)
  672. (format stream "frame-txxx: ~a, <~a/~a>" (vpprint-frame-header me) (desc me) (val me)))
  673. ;; V23/V24 UFID frames
  674. ;; <Header for 'Unique file identifier', ID: "UFID">
  675. ;; Owner identifier <text string> $00
  676. ;; Identifier <up to 64 bytes binary data>
  677. (defclass frame-ufid (id3-frame)
  678. ((name :accessor name)
  679. (value :accessor value))
  680. (:documentation "Unique file identifier frame"))
  681. (defmethod initialize-instance :after ((me frame-ufid) &key instream)
  682. (log5:with-context "frame-ufid"
  683. (with-slots (id len name value) me
  684. (multiple-value-bind (n v) (get-name-value-pair instream len 0 -1)
  685. (setf name n)
  686. (setf value v))
  687. (log-id3-frame "name = <~a>, value = ~a" name (printable-array value)))))
  688. (defmethod vpprint ((me frame-ufid) stream)
  689. (with-slots (id len name value) me
  690. (format stream "frame-ufid: ~a, name: <~a>, value: ~a" (vpprint-frame-header me) name (printable-array value))))
  691. ;;; V23/V24 URL frame
  692. ;;; <Header for 'URL link frame', ID: "W000" - "WZZZ", excluding "WXXX" described in 4.3.2.>
  693. ;;; URL <text string>
  694. (defclass frame-url-link (id3-frame)
  695. ((url :accessor url))
  696. (:documentation "URL link frame"))
  697. (defmethod initialize-instance :after ((me frame-url-link) &key instream)
  698. (with-slots (id len url) me
  699. (log5:with-context "url"
  700. (setf url (stream-read-iso-string-with-len instream len))
  701. (log-id3-frame "url = <~a>" url))))
  702. (defmethod vpprint ((me frame-url-link) stream)
  703. (with-slots (url) me
  704. (format stream "frame-url-link: ~a, url: ~a" (vpprint-frame-header me) url)))
  705. ;;; V23/V24 frames URL link frames
  706. (defclass frame-wcom (frame-url-link) ())
  707. (defclass frame-wcop (frame-url-link) ())
  708. (defclass frame-woaf (frame-url-link) ())
  709. (defclass frame-woar (frame-url-link) ())
  710. (defclass frame-woas (frame-url-link) ())
  711. (defclass frame-wors (frame-url-link) ())
  712. (defclass frame-wpay (frame-url-link) ())
  713. (defclass frame-wpub (frame-url-link) ())
  714. ;;; Identical to frame-txx
  715. (defclass frame-wxxx (frame-txx) ())
  716. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; frame finding/creation ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  717. (defun possibly-valid-frame-id? (frame-id)
  718. "test to see if a string is a potentially valid frame id"
  719. (labels ((numeric-char-p (c)
  720. (let ((code (char-code c)))
  721. (and (>= code (char-code #\0))
  722. (<= code (char-code #\9))))))
  723. ;; test each octet to see if it is alphanumeric
  724. (dotimes (i (length frame-id))
  725. (let ((c (aref frame-id i)))
  726. (when (not (or (numeric-char-p c)
  727. (and (alpha-char-p c) (upper-case-p c))))
  728. (return-from possibly-valid-frame-id? nil))))
  729. t))
  730. (defun find-frame-class (id)
  731. "Search by concatenating 'frame-' with ID and look for that symbol in this package"
  732. (log5:with-context "find-frame-class"
  733. (log-id3-frame "looking for class <~a>" id)
  734. (let ((found-class-symbol (find-symbol (string-upcase (concatenate 'string "frame-" id)) :ID3-FRAME))
  735. found-class)
  736. ;; if we found the class name, return the class (to be used for MAKE-INSTANCE)
  737. (when found-class-symbol
  738. (setf found-class (find-class found-class-symbol))
  739. (log-id3-frame "found class: ~a" found-class)
  740. (return-from find-frame-class found-class))
  741. (log-id3-frame "didn't find class, checking general cases")
  742. ;; if not a "normal" frame-id, look at general cases of
  743. ;; starting with a 'T' or a 'W'
  744. (setf found-class (case (aref id 0)
  745. (#\T (log-id3-frame "assuming text-info") (find-class (find-symbol "FRAME-TEXT-INFO" :ID3-FRAME)))
  746. (#\W (log-id3-frame "assuming url-link") (find-class (find-symbol "FRAME-URL-LINK" :ID3-FRAME)))
  747. (t
  748. ;; we don't recognize the frame name. if it could possibly be a real frame name,
  749. ;; then just read it raw
  750. (when (possibly-valid-frame-id? id)
  751. (log-id3-frame "just reading raw")
  752. (find-class (find-symbol "FRAME-RAW" :ID3-FRAME))))))
  753. (log-id3-frame "general case for id <~a> is ~a" id found-class)
  754. found-class)))
  755. (defun make-frame (version instream)
  756. "Create an appropriate mp3 frame by reading data from INSTREAM."
  757. (log5:with-context "make-frame"
  758. (let* ((pos (stream-seek instream))
  759. (byte (stream-read-u8 instream))
  760. frame-name frame-len frame-flags frame-class)
  761. (log-id3-frame "reading from position ~:d (size of stream = ~:d)" pos (stream-size instream))
  762. (when (zerop byte) ; XXX should this be correlated to PADDING in the extended header???
  763. (log-id3-frame "hit padding of size ~:d while making a frame" (- (stream-size instream) pos))
  764. (return-from make-frame nil)) ; hit padding
  765. (setf frame-name
  766. (concatenate 'string (string (code-char byte)) (stream-read-string-with-len instream (ecase version (2 2) (3 3) (4 3)))))
  767. (setf frame-len (ecase version
  768. (2 (stream-read-u24 instream))
  769. (3 (stream-read-u32 instream))
  770. (4 (stream-read-u32 instream :bits-per-byte 7))))
  771. (when (or (= version 3) (= version 4))
  772. (setf frame-flags (stream-read-u16 instream))
  773. (when (not (valid-frame-flags version frame-flags))
  774. (log-id3-frame "Invalid frame flags found ~a, will ignore" (print-frame-flags version frame-flags nil))
  775. (warn-user "Invalid frame flags found ~a, will ignore" (print-frame-flags version frame-flags nil))))
  776. (log-id3-frame "making frame: id:~a, version: ~d, len: ~:d, flags: ~a"
  777. frame-name version frame-len
  778. (print-frame-flags version frame-flags nil))
  779. (setf frame-class (find-frame-class frame-name))
  780. ;; edge case where found a frame name, but it is not valid or where making this frame
  781. ;; would blow past the end of the file/buffer
  782. (when (or (> (+ (stream-seek instream) frame-len) (stream-size instream))
  783. (null frame-class))
  784. (error 'id3-frame-condition :message "bad frame found" :object frame-name :location pos))
  785. (make-instance frame-class :pos pos :version version :id frame-name :len frame-len :flags frame-flags :instream instream))))
  786. (defmethod find-id3-frames ((mp3-file mp3-file-stream))
  787. "With an open mp3-file, make sure it is in fact an MP3 file, then read it's header and frames"
  788. (labels ((read-loop (version stream)
  789. (log5:with-context "read-loop-in-find-id3-frames"
  790. (log-id3-frame "Starting loop through ~:d bytes" (stream-size stream))
  791. (let (frames this-frame)
  792. (do ()
  793. ((>= (stream-seek stream) (stream-size stream)))
  794. (handler-case
  795. (progn
  796. (setf this-frame (make-frame version stream))
  797. (when (null this-frame)
  798. (log-id3-frame "hit padding: returning ~d frames" (length frames))
  799. (return-from read-loop (values t (nreverse frames))))
  800. (log-id3-frame "bottom of read-loop: pos = ~:d, size = ~:d" (stream-seek stream) (stream-size stream))
  801. (push this-frame frames))
  802. (condition (c)
  803. (log-id3-frame "got condition ~a when making frame" c)
  804. (return-from read-loop (values nil (nreverse frames))))))
  805. (log-id3-frame "Succesful read: returning ~d frames" (length frames))
  806. (values t (nreverse frames)))))) ; reverse this so we have frames in "file order"
  807. (log5:with-context "find-id3-frames"
  808. (log-id3-frame "~a is a valid mp3 file" (fn mp3-file))
  809. (setf (id3-header mp3-file) (make-instance 'id3-header :instream mp3-file))
  810. (with-slots (size ext-header frames flags version) (id3-header mp3-file)
  811. ;; At this point, we switch from reading the file stream and create a memory stream
  812. ;; rationale: it may need to be unsysnc'ed and it helps prevent run-away reads with
  813. ;; mis-formed frames
  814. (when (not (zerop size))
  815. (let ((mem-stream (make-mem-stream (stream-read-sequence mp3-file size
  816. :bits-per-byte (if (header-unsynchronized-p flags) 7 8)))))
  817. ;; Must make extended header here since it is subject to unsynchronization.
  818. (when (header-extended-p flags)
  819. (setf ext-header (make-instance 'id3-extended-header :instream mem-stream)))
  820. ;; Start reading frames from memory stream
  821. (multiple-value-bind (_ok _frames) (read-loop version mem-stream)
  822. (if (not _ok)
  823. (warn-user "File ~a had errors finding mp3 frames. potentially missed frames!" (fn mp3-file)))
  824. (log-id3-frame "ok = ~a, returning ~d frames" _ok (length _frames))
  825. (setf frames _frames)
  826. _ok)))))))
  827. (defun map-id3-frames (mp3-file &key (func (constantly t)))
  828. "Iterates through the ID3 frames found in an MP3 file"
  829. (mapcar func (frames (id3-header mp3-file))))
  830. #|
  831. XXX
  832. Random ideas for rewrite:
  833. -might be simplest to read in frame payloads (sync'ed appropriately) for all frames and then move parsing into
  834. accessor methods? This might be easier to handle sync/compression/etc.
  835. -probably should rewrite name/value pairs as a mixin class? Or more broadly, there is a finite set of frame-encodings,
  836. so abstact to that, then subclass for frame-????
  837. |#