audio-streams.lisp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. (deftype octet () '(unsigned-byte 8))
  7. (defmacro make-octets (len) `(make-array ,len :element-type 'octet))
  8. (defclass base-stream ()
  9. ((stream :accessor stream)))
  10. (defclass base-file-stream (base-stream)
  11. ((stream-filename :accessor stream-filename)))
  12. (defclass mp3-file-stream (base-file-stream)
  13. ((id3-header :accessor id3-header)
  14. (mpeg-info :accessor mpeg-info :initform nil)))
  15. (defclass mp4-file-stream (base-file-stream)
  16. ((mp4-atoms :accessor mp4-atoms :initform nil)))
  17. (defun make-file-stream (class-name filename &key (read-only t))
  18. (let ((new-stream (make-instance (find-class class-name))))
  19. (setf (stream new-stream) (if read-only
  20. (open filename :direction :input :element-type 'octet)
  21. (open filename :direction :io :if-exists :overwrite :element-type 'octet)))
  22. (setf (stream-filename new-stream) filename)
  23. new-stream))
  24. (defclass base-mem-stream (base-stream) ())
  25. (defun make-mem-stream (vector)
  26. (let ((new-stream (make-instance 'base-mem-stream)))
  27. (setf (stream new-stream) (ccl:make-vector-input-stream vector))
  28. new-stream))
  29. (defmethod stream-close ((in-stream base-file-stream))
  30. (with-slots (stream) in-stream
  31. (when stream
  32. (close stream)
  33. (setf stream nil))))
  34. (defmethod stream-close ((in-stream base-mem-stream))
  35. (with-slots (stream) in-stream
  36. (setf stream nil)))
  37. (defmethod stream-size ((in-stream base-stream))
  38. (ccl::stream-length (stream in-stream)))
  39. (defmethod stream-seek ((in-stream base-stream) offset from)
  40. (with-slots (stream) in-stream
  41. (ecase from
  42. (:start (ccl::stream-position stream offset))
  43. (:current (if (zerop offset)
  44. (ccl::stream-position stream)
  45. (ccl::stream-position stream (+ (ccl::stream-position stream) offset))))
  46. (:end (ccl::stream-position stream (- (ccl::stream-length stream) offset))))))
  47. (defun stream-read-octets (instream bytes &key (bits-per-byte 8))
  48. (loop with value = 0
  49. for low-bit downfrom (* bits-per-byte (1- bytes)) to 0 by bits-per-byte do
  50. (setf (ldb (byte bits-per-byte low-bit) value) (read-byte instream))
  51. finally (return value)))
  52. (defmethod stream-read-u8 ((in-stream base-stream) &key (bits-per-byte 8))
  53. "read 1 byte from file"
  54. (with-slots (stream) in-stream
  55. (stream-read-octets stream 1 :bits-per-byte bits-per-byte)))
  56. (defmethod stream-read-u16 ((in-stream base-stream) &key (bits-per-byte 8))
  57. "read 2 bytes from file"
  58. (with-slots (stream) in-stream
  59. (stream-read-octets stream 2 :bits-per-byte bits-per-byte)))
  60. (defmethod stream-read-u24 ((in-stream base-stream) &key (bits-per-byte 8))
  61. "read 3 bytes from file"
  62. (with-slots (stream) in-stream
  63. (stream-read-octets stream 3 :bits-per-byte bits-per-byte)))
  64. (defmethod stream-read-u32 ((in-stream base-stream) &key (bits-per-byte 8))
  65. "read 4 bytes from file"
  66. (with-slots (stream) in-stream
  67. (stream-read-octets stream 4 :bits-per-byte bits-per-byte)))
  68. (defmethod stream-read-u64 ((in-stream base-stream) &key (bits-per-byte 8))
  69. "read 8 bytes from file"
  70. (with-slots (stream) in-stream
  71. (stream-read-octets stream 8 :bits-per-byte bits-per-byte)))
  72. ;; (defmethod stream-read-string ((stream base-stream) &key size (terminators nil))
  73. ;; "Read normal string from stream. If size is provided, read exactly that many octets.
  74. ;; If terminators is supplied, it is a list of characters that can terminate a string (and hence stop read)"
  75. ;; (with-output-to-string (s)
  76. ;; (with-slots (stream) stream
  77. ;; (let ((terminated nil)
  78. ;; (count 0)
  79. ;; (byte))
  80. ;; (loop
  81. ;; (when (if size (= count size) terminated) (return))
  82. ;; (setf byte (read-byte stream))
  83. ;; (incf count)
  84. ;; (when (member byte terminators :test #'=)
  85. ;; (setf terminated t))
  86. ;; (when (not terminated)
  87. ;; (write-char (code-char byte) s)))))))
  88. (defmethod stream-read-sequence ((stream base-stream) size &key (bits-per-byte 8))
  89. "Read SIZE octets from input-file in BIT-PER-BYTE sizes"
  90. (log5:with-context "stream-read-sequence"
  91. (ecase bits-per-byte
  92. (8
  93. ;(log-stream "reading ~:d bytes as 8-bit sequence" size)
  94. (let ((octets (make-octets size)))
  95. (read-sequence octets (slot-value stream 'stream))
  96. octets))
  97. (7
  98. ;(log-stream "reading ~:d bytes as 7-bit sequence" size)
  99. (let* ((last-byte-was-FF nil)
  100. (byte nil)
  101. (octets (ccl:with-output-to-vector (out)
  102. (dotimes (i size)
  103. (setf byte (stream-read-u8 stream))
  104. (if last-byte-was-FF
  105. (if (not (zerop byte))
  106. (write-byte byte out))
  107. (write-byte byte out))
  108. (setf last-byte-was-FF (= byte #xFF))))))
  109. ;(log-stream "file pos is now: ~:d" (stream-seek stream 0 :current))
  110. ;(log-stream "~a" (utils:printable-array octets))
  111. octets)))))
  112. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; STRINGS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  113. ;;
  114. ;; decode octets as an iso-8859-1 string (encoding == 0)
  115. (defun stream-decode-iso-string (octets &key (start 0) (end nil))
  116. (ccl:decode-string-from-octets octets :start start :end end :external-format :iso-8859-1))
  117. ;;
  118. ;; decode octets as a ucs string (encoding == 1)
  119. ;; XXX: Coded this way because I can't seem to get a simple :external-format :ucs-2 to work correctly
  120. ;; AND some taggers encode a UCS-2 empty string w/o a byte-order mark (i.e. null strings are
  121. ;; sometimes encoded as #(00 00))
  122. (defun stream-decode-ucs-string (octets &key (start 0) (end nil))
  123. (labels ((get-byte-order-mark (octets)
  124. (let ((retval 0))
  125. (setf (ldb (byte 8 0) retval) (aref octets 1))
  126. (setf (ldb (byte 8 8) retval) (aref octets 0))
  127. (when (not (or (= #xfffe retval) (= #xfeff retval)))
  128. (error "got an invalid byte-order mark of ~x" retval))
  129. retval)))
  130. ;; special case: empty (and mis-coded) string
  131. (cond ((zerop (length octets))
  132. (make-string 0))
  133. (t
  134. ;;
  135. ;; else, we have a (hopefully) properly encoded string
  136. (let ((bom (get-byte-order-mark octets)))
  137. (ecase (the fixnum bom)
  138. (#xfffe (ccl:decode-string-from-octets octets :start (+ 2 start) :end end :external-format :ucs-2le))
  139. (#xfeff (ccl:decode-string-from-octets octets :start (+ 2 start) :end end :external-format :ucs-2be))
  140. (0 (make-string 0))))))))
  141. ;;
  142. ;; decode octets as a ucs-be string (encoding == 2)
  143. (defun stream-decode-ucs-be-string (octets &key (start 0) (end nil))
  144. (ccl:decode-string-from-octets octets :start start :end end :external-format :ucs-2be))
  145. ;;
  146. ;; decode octets as a utf-8 string
  147. (defun stream-decode-utf-8-string (octets &key (start 0) (end nil))
  148. (ccl:decode-string-from-octets octets :start start :end end :external-format :utf-8))
  149. ;;
  150. ;; decode octets depending on encoding
  151. (defun stream-decode-string (octets &key (start 0) (end nil) (encoding 0))
  152. (ecase encoding
  153. (0 (stream-decode-iso-string octets :start start :end end))
  154. (1 (stream-decode-ucs-string octets :start start :end end))
  155. (2 (stream-decode-ucs-be-string octets :start start :end end))
  156. (3 (stream-decode-utf-8-string octets :start start :end end))))
  157. ;;
  158. ;; read an iso-8859-1 string of length 'len' (encoding = 0)
  159. (defmethod stream-read-iso-string-with-len ((instream base-stream) len)
  160. (let ((octets (stream-read-sequence instream len)))
  161. (stream-decode-iso-string octets)))
  162. ;;
  163. ;; read an ucs-2 string of length 'len' (encoding = 1)
  164. (defmethod stream-read-ucs-string-with-len ((instream base-stream) len)
  165. (let ((octets (stream-read-sequence instream len)))
  166. (stream-decode-ucs-string octets)))
  167. ;;
  168. ;; read an ucs-2-be string of length 'len' (encoding = 2)
  169. (defmethod stream-read-ucs-be-string-with-len ((instream base-stream) len)
  170. (let ((octets (stream-read-sequence instream len)))
  171. (stream-decode-ucs-be-string octets)))
  172. ;;
  173. ;; read an utf-8 string of length 'len' (encoding = 3)
  174. (defmethod stream-read-utf-8-string-with-len ((instream base-stream) len)
  175. (let ((octets (stream-read-sequence instream len)))
  176. (stream-decode-utf-8-string octets)))
  177. ;;
  178. ;; Read in a string of a given encoding of length 'len'
  179. (defmethod stream-read-string-with-len ((instream base-stream) len &key (encoding 0))
  180. ;(format t "s-wth-len: ~a, ~d, ~d~%" instream len encoding)
  181. (ecase encoding
  182. (0 (stream-read-iso-string-with-len instream len))
  183. (1 (stream-read-ucs-string-with-len instream len))
  184. (2 (stream-read-ucs-be-string-with-len instream len))
  185. (3 (stream-read-utf-8-string-with-len instream len))))
  186. ;;
  187. ;; Read in a null terminated iso-8859-1 string
  188. (defmethod stream-read-iso-string ((instream base-stream))
  189. (let ((octets (ccl:with-output-to-vector (out)
  190. (do ((b (stream-read-u8 instream) (stream-read-u8 instream)))
  191. (nil)
  192. (when (zerop b)
  193. (return)) ; leave loop w/o writing
  194. (write-byte b out)))))
  195. (stream-decode-iso-string octets)))
  196. ;;
  197. ;; Read in a null terminated ucs string
  198. (defmethod stream-read-ucs-string ((instream base-stream))
  199. (let ((octets (ccl:with-output-to-vector (out)
  200. (do* ((b0 (stream-read-u8 instream)
  201. (stream-read-u8 instream))
  202. (b1 (stream-read-u8 instream)
  203. (stream-read-u8 instream)))
  204. (nil)
  205. (when (and (zerop b0) (zerop b1))
  206. (return))
  207. (write-byte b0 out)
  208. (write-byte b1 out)))))
  209. (stream-decode-ucs-string octets)))
  210. ;;
  211. ;; Read in a null terminated ucs-be string
  212. (defmethod stream-read-ucs-be-string ((instream base-stream))
  213. (let ((octets (ccl:with-output-to-vector (out)
  214. (do* ((b0 (stream-read-u8 instream)
  215. (stream-read-u8 instream))
  216. (b1 (stream-read-u8 instream)
  217. (stream-read-u8 instream)))
  218. (nil)
  219. (when (and (zerop b0) (zerop b1))
  220. (return))
  221. (write-byte b0 out)
  222. (write-byte b1 out)))))
  223. (stream-decode-ucs-be-string octets)))
  224. ;;
  225. ;; Read in a null terminated utf-8 string (encoding == 3)
  226. (defmethod stream-read-utf-8-string ((instream base-stream))
  227. (let ((octets (ccl:with-output-to-vector (out)
  228. (do ((b (stream-read-u8 instream)
  229. (stream-read-u8 instream)))
  230. (nil)
  231. (when (zerop b)
  232. (return))
  233. (write-byte b out)))))
  234. (stream-decode-utf-8-string octets)))
  235. ;;
  236. ;; Read in a null terminated string of a given encoding
  237. (defmethod stream-read-string ((instream base-stream) &key (encoding 0))
  238. (ecase encoding
  239. (0 (stream-read-iso-string instream))
  240. (1 (stream-read-ucs-string instream))
  241. (2 (stream-read-ucs-be-string instream))
  242. (3 (stream-read-utf-8-string instream))))
  243. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; FILES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  244. (defun parse-mp4-file (filename)
  245. (let (stream)
  246. (handler-case
  247. (progn
  248. (setf stream (make-file-stream 'mp4-file-stream filename))
  249. (mp4-atom:find-mp4-atoms stream))
  250. (mp4-atom:mp4-atom-condition (c)
  251. (warn-user "make-mp4-stream got condition: ~a" c)
  252. (when stream (stream-close stream))
  253. (setf stream nil)))
  254. stream))
  255. (defvar *get-mpeg-info* nil)
  256. (defun parse-mp3-file (filename &key (get-mpeg-info *get-mpeg-info*))
  257. (let (stream)
  258. (handler-case
  259. (progn
  260. (setf stream (make-file-stream 'mp3-file-stream filename))
  261. (id3-frame:find-id3-frames stream)
  262. (when get-mpeg-info
  263. (setf (mpeg-info stream) (mpeg:get-mpeg-info stream))))
  264. (id3-frame:id3-frame-condition (c)
  265. (warn-user "make-mp3-stream got condition: ~a" c)
  266. (when stream (stream-close stream))
  267. (setf stream nil)))
  268. stream))