audio-streams.lisp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. (deftype octet () '(unsigned-byte 8))
  5. (defmacro make-octets (len) `(make-array ,len :element-type 'octet))
  6. (defclass mem-stream ()
  7. ((stream-filename :accessor stream-filename :initform nil :initarg :stream-filename :documentation "if set, then MMAP file")
  8. (index :accessor index :initform 0)
  9. (stream-size :accessor stream-size :initform 0)
  10. (vect :accessor vect :initform nil :initarg :vect :documentation "if set, then the vector we want STREAM-ize"))
  11. (:documentation "A thin-wrapper class over mmaped-files and/or vectors."))
  12. (defmacro with-mem-stream-slots ((instance) &body body)
  13. `(with-slots (stream-filename index stream-size vect) ,instance
  14. (declare (fixnum index stream-size)
  15. (type (or (array (unsigned-byte 8) 1) null) vect))
  16. ,@body))
  17. (defun make-mem-stream (v) (make-instance 'mem-stream :vect v))
  18. (defun make-mmap-stream (f) (make-instance 'mem-stream :stream-filename f))
  19. (defmethod initialize-instance :after ((stream mem-stream) &key)
  20. "Stream initializer. If STREAM-FILENAME is set, MMAP a the file. Else, we assume VECT was set."
  21. (with-mem-stream-slots (stream)
  22. (when stream-filename
  23. #+CCL (setf vect (ccl:map-file-to-octet-vector stream-filename))
  24. #-CCL (error "Not Yet!")
  25. )
  26. (setf stream-size (length vect))))
  27. (defmethod stream-close ((stream mem-stream))
  28. "Close a stream, making the underlying object (file or vector) inaccessible."
  29. (declare #.utils:*standard-optimize-settings*)
  30. (with-mem-stream-slots (stream)
  31. (when stream-filename
  32. #+CCL (ccl:unmap-octet-vector vect)
  33. #-CCL (error "Not Yet")
  34. )
  35. (setf vect nil)))
  36. ;;; finding out current file position is so common, we also
  37. ;;; provide a macro
  38. (defmacro stream-here (stream) `(index ,stream))
  39. (defmethod stream-seek ((stream mem-stream) &optional (offset 0) (from :current))
  40. "Set INDEX to requested value. No error checking done here, but subsequent reads will fail if INDEX is out-of-bounds.
  41. As a convenience, OFFSET and FROM are optional, so (STREAM-SEEK stream) returns the current read-offset in stream."
  42. (declare #.utils:*standard-optimize-settings*)
  43. (declare (fixnum offset))
  44. (with-mem-stream-slots (stream)
  45. (ecase from
  46. (:start ; INDEX set to OFFSET from start of stream
  47. (setf index offset))
  48. (:current ; INDEX set relative to current INDEX, by OFFSET bytes
  49. (if (zerop offset)
  50. index
  51. (incf index offset)))
  52. (:end ; INDEX set to OFFSET from end of stream
  53. (setf index (- stream-size offset))))))
  54. (declaim (inline read-n-bytes))
  55. (defun read-n-bytes (stream n-bytes &key (bits-per-byte 8) (endian :little-endian))
  56. "Returns a FIXNUM constructed by reading N-BYTES. BITS-PER-BYTE contols how many bits should be used from each read byte."
  57. (declare #.utils:*standard-optimize-settings*)
  58. (declare (fixnum n-bytes))
  59. (with-mem-stream-slots (stream)
  60. (when (<= (+ index n-bytes) stream-size)
  61. (ecase endian
  62. (:little-endian
  63. (loop with value = 0
  64. for low-bit downfrom (* bits-per-byte (1- n-bytes)) to 0 by bits-per-byte do
  65. (setf (ldb (byte bits-per-byte low-bit) value) (aref vect index))
  66. (incf index)
  67. finally (return-from read-n-bytes value)))
  68. (:big-endian
  69. (loop with value = 0
  70. for low-bit upfrom 0 to (* bits-per-byte (1- n-bytes)) by bits-per-byte do
  71. (setf (ldb (byte bits-per-byte low-bit) value) (aref vect index))
  72. (incf index)
  73. finally (return-from read-n-bytes value))))))
  74. nil)
  75. (defun stream-read-u8 (stream)
  76. (declare #.utils:*standard-optimize-settings*)
  77. (with-mem-stream-slots (stream)
  78. (if (<= (+ index 1) stream-size)
  79. (let ((val (aref vect index)))
  80. (incf index)
  81. val)
  82. nil)))
  83. (defun stream-read-u16 (stream &key (bits-per-byte 8) (endian :little-endian)) (read-n-bytes stream 2 :bits-per-byte bits-per-byte :endian endian))
  84. (defun stream-read-u24 (stream &key (bits-per-byte 8) (endian :little-endian)) (read-n-bytes stream 3 :bits-per-byte bits-per-byte :endian endian))
  85. (defun stream-read-u32 (stream &key (bits-per-byte 8) (endian :little-endian)) (read-n-bytes stream 4 :bits-per-byte bits-per-byte :endian endian))
  86. (defun stream-read-u64 (stream &key (bits-per-byte 8) (endian :little-endian)) (read-n-bytes stream 8 :bits-per-byte bits-per-byte :endian endian))
  87. (defun stream-read-u128 (stream &key (bits-per-byte 8) (endian :little-endian)) (read-n-bytes stream 16 :bits-per-byte bits-per-byte :endian endian))
  88. (defmethod stream-read-sequence ((stream mem-stream) size &key (bits-per-byte 8))
  89. "Read in a sequence of octets at BITS-PER-BYTE. If BITS-PER-BYTE == 8, then simply return
  90. a displaced array from STREAMs underlying vector. If it is == 7, then we have to create a new vector and read into that."
  91. (declare #.utils:*standard-optimize-settings*)
  92. (with-mem-stream-slots (stream)
  93. (when (> (+ index size) stream-size)
  94. (setf size (- stream-size index)))
  95. (ecase bits-per-byte
  96. (8 (let ((octets (make-array size :element-type 'octet :displaced-to vect :displaced-index-offset index :adjustable nil)))
  97. (incf index size)
  98. (values octets size)))
  99. (7
  100. (let* ((last-byte-was-FF nil)
  101. (byte nil)
  102. (octets
  103. #-CCL (error "Not yet")
  104. #+CCL (ccl:with-output-to-vector (out)
  105. (dotimes (i size)
  106. (setf byte (stream-read-u8 stream))
  107. (if last-byte-was-FF
  108. (if (not (zerop byte))
  109. (write-byte byte out))
  110. (write-byte byte out))
  111. (setf last-byte-was-FF (= byte #xFF))))
  112. ))
  113. (values octets size))))))
  114. (defclass mp3-file-stream (mem-stream)
  115. ((id3-header :accessor id3-header :initform nil :documentation "holds all the ID3 info")
  116. (audio-info :accessor audio-info :initform nil :documentation "holds the bit-rate, etc info"))
  117. (:documentation "Stream for parsing MP3 files"))
  118. (defclass mp4-file-stream (mem-stream)
  119. ((mp4-atoms :accessor mp4-atoms :initform nil :documentation "holds tree of parsed MP4 atoms/boxes")
  120. (audio-info :accessor audio-info :initform nil :documentation "holds the bit-rate, etc info"))
  121. (:documentation "Stream for parsing MP4 audio files"))
  122. (defclass flac-file-stream (mem-stream)
  123. ((flac-headers :accessor flac-headers :initform nil :documentation "holds all the flac headers in file")
  124. (audio-info :accessor audio-info :initform nil :documentation "parsed audio info")
  125. (flac-tags :accessor flac-tags :initform nil :documentation "parsed comment tags."))
  126. (:documentation "Stream for parsing flac files"))
  127. (defun make-file-stream (filename)
  128. "Convenience function for creating a file stream. Detects file type and returns proper type stream."
  129. (declare #.utils:*standard-optimize-settings*)
  130. (let* ((new-stream (make-mmap-stream filename))
  131. (ret-stream))
  132. ;; detect file type and make RET-STREAM. if we don't recognize stream, RET-STREAM will be NULL
  133. (cond ((mp4-atom:is-valid-m4-file new-stream)
  134. (setf ret-stream (make-instance 'mp4-file-stream :vect (vect new-stream) :stream-filename (stream-filename new-stream))))
  135. ((flac-frame:is-valid-flac-file new-stream)
  136. (setf ret-stream (make-instance 'flac-file-stream :vect (vect new-stream) :stream-filename (stream-filename new-stream))))
  137. ((id3-frame:is-valid-mp3-file new-stream)
  138. (setf ret-stream (make-instance 'mp3-file-stream :vect (vect new-stream) :stream-filename (stream-filename new-stream)))))
  139. (stream-close new-stream)
  140. ret-stream))
  141. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Strings ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  142. ;;; Decode octets as an iso-8859-1 string (encoding == 0)
  143. (defun stream-decode-iso-string (octets &key (start 0) (end nil))
  144. (declare #.utils:*standard-optimize-settings*)
  145. #+CCL (ccl:decode-string-from-octets octets :start start :end end :external-format :iso-8859-1)
  146. #-CCL (error "Not Yet")
  147. )
  148. ;;;
  149. ;;; XXX: Coded this way because I can't seem to get a simple :external-format :ucs-2 to work correctly
  150. ;;; AND some taggers encode a UCS-2 empty string w/o a byte-order mark (i.e. null strings are
  151. ;;; sometimes encoded as #(00 00))
  152. (defun stream-decode-ucs-string (octets &key (start 0) (end nil))
  153. "Decode octets as a UCS string with a BOM (encoding == 1)"
  154. (declare #.utils:*standard-optimize-settings*)
  155. (labels ((get-byte-order-mark (octets)
  156. (let ((retval 0))
  157. (setf (ldb (byte 8 0) retval) (aref octets 1)
  158. (ldb (byte 8 8) retval) (aref octets 0))
  159. (when (not (or (= #xfffe retval) (= #xfeff retval)))
  160. (error "Got invalid byte-order mark of ~x in STREAM-DECODE-UCS-STRING" retval))
  161. retval)))
  162. ;; special case: empty (and mis-coded) string
  163. (cond ((zerop (length octets))
  164. (make-string 0))
  165. (t
  166. ;;
  167. ;; else, we have a (hopefully) properly encoded string
  168. (let ((bom (get-byte-order-mark octets)))
  169. (ecase (the fixnum bom)
  170. (#xfffe #+CCL (ccl:decode-string-from-octets octets :start (+ 2 start) :end end :external-format :ucs-2le)
  171. #-CCL (error "Not Yet")
  172. )
  173. (#xfeff #+CCL (ccl:decode-string-from-octets octets :start (+ 2 start) :end end :external-format :ucs-2be)
  174. #-CCL (error "Not Yet")
  175. )
  176. (0 (make-string 0))))))))
  177. (defun stream-decode-ucs-be-string (octets &key (start 0) (end nil))
  178. "Decode octets as a UCS-BE string (encoding == 2)"
  179. (declare #.utils:*standard-optimize-settings*)
  180. #+CCL (ccl:decode-string-from-octets octets :start start :end end :external-format :ucs-2be)
  181. #-CCL (error "Not Yet")
  182. )
  183. (defun stream-decode-utf-8-string (octets &key (start 0) (end nil))
  184. "Decode octets as a utf-8 string"
  185. (declare #.utils:*standard-optimize-settings*)
  186. #+CCL (ccl:decode-string-from-octets octets :start start :end end :external-format :utf-8)
  187. #-CCL (error "Not Yet")
  188. )
  189. (defun stream-decode-string (octets &key (start 0) (end nil) (encoding 0))
  190. "Decode octets depending on encoding"
  191. (declare #.utils:*standard-optimize-settings*)
  192. (ecase encoding
  193. (0 (stream-decode-iso-string octets :start start :end end))
  194. (1 (stream-decode-ucs-string octets :start start :end end))
  195. (2 (stream-decode-ucs-be-string octets :start start :end end))
  196. (3 (stream-decode-utf-8-string octets :start start :end end))))
  197. (defmethod stream-read-iso-string-with-len ((instream mem-stream) len)
  198. "Read an iso-8859-1 string of length 'len' (encoding = 0)"
  199. (declare #.utils:*standard-optimize-settings*)
  200. (stream-decode-iso-string (stream-read-sequence instream len)))
  201. (defmethod stream-read-ucs-string-with-len ((instream mem-stream) len)
  202. "Read an ucs-2 string of length 'len' (encoding = 1)"
  203. (declare #.utils:*standard-optimize-settings*)
  204. (stream-decode-ucs-string (stream-read-sequence instream len)))
  205. (defmethod stream-read-ucs-be-string-with-len ((instream mem-stream) len)
  206. "Read an ucs-2-be string of length 'len' (encoding = 2)"
  207. (declare #.utils:*standard-optimize-settings*)
  208. (stream-decode-ucs-be-string (stream-read-sequence instream len)))
  209. (defmethod stream-read-utf-8-string-with-len ((instream mem-stream) len)
  210. "Read an utf-8 string of length 'len' (encoding = 3)"
  211. (declare #.utils:*standard-optimize-settings*)
  212. (stream-decode-utf-8-string (stream-read-sequence instream len)))
  213. (defmethod stream-read-string-with-len ((instream mem-stream) len &key (encoding 0))
  214. "Read in a string of a given encoding of length 'len'"
  215. (declare #.utils:*standard-optimize-settings*)
  216. (ecase encoding
  217. (0 (stream-read-iso-string-with-len instream len))
  218. (1 (stream-read-ucs-string-with-len instream len))
  219. (2 (stream-read-ucs-be-string-with-len instream len))
  220. (3 (stream-read-utf-8-string-with-len instream len))))
  221. (defmethod stream-read-iso-string ((instream mem-stream))
  222. "Read in a null terminated iso-8859-1 string"
  223. (declare #.utils:*standard-optimize-settings*)
  224. (let ((octets #+CCL (ccl:with-output-to-vector (out)
  225. (do ((b (stream-read-u8 instream) (stream-read-u8 instream)))
  226. (nil)
  227. (when (zerop b)
  228. (return)) ; leave loop w/o writing
  229. (write-byte b out)))
  230. #-CCL (error "Not Yet")
  231. ))
  232. (stream-decode-iso-string octets)))
  233. (defmethod stream-read-ucs-string ((instream mem-stream))
  234. "Read in a null terminated UCS string."
  235. (declare #.utils:*standard-optimize-settings*)
  236. (let ((octets #+CCL (ccl:with-output-to-vector (out)
  237. (do* ((b0 (stream-read-u8 instream)
  238. (stream-read-u8 instream))
  239. (b1 (stream-read-u8 instream)
  240. (stream-read-u8 instream)))
  241. (nil)
  242. (when (and (zerop b0) (zerop b1))
  243. (return))
  244. (write-byte b0 out)
  245. (write-byte b1 out)))
  246. #-CCL (error "Not Yet")
  247. ))
  248. (stream-decode-ucs-string octets)))
  249. (defmethod stream-read-ucs-be-string ((instream mem-stream))
  250. "Read in a null terminated UCS-BE string."
  251. (declare #.utils:*standard-optimize-settings*)
  252. (let ((octets #+CCL (ccl:with-output-to-vector (out)
  253. (do* ((b0 (stream-read-u8 instream)
  254. (stream-read-u8 instream))
  255. (b1 (stream-read-u8 instream)
  256. (stream-read-u8 instream)))
  257. (nil)
  258. (when (and (zerop b0) (zerop b1))
  259. (return))
  260. (write-byte b0 out)
  261. (write-byte b1 out)))
  262. #-CCL (error "Not Yet")
  263. ))
  264. (stream-decode-ucs-be-string octets)))
  265. (defmethod stream-read-utf-8-string ((instream mem-stream))
  266. "Read in a null terminated utf-8 string (encoding == 3)"
  267. (declare #.utils:*standard-optimize-settings*)
  268. (let ((octets #+CCL (ccl:with-output-to-vector (out)
  269. (do ((b (stream-read-u8 instream)
  270. (stream-read-u8 instream)))
  271. (nil)
  272. (when (zerop b)
  273. (return))
  274. (write-byte b out)))
  275. #-CCL (error "Not Yet")
  276. ))
  277. (stream-decode-utf-8-string octets)))
  278. (defmethod stream-read-string ((instream mem-stream) &key (encoding 0))
  279. "Read in a null terminated string of a given encoding."
  280. (declare #.utils:*standard-optimize-settings*)
  281. (ecase encoding
  282. (0 (stream-read-iso-string instream))
  283. (1 (stream-read-ucs-string instream))
  284. (2 (stream-read-ucs-be-string instream))
  285. (3 (stream-read-utf-8-string instream))))
  286. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Files ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  287. (defvar *get-audio-info* t "controls whether the parsing functions also parse audio info like bit-rate, etc")
  288. (defmethod parse-audio-file ((stream mp4-file-stream) &key (get-audio-info *get-audio-info*) &allow-other-keys)
  289. "Parse an MP4A file by reading its ATOMS and decoding them."
  290. (declare #.utils:*standard-optimize-settings*)
  291. (handler-case
  292. (progn
  293. (mp4-atom:find-mp4-atoms stream)
  294. (when get-audio-info
  295. (setf (audio-info stream) (mp4-atom:get-mp4-audio-info stream))))
  296. (condition (c)
  297. (utils:warn-user "make-mp4-stream got condition: ~a" c))))
  298. (defmethod parse-audio-file ((stream flac-file-stream) &key (get-audio-info *get-audio-info*) &allow-other-keys)
  299. "Parse a flac file by reading its headers and decoding them."
  300. (declare #.utils:*standard-optimize-settings*)
  301. (declare (ignore get-audio-info)) ; audio info comes for "free" by parsing headers
  302. (handler-case
  303. (flac-frame:find-flac-frames stream)
  304. (condition (c)
  305. (utils:warn-user "make-flac-stream got condition: ~a" c))))
  306. (defmethod parse-audio-file ((stream mp3-file-stream) &key (get-audio-info *get-audio-info*) &allow-other-keys)
  307. "Parse an MP3 file by reading its FRAMES and decoding them."
  308. (declare #.utils:*standard-optimize-settings*)
  309. (handler-case
  310. (progn
  311. (id3-frame:find-id3-frames stream)
  312. (when get-audio-info
  313. (setf (audio-info stream) (mpeg:get-mpeg-audio-info stream))))
  314. (condition (c)
  315. (utils:warn-user "make-mp3-stream got condition: ~a" c))))