taglib-tests.lisp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 I/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. (defmacro redirect (filename &rest body)
  16. "Temporarily set *STANDARD-OUTPUT* to FILENAME and execute BODY."
  17. `(let ((*standard-output* (open ,filename :direction :output :if-does-not-exist :create :if-exists :supersede)))
  18. ,@body
  19. (finish-output *standard-output*)))
  20. ;;; A note re filesystem encoding: my music collection is housed on a Mac and shared via SAMBA.
  21. ;;; In order to make sure we get valid pathnames, we need to set CCL's filesystem encoding to
  22. ;;; :UTF-8
  23. ;;;;;;;;;;;;;;;;;;;; MP4 Tests ;;;;;;;;;;;;;;;;;;;;
  24. (defun mp4-test0 (&optional (file *song-m4a*))
  25. "Parse one MP3 file (with condition handling)."
  26. (let ((foo))
  27. (unwind-protect
  28. (handler-case
  29. (setf foo (parse-mp4-file file))
  30. (condition (c)
  31. (utils:warn-user "File: ~a~%Got condition: <~a>" file c)))
  32. (when foo (stream-close foo)))
  33. foo))
  34. (defun mp4-test2 (&optional (dir "Queen") &key (raw nil) (file-system-encoding :utf-8))
  35. "Walk :DIR and call SHOW-TAGS for each file (MP4/MP3) found."
  36. (set-pathname-encoding file-system-encoding)
  37. (osicat:walk-directory dir (lambda (f)
  38. (when (utils:has-extension f "m4a")
  39. (let ((file (mp4-test0 (merge-pathnames (ccl:current-directory) (pathname f)))))
  40. (when file
  41. (mp4-tag:show-tags file :raw raw)))))))
  42. ;;;;;;;;;;;;;;;;;;;; MP3 Tests ;;;;;;;;;;;;;;;;;;;;
  43. (defun mp3-test0 (&optional (file *song-mp3*))
  44. "Parse one MP3 file (with condition handling)."
  45. (let ((foo))
  46. (unwind-protect
  47. (handler-case
  48. (setf foo (parse-mp3-file file))
  49. (condition (c)
  50. (utils:warn-user "File: ~a~%Got condition: <~a>" file c)))
  51. (when foo (stream-close foo)))
  52. foo))
  53. (defun mp3-test2 (&optional (dir "Queen") &key (raw nil) (file-system-encoding :utf-8))
  54. "Walk :DIR and parse every MP3 we find."
  55. (set-pathname-encoding file-system-encoding)
  56. (osicat:walk-directory dir (lambda (f)
  57. (when (utils:has-extension f "mp3")
  58. (let ((file (mp3-test0 (merge-pathnames (ccl:current-directory) (pathname f)))))
  59. (when file
  60. (mp3-tag:show-tags file :raw raw)))))))
  61. ;;;;;;;;;;;;;;;;;;;; MP3/M4A tests ;;;;;;;;;;;;;;;;;;;;
  62. (defun test2 (&optional (dir "Queen") &key (raw nil) (file-system-encoding :utf-8))
  63. "Walk :DIR and call SHOW-TAGS for each file (MP4/MP3) found."
  64. (set-pathname-encoding file-system-encoding)
  65. (let ((mp3-count 0)
  66. (mp4-count 0)
  67. (other-count 0))
  68. (osicat:walk-directory dir (lambda (f)
  69. (let ((full-name (merge-pathnames (ccl:current-directory) (pathname f))))
  70. (cond ((utils:has-extension f "mp3")
  71. (incf mp3-count)
  72. (let ((file (mp3-test0 full-name)))
  73. (when file
  74. (mp3-tag:show-tags file :raw raw))))
  75. ((utils:has-extension f "m4a")
  76. (let ((file (mp4-test0 full-name)))
  77. (incf mp4-count)
  78. (when file
  79. (mp4-tag:show-tags file :raw raw))))
  80. (t (incf other-count))))))
  81. (format t "~&~:d MP3s, ~:d MP4s, ~:d Others, for a total of ~:d~%"
  82. mp3-count mp4-count other-count (+ mp3-count mp4-count other-count))))
  83. (defun time-test (&optional (dir "Queen") &key (file-system-encoding :utf-8) (do-audio-processing t))
  84. "Time parsing of DIR."
  85. (let ((mp3-count 0)
  86. (mp4-count 0)
  87. (other-count 0))
  88. (labels ((do-dir (dir)
  89. (osicat:walk-directory dir (lambda (f)
  90. (let ((full-name (merge-pathnames (ccl:current-directory) (pathname f))))
  91. (cond ((utils:has-extension f "mp3")
  92. (incf mp3-count)
  93. (mp3-test0 full-name))
  94. ((utils:has-extension f "m4a")
  95. (incf mp4-count)
  96. (mp4-test0 full-name))
  97. (t
  98. (incf other-count))))))))
  99. (set-pathname-encoding file-system-encoding)
  100. (let ((audio-streams:*get-audio-info* do-audio-processing))
  101. (time (do-dir dir)))
  102. (format t "~&~:d MP3s, ~:d MP4s, ~:d Others, for a total of ~:d~%"
  103. mp3-count mp4-count other-count (+ mp3-count mp4-count other-count)))))