audio-streams.lisp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. (audio-info :accessor audio-info :initform nil)))
  15. (defclass mp4-file-stream (base-file-stream)
  16. ((mp4-atoms :accessor mp4-atoms :initform nil)
  17. (audio-info :accessor audio-info :initform nil)))
  18. (defun make-file-stream (class-name filename &key (read-only t))
  19. (let ((new-stream (make-instance (find-class class-name))))
  20. (setf (stream new-stream) (if read-only
  21. (open filename :direction :input :element-type 'octet)
  22. (open filename :direction :io :if-exists :overwrite :element-type 'octet)))
  23. (setf (stream-filename new-stream) filename)
  24. new-stream))
  25. (defclass base-mem-stream (base-stream) ())
  26. (defun make-mem-stream (vector)
  27. (let ((new-stream (make-instance 'base-mem-stream)))
  28. (setf (stream new-stream) (ccl:make-vector-input-stream vector))
  29. new-stream))
  30. (defmethod stream-close ((in-stream base-file-stream))
  31. (with-slots (stream) in-stream
  32. (when stream
  33. (close stream)
  34. (setf stream nil))))
  35. (defmethod stream-close ((in-stream base-mem-stream))
  36. (with-slots (stream) in-stream
  37. (setf stream nil)))
  38. (defmethod stream-size ((in-stream base-stream))
  39. (ccl::stream-length (stream in-stream)))
  40. (defmethod stream-seek ((in-stream base-stream) offset from)
  41. (with-slots (stream) in-stream
  42. (ecase from
  43. (:start (ccl::stream-position stream offset))
  44. (:current (if (zerop offset)
  45. (ccl::stream-position stream)
  46. (ccl::stream-position stream (+ (ccl::stream-position stream) offset))))
  47. (:end (ccl::stream-position stream (- (ccl::stream-length stream) offset))))))
  48. (defun stream-read-octets (instream bytes &key (bits-per-byte 8))
  49. (loop with value = 0
  50. for low-bit downfrom (* bits-per-byte (1- bytes)) to 0 by bits-per-byte do
  51. (setf (ldb (byte bits-per-byte low-bit) value) (read-byte instream))
  52. finally (return value)))
  53. (defmethod stream-read-u8 ((in-stream base-stream) &key (bits-per-byte 8))
  54. "read 1 byte from file"
  55. (with-slots (stream) in-stream
  56. (stream-read-octets stream 1 :bits-per-byte bits-per-byte)))
  57. (defmethod stream-read-u16 ((in-stream base-stream) &key (bits-per-byte 8))
  58. "read 2 bytes from file"
  59. (with-slots (stream) in-stream
  60. (stream-read-octets stream 2 :bits-per-byte bits-per-byte)))
  61. (defmethod stream-read-u24 ((in-stream base-stream) &key (bits-per-byte 8))
  62. "read 3 bytes from file"
  63. (with-slots (stream) in-stream
  64. (stream-read-octets stream 3 :bits-per-byte bits-per-byte)))
  65. (defmethod stream-read-u32 ((in-stream base-stream) &key (bits-per-byte 8))
  66. "read 4 bytes from file"
  67. (with-slots (stream) in-stream
  68. (stream-read-octets stream 4 :bits-per-byte bits-per-byte)))
  69. (defmethod stream-read-u64 ((in-stream base-stream) &key (bits-per-byte 8))
  70. "read 8 bytes from file"
  71. (with-slots (stream) in-stream
  72. (stream-read-octets stream 8 :bits-per-byte bits-per-byte)))
  73. ;; (defmethod stream-read-string ((stream base-stream) &key size (terminators nil))
  74. ;; "Read normal string from stream. If size is provided, read exactly that many octets.
  75. ;; If terminators is supplied, it is a list of characters that can terminate a string (and hence stop read)"
  76. ;; (with-output-to-string (s)
  77. ;; (with-slots (stream) stream
  78. ;; (let ((terminated nil)
  79. ;; (count 0)
  80. ;; (byte))
  81. ;; (loop
  82. ;; (when (if size (= count size) terminated) (return))
  83. ;; (setf byte (read-byte stream))
  84. ;; (incf count)
  85. ;; (when (member byte terminators :test #'=)
  86. ;; (setf terminated t))
  87. ;; (when (not terminated)
  88. ;; (write-char (code-char byte) s)))))))
  89. (defmethod stream-read-sequence ((stream base-stream) size &key (bits-per-byte 8))
  90. "Read SIZE octets from input-file in BIT-PER-BYTE sizes"
  91. (log5:with-context "stream-read-sequence"
  92. (ecase bits-per-byte
  93. (8
  94. ;(log-stream "reading ~:d bytes as 8-bit sequence" size)
  95. (let ((octets (make-octets size)))
  96. (read-sequence octets (slot-value stream 'stream))
  97. octets))
  98. (7
  99. ;(log-stream "reading ~:d bytes as 7-bit sequence" size)
  100. (let* ((last-byte-was-FF nil)
  101. (byte nil)
  102. (octets (ccl:with-output-to-vector (out)
  103. (dotimes (i size)
  104. (setf byte (stream-read-u8 stream))
  105. (if last-byte-was-FF
  106. (if (not (zerop byte))
  107. (write-byte byte out))
  108. (write-byte byte out))
  109. (setf last-byte-was-FF (= byte #xFF))))))
  110. ;(log-stream "file pos is now: ~:d" (stream-seek stream 0 :current))
  111. ;(log-stream "~a" (utils:printable-array octets))
  112. octets)))))
  113. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; STRINGS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  114. ;;
  115. ;; decode octets as an iso-8859-1 string (encoding == 0)
  116. (defun stream-decode-iso-string (octets &key (start 0) (end nil))
  117. (ccl:decode-string-from-octets octets :start start :end end :external-format :iso-8859-1))
  118. ;;
  119. ;; decode octets as a ucs string (encoding == 1)
  120. ;; XXX: Coded this way because I can't seem to get a simple :external-format :ucs-2 to work correctly
  121. ;; AND some taggers encode a UCS-2 empty string w/o a byte-order mark (i.e. null strings are
  122. ;; sometimes encoded as #(00 00))
  123. (defun stream-decode-ucs-string (octets &key (start 0) (end nil))
  124. (labels ((get-byte-order-mark (octets)
  125. (let ((retval 0))
  126. (setf (ldb (byte 8 0) retval) (aref octets 1))
  127. (setf (ldb (byte 8 8) retval) (aref octets 0))
  128. (when (not (or (= #xfffe retval) (= #xfeff retval)))
  129. (error "got an invalid byte-order mark of ~x" retval))
  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 &key (get-audio-info *get-audio-info*))
  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. (when get-audio-info
  252. (setf (audio-info stream) (mp4-atom:get-mp4-audio-info stream))))
  253. (mp4-atom:mp4-atom-condition (c)
  254. (warn-user "make-mp4-stream got condition: ~a" c)
  255. (when stream (stream-close stream))
  256. (setf stream nil)))
  257. stream))
  258. (defvar *get-audio-info* nil)
  259. (defun parse-mp3-file (filename &key (get-audio-info *get-audio-info*))
  260. (let (stream)
  261. (handler-case
  262. (progn
  263. (setf stream (make-file-stream 'mp3-file-stream filename))
  264. (id3-frame:find-id3-frames stream)
  265. (when get-audio-info
  266. (setf (audio-info stream) (mpeg:get-mpeg-audio-info stream))))
  267. (id3-frame:id3-frame-condition (c)
  268. (warn-user "make-mp3-stream got condition: ~a" c)
  269. (when stream (stream-close stream))
  270. (setf stream nil)))
  271. stream))