audio-streams.lisp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. ;;; -*- Mode: Lisp; show-trailing-whitespace: t; Base: 10; indent-tabs: nil; Syntax: ANSI-Common-Lisp; Package: AUDIO-STREAMS; -*-
  2. ;;; Copyright (c) 2013, Mark VandenBrink. All rights reserved.
  3. (in-package #:audio-streams)
  4. (eval-when (:compile-toplevel :load-toplevel :execute)
  5. (defconstant +optimize-fastest+ '(optimize (speed 3) (safety 0) (debug 0)))
  6. (defmacro fastest (&body body)
  7. `(locally (declare ,+optimize-fastest+)
  8. ,@body)))
  9. (log5:defcategory cat-log-stream)
  10. (defmacro log-stream (&rest log-stuff) `(log5:log-for (cat-log-stream) ,@log-stuff))
  11. (define-condition audio-stream-condition ()
  12. ((location :initarg :location :reader location :initform nil)
  13. (object :initarg :object :reader object :initform nil)
  14. (messsage :initarg :message :reader message :initform "Undefined Condition"))
  15. (:report (lambda (condition stream)
  16. (format stream "audio-stream condition at location: <~a> with object: <~a>: message: <~a>"
  17. (location condition) (object condition) (message condition)))))
  18. (defmethod print-object ((me audio-stream-condition) stream)
  19. (format stream "location: <~a>, object: <~a>, message: <~a>" (location me) (object me) (message me)))
  20. (deftype octet () '(unsigned-byte 8))
  21. (defmacro make-octets (len) `(make-array ,len :element-type 'octet))
  22. (defclass mem-stream ()
  23. ((stream-filename :accessor stream-filename :initform nil :initarg :stream-filename :documentation "if set, then MMAP file")
  24. (index :accessor index :initform 0)
  25. (stream-size :accessor stream-size :initform 0)
  26. (vect :accessor vect :initform nil :initarg :vect :documentation "if set, then the vector we want STREAM-ize"))
  27. (:documentation "A thin-wrapper class over mmaped-files and/or vectors."))
  28. (defmacro with-mem-stream-slots ((instance) &body body)
  29. `(with-slots (stream-filename index stream-size vect) ,instance
  30. (declare (integer index stream-size)
  31. (type (array (unsigned-byte 8) 1) vect))
  32. ,@body))
  33. (defun make-mem-stream (v) (make-instance 'mem-stream :vect v))
  34. (defun make-mmap-stream (f) (make-instance 'mem-stream :stream-filename f))
  35. (defmethod initialize-instance :after ((stream mem-stream) &key)
  36. "Stream initializer. If STREAM-FILENAME is set, MMAP a the file. Else, we assume VECT was set."
  37. (with-mem-stream-slots (stream)
  38. (when stream-filename
  39. (setf vect (ccl:map-file-to-octet-vector stream-filename)))
  40. (setf stream-size (length vect))))
  41. (defmethod stream-close ((stream mem-stream))
  42. "Close a stream, making the underlying object (file or vector) inaccessible."
  43. (with-mem-stream-slots (stream)
  44. (when stream-filename
  45. (ccl:unmap-octet-vector vect))
  46. (setf vect nil)))
  47. (defmethod stream-seek ((stream mem-stream) &optional (offset 0) (from :current))
  48. "Set INDEX to requested value. No error checking done here, but subsequent reads will fail if INDEX is out-of-bounds.
  49. As a convenience, OFFSET and FROM are optional, so (STREAM-SEEK stream) returns the current read-offset in stream."
  50. (with-mem-stream-slots (stream)
  51. (ecase from
  52. (:start ; INDEX set to OFFSET from start of stream
  53. (setf index offset))
  54. (:current ; INDEX set relative to current INDEX, by OFFSET bytes
  55. (if (zerop offset)
  56. index
  57. (incf index offset)))
  58. (:end ; INDEX set to OFFSET from end of stream
  59. (setf index (- stream-size offset))))))
  60. (defun read-n-bytes (stream n-bytes &key (bits-per-byte 8))
  61. "Returns a FIXNUM constructed by reading N-BYTES. BITS-PER-BYTE contols how many bits should be used from each read byte."
  62. (fastest
  63. (with-mem-stream-slots (stream)
  64. (declare (integer index n-bytes stream-size))
  65. (when (<= (+ index n-bytes) stream-size)
  66. (loop with value = 0
  67. for low-bit downfrom (* bits-per-byte (1- n-bytes)) to 0 by bits-per-byte do
  68. (setf (ldb (byte bits-per-byte low-bit) value) (aref vect index))
  69. (incf index)
  70. finally (return-from read-n-bytes value))))
  71. nil))
  72. (declaim (inline read-n-bytes))
  73. (defmethod stream-read-u8 ((stream mem-stream) &key (bits-per-byte 8)) (read-n-bytes stream 1 :bits-per-byte bits-per-byte))
  74. (defmethod stream-read-u16 ((stream mem-stream) &key (bits-per-byte 8)) (read-n-bytes stream 2 :bits-per-byte bits-per-byte))
  75. (defmethod stream-read-u24 ((stream mem-stream) &key (bits-per-byte 8)) (read-n-bytes stream 3 :bits-per-byte bits-per-byte))
  76. (defmethod stream-read-u32 ((stream mem-stream) &key (bits-per-byte 8)) (read-n-bytes stream 4 :bits-per-byte bits-per-byte))
  77. (defmethod stream-read-u64 ((stream mem-stream) &key (bits-per-byte 8)) (read-n-bytes stream 8 :bits-per-byte bits-per-byte))
  78. (defmethod stream-read-sequence ((stream mem-stream) size &key (bits-per-byte 8))
  79. "Read in a sequence of octets at BITS-PER-BYTE. If BITS-PER-BYTE == 8, then simply return
  80. a displaced array from STREAMs underlying vector. If it is == 7, then we have to create a new vector and read into that."
  81. (fastest
  82. (with-mem-stream-slots (stream)
  83. (when (> (+ index size) stream-size)
  84. (setf size (- stream-size index)))
  85. (ecase bits-per-byte
  86. (8 (let ((octets (make-array size :element-type 'octet :displaced-to vect :displaced-index-offset index :adjustable nil)))
  87. (incf index size)
  88. (values octets size)))
  89. (7
  90. (let* ((last-byte-was-FF nil)
  91. (byte nil)
  92. (octets (ccl:with-output-to-vector (out)
  93. (dotimes (i size)
  94. (setf byte (stream-read-u8 stream))
  95. (if last-byte-was-FF
  96. (if (not (zerop byte))
  97. (write-byte byte out))
  98. (write-byte byte out))
  99. (setf last-byte-was-FF (= byte #xFF))))))
  100. (values octets size)))))))
  101. (defclass mp3-file-stream (mem-stream)
  102. ((id3-header :accessor id3-header :initform nil :documentation "holds all the ID3 info")
  103. (audio-info :accessor audio-info :initform nil :documentation "holds the bit-rate, etc info"))
  104. (:documentation "Stream for parsing MP3 files"))
  105. (defclass mp4-file-stream (mem-stream)
  106. ((mp4-atoms :accessor mp4-atoms :initform nil :documentation "holds tree of parsed MP4 atoms/boxes")
  107. (audio-info :accessor audio-info :initform nil :documentation "holds the bit-rate, etc info"))
  108. (:documentation "Stream for parsing MP4 audio files"))
  109. (defun make-file-stream (filename)
  110. "Convenience function for creating a file stream. Detects file type and returns proper type stream."
  111. (let* ((new-stream (make-mmap-stream filename))
  112. (ret-stream))
  113. ;; detect file type and make RET-STREAM. if we don't recognize stream, RET-STREAM will be NULL
  114. (cond ((mp4-atom:is-valid-m4-file new-stream)
  115. (setf ret-stream (make-instance 'mp4-file-stream :vect (vect new-stream) :stream-filename (stream-filename new-stream))))
  116. ((id3-frame:is-valid-mp3-file new-stream)
  117. (setf ret-stream (make-instance 'mp3-file-stream :vect (vect new-stream) :stream-filename (stream-filename new-stream)))))
  118. (stream-close new-stream)
  119. ret-stream))
  120. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Strings ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  121. ;;; Decode octets as an iso-8859-1 string (encoding == 0)
  122. (defun stream-decode-iso-string (octets &key (start 0) (end nil))
  123. (ccl:decode-string-from-octets octets :start start :end end :external-format :iso-8859-1))
  124. ;;;
  125. ;;; XXX: Coded this way because I can't seem to get a simple :external-format :ucs-2 to work correctly
  126. ;;; AND some taggers encode a UCS-2 empty string w/o a byte-order mark (i.e. null strings are
  127. ;;; sometimes encoded as #(00 00))
  128. (defun stream-decode-ucs-string (octets &key (start 0) (end nil))
  129. "Decode octets as a UCS string with a BOM (encoding == 1)"
  130. (labels ((get-byte-order-mark (octets)
  131. (let ((retval 0))
  132. (setf (ldb (byte 8 0) retval) (aref octets 1))
  133. (setf (ldb (byte 8 8) retval) (aref octets 0))
  134. (when (not (or (= #xfffe retval) (= #xfeff retval)))
  135. (error 'audio-stream-condition
  136. :location "stream-decode-ucs-string"
  137. :object nil
  138. :message (format nil "got an invalid byte-order mark of ~x" retval)))
  139. retval)))
  140. ;; special case: empty (and mis-coded) string
  141. (cond ((zerop (length octets))
  142. (make-string 0))
  143. (t
  144. ;;
  145. ;; else, we have a (hopefully) properly encoded string
  146. (let ((bom (get-byte-order-mark octets)))
  147. (ecase (the fixnum bom)
  148. (#xfffe (ccl:decode-string-from-octets octets :start (+ 2 start) :end end :external-format :ucs-2le))
  149. (#xfeff (ccl:decode-string-from-octets octets :start (+ 2 start) :end end :external-format :ucs-2be))
  150. (0 (make-string 0))))))))
  151. (defun stream-decode-ucs-be-string (octets &key (start 0) (end nil))
  152. "Decode octets as a UCS-BE string (encoding == 2)"
  153. (ccl:decode-string-from-octets octets :start start :end end :external-format :ucs-2be))
  154. (defun stream-decode-utf-8-string (octets &key (start 0) (end nil))
  155. "Decode octets as a utf-8 string"
  156. (ccl:decode-string-from-octets octets :start start :end end :external-format :utf-8))
  157. (defun stream-decode-string (octets &key (start 0) (end nil) (encoding 0))
  158. "Decode octets depending on encoding"
  159. (ecase encoding
  160. (0 (stream-decode-iso-string octets :start start :end end))
  161. (1 (stream-decode-ucs-string octets :start start :end end))
  162. (2 (stream-decode-ucs-be-string octets :start start :end end))
  163. (3 (stream-decode-utf-8-string octets :start start :end end))))
  164. (defmethod stream-read-iso-string-with-len ((instream mem-stream) len)
  165. "Read an iso-8859-1 string of length 'len' (encoding = 0)"
  166. (let ((octets (stream-read-sequence instream len)))
  167. (stream-decode-iso-string octets)))
  168. (defmethod stream-read-ucs-string-with-len ((instream mem-stream) len)
  169. "Read an ucs-2 string of length 'len' (encoding = 1)"
  170. (let ((octets (stream-read-sequence instream len)))
  171. (stream-decode-ucs-string octets)))
  172. (defmethod stream-read-ucs-be-string-with-len ((instream mem-stream) len)
  173. "Read an ucs-2-be string of length 'len' (encoding = 2)"
  174. (let ((octets (stream-read-sequence instream len)))
  175. (stream-decode-ucs-be-string octets)))
  176. (defmethod stream-read-utf-8-string-with-len ((instream mem-stream) len)
  177. "Read an utf-8 string of length 'len' (encoding = 3)"
  178. (let ((octets (stream-read-sequence instream len)))
  179. (stream-decode-utf-8-string octets)))
  180. (defmethod stream-read-string-with-len ((instream mem-stream) len &key (encoding 0))
  181. "Read in a string of a given encoding of length 'len'"
  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. (defmethod stream-read-iso-string ((instream mem-stream))
  188. "Read in a null terminated iso-8859-1 string"
  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. (defmethod stream-read-ucs-string ((instream mem-stream))
  197. "Read in a null terminated UCS string."
  198. (let ((octets (ccl:with-output-to-vector (out)
  199. (do* ((b0 (stream-read-u8 instream)
  200. (stream-read-u8 instream))
  201. (b1 (stream-read-u8 instream)
  202. (stream-read-u8 instream)))
  203. (nil)
  204. (when (and (zerop b0) (zerop b1))
  205. (return))
  206. (write-byte b0 out)
  207. (write-byte b1 out)))))
  208. (stream-decode-ucs-string octets)))
  209. (defmethod stream-read-ucs-be-string ((instream mem-stream))
  210. "Read in a null terminated UCS-BE string."
  211. (let ((octets (ccl:with-output-to-vector (out)
  212. (do* ((b0 (stream-read-u8 instream)
  213. (stream-read-u8 instream))
  214. (b1 (stream-read-u8 instream)
  215. (stream-read-u8 instream)))
  216. (nil)
  217. (when (and (zerop b0) (zerop b1))
  218. (return))
  219. (write-byte b0 out)
  220. (write-byte b1 out)))))
  221. (stream-decode-ucs-be-string octets)))
  222. (defmethod stream-read-utf-8-string ((instream mem-stream))
  223. "Read in a null terminated utf-8 string (encoding == 3)"
  224. (let ((octets (ccl:with-output-to-vector (out)
  225. (do ((b (stream-read-u8 instream)
  226. (stream-read-u8 instream)))
  227. (nil)
  228. (when (zerop b)
  229. (return))
  230. (write-byte b out)))))
  231. (stream-decode-utf-8-string octets)))
  232. (defmethod stream-read-string ((instream mem-stream) &key (encoding 0))
  233. "Read in a null terminated string of a given encoding."
  234. (ecase encoding
  235. (0 (stream-read-iso-string instream))
  236. (1 (stream-read-ucs-string instream))
  237. (2 (stream-read-ucs-be-string instream))
  238. (3 (stream-read-utf-8-string instream))))
  239. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Files ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  240. (defvar *get-audio-info* t "controls whether the parsing functions also parse audio info like bit-rate, etc")
  241. (defmethod parse-audio-file ((stream mp4-file-stream) &key (get-audio-info *get-audio-info*) &allow-other-keys)
  242. "Parse an MP4A file by reading it's ATOMS and decoding them."
  243. (handler-case
  244. (progn
  245. (mp4-atom:find-mp4-atoms stream)
  246. (when get-audio-info
  247. (setf (audio-info stream) (mp4-atom:get-mp4-audio-info stream))))
  248. (mp4-atom:mp4-atom-condition (c)
  249. (utils:warn-user "make-mp4-stream got condition: ~a" c))))
  250. (defmethod parse-audio-file ((stream mp3-file-stream) &key (get-audio-info *get-audio-info*) &allow-other-keys)
  251. "Parse an MP3 file by reading it's FRAMES and decoding them."
  252. (handler-case
  253. (progn
  254. (id3-frame:find-id3-frames stream)
  255. (when get-audio-info
  256. (setf (audio-info stream) (mpeg:get-mpeg-audio-info stream))))
  257. (id3-frame:id3-frame-condition (c)
  258. (utils:warn-user "make-mp3-stream got condition: ~a" c))))