flac-frame.lisp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. ;;; -*- Mode: Lisp; show-trailing-whitespace: t; Base: 10; indent-tabs: nil; Syntax: ANSI-Common-Lisp; Package: FLAC-FRAME; -*-
  2. ;;; Copyright (c) 2013, Mark VandenBrink. All rights reserved.
  3. (in-package #:flac-frame)
  4. (log5:defcategory cat-log-flac-frame)
  5. (defmacro log-flac-frame (&rest log-stuff) `(log5:log-for (cat-log-flac-frame) ,@log-stuff))
  6. ;;; FLAC header types
  7. (defconstant +metadata-streaminfo+ 0)
  8. (defconstant +metadata-padding+ 1)
  9. (defconstant +metadata-application+ 2)
  10. (defconstant +metadata-seektable+ 3)
  11. (defconstant +metadata-comment+ 4)
  12. (defconstant +metadata-cuesheet+ 5)
  13. (defconstant +metadata-picture+ 6)
  14. (defclass flac-header ()
  15. ((pos :accessor pos :initarg :pos :documentation "file location of this flac header")
  16. (last-bit :accessor last-bit :initarg :last-bit :documentation "if set, this is the last flac header in file")
  17. (header-type :accessor header-type :initarg :header-type :documentation "one of the flac header types above")
  18. (header-len :accessor header-len :initarg :header-len :documentation "how long the info associated w/header is"))
  19. (:documentation "Representation of FLAC stream header"))
  20. (defmacro with-flac-slots ((instance) &body body)
  21. `(with-slots (pos last-bit header-type header-len) ,instance
  22. ,@body))
  23. (defmethod vpprint ((me flac-header) stream)
  24. (with-flac-slots (me)
  25. (format stream "pos = ~:d, last-bit = ~b, header-type = ~d, length = ~:d"
  26. pos
  27. last-bit
  28. header-type
  29. header-len)))
  30. (defun is-valid-flac-file (flac-file)
  31. "Make sure this is a FLAC file. Look for FLAC header at begining"
  32. (declare #.utils:*standard-optimize-settings*)
  33. (log5:with-context "is-valid-flac-file"
  34. (stream-seek flac-file 0 :start)
  35. (let ((valid nil))
  36. (when (> (stream-size flac-file) 4)
  37. (unwind-protect
  38. (handler-case
  39. (let ((hdr (stream-read-string-with-len flac-file 4)))
  40. (log-flac-frame "got <~a> for flac header" hdr)
  41. (setf valid (string= "fLaC" hdr))
  42. (log-flac-frame "valid = ~a" valid))
  43. (condition (c)
  44. (utils:warn-user "is-valid-flac-file: got condition ~a" c)))
  45. (stream-seek flac-file 0 :start)))
  46. valid)))
  47. (defun make-flac-header (stream)
  48. "Make a flac header from current position in stream"
  49. (declare #.utils:*standard-optimize-settings*)
  50. (log5:with-context "make-flac-header"
  51. (let* ((header (stream-read-u32 stream))
  52. (flac-header (make-instance 'flac-header
  53. :pos (- (stream-seek stream) 4)
  54. :last-bit (utils:get-bitfield header 31 1)
  55. :header-type (utils:get-bitfield header 30 7)
  56. :header-len (utils:get-bitfield header 23 24))))
  57. (log-flac-frame "header = ~a" (vpprint flac-header nil))
  58. flac-header)))
  59. (defparameter *flac-tag-pattern* "(^[a-zA-Z]+)=(.*$)" "used to parse FLAC/ORBIS comments")
  60. (defclass flac-tags ()
  61. ((vendor-str :accessor vendor-str :initarg :vendor-str :initform nil)
  62. (comments :accessor comments :initarg :comments :initform nil)
  63. (tags :accessor tags :initform (make-hash-table :test 'equal))))
  64. (defmethod flac-add-tag ((me flac-tags) new-tag new-val)
  65. (declare #.utils:*standard-optimize-settings*)
  66. (let ((l-new-tag (string-downcase new-tag)))
  67. (setf (gethash l-new-tag (tags me)) new-val)))
  68. (defmethod flac-get-tag ((me flac-tags) key)
  69. (declare #.utils:*standard-optimize-settings*)
  70. (gethash (string-downcase key) (tags me)))
  71. (defun flac-get-tags (stream)
  72. "Loop through file and find all comment tags."
  73. (declare #.utils:*standard-optimize-settings*)
  74. (log5:with-context "flac-get-tags"
  75. (let* ((tags (make-instance 'flac-tags))
  76. (vendor-len (stream-read-u32 stream :endian :big-endian))
  77. (vendor-str (stream-read-utf-8-string-with-len stream vendor-len))
  78. (lst-len (stream-read-u32 stream :endian :big-endian)))
  79. (setf (vendor-str tags) vendor-str)
  80. (dotimes (i lst-len)
  81. (let* ((comment-len (stream-read-u32 stream :endian :big-endian))
  82. (comment (stream-read-utf-8-string-with-len stream comment-len)))
  83. (push comment (comments tags))
  84. (optima:match comment ((optima.ppcre:ppcre *flac-tag-pattern* tag value)
  85. (log-flac-frame "got ~a/~a" tag value)
  86. (flac-add-tag tags tag value)))))
  87. (setf (comments tags) (nreverse (comments tags)))
  88. tags)))
  89. (defmethod find-flac-frames ((stream flac-file-stream))
  90. "Loop through file and find all FLAC headers. If we find comment or audio-info headers, go ahead and parse them too."
  91. (declare #.utils:*standard-optimize-settings*)
  92. (log5:with-context "find-flac-frames"
  93. (stream-seek stream 4 :start)
  94. (handler-case
  95. (let (headers)
  96. (loop for h = (make-flac-header stream) then (make-flac-header stream) do
  97. (push h headers)
  98. (log-flac-frame "Found flac frame: ~a" (vpprint h nil))
  99. (cond
  100. ((= +metadata-comment+ (header-type h))
  101. (setf (flac-tags stream) (flac-get-tags stream)))
  102. ((= +metadata-streaminfo+ (header-type h))
  103. (setf (audio-info stream) (get-flac-audio-info stream)))
  104. (t (stream-seek stream (header-len h) :current)))
  105. (when (not (zerop (last-bit h))) (return)))
  106. (setf (flac-headers stream) (nreverse headers)))
  107. (condition (c)
  108. (utils:warn-user "find-flac-frames got condition ~a" c)
  109. (log-flac-frame "got condition ~a when finding flac frames" c)))))
  110. (defclass flac-audio-properties ()
  111. ((min-block-size :accessor min-block-size :initarg :min-block-size :initform 0)
  112. (max-block-size :accessor max-block-size :initarg :max-block-size :initform 0)
  113. (min-frame-size :accessor min-frame-size :initarg :min-frame-size :initform 0)
  114. (max-frame-size :accessor max-frame-size :initarg :max-frame-size :initform 0)
  115. (sample-rate :accessor sample-rate :initarg :sample-rate :initform 0)
  116. (num-channels :accessor num-channels :initarg :num-channels :initform 0)
  117. (bits-per-sample :accessor bits-per-sample :initarg :bits-per-sample :initform 0)
  118. (total-samples :accessor total-samples :initarg :total-samples :initform 0)
  119. (md5-sig :accessor md5-sig :initarg :md5-sig :initform 0))
  120. (:documentation "FLAC audio file properties"))
  121. (defmethod vpprint ((me flac-audio-properties) stream)
  122. (format stream
  123. "min/max block size: ~:d/~:d; min/max frame size: ~:d/~:d; sample rate: ~d Hz; # channels: ~d; bps: ~:d; total-samples: ~:d; sig: ~x"
  124. (min-block-size me) (max-block-size me)
  125. (min-frame-size me) (max-frame-size me)
  126. (sample-rate me) (num-channels me) (bits-per-sample me)
  127. (total-samples me) (md5-sig me)))
  128. (defun get-flac-audio-info (flac-stream)
  129. "Read in the the audio properties from current file position."
  130. (declare #.utils:*standard-optimize-settings*)
  131. (let ((info (make-instance 'flac-audio-properties)))
  132. (setf (min-block-size info) (stream-read-u16 flac-stream)
  133. (max-block-size info) (stream-read-u16 flac-stream)
  134. (min-frame-size info) (stream-read-u24 flac-stream)
  135. (max-frame-size info) (stream-read-u24 flac-stream))
  136. (let* ((int1 (stream-read-u32 flac-stream))
  137. (int2 (stream-read-u32 flac-stream)))
  138. (setf (total-samples info) (logior (ash (get-bitfield int1 3 4) -32) int2)
  139. (bits-per-sample info) (1+ (get-bitfield int1 8 5))
  140. (num-channels info) (1+ (get-bitfield int1 11 3))
  141. (sample-rate info) (get-bitfield int1 31 20)
  142. (md5-sig info) (stream-read-u128 flac-stream)))
  143. info))
  144. (defun flac-show-raw-tag (flac-file-stream out-stream)
  145. "Spit out the raw form of comments we found"
  146. (declare #.utils:*standard-optimize-settings*)
  147. (format out-stream "Vendor string: <~a>~%" (vendor-str (flac-tags flac-file-stream)))
  148. (dotimes (i (length (comments (flac-tags flac-file-stream))))
  149. (format out-stream "~4t[~d]: <~a>~%" i (nth i (comments (flac-tags flac-file-stream))))))