mpeg.lisp 21 KB

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