id3.lisp 47 KB

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