id3-frame.lisp 45 KB

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