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