id3-frame.lisp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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. (defmethod vpprint ((me id3-ext-header) stream)
  100. (with-slots (size flags padding crc) me
  101. (format stream "extended header: size: ~d, flags: ~x, padding ~:d, crc = ~x~%"
  102. size flags padding crc)))
  103. ;;; NB: v2.2 only really defines bit-7. It does document bit-6 as being the compression flag, but then states
  104. ;;; that if it is set, the software should "ignore the entire tag if this (bit-6) is set"
  105. (defmacro header-unsynchronized-p (flags) `(logbitp 7 ,flags)) ; all share this flag
  106. (defmacro header-extended-p (flags) `(logbitp 6 ,flags)) ; 2.3/2.4
  107. (defmacro header-experimental-p (flags) `(logbitp 5 ,flags)) ; 2.3/2.4
  108. (defmacro header-footer-p (flags) `(logbitp 4 ,flags)) ; 2.4 only
  109. (defmacro print-header-flags (stream flags)
  110. `(format ,stream "0x~2,'0x: ~:[0/~;unsynchronized-frames/~]~:[0/~;extended-header/~]~:[0/~;expermental-tag/~]~:[0~;footer-present~]"
  111. ,flags
  112. (header-unsynchronized-p ,flags)
  113. (header-extended-p ,flags)
  114. (header-experimental-p ,flags)
  115. (header-footer-p ,flags)))
  116. (defmethod vpprint ((me id3-header) stream)
  117. (with-slots (version revision flags v21-tag-header size ext-header frames) me
  118. (format stream "~a"
  119. (with-output-to-string (s)
  120. (format s "Header: version/revision: ~d/~d, flags: ~a, size = ~:d bytes; ~a; ~a"
  121. version revision (print-header-flags nil flags) size
  122. (if (header-extended-p flags)
  123. (concatenate 'string "Extended header: " (vpprint ext-header nil))
  124. "No extended header")
  125. (if v21-tag-header
  126. (concatenate 'string "V21 tag: " (vpprint v21-tag-header nil))
  127. "No V21 tag"))
  128. (when frames
  129. (format s "~&~4tFrames[~d]:~%" (length frames))
  130. (dolist (f frames)
  131. (format s "~8t~a~%" (vpprint f nil))))))))
  132. (defmethod initialize-instance :after ((me id3-header) &key instream &allow-other-keys)
  133. "Fill in an mp3-header from INSTREAM."
  134. (log5:with-context "id3-header-initializer"
  135. (with-slots (version revision flags size ext-header frames v21-tag-header) me
  136. (stream-seek instream 128 :end)
  137. (when (string= "TAG" (stream-read-string-with-len instream 3))
  138. (log-id3-frame "looking at last 128 bytes at ~:d to try to read id3v21 header" (stream-seek instream 0 :current))
  139. (handler-case
  140. (setf v21-tag-header (make-instance 'v21-tag-header :instream instream))
  141. (id3-frame-condition (c)
  142. (log-id3-frame "reading v21 got condition: ~a" c))))
  143. (stream-seek instream 0 :start)
  144. (when (string= "ID3" (stream-read-string-with-len instream 3))
  145. (setf version (stream-read-u8 instream))
  146. (setf revision (stream-read-u8 instream))
  147. (setf flags (stream-read-u8 instream))
  148. (setf size (stream-read-u32 instream :bits-per-byte 7))
  149. (when (header-unsynchronized-p flags)
  150. (log-id3-frame "header flags indicate unsync"))
  151. (assert (not (header-footer-p flags)) () "Can't decode ID3 footer's yet")
  152. (log-id3-frame "id3 header = ~a" (vpprint me nil))))))
  153. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; frames ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  154. ;;;
  155. ;;; General plan: for each frame type we are interested in, DEFCLASS a class with
  156. ;;; specfic naming convention: frame-xxx/frame-xxxx, where xxx is valid ID3V2.2 frame name
  157. ;;; and xxxx is a valid ID3V2.[34] frame name. Upon finding a frame name in an MP3 file,
  158. ;;; we can then do a FIND-CLASS on the "frame-xxx", and a MAKE-INSTANCE on the found class
  159. ;;; to read in that class (each defined class is assumed to have an INITIALIZE-INSTANCE method
  160. ;;; that reads in data to build class.
  161. ;;;
  162. ;;; Each frame class assumes that the STREAM being passed in is sync-safe.
  163. ;;;
  164. ;;; For any class we don't want to parse (eg, haven't gotten around to it yet, etc), we create
  165. ;;; a RAW-FRAME class that can be subclassed. RAW-FRAME simply reads in the frame header, and then
  166. ;;; the frame "payload" as raw OCTETS.
  167. ;;;
  168. ;;; many ID3 tags are name/value pairs, with the name/value encoded in various ways
  169. ;;; this routine assumes that the "name" is always a string with a "normal" encoding (i.e. 0, 1, 2, or 3).
  170. ;;; The "value", however, also accepts any negative number, which means read
  171. ;;; the bytes an raw octets.
  172. (defun get-name-value-pair (instream len name-encoding value-encoding)
  173. (log5:with-context "get-name-value-pair"
  174. (log-id3-frame "reading from ~:d, len ~:d, name-encoding = ~d, value-encoding = ~d" (stream-seek instream 0 :current) len name-encoding value-encoding)
  175. (let* ((old-pos (stream-seek instream 0 :current))
  176. (name (stream-read-string instream :encoding name-encoding))
  177. (name-len (- (stream-seek instream 0 :current) old-pos))
  178. (value))
  179. (log-id3-frame "name = <~a>, name-len = ~d" name name-len)
  180. (setf value (if (>= value-encoding 0)
  181. (stream-read-string-with-len instream (- len name-len) :encoding value-encoding)
  182. (stream-read-sequence instream (- len name-len)))) ; if < 0, then just read as octets
  183. (values name value))))
  184. (defclass id3-frame ()
  185. ((pos :accessor pos :initarg :pos :documentation "the offset in the buffer were this frame was found")
  186. (id :accessor id :initarg :id :documentation "the 3-4 character name of this frame")
  187. (len :accessor len :initarg :len :documentation "the length of this frame")
  188. (version :accessor version :initarg :version :documentation "the ID3-HEADER version number stored here for convenience")
  189. (flags :accessor flags :initarg :flags :initform nil :documentation "the frame's flags"))
  190. (:documentation "Base class for an ID3 frame. Used for versions 2.2, 2.3, and 2.4"))
  191. ;;; The frame flags are the same for V22/V23
  192. (defmacro frame-23-altertag-p (frame-flags) `(logbitp 15 ,frame-flags))
  193. (defmacro frame-23-alterfile-p (frame-flags) `(logbitp 14 ,frame-flags))
  194. (defmacro frame-23-readonly-p (frame-flags) `(logbitp 13 ,frame-flags))
  195. (defmacro frame-23-compress-p (frame-flags) `(logbitp 7 ,frame-flags))
  196. (defmacro frame-23-encrypt-p (frame-flags) `(logbitp 6 ,frame-flags))
  197. (defmacro frame-23-group-p (frame-flags) `(logbitp 5 ,frame-flags))
  198. ;;; frame flags are different for 2.4. Also note, that some flags indicate that additional data
  199. ;;; follows the frame header and these must be read in the order of the flags
  200. (defmacro frame-24-altertag-p (frame-flags) `(logbitp 14 ,frame-flags)) ; no additional data
  201. (defmacro frame-24-alterfile-p (frame-flags) `(logbitp 13 ,frame-flags)) ; no additional data
  202. (defmacro frame-24-readonly-p (frame-flags) `(logbitp 12 ,frame-flags)) ; no additional data
  203. (defmacro frame-24-groupid-p (frame-flags) `(logbitp 6 ,frame-flags)) ; one byte added to frame
  204. (defmacro frame-24-compress-p (frame-flags) `(logbitp 3 ,frame-flags)) ; one byte added to frame
  205. (defmacro frame-24-encrypt-p (frame-flags) `(logbitp 2 ,frame-flags)) ; wonky case, may or may not be set, dependin on encryption type
  206. (defmacro frame-24-unsynch-p (frame-flags) `(logbitp 1 ,frame-flags)) ; *may* have a 4-byte field after header, iff datalen is set
  207. (defmacro frame-24-datalen-p (frame-flags) `(logbitp 0 ,frame-flags)) ; if unsynch is set and this too, 4-bytes are added to frame
  208. ;; NB version 2.2 does NOT have FLAGS field in a frame; hence, the ECASE
  209. (defun valid-frame-flags (header-version frame-flags)
  210. (ecase header-version
  211. (3 (zerop (logand #b0001111100011111 frame-flags)))
  212. (4 (zerop (logand #b1000111110110000 frame-flags)))))
  213. (defun print-frame-flags (version flags stream)
  214. (ecase version
  215. (2 (format stream "None, "))
  216. (3 (format stream
  217. "flags: 0x~4,'0x: ~:[0/~;tag-alter-preservation/~]~:[0/~;file-alter-preservation/~]~:[0/~;read-only/~]~:[0/~;compress/~]~:[0/~;encypt/~]~:[0~;group~], "
  218. flags
  219. (frame-23-altertag-p flags)
  220. (frame-23-alterfile-p flags)
  221. (frame-23-readonly-p flags)
  222. (frame-23-compress-p flags)
  223. (frame-23-encrypt-p flags)
  224. (frame-23-group-p flags)))
  225. (4 (format stream
  226. "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~], "
  227. flags
  228. (frame-24-altertag-p flags)
  229. (frame-24-alterfile-p flags)
  230. (frame-24-readonly-p flags)
  231. (frame-24-groupid-p flags)
  232. (frame-24-compress-p flags)
  233. (frame-24-encrypt-p flags)
  234. (frame-24-unsynch-p flags)
  235. (frame-24-datalen-p flags)))))
  236. (defun vpprint-frame-header (id3-frame)
  237. (with-output-to-string (stream)
  238. (with-slots (pos version id len flags) id3-frame
  239. (format stream "offset: ~:d, version = ~d, id: ~a, len: ~:d, ~a" pos version id len
  240. (if flags
  241. (print-frame-flags version flags stream)
  242. "flags: none")))))
  243. (defclass frame-raw (id3-frame)
  244. ((octets :accessor octets :initform nil))
  245. (:documentation "Frame class that slurps in frame contents w/no attempt to grok them"))
  246. (defmethod initialize-instance :after ((me frame-raw) &key instream)
  247. (log5:with-context "frame-raw"
  248. (with-slots (pos len octets) me
  249. (log-id3-frame "reading ~:d bytes from position ~:d" len pos)
  250. (setf octets (stream-read-sequence instream len))
  251. (log-id3-frame "frame: ~a" (vpprint me nil)))))
  252. (defmethod vpprint ((me frame-raw) stream)
  253. (with-slots (octets) me
  254. (format stream "frame-raw: ~a, ~a" (vpprint-frame-header me) (printable-array octets))))
  255. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; V2.1 frames ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  256. ;;; v22 frames I haven't written a class for yet
  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. ;;; v23/v24 frame I haven't written a class for yet
  447. (defclass frame-aenc (frame-raw) ())
  448. (defclass frame-aspi (frame-raw) ())
  449. (defclass frame-comr (frame-raw) ())
  450. (defclass frame-encr (frame-raw) ())
  451. (defclass frame-equ2 (frame-raw) ())
  452. (defclass frame-equa (frame-raw) ())
  453. (defclass frame-etco (frame-raw) ())
  454. (defclass frame-geob (frame-raw) ())
  455. (defclass frame-grid (frame-raw) ())
  456. (defclass frame-ipls (frame-raw) ())
  457. (defclass frame-link (frame-raw) ())
  458. (defclass frame-mcdi (frame-raw) ())
  459. (defclass frame-mllt (frame-raw) ())
  460. (defclass frame-ncon (frame-raw) ())
  461. (defclass frame-owne (frame-raw) ())
  462. (defclass frame-popm (frame-raw) ())
  463. (defclass frame-poss (frame-raw) ())
  464. (defclass frame-rbuf (frame-raw) ())
  465. (defclass frame-rva2 (frame-raw) ())
  466. (defclass frame-rvad (frame-raw) ())
  467. (defclass frame-rvrb (frame-raw) ())
  468. (defclass frame-seek (frame-raw) ())
  469. (defclass frame-sign (frame-raw) ())
  470. (defclass frame-sylt (frame-raw) ())
  471. (defclass frame-sytc (frame-raw) ())
  472. (defclass frame-user (frame-raw) ())
  473. ;;; V23/V24 text-info frames
  474. (defclass frame-talb (frame-text-info) ())
  475. (defclass frame-tbpm (frame-text-info) ())
  476. (defclass frame-tcmp (frame-text-info) ())
  477. (defclass frame-tcom (frame-text-info) ())
  478. (defclass frame-tcon (frame-text-info) ())
  479. (defclass frame-tcop (frame-text-info) ())
  480. (defclass frame-tdat (frame-text-info) ())
  481. (defclass frame-tden (frame-text-info) ())
  482. (defclass frame-tdly (frame-text-info) ())
  483. (defclass frame-tdor (frame-text-info) ())
  484. (defclass frame-tdrc (frame-text-info) ())
  485. (defclass frame-tdrl (frame-text-info) ())
  486. (defclass frame-tdtg (frame-text-info) ())
  487. (defclass frame-tenc (frame-text-info) ())
  488. (defclass frame-text (frame-text-info) ())
  489. (defclass frame-tflt (frame-text-info) ())
  490. (defclass frame-time (frame-text-info) ())
  491. (defclass frame-tipl (frame-text-info) ())
  492. (defclass frame-tit1 (frame-text-info) ())
  493. (defclass frame-tit2 (frame-text-info) ())
  494. (defclass frame-tit3 (frame-text-info) ())
  495. (defclass frame-tkey (frame-text-info) ())
  496. (defclass frame-tlan (frame-text-info) ())
  497. (defclass frame-tlen (frame-text-info) ())
  498. (defclass frame-tmcl (frame-text-info) ())
  499. (defclass frame-tmed (frame-text-info) ())
  500. (defclass frame-tmoo (frame-text-info) ())
  501. (defclass frame-toal (frame-text-info) ())
  502. (defclass frame-tofn (frame-text-info) ())
  503. (defclass frame-toly (frame-text-info) ())
  504. (defclass frame-tope (frame-text-info) ())
  505. (defclass frame-tory (frame-text-info) ())
  506. (defclass frame-town (frame-text-info) ())
  507. (defclass frame-tpe1 (frame-text-info) ())
  508. (defclass frame-tpe2 (frame-text-info) ())
  509. (defclass frame-tpe3 (frame-text-info) ())
  510. (defclass frame-tpe4 (frame-text-info) ())
  511. (defclass frame-tpos (frame-text-info) ())
  512. (defclass frame-tpro (frame-text-info) ())
  513. (defclass frame-tpub (frame-text-info) ())
  514. (defclass frame-trda (frame-text-info) ())
  515. (defclass frame-trsn (frame-text-info) ())
  516. (defclass frame-trso (frame-text-info) ())
  517. (defclass frame-tsoa (frame-text-info) ())
  518. (defclass frame-tsop (frame-text-info) ())
  519. (defclass frame-tsot (frame-text-info) ())
  520. (defclass frame-tsst (frame-text-info) ())
  521. (defclass frame-tsse (frame-text-info) ())
  522. (defclass frame-tsrc (frame-text-info) ())
  523. (defclass frame-tsiz (frame-text-info) ())
  524. (defclass frame-tyer (frame-text-info) ())
  525. (defclass frame-trck (frame-text-info) ())
  526. (defparameter *picture-type*
  527. '("Other"
  528. "32x32 pixels 'file icon' (PNG only)"
  529. "Other file icon"
  530. "Cover (front)"
  531. "Cover (back)"
  532. "Leaflet page"
  533. "Media (e.g. lable side of CD)"
  534. "Lead artist/lead performer/soloist"
  535. "Artist/performer"
  536. "Conductor"
  537. "Band/Orchestra"
  538. "Composer"
  539. "Lyricist/text writer"
  540. "Recording Location"
  541. "During recording"
  542. "During performance"
  543. "Movie/video screen capture"
  544. "A bright coloured fish" ; how do you know the fish is smart :)
  545. "Illustration"
  546. "Band/artist logotype"
  547. "Publisher/Studio logotype"))
  548. (defun get-picture-type (n)
  549. "Function to return picture types for APIC frames"
  550. (if (and (>= n 0) (< n (length *picture-type*)))
  551. (nth n *picture-type*)
  552. "Unknown"))
  553. ;; V23/V24 APIC frames
  554. ;; <Header for 'Attached picture', ID: "APIC">
  555. ;; Text encoding $xx
  556. ;; MIME type <text string> $00
  557. ;; Picture type $xx
  558. ;; Description <text string according to encoding> $00 (00)
  559. ;; Picture data <binary data>
  560. (defclass frame-apic (id3-frame)
  561. ((encoding :accessor encoding)
  562. (mime :accessor mime)<
  563. (type :accessor type)
  564. (desc :accessor desc)
  565. (data :accessor data))
  566. (:documentation "Holds an attached picture (cover art)"))
  567. (defmethod initialize-instance :after ((me frame-apic) &key instream)
  568. (log5:with-context "frame-apic"
  569. (with-slots (id len encoding mime type desc data) me
  570. (setf encoding (stream-read-u8 instream))
  571. (setf mime (stream-read-iso-string instream))
  572. (setf type (stream-read-u8 instream))
  573. (multiple-value-bind (n v) (get-name-value-pair instream (- len 1 (length mime) 1 1) encoding -1)
  574. (setf desc n)
  575. (setf data v)
  576. (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))))))
  577. (defmethod vpprint ((me frame-apic) stream)
  578. (with-slots (encoding mime type desc data) me
  579. (format stream "frame-apic: ~a, encoding ~d, mime type: ~a, picture type: ~d (~a), description <~a>, data: ~a"
  580. (vpprint-frame-header me) encoding mime type (get-picture-type type) desc (printable-array data))))
  581. ;;; V23/V24 COMM frames
  582. ;;; <Header for 'Comment', ID: "COMM">
  583. ;;; Text encoding $xx
  584. ;;; Language $xx xx xx
  585. ;;; Short content descrip. <text string according to encoding> $00 (00)
  586. ;;; The actual text <full text string according to encoding>
  587. (defclass frame-comm (id3-frame)
  588. ((encoding :accessor encoding)
  589. (lang :accessor lang)
  590. (desc :accessor desc)
  591. (val :accessor val))
  592. (:documentation "Comment frame"))
  593. (defmethod initialize-instance :after ((me frame-comm) &key instream)
  594. (log5:with-context "frame-comm"
  595. (with-slots (encoding lang len desc val) me
  596. (setf encoding (stream-read-u8 instream))
  597. (setf lang (stream-read-iso-string-with-len instream 3))
  598. (multiple-value-bind (n v) (get-name-value-pair instream (- len 1 3) encoding encoding)
  599. (setf desc n)
  600. ;; iTunes broken-ness... for frame-coms, there can be an additional null or two at the end
  601. (setf val (upto-null v)))
  602. (log-id3-frame "encoding = ~d, lang = <~a>, desc = <~a>, val = <~a>" encoding lang desc val))))
  603. (defmethod vpprint ((me frame-comm) stream)
  604. (with-slots (encoding lang desc val) me
  605. (format stream "frame-comm: ~a, encoding: ~d, lang: <~a> (~a), desc = <~a>, val = <~a>"
  606. (vpprint-frame-header me) encoding lang (get-iso-639-2-language lang) desc val)))
  607. ;;; Unsynchronized lyrics frames look very much like comment frames...
  608. (defclass frame-uslt (frame-comm) ())
  609. ;;; v23/24 PCNT frames
  610. ;;; <Header for 'Play counter', ID: "PCNT">
  611. ;;; Counter $xx xx xx xx (xx ...)
  612. (defclass frame-pcnt (id3-frame)
  613. ((play-count :accessor play-count))
  614. (:documentation "Play count frame"))
  615. (defmethod initialize-instance :after ((me frame-pcnt) &key instream)
  616. (log5:with-context "frame-pcnt"
  617. (with-slots (play-count len) me
  618. (assert (= 4 len) () "Ran into a play count with ~d bytes" len)
  619. (setf play-count (stream-read-u32 instream)) ; probably safe---play count *can* be longer than 4 bytes, but...
  620. (log-id3-frame "play count = <~d>" play-count))))
  621. (defmethod vpprint ((me frame-pcnt) stream)
  622. (with-slots (play-count) me
  623. (format stream "frame-pcnt: ~a, count = ~d" (vpprint-frame-header me) play-count)))
  624. ;;; V23/V24 PRIV frames
  625. ;;; <Header for 'Private frame', ID: "PRIV">
  626. ;;; Owner identifier <text string> $00
  627. ;;; The private data <binary data>
  628. (defclass frame-priv (id3-frame)
  629. ((name :accessor name)
  630. (value :accessor value))
  631. (:documentation "Private frame"))
  632. (defmethod initialize-instance :after ((me frame-priv) &key instream)
  633. (log5:with-context "frame-priv"
  634. (with-slots (id len name value) me
  635. (multiple-value-bind (n v) (get-name-value-pair instream len 0 -1)
  636. (setf name n)
  637. (setf value v)
  638. (log-id3-frame "name = <~a>, value = <~a>" name value)))))
  639. (defmethod vpprint ((me frame-priv) stream)
  640. (with-slots (id len name value) me
  641. (format stream "frame-priv: ~a, name: <~a>, data: ~a" (vpprint-frame-header me) name (printable-array value))))
  642. ;; V23/V24 TXXX frames
  643. ;; <Header for 'User defined text information frame', ID: "TXXX">
  644. ;; Text encoding $xx
  645. ;; Description <text string according to encoding> $00 (00)
  646. ;; Value <text string according to encoding>
  647. (defclass frame-txxx (id3-frame)
  648. ((encoding :accessor encoding)
  649. (desc :accessor desc)
  650. (val :accessor val))
  651. (:documentation "TXXX frame"))
  652. (defmethod initialize-instance :after ((me frame-txxx) &key instream)
  653. (log5:with-context "frame-txxx"
  654. (with-slots (encoding len desc val) me
  655. (setf encoding (stream-read-u8 instream))
  656. (multiple-value-bind (n v) (get-name-value-pair instream
  657. (- len 1)
  658. encoding
  659. encoding)
  660. (setf desc n)
  661. (setf val v))
  662. (log-id3-frame "encoding = ~d, desc = <~a>, value = <~a>" encoding desc val))))
  663. (defmethod vpprint ((me frame-txxx) stream)
  664. (format stream "frame-txxx: ~a, <~a/~a>" (vpprint-frame-header me) (desc me) (val me)))
  665. ;; V23/V24 UFID frames
  666. ;; <Header for 'Unique file identifier', ID: "UFID">
  667. ;; Owner identifier <text string> $00
  668. ;; Identifier <up to 64 bytes binary data>
  669. (defclass frame-ufid (id3-frame)
  670. ((name :accessor name)
  671. (value :accessor value))
  672. (:documentation "Unique file identifier frame"))
  673. (defmethod initialize-instance :after ((me frame-ufid) &key instream)
  674. (log5:with-context "frame-ufid"
  675. (with-slots (id len name value) me
  676. (multiple-value-bind (n v) (get-name-value-pair instream len 0 -1)
  677. (setf name n)
  678. (setf value v))
  679. (log-id3-frame "name = <~a>, value = ~a" name (printable-array value)))))
  680. (defmethod vpprint ((me frame-ufid) stream)
  681. (with-slots (id len name value) me
  682. (format stream "frame-ufid: ~a, name: <~a>, value: ~a" (vpprint-frame-header me) name (printable-array value))))
  683. ;;; V23/V24 URL frame
  684. ;;; <Header for 'URL link frame', ID: "W000" - "WZZZ", excluding "WXXX" described in 4.3.2.>
  685. ;;; URL <text string>
  686. (defclass frame-url-link (id3-frame)
  687. ((url :accessor url))
  688. (:documentation "URL link frame"))
  689. (defmethod initialize-instance :after ((me frame-url-link) &key instream)
  690. (with-slots (id len url) me
  691. (log5:with-context "url"
  692. (setf url (stream-read-iso-string-with-len instream len))
  693. (log-id3-frame "url = <~a>" url))))
  694. (defmethod vpprint ((me frame-url-link) stream)
  695. (with-slots (url) me
  696. (format stream "frame-url-link: ~a, url: ~a" (vpprint-frame-header me) url)))
  697. ;;; V23/V24 frames URL link frames
  698. (defclass frame-wcom (frame-url-link) ())
  699. (defclass frame-wcop (frame-url-link) ())
  700. (defclass frame-woaf (frame-url-link) ())
  701. (defclass frame-woar (frame-url-link) ())
  702. (defclass frame-woas (frame-url-link) ())
  703. (defclass frame-wors (frame-url-link) ())
  704. (defclass frame-wpay (frame-url-link) ())
  705. (defclass frame-wpub (frame-url-link) ())
  706. ;;; Identical to frame-txx
  707. (defclass frame-wxxx (frame-txx) ())
  708. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; frame finding/creation ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  709. (defun possibly-valid-frame-id? (frame-id)
  710. "test to see if a string is a potentially valid frame id"
  711. (labels ((numeric-char-p (c)
  712. (let ((code (char-code c)))
  713. (and (>= code (char-code #\0))
  714. (<= code (char-code #\9))))))
  715. ;; test each octet to see if it is alphanumeric
  716. (dotimes (i (length frame-id))
  717. (let ((c (aref frame-id i)))
  718. (when (not (or (numeric-char-p c)
  719. (and (alpha-char-p c) (upper-case-p c))))
  720. (return-from possibly-valid-frame-id? nil))))
  721. t))
  722. (defun find-frame-class (id)
  723. "Search by concatenating 'frame-' with ID and look for that symbol in this package"
  724. (log5:with-context "find-frame-class"
  725. (log-id3-frame "looking for class <~a>" id)
  726. (let ((found-class-symbol (find-symbol (string-upcase (concatenate 'string "frame-" id)) :ID3-FRAME))
  727. found-class)
  728. ;; if we found the class name, return the class (to be used for MAKE-INSTANCE)
  729. (when found-class-symbol
  730. (setf found-class (find-class found-class-symbol))
  731. (log-id3-frame "found class: ~a" found-class)
  732. (return-from find-frame-class found-class))
  733. (log-id3-frame "didn't find class, checking general cases")
  734. ;; if not a "normal" frame-id, look at general cases of
  735. ;; starting with a 'T' or a 'W'
  736. (setf found-class (case (aref id 0)
  737. (#\T (log-id3-frame "assuming text-info") (find-class (find-symbol "FRAME-TEXT-INFO" :ID3-FRAME)))
  738. (#\W (log-id3-frame "assuming url-link") (find-class (find-symbol "FRAME-URL-LINK" :ID3-FRAME)))
  739. (t
  740. ;; we don't recognize the frame name. if it could possibly be a real frame name,
  741. ;; then just read it raw
  742. (when (possibly-valid-frame-id? id)
  743. (log-id3-frame "just reading raw")
  744. (find-class (find-symbol "FRAME-RAW" :ID3-FRAME))))))
  745. (log-id3-frame "general case for id <~a> is ~a" id found-class)
  746. found-class)))
  747. (defun make-frame (version instream)
  748. "Create an appropriate mp3 frame by reading data from INSTREAM."
  749. (log5:with-context "make-frame"
  750. (let* ((pos (stream-seek instream 0 :current))
  751. (byte (stream-read-u8 instream))
  752. frame-name frame-len frame-flags frame-class)
  753. (log-id3-frame "reading from position ~:d (size of stream = ~:d)" pos (stream-size instream))
  754. (when (zerop byte) ; XXX should this be correlated to PADDING in the extended header???
  755. (log-id3-frame "hit padding of size ~:d while making a frame" 9999) ;(- (stream-size instream) pos))
  756. (return-from make-frame nil)) ; hit padding
  757. (setf frame-name
  758. (concatenate 'string (string (code-char byte)) (stream-read-string-with-len instream (ecase version (2 2) (3 3) (4 3)))))
  759. (setf frame-len (ecase version
  760. (2 (stream-read-u24 instream))
  761. (3 (stream-read-u32 instream))
  762. (4 (stream-read-u32 instream :bits-per-byte 7))))
  763. (when (or (= version 3) (= version 4))
  764. (setf frame-flags (stream-read-u16 instream))
  765. (when (not (valid-frame-flags version frame-flags))
  766. (warn-user "Invalid frame flags found ~a, will ignore" (print-frame-flags version frame-flags nil))))
  767. (log-id3-frame "making frame: id:~a, version: ~d, len: ~:d, flags: ~a"
  768. frame-name version frame-len
  769. (print-frame-flags version frame-flags nil))
  770. (setf frame-class (find-frame-class frame-name))
  771. ;; edge case where found a frame name, but it is not valid or where making this frame
  772. ;; would blow past the end of the file/buffer
  773. (when (or (> (+ (stream-seek instream 0 :current) frame-len) (stream-size instream))
  774. (null frame-class))
  775. (error 'id3-frame-condition :message "bad frame found" :object frame-name :location pos))
  776. (make-instance frame-class :pos pos :version version :id frame-name :len frame-len :flags frame-flags :instream instream))))
  777. (defun find-id3-frames (mp3-file)
  778. "With an open mp3-file, make sure it is in fact an MP3 file, then read it's header and frames"
  779. (labels ((read-loop (version stream)
  780. (log5:with-context "read-loop-in-find-id3-frames"
  781. (log-id3-frame "Starting loop through ~:d bytes" (stream-size stream))
  782. (let (frames this-frame)
  783. (do ()
  784. ((>= (stream-seek stream 0 :current) (stream-size stream)))
  785. (handler-case
  786. (progn
  787. (setf this-frame (make-frame version stream))
  788. (when (null this-frame)
  789. (log-id3-frame "hit padding: returning ~d frames" (length frames))
  790. (return-from read-loop (values t (nreverse frames))))
  791. (log-id3-frame "bottom of read-loop: pos = ~:d, size = ~:d" (stream-seek stream 0 :current) (stream-size stream))
  792. (push this-frame frames))
  793. (condition (c)
  794. ;; XXX need to 'handle' warnings here...
  795. (log-id3-frame "got condition ~a when making frame" c)
  796. (return-from read-loop (values nil (nreverse frames))))))
  797. (log-id3-frame "Succesful read: returning ~d frames" (length frames))
  798. (values t (nreverse frames)))))) ; reverse this so we have frames in "file order"
  799. (log5:with-context "find-id3-frames"
  800. (when (not (is-valid-mp3-file mp3-file))
  801. (log-id3-frame "~a is not an mp3 file" (stream-filename mp3-file))
  802. (error 'id3-frame-condition :location "find-id3-frames" :object (stream-filename mp3-file) :message "is not an mp3 file"))
  803. (log-id3-frame "~a is a valid mp3 file" (stream-filename mp3-file))
  804. (setf (id3-header mp3-file) (make-instance 'id3-header :instream mp3-file))
  805. (with-slots (size ext-header frames flags version) (id3-header mp3-file)
  806. ;; at this point, we switch from reading the file stream and create a memory stream
  807. ;; rationale: it may need to be unsysnc'ed and it helps prevent run-away reads with
  808. ;; mis-formed frames
  809. (when (not (zerop size))
  810. (let ((mem-stream (make-mem-stream (stream-read-sequence mp3-file size
  811. :bits-per-byte (if (header-unsynchronized-p flags) 7 8)))))
  812. ;; must make extended header here since it is subject to unsynchronization.
  813. (when (header-extended-p flags)
  814. (setf ext-header (make-instance 'id3-extended-header :instream mem-stream)))
  815. ;; start reading frames from memory stream
  816. (multiple-value-bind (_ok _frames) (read-loop version mem-stream)
  817. (if (not _ok)
  818. (warn-user "File ~a had errors finding mp3 frames. potentially missed frames!" (stream-filename mp3-file)))
  819. (log-id3-frame "ok = ~a, returning ~d frames" _ok (length _frames))
  820. (setf frames _frames)
  821. _ok)))))))
  822. (defun map-id3-frames (mp3-file &key (func (constantly t)))
  823. "Iterates through the ID3 frames found in an MP3 file"
  824. (mapcar func (frames (id3-header mp3-file))))
  825. #|
  826. XXX
  827. Random ideas for rewrite:
  828. -might be simplest to read in frame payloads (sync'ed appropriately) for all frames and then move parsing into
  829. accessor methods? This might be easier to handle sync/compression/etc.
  830. -probably should rewrite name/value pairs as a mixin class? Or more broadly, there is a finite set of frame-encodings,
  831. so abstact to that, then subclass for frame-????
  832. |#