taglib-tests.lisp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ;;; -*- Mode: Lisp; show-trailing-whitespace: t; Base: 10; indent-tabs: nil; Syntax: ANSI-Common-Lisp; Package: TAGLIB-TESTS; -*-
  2. ;;; Copyright (c) 2013, Mark VandenBrink. All rights reserved.
  3. (in-package #:cl-user)
  4. (defpackage #:taglib-tests
  5. (:use #:common-lisp #:logging #:audio-streams))
  6. (in-package #:taglib-tests)
  7. ;;; some convenient songs to parse
  8. (defparameter *song-m4a* "Queen/Queen 01 Keep Yourself Alive.m4a")
  9. (defparameter *song-mp3* "Queen/Sheer Heart Attack/07 In The Lap Of The Gods.mp3")
  10. ;;;
  11. ;;; Set the pathname (aka filename) encoding in CCL for appropriate platorm
  12. (defun set-pathname-encoding (enc) (setf (ccl:pathname-encoding-name) enc))
  13. (defun set-pathname-encoding-for-osx () (set-pathname-encoding :utf-8))
  14. (defun set-pathname-encoding-for-linux () (set-pathname-encoding nil))
  15. (defmethod has-extension ((n string) ext)
  16. "Probably should use CL's PATHNAME methods, but simply looking at the .XXX portion of a filename
  17. to see if it matches. This is the string version that makes a PATHNAME and calls the PATHNAME version."
  18. (has-extension (parse-namestring n) ext))
  19. (defmethod has-extension ((p pathname) ext)
  20. "Probably should use CL's PATHNAME methods , but simply looking at the .XXX portion of a filename
  21. to see if it matches. PATHNAME version."
  22. (let ((e (pathname-type p)))
  23. (if e
  24. (string= (string-downcase e) (string-downcase ext))
  25. nil)))
  26. (defmacro redirect (filename &rest body)
  27. "Temporarily set *STANDARD-OUTPUT* to FILENAME and execute BODY."
  28. `(let ((*standard-output* (open ,filename :direction :output :if-does-not-exist :create :if-exists :supersede)))
  29. ,@body
  30. (finish-output *standard-output*)))
  31. ;;; A note re filesystem encoding: my music collection is housed on a Mac and shared via SAMBA.
  32. ;;; In order to make sure we get valid pathnames, we need to set CCL's filesystem encoding to
  33. ;;; :UTF-8
  34. ;;;;;;;;;;;;;;;;;;;; MP4 Tests ;;;;;;;;;;;;;;;;;;;;
  35. (defun mp4-test0 (file)
  36. "Parse one MP3 file (with condition handling)."
  37. (let ((dir (ccl:current-directory))
  38. (foo))
  39. (unwind-protect
  40. (handler-case
  41. (setf foo (parse-mp4-file file))
  42. (condition (c)
  43. (utils:warn-user "Dir: ~a~%File: ~a~%Got condition: <~a>~%" dir file c)))
  44. (when foo (stream-close foo)))
  45. foo))
  46. (defun mp4-test1 ()
  47. (mp4-test0 *song-m4a*))
  48. (defun mp4-test2 (&key (dir "Queen") (raw nil) (file-system-encoding :utf-8))
  49. "Walk :DIR and call SHOW-TAGS for each file (MP4/MP3) found."
  50. (set-pathname-encoding file-system-encoding)
  51. (osicat:walk-directory dir (lambda (f)
  52. (when (has-extension f "m4a")
  53. (let ((file (mp4-test0 (merge-pathnames (ccl:current-directory) (pathname f)))))
  54. (when file
  55. (mp4-tag:show-tags file :raw raw)))))))
  56. ;;;;;;;;;;;;;;;;;;;; MP3 Tests ;;;;;;;;;;;;;;;;;;;;
  57. (defun mp3-test0 (file)
  58. "Parse one MP3 file (with condition handling)."
  59. (let ((dir (ccl:current-directory))
  60. (foo))
  61. (unwind-protect
  62. (handler-case
  63. (setf foo (parse-mp3-file file))
  64. (condition (c)
  65. (utils:warn-user "Dir: ~a~%File: ~a~%Got condition: <~a>~%" dir file c)))
  66. (when foo (stream-close foo)))
  67. foo))
  68. (defun mp3-test1 ()
  69. (mp3-test0 *song-mp3*))
  70. (defun mp3-test2 (&key (dir "Queen") (raw nil) (file-system-encoding :utf-8))
  71. "Walk :DIR and parse every MP3 we find."
  72. (set-pathname-encoding file-system-encoding)
  73. (osicat:walk-directory dir (lambda (f)
  74. (when (has-extension f "mp3")
  75. (let ((file (mp3-test0 (merge-pathnames (ccl:current-directory) (pathname f)))))
  76. (when file
  77. (mp3-tag:show-tags file :raw raw)))))))
  78. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  79. (defun test2 (&key (dir "Queen") (raw nil) (file-system-encoding :utf-8))
  80. "Walk :DIR and call SHOW-TAGS for each file (MP4/MP3) found."
  81. (set-pathname-encoding file-system-encoding)
  82. (osicat:walk-directory dir (lambda (f)
  83. (let ((full-name (merge-pathnames (ccl:current-directory) (pathname f))))
  84. (cond ((has-extension f "mp3")
  85. (let ((file (mp3-test0 full-name)))
  86. (when file
  87. (mp3-tag:show-tags file :raw raw))))
  88. ((has-extension f "m4a")
  89. (let ((file (mp4-test0 full-name)))
  90. (when file
  91. (mp4-tag:show-tags file :raw raw)))))))))
  92. (defun time-test (dir &key (file-system-encoding :utf-8) (do-audio-processing t))
  93. "Time parsing of DIR."
  94. (let ((mp3-count 0)
  95. (mp4-count 0)
  96. (other-count 0))
  97. (labels ((do-dir (dir)
  98. (osicat:walk-directory dir (lambda (f)
  99. (let ((full-name (merge-pathnames (ccl:current-directory) (pathname f))))
  100. (cond ((has-extension f "mp3")
  101. (incf mp3-count)
  102. (mp3-test0 full-name))
  103. ((has-extension f "m4a")
  104. (incf mp4-count)
  105. (mp4-test0 full-name))
  106. (t
  107. (incf other-count))))))))
  108. (set-pathname-encoding file-system-encoding)
  109. (let ((audio-streams:*get-audio-info* do-audio-processing))
  110. (time (do-dir dir)))
  111. (format t "~:d MP3s, ~:d MP4s, ~:d Others~%"
  112. mp3-count mp4-count other-count))))