id3-frame.lisp 47 KB

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