utils.lisp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. (in-package :cl-user)
  2. (defpackage chad-music.utils
  3. (:use :cl #:audio-streams #:alexandria)
  4. (:export #:*standard-optimize-settings*
  5. #:text-tag
  6. #:bit-rate
  7. #:is-vbr
  8. #:duration
  9. #:publisher
  10. #:country))
  11. (in-package :chad-music.utils)
  12. (eval-when (:compile-toplevel :load-toplevel :execute)
  13. #+dbg
  14. (defvar *standard-optimize-settings* '(optimize (debug 3)))
  15. #-dbg
  16. (defvar *standard-optimize-settings* '(optimize (speed 3) (safety 0) (space 0) (debug 0))))
  17. (defgeneric text-tag (stream desc) (:method ((object t) desc) nil))
  18. (defmethod text-tag ((mp3 id3:mp3-file) desc)
  19. (declare #.*standard-optimize-settings*)
  20. (loop for frame in (id3:get-frames mp3 '("TXXX"))
  21. when (string-equal (id3:desc frame) desc)
  22. do (return-from text-tag (id3:val frame))))
  23. (defgeneric bit-rate (stream) (:method ((object t)) nil))
  24. (defgeneric is-vbr (stream) (:method ((object t)) nil))
  25. (defgeneric duration (stream) (:method ((object t)) nil))
  26. (defgeneric publisher (stream) (:method ((object t)) nil))
  27. (defgeneric country (stream) (:method ((object t)) nil))
  28. (defmethod bit-rate ((mp3 id3:mp3-file))
  29. (let ((info (id3:audio-info mp3)))
  30. (when info
  31. (round (mpeg::bit-rate info) 1000))))
  32. (defmethod is-vbr ((mp3 id3:mp3-file))
  33. (let ((info (id3:audio-info mp3)))
  34. (when info
  35. (mpeg::is-vbr info))))
  36. (defmethod duration ((mp3 id3:mp3-file))
  37. (let ((info (id3:audio-info mp3)))
  38. (when info
  39. (mpeg::len info))))
  40. (defmethod publisher ((mp3 id3:mp3-file))
  41. (declare #.utils:*standard-optimize-settings*)
  42. (let ((frames (id3:get-frames mp3 '("TPUB" "TPB"))))
  43. (when frames
  44. (return-from publisher (id3:info (first frames)))))
  45. nil)
  46. (defmethod country ((mp3 id3:mp3-file))
  47. (text-tag mp3 "MusicBrainz Album Release Country"))
  48. (defmethod bit-rate ((m4a m4a:mp4-file))
  49. (let ((info (m4a:audio-info m4a)))
  50. (when info
  51. (round (m4a::avg-bit-rate info) 1000))))
  52. (defmethod duration ((m4a m4a:mp4-file))
  53. (let ((info (m4a:audio-info m4a)))
  54. (when info
  55. (m4a::seconds info))))