taglib-tests.lisp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. ;;; A note re filesystem encoding: my music collection is housed on a Mac and shared via SAMBA.
  16. ;;; In order to make sure we get valid pathnames, we need to set CCL's filesystem encoding to
  17. ;;; :UTF-8
  18. (defun do-audio-file (&optional (file *song-m4a*) &key (func (constantly t)))
  19. "Parse one audio file (with condition handling)."
  20. (let ((foo))
  21. (unwind-protect
  22. (handler-case
  23. (progn
  24. (setf foo (make-file-stream file))
  25. (when foo
  26. (parse-audio-file foo)) ; only call parse-audio if we got back a MP3/M4A
  27. (funcall func foo)) ; call func even is foo is null so it can account for non MP3/M4A files
  28. (condition (c)
  29. (utils:warn-user "File: ~a~%Got condition: <~a>" file c)))
  30. (when foo
  31. (stream-close foo)))))
  32. (defun do-audio-dir (&optional (dir "Queen") &key (file-system-encoding :utf-8)
  33. (mp3-func #'mp3-tag:show-tags)
  34. (mp4-func #'mp4-tag:show-tags))
  35. "Walk :DIR and FUNCALL specified function for each file (MP4/MP3) found."
  36. (set-pathname-encoding file-system-encoding)
  37. (let ((mp3-count 0)
  38. (mp4-count 0)
  39. (other-count 0))
  40. (osicat:walk-directory dir (lambda (f)
  41. (let ((full-name (merge-pathnames (ccl:current-directory) (pathname f))))
  42. (do-audio-file full-name :func (lambda (s)
  43. (cond ((typep s 'mp3-file-stream)
  44. (incf mp3-count)
  45. (when mp3-func
  46. (funcall mp3-func s)))
  47. ((typep s 'mp4-file-stream)
  48. (incf mp4-count)
  49. (when mp4-func
  50. (funcall mp4-func s)))
  51. ((null s) (incf other-count))))))))
  52. (format t "~&~:d MP3s, ~:d MP4s, ~:d Others, for a total of ~:d~%"
  53. mp3-count mp4-count other-count (+ mp3-count mp4-count other-count))))
  54. (defun time-test (&optional (dir "Queen") &key (file-system-encoding :utf-8) (do-audio-processing t))
  55. "Time parsing of DIR."
  56. (let ((audio-streams:*get-audio-info* do-audio-processing))
  57. (time (do-audio-dir dir :file-system-encoding file-system-encoding :mp3-func nil :mp4-func nil))))