flac-frame.lisp 7.5 KB

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