audio-streams.lisp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. ((fn :accessor fn :initform nil :initarg :fn)
  24. (index :accessor index :initform 0)
  25. (len :accessor len :initform 0)
  26. (vect :accessor vect :initform nil :initarg :vect))
  27. (:documentation "A thin-wrapper class over mmaped-files and/or vectors"))
  28. (defmacro with-mem-stream-slots ((instance) &body body)
  29. `(with-slots (fn index len vect) ,instance
  30. (declare (integer index len)
  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 :fn f))
  35. (defmethod initialize-instance :after ((stream mem-stream) &key)
  36. (with-mem-stream-slots (stream)
  37. (when fn
  38. (setf vect (ccl:map-file-to-octet-vector fn)))
  39. (setf len (length vect))))
  40. (defmethod stream-close ((stream mem-stream))
  41. (with-mem-stream-slots (stream)
  42. (when fn
  43. (ccl:unmap-octet-vector vect))
  44. (setf vect nil)))
  45. (defmethod stream-seek ((stream mem-stream) &optional (offset 0) (from :current))
  46. (with-mem-stream-slots (stream)
  47. (ecase from
  48. (:start (setf index offset))
  49. (:current
  50. (if (zerop offset)
  51. index
  52. (incf index offset)))
  53. (:end (setf index (- len offset))))))
  54. ;;; probably should just rename :ACCESSOR LEN to STREAM-SIZE? XXX
  55. (defmethod stream-size ((stream mem-stream)) (len stream))
  56. (defun read-n-bytes (stream n-bytes &key (bits-per-byte 8))
  57. (fastest
  58. (with-mem-stream-slots (stream)
  59. (when (<= (+ index n-bytes) len)
  60. (loop with value = 0
  61. for low-bit downfrom (* bits-per-byte (1- n-bytes)) to 0 by bits-per-byte do
  62. (setf (ldb (byte bits-per-byte low-bit) value) (aref vect index))
  63. (incf index)
  64. finally (return-from read-n-bytes value))))
  65. nil))
  66. (declaim (inline read-n-bytes))
  67. (defmethod stream-read-u8 ((stream mem-stream) &key (bits-per-byte 8)) (read-n-bytes stream 1 :bits-per-byte bits-per-byte))
  68. (defmethod stream-read-u16 ((stream mem-stream) &key (bits-per-byte 8)) (read-n-bytes stream 2 :bits-per-byte bits-per-byte))
  69. (defmethod stream-read-u24 ((stream mem-stream) &key (bits-per-byte 8)) (read-n-bytes stream 3 :bits-per-byte bits-per-byte))
  70. (defmethod stream-read-u32 ((stream mem-stream) &key (bits-per-byte 8)) (read-n-bytes stream 4 :bits-per-byte bits-per-byte))
  71. (defmethod stream-read-u64 ((stream mem-stream) &key (bits-per-byte 8)) (read-n-bytes stream 8 :bits-per-byte bits-per-byte))
  72. (defmethod stream-read-sequence ((stream mem-stream) size &key (bits-per-byte 8))
  73. (fastest
  74. (with-mem-stream-slots (stream)
  75. (when (> (+ index size) len)
  76. (setf size (- len index)))
  77. (ecase bits-per-byte
  78. (8 (let ((octets (make-array size :element-type 'octet :displaced-to vect :displaced-index-offset index :adjustable nil)))
  79. (incf index size)
  80. (values octets size)))
  81. (7
  82. (let* ((last-byte-was-FF nil)
  83. (byte nil)
  84. (octets (ccl:with-output-to-vector (out)
  85. (dotimes (i size)
  86. (setf byte (stream-read-u8 stream))
  87. (if last-byte-was-FF
  88. (if (not (zerop byte))
  89. (write-byte byte out))
  90. (write-byte byte out))
  91. (setf last-byte-was-FF (= byte #xFF))))))
  92. (values octets size)))))))
  93. (defclass mp3-file-stream (mem-stream)
  94. ((id3-header :accessor id3-header :initform nil :documentation "holds all the ID3 info")
  95. (audio-info :accessor audio-info :initform nil :documentation "holds the bit-rate, etc info"))
  96. (:documentation "Stream for parsing MP3 files"))
  97. (defclass mp4-file-stream (mem-stream)
  98. ((mp4-atoms :accessor mp4-atoms :initform nil :documentation "holds tree of parsed MP4 atoms/boxes")
  99. (audio-info :accessor audio-info :initform nil :documentation "holds the bit-rate, etc info"))
  100. (:documentation "Stream for parsing MP4A files"))
  101. (defun make-file-stream (filename)
  102. "Convenience function for creating a file stream."
  103. (let* ((new-stream (make-mmap-stream filename))
  104. (ret-stream))
  105. (cond ((mp4-atom:is-valid-m4-file new-stream)
  106. (setf ret-stream (make-instance 'mp4-file-stream :vect (vect new-stream) :fn (fn new-stream))))
  107. ((id3-frame:is-valid-mp3-file new-stream)
  108. (setf ret-stream (make-instance 'mp3-file-stream :vect (vect new-stream) :fn (fn new-stream)))))
  109. (stream-close new-stream)
  110. ret-stream))
  111. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Strings ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  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. ;;; XXX: Coded this way because I can't seem to get a simple :external-format :ucs-2 to work correctly
  118. ;;; AND some taggers encode a UCS-2 empty string w/o a byte-order mark (i.e. null strings are
  119. ;;; sometimes encoded as #(00 00))
  120. (defun stream-decode-ucs-string (octets &key (start 0) (end nil))
  121. "Decode octets as a UCS string with a BOM (encoding == 1)"
  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. (error 'audio-stream-condition
  128. :location "stream-decode-ucs-string"
  129. :object nil
  130. :message (format nil "got an invalid byte-order mark of ~x" retval)))
  131. retval)))
  132. ;; special case: empty (and mis-coded) string
  133. (cond ((zerop (length octets))
  134. (make-string 0))
  135. (t
  136. ;;
  137. ;; else, we have a (hopefully) properly encoded string
  138. (let ((bom (get-byte-order-mark octets)))
  139. (ecase (the fixnum bom)
  140. (#xfffe (ccl:decode-string-from-octets octets :start (+ 2 start) :end end :external-format :ucs-2le))
  141. (#xfeff (ccl:decode-string-from-octets octets :start (+ 2 start) :end end :external-format :ucs-2be))
  142. (0 (make-string 0))))))))
  143. (defun stream-decode-ucs-be-string (octets &key (start 0) (end nil))
  144. "Decode octets as a UCS-BE string (encoding == 2)"
  145. (ccl:decode-string-from-octets octets :start start :end end :external-format :ucs-2be))
  146. (defun stream-decode-utf-8-string (octets &key (start 0) (end nil))
  147. "Decode octets as a utf-8 string"
  148. (ccl:decode-string-from-octets octets :start start :end end :external-format :utf-8))
  149. (defun stream-decode-string (octets &key (start 0) (end nil) (encoding 0))
  150. "Decode octets depending on encoding"
  151. (ecase encoding
  152. (0 (stream-decode-iso-string octets :start start :end end))
  153. (1 (stream-decode-ucs-string octets :start start :end end))
  154. (2 (stream-decode-ucs-be-string octets :start start :end end))
  155. (3 (stream-decode-utf-8-string octets :start start :end end))))
  156. (defmethod stream-read-iso-string-with-len ((instream mem-stream) len)
  157. "Read an iso-8859-1 string of length 'len' (encoding = 0)"
  158. (let ((octets (stream-read-sequence instream len)))
  159. (stream-decode-iso-string octets)))
  160. (defmethod stream-read-ucs-string-with-len ((instream mem-stream) len)
  161. "Read an ucs-2 string of length 'len' (encoding = 1)"
  162. (let ((octets (stream-read-sequence instream len)))
  163. (stream-decode-ucs-string octets)))
  164. (defmethod stream-read-ucs-be-string-with-len ((instream mem-stream) len)
  165. "Read an ucs-2-be string of length 'len' (encoding = 2)"
  166. (let ((octets (stream-read-sequence instream len)))
  167. (stream-decode-ucs-be-string octets)))
  168. (defmethod stream-read-utf-8-string-with-len ((instream mem-stream) len)
  169. "Read an utf-8 string of length 'len' (encoding = 3)"
  170. (let ((octets (stream-read-sequence instream len)))
  171. (stream-decode-utf-8-string octets)))
  172. (defmethod stream-read-string-with-len ((instream mem-stream) len &key (encoding 0))
  173. "Read in a string of a given encoding of length 'len'"
  174. (ecase encoding
  175. (0 (stream-read-iso-string-with-len instream len))
  176. (1 (stream-read-ucs-string-with-len instream len))
  177. (2 (stream-read-ucs-be-string-with-len instream len))
  178. (3 (stream-read-utf-8-string-with-len instream len))))
  179. (defmethod stream-read-iso-string ((instream mem-stream))
  180. "Read in a null terminated iso-8859-1 string"
  181. (let ((octets (ccl:with-output-to-vector (out)
  182. (do ((b (stream-read-u8 instream) (stream-read-u8 instream)))
  183. (nil)
  184. (when (zerop b)
  185. (return)) ; leave loop w/o writing
  186. (write-byte b out)))))
  187. (stream-decode-iso-string octets)))
  188. (defmethod stream-read-ucs-string ((instream mem-stream))
  189. "Read in a null terminated UCS string."
  190. (let ((octets (ccl:with-output-to-vector (out)
  191. (do* ((b0 (stream-read-u8 instream)
  192. (stream-read-u8 instream))
  193. (b1 (stream-read-u8 instream)
  194. (stream-read-u8 instream)))
  195. (nil)
  196. (when (and (zerop b0) (zerop b1))
  197. (return))
  198. (write-byte b0 out)
  199. (write-byte b1 out)))))
  200. (stream-decode-ucs-string octets)))
  201. (defmethod stream-read-ucs-be-string ((instream mem-stream))
  202. "Read in a null terminated UCS-BE string."
  203. (let ((octets (ccl:with-output-to-vector (out)
  204. (do* ((b0 (stream-read-u8 instream)
  205. (stream-read-u8 instream))
  206. (b1 (stream-read-u8 instream)
  207. (stream-read-u8 instream)))
  208. (nil)
  209. (when (and (zerop b0) (zerop b1))
  210. (return))
  211. (write-byte b0 out)
  212. (write-byte b1 out)))))
  213. (stream-decode-ucs-be-string octets)))
  214. (defmethod stream-read-utf-8-string ((instream mem-stream))
  215. "Read in a null terminated utf-8 string (encoding == 3)"
  216. (let ((octets (ccl:with-output-to-vector (out)
  217. (do ((b (stream-read-u8 instream)
  218. (stream-read-u8 instream)))
  219. (nil)
  220. (when (zerop b)
  221. (return))
  222. (write-byte b out)))))
  223. (stream-decode-utf-8-string octets)))
  224. (defmethod stream-read-string ((instream mem-stream) &key (encoding 0))
  225. "Read in a null terminated string of a given encoding."
  226. (ecase encoding
  227. (0 (stream-read-iso-string instream))
  228. (1 (stream-read-ucs-string instream))
  229. (2 (stream-read-ucs-be-string instream))
  230. (3 (stream-read-utf-8-string instream))))
  231. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Files ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  232. (defvar *get-audio-info* t "controls whether the parsing functions also parse audio info like bit-rate, etc")
  233. (defmethod parse-audio-file ((stream mp4-file-stream) &key (get-audio-info *get-audio-info*) &allow-other-keys)
  234. "Parse an MP4A file by reading it's ATOMS and decoding them."
  235. (handler-case
  236. (progn
  237. (mp4-atom:find-mp4-atoms stream)
  238. (when get-audio-info
  239. (setf (audio-info stream) (mp4-atom:get-mp4-audio-info stream))))
  240. (mp4-atom:mp4-atom-condition (c)
  241. (utils:warn-user "make-mp4-stream got condition: ~a" c))))
  242. (defmethod parse-audio-file ((stream mp3-file-stream) &key (get-audio-info *get-audio-info*) &allow-other-keys)
  243. "Parse an MP3 file by reading it's FRAMES and decoding them."
  244. (handler-case
  245. (progn
  246. (id3-frame:find-id3-frames stream)
  247. (when get-audio-info
  248. (setf (audio-info stream) (mpeg:get-mpeg-audio-info stream))))
  249. (id3-frame:id3-frame-condition (c)
  250. (utils:warn-user "make-mp3-stream got condition: ~a" c))))