audio-streams.lisp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. ;;; -*- Mode: Lisp; show-trailing-whitespace: t; Base: 10; indent-tabs: nil; Syntax: ANSI-Common-Lisp; Package: STREAMS; -*-
  2. ;;; Copyright (c) 2013, Mark VandenBrink. All rights reserved.
  3. (in-package #:audio-streams)
  4. (log5:defcategory cat-log-stream)
  5. (defmacro log-stream (&rest log-stuff) `(log5:log-for (cat-log-stream) ,@log-stuff))
  6. (define-condition audio-stream-condition ()
  7. ((location :initarg :location :reader location :initform nil)
  8. (object :initarg :object :reader object :initform nil)
  9. (messsage :initarg :message :reader message :initform "Undefined Condition"))
  10. (:report (lambda (condition stream)
  11. (format stream "audio-stream condition at location: <~a> with object: <~a>: message: <~a>"
  12. (location condition) (object condition) (message condition)))))
  13. (defmethod print-object ((me audio-stream-condition) stream)
  14. (format stream "location: <~a>, object: <~a>, message: <~a>" (location me) (object me) (message me)))
  15. (deftype octet () '(unsigned-byte 8))
  16. (defmacro make-octets (len) `(make-array ,len :element-type 'octet))
  17. ;;;
  18. ;;; A simple stream interface for parsing audio files. Currently, we have two basic stream types:
  19. ;;; file-based and in-memory based, both of which implement the stream protocol of read, seek, etc.
  20. ;;;
  21. ;;; Not prefixing this with #+USE-MMAP so as to make stream seek easier
  22. (defclass mmap-stream-mixin ()
  23. ((orig-vector :accessor orig-vector))
  24. (:documentation "Use CCLs MMAP facility to get a stream."))
  25. (defclass base-stream ()
  26. ((stream :accessor stream))
  27. (:documentation "Base class for audio-stream implementation"))
  28. (defclass base-file-stream #-USE-MMAP (base-stream) #+USE-MMAP (base-stream mmap-stream-mixin)
  29. ((stream-filename :accessor stream-filename)
  30. (orig-size :accessor orig-size :documentation "ccl::stream-position let's you seek beyond EOF"))
  31. (:documentation "File-based audio stream"))
  32. (defclass mp3-file-stream (base-file-stream)
  33. ((id3-header :accessor id3-header :initform nil :documentation "holds all the ID3 info")
  34. (audio-info :accessor audio-info :initform nil :documentation "holds the bit-rate, etc info"))
  35. (:documentation "Stream for parsing MP3 files"))
  36. (defclass mp4-file-stream (base-file-stream)
  37. ((mp4-atoms :accessor mp4-atoms :initform nil :documentation "holds tree of parsed MP4 atoms/boxes")
  38. (audio-info :accessor audio-info :initform nil :documentation "holds the bit-rate, etc info"))
  39. (:documentation "Stream for parsing MP4A files"))
  40. (defun make-file-stream (class-name filename &key (read-only t))
  41. "Convenience function for creating a file stream."
  42. (let ((new-stream (make-instance (find-class class-name))))
  43. #-USE-MMAP (progn
  44. (setf (stream new-stream) (if read-only
  45. (open filename :direction :input :element-type 'octet)
  46. (open filename :direction :io :if-exists :overwrite :element-type 'octet)))
  47. (setf (orig-size new-stream) (file-length (stream new-stream))))
  48. #+USE-MMAP (progn
  49. (assert read-only () "Can not do read/write with MMAP files.")
  50. (setf (orig-vector new-stream) (ccl:map-file-to-octet-vector filename))
  51. (setf (orig-size new-stream) (length (orig-vector new-stream))) ; ccl::stream-position let's you seek beyond EOF
  52. (setf (stream new-stream) (ccl:make-vector-input-stream (orig-vector new-stream))))
  53. (setf (stream-filename new-stream) filename)
  54. new-stream))
  55. (defclass base-mem-stream (base-stream)
  56. ()
  57. (:documentation "In-memory stream"))
  58. (defun make-mem-stream (vector)
  59. "Convenience function to turn a vector into a stream."
  60. (let ((new-stream (make-instance 'base-mem-stream)))
  61. (setf (stream new-stream) (ccl:make-vector-input-stream vector))
  62. new-stream))
  63. (defmethod stream-close ((in-stream base-file-stream))
  64. "Close the underlying file."
  65. (with-slots (stream) in-stream
  66. (when stream
  67. #-USE-MMAP (close stream)
  68. #+USE-MMAP (ccl:unmap-octet-vector (orig-vector in-stream))
  69. (setf stream nil))))
  70. (defmethod stream-close ((in-stream base-mem-stream))
  71. "'Close' a memory stream by setting it to nil"
  72. (with-slots (stream) in-stream
  73. (setf stream nil)))
  74. (defmethod stream-size ((in-stream base-stream))
  75. "Returns the length of the underlying stream"
  76. (ccl::stream-length (stream in-stream)))
  77. ;;;
  78. ;;; I'm using ccl::stream-position, which I really shouldn't here...
  79. (defmethod stream-seek ((in-stream base-stream) &optional (offset 0) (from :current))
  80. "C-like stream positioner. Takes an offset and a location (one of :start, :end, :current).
  81. If offset is not passed, then assume 0. If from is not passed, assume from current location.
  82. Thus (stream-seek in) == (stream-seek in 0 :current)"
  83. (with-slots (stream) in-stream
  84. (ecase from
  85. (:start
  86. (when (or (typep in-stream 'mmap-stream-mixin) (typep in-stream 'base-file-stream))
  87. (if (> offset (orig-size in-stream))
  88. (error 'audio-stream-condition :location "stream-seek" :object in-stream :message "Seeking beyond end of file")))
  89. (ccl::stream-position stream offset))
  90. (:current
  91. (if (zerop offset)
  92. (ccl::stream-position stream)
  93. (progn
  94. (when (or (typep in-stream 'mmap-stream-mixin) (typep in-stream 'base-file-stream))
  95. (if (> (+ (ccl::stream-position stream) offset) (orig-size in-stream))
  96. (error 'audio-stream-condition :location "stream-seek" :object in-stream :message "Seeking beyond end of file")))
  97. (ccl::stream-position stream (+ (ccl::stream-position stream) offset)))))
  98. (:end
  99. (when (or (typep in-stream 'mmap-stream-mixin) (typep in-stream 'base-file-stream))
  100. (if (> (- (ccl::stream-length stream) offset) (orig-size in-stream))
  101. (error 'audio-stream-condition :location "stream-seek" :object in-stream :message "Seeking beyond end of file")))
  102. (ccl::stream-position stream (- (ccl::stream-length stream) offset))))))
  103. (defun stream-read-octets (instream bytes &key (bits-per-byte 8))
  104. "Used to slurp in octets for the stream-read-* methods"
  105. (loop with value = 0
  106. for low-bit downfrom (* bits-per-byte (1- bytes)) to 0 by bits-per-byte do
  107. (setf (ldb (byte bits-per-byte low-bit) value) (read-byte instream))
  108. finally (return value)))
  109. (defmethod stream-read-u8 ((in-stream base-stream) &key (bits-per-byte 8))
  110. "Read 1 byte from file"
  111. (with-slots (stream) in-stream
  112. (stream-read-octets stream 1 :bits-per-byte bits-per-byte)))
  113. (defmethod stream-read-u16 ((in-stream base-stream) &key (bits-per-byte 8))
  114. "Read 2 bytes from file"
  115. (with-slots (stream) in-stream
  116. (stream-read-octets stream 2 :bits-per-byte bits-per-byte)))
  117. (defmethod stream-read-u24 ((in-stream base-stream) &key (bits-per-byte 8))
  118. "Read 3 bytes from file"
  119. (with-slots (stream) in-stream
  120. (stream-read-octets stream 3 :bits-per-byte bits-per-byte)))
  121. (defmethod stream-read-u32 ((in-stream base-stream) &key (bits-per-byte 8))
  122. "Read 4 bytes from file"
  123. (with-slots (stream) in-stream
  124. (stream-read-octets stream 4 :bits-per-byte bits-per-byte)))
  125. (defmethod stream-read-u64 ((in-stream base-stream) &key (bits-per-byte 8))
  126. "Read 8 bytes from file"
  127. (with-slots (stream) in-stream
  128. (stream-read-octets stream 8 :bits-per-byte bits-per-byte)))
  129. (defmethod stream-read-sequence ((stream base-stream) size &key (bits-per-byte 8))
  130. "Read SIZE octets from input-file in BIT-PER-BYTE sizes"
  131. (log5:with-context "stream-read-sequence"
  132. (ecase bits-per-byte
  133. (8
  134. (let ((octets (make-octets size)))
  135. (read-sequence octets (slot-value stream 'stream))
  136. octets))
  137. (7
  138. (let* ((last-byte-was-FF nil)
  139. (byte nil)
  140. (octets (ccl:with-output-to-vector (out)
  141. (dotimes (i size)
  142. (setf byte (stream-read-u8 stream))
  143. (if last-byte-was-FF
  144. (if (not (zerop byte))
  145. (write-byte byte out))
  146. (write-byte byte out))
  147. (setf last-byte-was-FF (= byte #xFF))))))
  148. octets)))))
  149. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; STRINGS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  150. ;;;
  151. ;;; Decode octets as an iso-8859-1 string (encoding == 0)
  152. (defun stream-decode-iso-string (octets &key (start 0) (end nil))
  153. (ccl:decode-string-from-octets octets :start start :end end :external-format :iso-8859-1))
  154. ;;;
  155. ;;; XXX: Coded this way because I can't seem to get a simple :external-format :ucs-2 to work correctly
  156. ;;; AND some taggers encode a UCS-2 empty string w/o a byte-order mark (i.e. null strings are
  157. ;;; sometimes encoded as #(00 00))
  158. (defun stream-decode-ucs-string (octets &key (start 0) (end nil))
  159. "Decode octets as a UCS string with a BOM (encoding == 1)"
  160. (labels ((get-byte-order-mark (octets)
  161. (let ((retval 0))
  162. (setf (ldb (byte 8 0) retval) (aref octets 1))
  163. (setf (ldb (byte 8 8) retval) (aref octets 0))
  164. (when (not (or (= #xfffe retval) (= #xfeff retval)))
  165. (error 'audio-stream-condition
  166. :location "stream-decode-ucs-string"
  167. :object nil
  168. :message (format nil "got an invalid byte-order mark of ~x" retval)))
  169. retval)))
  170. ;; special case: empty (and mis-coded) string
  171. (cond ((zerop (length octets))
  172. (make-string 0))
  173. (t
  174. ;;
  175. ;; else, we have a (hopefully) properly encoded string
  176. (let ((bom (get-byte-order-mark octets)))
  177. (ecase (the fixnum bom)
  178. (#xfffe (ccl:decode-string-from-octets octets :start (+ 2 start) :end end :external-format :ucs-2le))
  179. (#xfeff (ccl:decode-string-from-octets octets :start (+ 2 start) :end end :external-format :ucs-2be))
  180. (0 (make-string 0))))))))
  181. (defun stream-decode-ucs-be-string (octets &key (start 0) (end nil))
  182. "Decode octets as a UCS-BE string (encoding == 2)"
  183. (ccl:decode-string-from-octets octets :start start :end end :external-format :ucs-2be))
  184. (defun stream-decode-utf-8-string (octets &key (start 0) (end nil))
  185. "Decode octets as a utf-8 string"
  186. (ccl:decode-string-from-octets octets :start start :end end :external-format :utf-8))
  187. (defun stream-decode-string (octets &key (start 0) (end nil) (encoding 0))
  188. "Decode octets depending on encoding"
  189. (ecase encoding
  190. (0 (stream-decode-iso-string octets :start start :end end))
  191. (1 (stream-decode-ucs-string octets :start start :end end))
  192. (2 (stream-decode-ucs-be-string octets :start start :end end))
  193. (3 (stream-decode-utf-8-string octets :start start :end end))))
  194. (defmethod stream-read-iso-string-with-len ((instream base-stream) len)
  195. "Read an iso-8859-1 string of length 'len' (encoding = 0)"
  196. (let ((octets (stream-read-sequence instream len)))
  197. (stream-decode-iso-string octets)))
  198. (defmethod stream-read-ucs-string-with-len ((instream base-stream) len)
  199. "Read an ucs-2 string of length 'len' (encoding = 1)"
  200. (let ((octets (stream-read-sequence instream len)))
  201. (stream-decode-ucs-string octets)))
  202. (defmethod stream-read-ucs-be-string-with-len ((instream base-stream) len)
  203. "Read an ucs-2-be string of length 'len' (encoding = 2)"
  204. (let ((octets (stream-read-sequence instream len)))
  205. (stream-decode-ucs-be-string octets)))
  206. (defmethod stream-read-utf-8-string-with-len ((instream base-stream) len)
  207. "Read an utf-8 string of length 'len' (encoding = 3)"
  208. (let ((octets (stream-read-sequence instream len)))
  209. (stream-decode-utf-8-string octets)))
  210. (defmethod stream-read-string-with-len ((instream base-stream) len &key (encoding 0))
  211. "Read in a string of a given encoding of length 'len'"
  212. (ecase encoding
  213. (0 (stream-read-iso-string-with-len instream len))
  214. (1 (stream-read-ucs-string-with-len instream len))
  215. (2 (stream-read-ucs-be-string-with-len instream len))
  216. (3 (stream-read-utf-8-string-with-len instream len))))
  217. (defmethod stream-read-iso-string ((instream base-stream))
  218. "Read in a null terminated iso-8859-1 string"
  219. (let ((octets (ccl:with-output-to-vector (out)
  220. (do ((b (stream-read-u8 instream) (stream-read-u8 instream)))
  221. (nil)
  222. (when (zerop b)
  223. (return)) ; leave loop w/o writing
  224. (write-byte b out)))))
  225. (stream-decode-iso-string octets)))
  226. (defmethod stream-read-ucs-string ((instream base-stream))
  227. "Read in a null terminated UCS string."
  228. (let ((octets (ccl:with-output-to-vector (out)
  229. (do* ((b0 (stream-read-u8 instream)
  230. (stream-read-u8 instream))
  231. (b1 (stream-read-u8 instream)
  232. (stream-read-u8 instream)))
  233. (nil)
  234. (when (and (zerop b0) (zerop b1))
  235. (return))
  236. (write-byte b0 out)
  237. (write-byte b1 out)))))
  238. (stream-decode-ucs-string octets)))
  239. (defmethod stream-read-ucs-be-string ((instream base-stream))
  240. "Read in a null terminated UCS-BE string."
  241. (let ((octets (ccl:with-output-to-vector (out)
  242. (do* ((b0 (stream-read-u8 instream)
  243. (stream-read-u8 instream))
  244. (b1 (stream-read-u8 instream)
  245. (stream-read-u8 instream)))
  246. (nil)
  247. (when (and (zerop b0) (zerop b1))
  248. (return))
  249. (write-byte b0 out)
  250. (write-byte b1 out)))))
  251. (stream-decode-ucs-be-string octets)))
  252. (defmethod stream-read-utf-8-string ((instream base-stream))
  253. "Read in a null terminated utf-8 string (encoding == 3)"
  254. (let ((octets (ccl:with-output-to-vector (out)
  255. (do ((b (stream-read-u8 instream)
  256. (stream-read-u8 instream)))
  257. (nil)
  258. (when (zerop b)
  259. (return))
  260. (write-byte b out)))))
  261. (stream-decode-utf-8-string octets)))
  262. (defmethod stream-read-string ((instream base-stream) &key (encoding 0))
  263. "Read in a null terminated string of a given encoding."
  264. (ecase encoding
  265. (0 (stream-read-iso-string instream))
  266. (1 (stream-read-ucs-string instream))
  267. (2 (stream-read-ucs-be-string instream))
  268. (3 (stream-read-utf-8-string instream))))
  269. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; FILES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  270. (defvar *get-audio-info* t "controls whether the parsing functions also parse audio info like bit-rate, etc")
  271. (defun parse-mp4-file (filename &key (get-audio-info *get-audio-info*))
  272. "Parse an MP4A file by reading it's ATOMS and decoding them."
  273. (let (stream)
  274. (handler-case
  275. (progn
  276. (setf stream (make-file-stream 'mp4-file-stream filename))
  277. (mp4-atom:find-mp4-atoms stream)
  278. (when get-audio-info
  279. (setf (audio-info stream) (mp4-atom:get-mp4-audio-info stream))))
  280. (mp4-atom:mp4-atom-condition (c)
  281. (warn-user "make-mp4-stream got condition: ~a" c)
  282. (when stream (stream-close stream))
  283. (setf stream nil)))
  284. stream))
  285. (defun parse-mp3-file (filename &key (get-audio-info *get-audio-info*))
  286. "Parse an MP3 file by reading it's FRAMES and decoding them."
  287. (let (stream)
  288. (handler-case
  289. (progn
  290. (setf stream (make-file-stream 'mp3-file-stream filename))
  291. (id3-frame:find-id3-frames stream)
  292. (when get-audio-info
  293. (setf (audio-info stream) (mpeg:get-mpeg-audio-info stream))))
  294. (id3-frame:id3-frame-condition (c)
  295. (warn-user "make-mp3-stream got condition: ~a" c)
  296. (when stream (stream-close stream))
  297. (setf stream nil)))
  298. stream))