streams.lisp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. ;;; -*- Mode: Lisp; show-trailing-whitespace: t; Base: 10; indent-tabs: nil; Syntax: ANSI-Common-Lisp; Package: STREAMS; -*-
  2. ;;; Copyright (c) 2013, Mark VandenBrink. All rights reserved.
  3. (in-package #:audio-streams)
  4. (log5:defcategory cat-log-stream)
  5. (defmacro log-stream (&rest log-stuff) `(log5:log-for (cat-log-stream) ,@log-stuff))
  6. (deftype octet () '(unsigned-byte 8))
  7. (defmacro make-octets (len) `(make-array ,len :element-type 'octet))
  8. (defclass base-stream ()
  9. ((filename :accessor filename :initarg :filename)
  10. (instream :accessor instream :initform nil)
  11. (endian :accessor endian :initarg :endian :initform nil) ; controls endian-ness of read/writes
  12. (modified :accessor modified :initform nil) ; for when we implement writing tags
  13. (file-size :accessor file-size))
  14. (:documentation "Base class for all audio file types"))
  15. (defmethod initialize-instance :after ((me base-stream) &key read-only &allow-other-keys)
  16. (log5:with-context "base-stream-initializer"
  17. (with-slots (instream filename file-size endian) me
  18. (setf instream (if read-only
  19. (open filename :direction :input :element-type 'octet)
  20. (open filename :direction :io :if-exists :overwrite :element-type 'octet)))
  21. (setf file-size (file-length instream))
  22. (log-stream "stream = ~a, name = ~a, size = ~:d~%endian = ~a"
  23. instream filename file-size endian))))
  24. (defmethod stream-close ((me base-stream))
  25. "Close an open stream."
  26. (with-slots (instream modified) me
  27. (when modified
  28. (warn "at some point, should I add code to auto-write modified audio-files?")
  29. (setf modified nil))
  30. (when instream
  31. (close instream)
  32. (setf instream nil))))
  33. (defmethod stream-seek ((me base-stream) offset from)
  34. "C-library like seek function. from can be one of :current, :start, :end.
  35. Returns the current offset into the stream"
  36. (assert (member from '(:current :start :end)) () "seek takes one of :current, :start, :end")
  37. (with-slots (instream file-size) me
  38. (ecase from
  39. (:start (file-position instream offset))
  40. (:current
  41. (let ((current (file-position instream)))
  42. (file-position instream (+ current offset))))
  43. (:end
  44. (file-position instream (- file-size offset))))))
  45. ;;
  46. ;; From Practical Common Lisp by Peter Seibel.
  47. (defun read-octets (instream bytes &key (bits-per-byte 8) (endian :little-endian))
  48. (ecase endian
  49. (:big-endian
  50. (loop with value = 0
  51. for low-bit from 0 to (* bits-per-byte (1- bytes)) by bits-per-byte do
  52. (setf (ldb (byte bits-per-byte low-bit) value) (read-byte instream))
  53. finally (return value)))
  54. (:little-endian
  55. (loop with value = 0
  56. for low-bit downfrom (* bits-per-byte (1- bytes)) to 0 by bits-per-byte do
  57. (setf (ldb (byte bits-per-byte low-bit) value) (read-byte instream))
  58. finally (return value)))))
  59. (defmethod stream-read-u8 ((me base-stream))
  60. "read 1 byte from file"
  61. (with-slots (endian instream) me
  62. (read-octets instream 1 :endian endian)))
  63. (defmethod stream-read-u16 ((me base-stream))
  64. "read 2 bytes from file"
  65. (with-slots (endian instream) me
  66. (read-octets instream 2 :endian endian)))
  67. (defmethod stream-read-u24 ((me base-stream))
  68. "read 3 bytes from file"
  69. (with-slots (endian instream) me
  70. (read-octets instream 3 :endian endian)))
  71. (defmethod stream-read-u32 ((me base-stream))
  72. "read 4 bytes from file"
  73. (with-slots (endian instream) me
  74. (read-octets instream 4 :endian endian)))
  75. (defmethod stream-read-string ((me base-stream) &key size (terminators nil))
  76. "Read normal string from file. If size is provided, read exactly that many octets.
  77. If terminators is supplied, it is a list of characters that can terminate a string (and hence stop read)"
  78. (with-output-to-string (s)
  79. (with-slots (instream) me
  80. (let ((terminated nil)
  81. (count 0)
  82. (byte))
  83. (loop
  84. (when (if size (= count size) terminated) (return))
  85. (setf byte (read-byte instream))
  86. (incf count)
  87. (log-stream "count = ~d, terminators = ~a, byte-read was ~c" count terminators (code-char byte))
  88. (when (member byte terminators :test #'=)
  89. (setf terminated t))
  90. (when (not terminated)
  91. (write-char (code-char byte) s)))))))
  92. (defmethod stream-read-octets ((me base-stream) size)
  93. "Read SIZE octets from input-file"
  94. (let* ((octets (make-octets size))
  95. (read-len (read-sequence octets (slot-value me 'instream))))
  96. (assert (= read-len size))
  97. octets))
  98. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; MP4 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  99. (log5:defcategory cat-log-mp4-stream)
  100. (defmacro log-mp4-stream (&rest log-stuff) `(log5:log-for (cat-log-mp4-stream) ,@log-stuff))
  101. (defclass mp4-stream (base-stream)
  102. ((mp4-atoms :accessor mp4-atoms :initform nil))
  103. (:documentation "Class to access m4a/mp4 files"))
  104. (defun make-mp4-stream (filename read-only &key)
  105. "Convenience function to create an instance of MP4-FILE with appropriate init args"
  106. (log5:with-context "make-mp4-stream"
  107. (log-mp4-stream "opening ~a" filename)
  108. (let (handle)
  109. (handler-case
  110. (progn
  111. (setf handle (make-instance 'mp4-stream :filename filename :endian :big-endian :read-only read-only))
  112. (with-slots (mp4-atoms) handle
  113. (log-mp4-stream "getting atoms")
  114. (setf mp4-atoms (mp4-atom:find-mp4-atoms handle))))
  115. (condition (c)
  116. (warn "make-mp4-stream got condition: ~a" c)
  117. (when handle (stream-close handle))
  118. (setf handle nil)))
  119. handle)))
  120. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; MP3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  121. (log5:defcategory cat-log-mp3-stream)
  122. (defmacro log-mp3-stream (&rest log-stuff) `(log5:log-for (cat-log-mp3-stream) ,@log-stuff))
  123. (defclass mp3-stream (base-stream)
  124. ((mp3-header :accessor mp3-header :initform nil))
  125. (:documentation "Class to access mp3 files"))
  126. (defun make-mp3-stream (filename read-only &key)
  127. "Convenience function to create an instance of MP3-FILE with appropriate init args.
  128. NB: we assume non-syncsafe as default"
  129. (log5:with-context "make-mp3-stream"
  130. (log-mp3-stream "opening ~a" filename)
  131. (let (handle)
  132. (handler-case
  133. (progn
  134. (setf handle (make-instance 'mp3-stream :filename filename :endian :big-endian :read-only read-only))
  135. (with-slots (mp3-header) handle
  136. (log-mp3-stream "getting frames")
  137. (setf mp3-header (mp3-frame:find-mp3-frames handle))))
  138. (condition (c)
  139. (warn "make-mp3-stream got condition: ~a" c)
  140. (when handle (stream-close handle))
  141. (setf handle nil)))
  142. handle)))
  143. (defmethod stream-read-sync-safe-u32 ((me mp3-stream))
  144. "Read a sync-safe integer from file. Used by mp3 files"
  145. (let* ((ret 0))
  146. (setf (ldb (byte 7 21) ret) (stream-read-u8 me))
  147. (setf (ldb (byte 7 14) ret) (stream-read-u8 me))
  148. (setf (ldb (byte 7 7) ret) (stream-read-u8 me))
  149. (setf (ldb (byte 7 0) ret) (stream-read-u8 me))
  150. ret))
  151. (defmethod stream-read-sync-safe-octets ((me mp3-stream) len)
  152. "Used to undo sync-safe read of file"
  153. (let* ((last-byte-was-FF nil)
  154. (byte nil)
  155. (de-synced-data (flexi-streams:with-output-to-sequence (out :element-type 'octet)
  156. (dotimes (i len)
  157. (setf byte (stream-read-u8 me))
  158. (if last-byte-was-FF
  159. (if (not (zerop byte))
  160. (write-byte byte out))
  161. (write-byte byte out))
  162. (setf last-byte-was-FF (= byte #xFF))))))
  163. de-synced-data))