mpeg.lisp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. ;;; -*- Mode: Lisp; show-trailing-whitespace: t; Base: 10; indent-tabs: nil; Syntax: ANSI-Common-Lisp; Package: MPEG; -*-
  2. ;;; Copyright (c) 2013, Mark VandenBrink. All rights reserved.
  3. ;;; Parsing MPEG audio frames. See http://www.datavoyage.com/mpgscript/mpeghdr.htm for format of a frame.
  4. (in-package #:mpeg)
  5. (defconstant* +sync-word+ #x7ff "NB: this is 11 bits so as to be able to recognize V2.5")
  6. ;;; the versions
  7. (defconstant* +mpeg-2.5+ 0)
  8. (defconstant* +v-reserved+ 1)
  9. (defconstant* +mpeg-2+ 2)
  10. (defconstant* +mpeg-1+ 3)
  11. (defun valid-version (version)
  12. (declare #.utils:*standard-optimize-settings*)
  13. (or ;; can't deal with 2.5's yet (= (the fixnum +mpeg-2.5+) (the fixnum version))
  14. (= (the fixnum +mpeg-2+) (the fixnum version))
  15. (= (the fixnum +mpeg-1+) (the fixnum version))))
  16. (defun get-mpeg-version-string (version)
  17. (declare #.utils:*standard-optimize-settings*)
  18. (nth version '("MPEG 2.5" "Reserved" "MPEG 2" "MPEG 1")))
  19. ;;; the layers
  20. (defconstant* +layer-reserved+ 0)
  21. (defconstant* +layer-3+ 1)
  22. (defconstant* +layer-2+ 2)
  23. (defconstant* +layer-1+ 3)
  24. (defun valid-layer (layer)
  25. (declare #.utils:*standard-optimize-settings*)
  26. (or (= (the fixnum +layer-3+) (the fixnum layer))
  27. (= (the fixnum +layer-2+) (the fixnum layer))
  28. (= (the fixnum +layer-1+) (the fixnum layer))))
  29. (defun get-layer-string (layer)
  30. (declare #.utils:*standard-optimize-settings*)
  31. (nth layer '("Reserved" "Layer III" "Layer II" "Layer I")))
  32. ;;; the modes
  33. (defconstant* +channel-mode-stereo+ 0)
  34. (defconstant* +channel-mode-joint+ 1)
  35. (defconstant* +channel-mode-dual+ 2)
  36. (defconstant* +channel-mode-mono+ 3)
  37. (defun get-channel-mode-string (mode)
  38. (declare #.utils:*standard-optimize-settings*)
  39. (nth mode '("Stereo" "Joint" "Dual" "Mono")))
  40. ;;; the emphases
  41. (defconstant* +emphasis-none+ 0)
  42. (defconstant* +emphasis-50-15+ 1)
  43. (defconstant* +emphasis-reserved+ 2)
  44. (defconstant* +emphasis-ccit+ 3)
  45. (defun get-emphasis-string (e)
  46. (declare #.utils:*standard-optimize-settings*)
  47. (nth e '("None" "50/15 ms" "Reserved" "CCIT J.17")))
  48. (defun valid-emphasis (e)
  49. (declare #.utils:*standard-optimize-settings*)
  50. (or (= (the fixnum e) (the fixnum +emphasis-none+))
  51. (= (the fixnum e) (the fixnum +emphasis-50-15+))
  52. (= (the fixnum e) (the fixnum +emphasis-ccit+))))
  53. ;;; the modes
  54. (defconstant* +mode-extension-0+ 0)
  55. (defconstant* +mode-extension-1+ 1)
  56. (defconstant* +mode-extension-2+ 2)
  57. (defconstant* +mode-extension-3+ 3)
  58. (defun get-mode-extension-string (channel-mode layer mode-extension)
  59. (declare #.utils:*standard-optimize-settings*)
  60. (if (not (= channel-mode +channel-mode-joint+))
  61. ""
  62. (if (or (= layer +layer-1+)
  63. (= layer +layer-2+))
  64. (format nil "Bands ~[4~;8~;12~;16~] to 31" mode-extension)
  65. (format nil "Intensity Stereo: ~[off~;on~], MS Stereo: ~[off~;on~]" (ash mode-extension -1) (logand mode-extension 1)))))
  66. (defun get-samples-per-frame (version layer)
  67. (declare #.utils:*standard-optimize-settings*)
  68. (cond ((= (the fixnum layer) (the fixnum +layer-1+)) 384)
  69. ((= (the fixnum layer) (the fixnum +layer-2+)) 1152)
  70. ((= (the fixnum layer) (the fixnum +layer-3+))
  71. (cond ((= (the fixnum version) +mpeg-1+) 1152)
  72. ((or (= (the fixnum version) (the fixnum +mpeg-2+))
  73. (= (the fixnum version) (the fixnum +mpeg-2.5+))) 576)))))
  74. (defclass frame ()
  75. ((pos :accessor pos :initarg :pos)
  76. (hdr-u32 :accessor hdr-u32 :initarg :hdr-u32)
  77. (samples :accessor samples :initarg :samples)
  78. (sync :accessor sync :initarg :sync)
  79. (version :accessor version :initarg :version)
  80. (layer :accessor layer :initarg :layer)
  81. (protection :accessor protection :initarg :protection)
  82. (bit-rate :accessor bit-rate :initarg :bit-rate)
  83. (sample-rate :accessor sample-rate :initarg :sample-rate)
  84. (padded :accessor padded :initarg :padded)
  85. (private :accessor private :initarg :private)
  86. (channel-mode :accessor channel-mode :initarg :channel-mode)
  87. (mode-extension :accessor mode-extension :initarg :mode-extension)
  88. (copyright :accessor copyright :initarg :copyright)
  89. (original :accessor original :initarg :original)
  90. (emphasis :accessor emphasis :initarg :emphasis)
  91. (size :accessor size :initarg :size)
  92. (vbr :accessor vbr :initarg :vbr)
  93. (payload :accessor payload :initarg :payload))
  94. (:documentation "Data in and associated with an MPEG audio frame.")
  95. (:default-initargs :pos nil :hdr-u32 nil :samples 0 :sync 0 :version 0 :layer 0 :protection 0 :bit-rate 0
  96. :sample-rate 0 :padded 0 :private 0 :channel-mode 0 :mode-extension 0
  97. :copyright 0 :original 0 :emphasis 0 :size nil :vbr nil :payload nil))
  98. (defmacro with-frame-slots ((instance) &body body)
  99. `(with-slots (pos hdr-u32 samples sync version layer protection bit-rate sample-rate
  100. padded private channel-mode mode-extension copyright
  101. original emphasis size vbr payload) ,instance
  102. ,@body))
  103. (let ((bit-array-table
  104. (make-array '(14 5) :initial-contents
  105. '((32 32 32 32 8)
  106. (64 48 40 48 16)
  107. (96 56 48 56 24)
  108. (128 64 56 64 32)
  109. (160 80 64 80 40)
  110. (192 96 80 96 48)
  111. (224 112 96 112 56)
  112. (256 128 112 128 64)
  113. (288 160 128 144 80)
  114. (320 192 160 160 96)
  115. (352 224 192 176 112)
  116. (384 256 224 192 128)
  117. (416 320 256 224 144)
  118. (448 384 320 256 160)))))
  119. (defun valid-bit-rate-index (br-index)
  120. (declare #.utils:*standard-optimize-settings*)
  121. (and (> (the fixnum br-index) 0) (< (the fixnum br-index) 15)))
  122. (defun get-bit-rate (version layer bit-rate-index)
  123. (declare #.utils:*standard-optimize-settings*)
  124. (let ((row (1- bit-rate-index))
  125. (col (cond ((= (the fixnum version) (the fixnum +mpeg-1+))
  126. (cond ((= (the fixnum layer) (the fixnum +layer-1+)) 0)
  127. ((= (the fixnum layer) (the fixnum +layer-2+)) 1)
  128. ((= (the fixnum layer) (the fixnum +layer-3+)) 2)
  129. (t nil)))
  130. ((= (the fixnum version) (the fixnum +mpeg-2+))
  131. (cond ((= (the fixnum layer) (the fixnum +layer-1+)) 3)
  132. ((= (the fixnum layer) (the fixnum +layer-2+)) 4)
  133. ((= (the fixnum layer) (the fixnum +layer-3+)) 4)
  134. (t nil)))
  135. (t (error "don't support MPEG 2.5 yet")))))
  136. (if (or (null col) (< row 0) (> row 14))
  137. nil
  138. (* 1000 (aref bit-array-table row col))))))
  139. (defun valid-sample-rate-index (sr-index)
  140. (declare #.utils:*standard-optimize-settings*)
  141. (and (>= (the fixnum sr-index) 0)
  142. (< (the fixnum sr-index) 3)))
  143. (defun get-sample-rate (version sr-index)
  144. (declare #.utils:*standard-optimize-settings*)
  145. (cond ((= (the fixnum version) (the fixnum +mpeg-1+))
  146. (case (the fixnum sr-index) (0 44100) (1 48000) (2 32000)))
  147. ((= (the fixnum version) (the fixnum +mpeg-2+))
  148. (case (the fixnum sr-index) (0 22050) (1 24000) (2 16000)))
  149. (t nil)))
  150. (defun get-frame-size (version layer bit-rate sample-rate padded)
  151. (declare #.utils:*standard-optimize-settings*)
  152. (truncate (float (cond ((= (the fixnum layer) (the fixnum +layer-1+))
  153. (* 4 (+ (/ (* 12 bit-rate) sample-rate) padded)))
  154. ((= (the fixnum layer) (the fixnum +layer-2+))
  155. (+ (* 144 (/ bit-rate sample-rate)) padded))
  156. ((= (the fixnum layer) (the fixnum +layer-3+))
  157. (if (= (the fixnum version) (the fixnum +mpeg-1+))
  158. (+ (* 144 (/ bit-rate sample-rate)) padded)
  159. (+ (* 72 (/ bit-rate sample-rate)) padded)))))))
  160. (defmethod load-frame ((me frame) &key instream (read-payload nil))
  161. "Load an MPEG frame from current file position. If READ-PAYLOAD is set, read in frame's content."
  162. (declare #.utils:*standard-optimize-settings*)
  163. (handler-case
  164. (with-frame-slots (me)
  165. (when (null hdr-u32) ; has header already been read in?
  166. (setf pos (stream-seek instream)
  167. hdr-u32 (stream-read-u32 instream))
  168. (when (null hdr-u32)
  169. (return-from load-frame nil)))
  170. (if (parse-header me)
  171. (progn
  172. (setf size (get-frame-size version layer bit-rate sample-rate padded))
  173. (when read-payload
  174. (setf payload (stream-read-sequence instream (- size 4))))
  175. t)
  176. nil))
  177. (end-of-file (c)
  178. (declare (ignore c))
  179. nil)))
  180. (defmethod parse-header ((me frame))
  181. "Given a frame, verify that is a valid MPEG audio frame by examining the header.
  182. A header looks like this:
  183. Bits 31-21 (11 bits): the sync word. Must be #xffe (NB version 2.5 standard)
  184. Bits 20-19 (2 bits): the version
  185. Bits 18-17 (2 bits): the layer
  186. Bit 16 (1 bit ): the protection bit
  187. Bits 15-12 (4 bits): the bit-rate index
  188. Bits 11-10 (2 bits): the sample-rate index
  189. Bit 9 (1 bit ): the padding bit
  190. Bit 8 (1 bit ): the private bit
  191. Bits 7-6 (2 bits): the channel mode
  192. Bits 5-4 (2 bits): the mode extension
  193. Bit 3 (1 bit ): the copyright bit
  194. Bit 2 (1 bit ): the original bit
  195. Bits 1-0 (2 bits): the emphasis"
  196. (declare #.utils:*standard-optimize-settings*)
  197. (with-frame-slots (me)
  198. ;; check sync word
  199. (setf sync (get-bitfield hdr-u32 31 11))
  200. (when (not (= sync +sync-word+))
  201. (return-from parse-header nil))
  202. ;; check version
  203. (setf version (get-bitfield hdr-u32 20 2))
  204. (when (not (valid-version version))
  205. (return-from parse-header nil))
  206. ;; check layer
  207. (setf layer (get-bitfield hdr-u32 18 2))
  208. (when (not (valid-layer layer))
  209. (return-from parse-header nil))
  210. (setf protection (get-bitfield hdr-u32 16 1)
  211. samples (get-samples-per-frame version layer))
  212. ;; check bit-rate
  213. (let ((br-index (get-bitfield hdr-u32 15 4)))
  214. (when (not (valid-bit-rate-index br-index))
  215. (return-from parse-header nil))
  216. (setf bit-rate (get-bit-rate version layer br-index)))
  217. ;; check sample rate
  218. (let ((sr-index (get-bitfield hdr-u32 11 2)))
  219. (when (not (valid-sample-rate-index sr-index))
  220. (return-from parse-header nil))
  221. (setf sample-rate (get-sample-rate version sr-index)))
  222. (setf padded (get-bitfield hdr-u32 9 1)
  223. private (get-bitfield hdr-u32 8 1)
  224. channel-mode (get-bitfield hdr-u32 7 2)
  225. mode-extension (get-bitfield hdr-u32 5 2)
  226. copyright (get-bitfield hdr-u32 3 1)
  227. original (get-bitfield hdr-u32 2 1)
  228. emphasis (get-bitfield hdr-u32 1 2))
  229. ;; check emphasis
  230. (when (not (valid-emphasis emphasis))
  231. (return-from parse-header nil))
  232. t))
  233. (defmethod vpprint ((me frame) stream)
  234. (format stream "~a"
  235. (with-output-to-string (s)
  236. (with-frame-slots (me)
  237. (format s "MPEG Frame: position in file = ~:d, header in (hex) bytes = ~x, size = ~d, sync word = ~x, " pos hdr-u32 size sync)
  238. (when vbr
  239. (format s "~&vbr-info: ~a~%" vbr))
  240. (format s "version = ~a, layer = ~a, crc protected? = ~[yes~;no~], bit-rate = ~:d bps, sampling rate = ~:d bps, padded? = ~[no~;yes~], private bit set? = ~[no~;yes~], channel mode = ~a, "
  241. (get-mpeg-version-string version) (get-layer-string layer)
  242. protection bit-rate sample-rate padded private (get-channel-mode-string channel-mode))
  243. (format s "mode extension = ~a, copyrighted? = ~[no~;yes~], original? = ~[no~;yes~], emphasis = ~a"
  244. (get-mode-extension-string channel-mode layer mode-extension) copyright original (get-emphasis-string emphasis))
  245. (when payload
  246. (format s "~%frame payload[~:d] = ~a~%" (length payload) (utils:printable-array payload)))))))
  247. (defclass vbr-info ()
  248. ((tag :accessor tag :initarg :tag)
  249. (flags :accessor flags :initarg :flags)
  250. (frames :accessor frames :initarg :frames)
  251. (bytes :accessor bytes :initarg :bytes)
  252. (tocs :accessor tocs :initarg :tocs)
  253. (scale :accessor scale :initarg :scale))
  254. (:default-initargs :tag nil :flags 0 :frames nil :bytes nil :tocs nil :scale nil))
  255. (defmacro with-vbr-info-slots ((instance) &body body)
  256. `(with-slots (tag flags frames bytes tocs scale) ,instance
  257. ,@body))
  258. (defconstant* +vbr-frames+ 1)
  259. (defconstant* +vbr-bytes+ 2)
  260. (defconstant* +vbr-tocs+ 4)
  261. (defconstant* +vbr-scale+ 8)
  262. (defun get-side-info-size (version channel-mode)
  263. (declare #.utils:*standard-optimize-settings*)
  264. (cond ((= (the fixnum version) (the fixnum +mpeg-1+))
  265. (cond ((= (the fixnum channel-mode) (the fixnum +channel-mode-mono+)) 17)
  266. (t 32)))
  267. (t (cond ((= (the fixnum channel-mode) (the fixnum +channel-mode-mono+)) 9)
  268. (t 17)))))
  269. (defmethod check-vbr ((me frame) fn)
  270. (declare #.utils:*standard-optimize-settings*)
  271. (with-frame-slots (me)
  272. (let ((i (get-side-info-size version channel-mode)))
  273. (when (>= i (length payload))
  274. (return-from check-vbr nil))
  275. (when (or (and (= (aref payload (+ i 0)) (char-code #\X))
  276. (= (aref payload (+ i 1)) (char-code #\i))
  277. (= (aref payload (+ i 2)) (char-code #\n))
  278. (= (aref payload (+ i 3)) (char-code #\g)))
  279. (and (= (aref payload (+ i 0)) (char-code #\I))
  280. (= (aref payload (+ i 1)) (char-code #\n))
  281. (= (aref payload (+ i 2)) (char-code #\f))
  282. (= (aref payload (+ i 3)) (char-code #\o))))
  283. (setf vbr (make-instance 'vbr-info))
  284. (let ((v (make-audio-stream (payload me))))
  285. (stream-seek v i :start) ; seek to Xing/Info offset
  286. (setf (tag vbr) (stream-read-iso-string v 4)
  287. (flags vbr) (stream-read-u32 v))
  288. (when (logand (flags vbr) +vbr-frames+)
  289. (setf (frames vbr) (stream-read-u32 v))
  290. ;; some VBR files have the Xing/Info header, but it is not correctly formulated.
  291. ;; just warn the user.
  292. (when (zerop (frames vbr))
  293. (warn-user "file ~a Xing/Info header flags has FRAMES set, but field is zero." fn)))
  294. (when (logand (flags vbr) +vbr-bytes+)
  295. (setf (bytes vbr) (stream-read-u32 v)))
  296. (when (logand (flags vbr) +vbr-tocs+)
  297. (setf (tocs vbr) (stream-read-sequence v 100)))
  298. (when (logand (flags vbr) +vbr-scale+)
  299. (setf (scale vbr) (stream-read-u32 v))))))))
  300. (defmethod vpprint ((me vbr-info) stream)
  301. (with-vbr-info-slots (me)
  302. (format stream "tag = ~a, flags = 0x~x, frame~p = ~:d, bytes = ~:d, tocs = ~d, scale = ~d, "
  303. tag flags frames frames bytes tocs scale)))
  304. (defun find-first-sync (instream)
  305. (declare #.utils:*standard-optimize-settings*)
  306. (let ((hdr-u32)
  307. (count 0)
  308. (pos))
  309. (handler-case
  310. (loop
  311. (setf pos (stream-seek instream)
  312. hdr-u32 (stream-read-u32 instream))
  313. (when (null hdr-u32)
  314. (return-from find-first-sync nil))
  315. (incf count)
  316. (when (= (logand hdr-u32 #xffe00000) #xffe00000) ; magic number is potential sync frame header
  317. (let ((hdr (make-instance 'frame :hdr-u32 hdr-u32 :pos pos)))
  318. (if (load-frame hdr :instream instream :read-payload t)
  319. (progn
  320. (check-vbr hdr (stream-filename instream))
  321. (return-from find-first-sync hdr))))))
  322. (condition (c) (progn
  323. (warn-user "Condtion <~a> signaled while looking for first sync" c)
  324. (error c))))
  325. nil))
  326. (defmethod next-frame ((me frame) &key instream read-payload)
  327. "Get next frame. If READ-PAYLOAD is true, read in contents for frame, else, seek to next frame header."
  328. (declare #.utils:*standard-optimize-settings*)
  329. (let ((nxt-frame (make-instance 'frame)))
  330. (when (not (payload me))
  331. (stream-seek instream (- (size me) 4) :current))
  332. (if (load-frame nxt-frame :instream instream :read-payload read-payload)
  333. nxt-frame
  334. nil)))
  335. (defparameter *max-frames-to-read* most-positive-fixnum "when trying to determine bit-rate, etc, read at most this many frames")
  336. (defun map-frames (in func &key (start-pos nil) (read-payload nil) (max nil))
  337. "Loop through the MPEG audio frames in a file. If *MAX-FRAMES-TO-READ* is set, return after reading that many frames."
  338. (declare #.utils:*standard-optimize-settings*)
  339. (when start-pos
  340. (stream-seek in start-pos :start))
  341. (loop
  342. for max-frames = (if max max *max-frames-to-read*)
  343. for count = 0 then (incf count)
  344. for frame = (find-first-sync in) then (next-frame frame :instream in :read-payload read-payload)
  345. while (and frame (< count max-frames)) do
  346. (funcall func frame)))
  347. (defclass mpeg-audio-info ()
  348. ((is-vbr :accessor is-vbr :initarg :is-vbr :initform nil)
  349. (n-frames :accessor n-frames :initarg :n-frames :initform 0)
  350. (bit-rate :accessor bit-rate :initarg :bit-rate :initform nil)
  351. (sample-rate :accessor sample-rate :initarg :sample-rate :initform nil)
  352. (len :accessor len :initarg :len :initform nil)
  353. (version :accessor version :initarg :version :initform nil)
  354. (layer :accessor layer :initarg :layer :initform nil)))
  355. (defmethod vpprint ((me mpeg-audio-info) stream)
  356. (with-slots (is-vbr sample-rate bit-rate len version layer n-frames) me
  357. (format stream "~:d frame~p read, ~a, ~a, ~:[CBR,~;VBR,~] sample rate: ~:d Hz, bit rate: ~:d Kbps, duration: ~:d:~2,'0d"
  358. n-frames n-frames
  359. (get-mpeg-version-string version)
  360. (get-layer-string layer)
  361. is-vbr
  362. sample-rate
  363. (round (/ bit-rate 1000))
  364. (floor (/ len 60)) (round (mod len 60)))))
  365. (defun calc-bit-rate-exhaustive (instream start info)
  366. "Map every MPEG frame in INSTREAM and calculate the bit-rate"
  367. (declare #.utils:*standard-optimize-settings*)
  368. (let ((total-len 0)
  369. (bit-rate-total 0))
  370. (with-slots (is-vbr sample-rate bit-rate len version layer n-frames) info
  371. (map-frames instream (lambda (f)
  372. (incf n-frames)
  373. (incf total-len (float (/ (samples f) (sample-rate f))))
  374. (incf bit-rate-total (bit-rate f)))
  375. :read-payload nil :start-pos start)
  376. (when (or (< n-frames 10) (zerop bit-rate-total))
  377. (return-from calc-bit-rate-exhaustive))
  378. (setf is-vbr t
  379. len total-len
  380. bit-rate (float (/ bit-rate-total n-frames))))))
  381. (defun get-mpeg-audio-info (instream mp3-file)
  382. "Get MPEG Layer 3 audio information.
  383. If the first MPEG frame we find is a Xing/Info header, return that as info.
  384. Else, we assume CBR and calculate the duration, etc."
  385. (declare #.utils:*standard-optimize-settings*)
  386. (let ((first-frame (find-first-sync instream))
  387. (info (make-instance 'mpeg-audio-info)))
  388. (when (null first-frame)
  389. (return-from get-mpeg-audio-info))
  390. (with-slots (is-vbr sample-rate bit-rate len version layer n-frames) info
  391. (setf version (version first-frame)
  392. layer (layer first-frame)
  393. sample-rate (sample-rate first-frame))
  394. (if (vbr first-frame)
  395. ;; found a Xing header, now check to see if it is correct
  396. (if (zerop (frames (vbr first-frame)))
  397. ;; Xing header broken, read all frames to calc
  398. (calc-bit-rate-exhaustive instream (pos first-frame) info)
  399. ;; Good Xing header, use info in VBR to calc
  400. (setf n-frames 1
  401. is-vbr t
  402. len (float (* (frames (vbr first-frame))
  403. (/ (samples first-frame)
  404. (sample-rate first-frame))))
  405. bit-rate (float (/ (* 8 (bytes (vbr first-frame))) len))))
  406. ;; No Xing header found. Assume CBR and calculate based on first frame
  407. (let* ((first (pos first-frame))
  408. (last (- (stream-size instream)
  409. (if (id3-frame:v21-tag-header
  410. (id3-frame:id3-header mp3-file)) 128 0)))
  411. (n-fr (round (/ (float (- last first))
  412. (float (size first-frame)))))
  413. (n-sec (round (/ (float (* (size first-frame) n-fr))
  414. (float (* 125 (float
  415. (/ (bit-rate first-frame) 1000))))))))
  416. (setf is-vbr nil
  417. n-frames 1
  418. len n-sec
  419. bit-rate (float (bit-rate first-frame))))))
  420. (setf (id3-frame:audio-info mp3-file) info)))