id3-frame.lisp 44 KB

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