audio-streams.lisp 13 KB

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