audio-streams.lisp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. ;;;; Generic stream support
  5. (deftype octet () '(unsigned-byte 8))
  6. (deftype octets () '(simple-array octet (*)))
  7. (defmacro make-octets (len) `(make-array ,len :element-type 'octet))
  8. (defun make-audio-stream (arg)
  9. "Creates a stream for ARG"
  10. (declare #.utils:*standard-optimize-settings*)
  11. (labels ((make-file-stream (name)
  12. (let ((fd (open name :direction :input :element-type 'octet)))
  13. (if fd
  14. (flex:make-flexi-stream fd :element-type 'octet)
  15. nil))))
  16. (etypecase arg
  17. (string (make-file-stream arg))
  18. (pathname (make-file-stream arg))
  19. (vector (flex:make-in-memory-input-stream arg)))))
  20. (defgeneric stream-size (stream))
  21. (defmethod stream-size ((stream flex:flexi-input-stream))
  22. (declare #.utils:*standard-optimize-settings*)
  23. (file-length (flex:flexi-stream-stream stream)))
  24. (defmethod stream-size ((stream flex:in-memory-stream))
  25. (declare #.utils:*standard-optimize-settings*)
  26. (flex::vector-stream-end stream))
  27. (defgeneric stream-filename (stream))
  28. (defmethod stream-filename ((stream flex:flexi-stream))
  29. (declare #.utils:*standard-optimize-settings*)
  30. (pathname (flex:flexi-stream-stream stream)))
  31. (defun stream-seek (stream
  32. &optional (offset 0) (from :current))
  33. "Move the FILE-POSITION of a stream"
  34. (declare #.utils:*standard-optimize-settings*)
  35. (declare (fixnum offset))
  36. (ecase from
  37. (:start (file-position stream offset))
  38. (:current (file-position stream (+ (file-position stream) offset)))
  39. (:end (file-position stream (- (stream-size stream)
  40. offset)))))
  41. (declaim (inline read-n-bytes))
  42. ;;;; Support for the uxx readers
  43. (defun read-n-bytes (stream n-bytes
  44. &key (bits-per-byte 8) (endian :little-endian))
  45. "Returns a FIXNUM constructed by reading N-BYTES. BITS-PER-BYTE controls how
  46. many bits should be used from each read byte."
  47. (declare #.utils:*standard-optimize-settings*)
  48. (declare (fixnum n-bytes))
  49. (ecase endian
  50. (:little-endian
  51. (loop with value = 0
  52. for low-bit downfrom (* bits-per-byte (1- n-bytes)) to 0
  53. by bits-per-byte do
  54. (awhen (read-byte stream nil nil)
  55. (setf (ldb (byte bits-per-byte low-bit) value) it))
  56. finally (return-from read-n-bytes value)))
  57. (:big-endian
  58. (loop with value = 0
  59. for low-bit upfrom 0 to (* bits-per-byte (1- n-bytes))
  60. by bits-per-byte do
  61. (awhen (read-byte stream nil nil)
  62. (setf (ldb (byte bits-per-byte low-bit) value) it))
  63. finally (return-from read-n-bytes value)))))
  64. ;;;; Number readers
  65. (defun stream-read-u8 (stream)
  66. (declare #.utils:*standard-optimize-settings*)
  67. (read-byte stream nil nil))
  68. (defun stream-read-u16 (stream &key (bits-per-byte 8) (endian :little-endian))
  69. (read-n-bytes stream 2 :bits-per-byte bits-per-byte :endian endian))
  70. (defun stream-read-u24 (stream &key (bits-per-byte 8) (endian :little-endian))
  71. (read-n-bytes stream 3 :bits-per-byte bits-per-byte :endian endian))
  72. (defun stream-read-u32 (stream &key (bits-per-byte 8) (endian :little-endian))
  73. (read-n-bytes stream 4 :bits-per-byte bits-per-byte :endian endian))
  74. (defun stream-read-u64 (stream &key (bits-per-byte 8) (endian :little-endian))
  75. (read-n-bytes stream 8 :bits-per-byte bits-per-byte :endian endian))
  76. (defun stream-read-u128 (stream &key (bits-per-byte 8) (endian :little-endian))
  77. (read-n-bytes stream 16 :bits-per-byte bits-per-byte :endian endian))
  78. ;;;; Sequences
  79. (defun stream-read-sequence (stream size &key (bits-per-byte 8))
  80. "Read in a sequence of octets at BITS-PER-BYTE"
  81. (declare #.utils:*standard-optimize-settings*)
  82. (ecase bits-per-byte
  83. (8 (let ((octets (make-octets size)))
  84. (values octets (read-sequence octets stream))))
  85. (7 (let* ((last-byte-was-FF nil)
  86. (byte nil)
  87. (octets (flex:with-output-to-sequence (out :element-type 'octet)
  88. (dotimes (i size)
  89. (setf byte (stream-read-u8 stream))
  90. (if last-byte-was-FF
  91. (if (not (zerop byte))
  92. (write-byte byte out))
  93. (write-byte byte out))
  94. (setf last-byte-was-FF (= byte #xFF))))))
  95. (values octets size)))))
  96. ;;;; Strings: decoders
  97. ;;; Decode octets as an iso-8859-1 string (encoding == 0)
  98. (defun stream-decode-iso-string (octets &key (start 0) (end (length octets)))
  99. (declare #.utils:*standard-optimize-settings*)
  100. (flex:octets-to-string octets :start start
  101. :end end :external-format :iso-8859-1))
  102. ;;;
  103. ;;; XXX: Coded this way because I can't seem to get a simple :external-format
  104. ;;; :ucs-2 to work correctly AND some taggers encode a UCS-2 empty string w/o
  105. ;;; a byte-order mark (i.e. null strings are sometimes encoded as #(00 00))
  106. (defun stream-decode-ucs-string (octets &key (start 0) (end (length octets)))
  107. "Decode octets as a UCS string with a BOM (encoding == 1)"
  108. (declare #.utils:*standard-optimize-settings*)
  109. (labels ((get-byte-order-mark (octets)
  110. (let ((retval 0))
  111. (setf (ldb (byte 8 0) retval) (aref octets 1)
  112. (ldb (byte 8 8) retval) (aref octets 0))
  113. (when (not (or (= #xfffe retval) (= #xfeff retval)))
  114. (error
  115. "Got invalid byte-order mark of ~x in STREAM-DECODE-UCS-STRING"
  116. retval))
  117. retval)))
  118. ;; special case: empty (and mis-coded) string
  119. (cond ((zerop (length octets))
  120. (make-string 0))
  121. (t
  122. ;;
  123. ;; else, we have a (hopefully) properly encoded string
  124. (when (oddp end)
  125. (warn-user
  126. "Malformed UCS string, length (~d) is odd---decrementing by 1"
  127. end)
  128. (setf end (1- end)))
  129. (let ((bom (get-byte-order-mark octets)))
  130. (ecase (the fixnum bom)
  131. (#xfffe (flex:octets-to-string octets
  132. :start (+ 2 start)
  133. :end end
  134. :external-format :ucs-2le))
  135. (#xfeff (flex:octets-to-string octets
  136. :start (+ 2 start)
  137. :end end
  138. :external-format :ucs-2be))
  139. (0 (make-string 0))))))))
  140. (defun stream-decode-ucs-be-string (octets &key (start 0) (end (length octets)))
  141. "Decode octets as a UCS-BE string (encoding == 2)"
  142. (declare #.utils:*standard-optimize-settings*)
  143. (flex:octets-to-string octets :start start
  144. :end end :external-format :ucs-2be))
  145. (defun stream-decode-utf-8-string (octets &key (start 0) (end (length octets)))
  146. "Decode octets as a utf-8 string"
  147. (declare #.utils:*standard-optimize-settings*)
  148. (flex:octets-to-string octets :start start :end end :external-format :utf-8))
  149. (defun stream-decode-string (octets &key (start 0)
  150. (end (length octets))
  151. (encoding 0))
  152. "Decode octets depending on encoding"
  153. (declare #.utils:*standard-optimize-settings*)
  154. (ecase encoding
  155. (0 (stream-decode-iso-string octets :start start :end end))
  156. (1 (stream-decode-ucs-string octets :start start :end end))
  157. (2 (stream-decode-ucs-be-string octets :start start :end end))
  158. (3 (stream-decode-utf-8-string octets :start start :end end))))
  159. ;;;; Strings: readers
  160. (defun stream-read-iso-string-with-len (instream len)
  161. "Read an iso-8859-1 string of length 'len' (encoding = 0)"
  162. (declare #.utils:*standard-optimize-settings*)
  163. (stream-decode-iso-string (stream-read-sequence instream len)))
  164. (defun stream-read-ucs-string-with-len (instream len)
  165. "Read an ucs-2 string of length 'len' (encoding = 1)"
  166. (declare #.utils:*standard-optimize-settings*)
  167. (stream-decode-ucs-string (stream-read-sequence instream len)))
  168. (defun stream-read-ucs-be-string-with-len (instream len)
  169. "Read an ucs-2-be string of length 'len' (encoding = 2)"
  170. (declare #.utils:*standard-optimize-settings*)
  171. (stream-decode-ucs-be-string (stream-read-sequence instream len)))
  172. (defun stream-read-utf-8-string-with-len (instream len)
  173. "Read an utf-8 string of length 'len' (encoding = 3)"
  174. (declare #.utils:*standard-optimize-settings*)
  175. (stream-decode-utf-8-string (stream-read-sequence instream len)))
  176. (defun stream-read-string-with-len (instream len &key (encoding 0))
  177. "Read in a string of a given encoding of length 'len'"
  178. (declare #.utils:*standard-optimize-settings*)
  179. (ecase encoding
  180. (0 (stream-read-iso-string-with-len instream len))
  181. (1 (stream-read-ucs-string-with-len instream len))
  182. (2 (stream-read-ucs-be-string-with-len instream len))
  183. (3 (stream-read-utf-8-string-with-len instream len))))
  184. (defun stream-read-iso-string (instream)
  185. "Read in a null-terminated iso-8859-1 string"
  186. (declare #.utils:*standard-optimize-settings*)
  187. (let ((octets (flex:with-output-to-sequence (out)
  188. (do ((b (stream-read-u8 instream) (stream-read-u8 instream)))
  189. (nil)
  190. (when (zerop b)
  191. (return)) ; leave loop w/o writing
  192. (write-byte b out)))))
  193. (stream-decode-iso-string octets)))
  194. (defun stream-read-ucs-string (instream)
  195. "Read in a null-terminated UCS string."
  196. (declare #.utils:*standard-optimize-settings*)
  197. (let ((octets (flex:with-output-to-sequence (out)
  198. (do* ((b0 (stream-read-u8 instream)
  199. (stream-read-u8 instream))
  200. (b1 (stream-read-u8 instream)
  201. (stream-read-u8 instream)))
  202. (nil)
  203. (when (and (zerop b0) (zerop b1))
  204. (return))
  205. (write-byte b0 out)
  206. (write-byte b1 out)))))
  207. (stream-decode-ucs-string octets)))
  208. (defun stream-read-ucs-be-string (instream)
  209. "Read in a null-terminated UCS-BE string."
  210. (declare #.utils:*standard-optimize-settings*)
  211. (let ((octets (flex:with-output-to-sequence (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. (defun stream-read-utf-8-string (instream)
  223. "Read in a null-terminated utf-8 string (encoding == 3)"
  224. (declare #.utils:*standard-optimize-settings*)
  225. (let ((octets (flex:with-output-to-sequence (out)
  226. (do ((b (stream-read-u8 instream)
  227. (stream-read-u8 instream)))
  228. (nil)
  229. (when (zerop b)
  230. (return))
  231. (write-byte b out)))))
  232. (stream-decode-utf-8-string octets)))
  233. (defun stream-read-string (instream &key (encoding 0))
  234. "Read in a null-terminated string of a given encoding."
  235. (declare #.utils:*standard-optimize-settings*)
  236. (ecase encoding
  237. (0 (stream-read-iso-string instream))
  238. (1 (stream-read-ucs-string instream))
  239. (2 (stream-read-ucs-be-string instream))
  240. (3 (stream-read-utf-8-string instream))))
  241. ;;;; Files
  242. (defvar *get-audio-info* t
  243. "controls whether the parsing functions parse audio info like bit-rate, etc")
  244. (defun open-audio-file (filename &optional (get-audio-info *get-audio-info*))
  245. "Open and parse FILENAME for tag and optionally audio info"
  246. (declare #.utils:*standard-optimize-settings*)
  247. (let ((stream)
  248. (info))
  249. (unwind-protect
  250. (progn
  251. (setf stream (make-audio-stream filename))
  252. (when stream
  253. (setf info
  254. (cond ((id3-frame:is-valid-mp3-file stream)
  255. (id3-frame:parse-audio-file stream get-audio-info))
  256. ((mp4-atom:is-valid-m4-file stream)
  257. (mp4-atom:parse-audio-file stream get-audio-info))
  258. ((flac-frame:is-valid-flac-file stream)
  259. (flac-frame:parse-audio-file stream get-audio-info))
  260. (t nil)))))
  261. (when stream
  262. (close stream)))
  263. info))