taglib-tests.lisp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. (defparameter *song-flac* "Frank Zappa/Baby Snakes/02. Baby Snakes.flac")
  11. ;;;
  12. ;;; Set the pathname (aka filename) encoding in CCL for appropriate platorm
  13. (defun set-pathname-encoding (enc) (setf (ccl:pathname-encoding-name) enc))
  14. (defun set-pathname-encoding-for-osx () (set-pathname-encoding :utf-8))
  15. (defun set-pathname-encoding-for-linux () (set-pathname-encoding nil))
  16. ;;; A note re filesystem encoding: my music collection is housed on a Mac and shared via SAMBA.
  17. ;;; In order to make sure we get valid pathnames, we need to set CCL's filesystem encoding to
  18. ;;; :UTF-8
  19. (defun do-audio-file (&optional (file *song-m4a*) &key (func (constantly t)))
  20. "Parse one audio file (with condition handling)."
  21. (let ((foo))
  22. (unwind-protect
  23. (handler-case
  24. (progn
  25. (setf foo (make-file-stream file))
  26. (when foo
  27. (parse-audio-file foo)) ; only call parse-audio if we got back a known file type
  28. (funcall func foo)) ; call func even is foo is null so it can account for unkown file types
  29. (condition (c)
  30. (utils:warn-user "File: ~a~%Got condition: <~a>" file c)))
  31. (when foo
  32. (stream-close foo)))))
  33. (defun do-audio-dir (&optional (dir "Queen") &key (file-system-encoding :utf-8)
  34. (func #'abstract-tag:show-tags))
  35. "Walk :DIR and FUNCALL specified function for each file audio found."
  36. (set-pathname-encoding file-system-encoding)
  37. (let ((mp3-count 0)
  38. (flac-count 0)
  39. (mp4-count 0)
  40. (other-count 0))
  41. (cl-fad:walk-directory dir (lambda (f)
  42. (do-audio-file f :func (lambda (s)
  43. (cond ((typep s 'mp3-file-stream)
  44. (incf mp3-count)
  45. (when func (funcall func s)))
  46. ((typep s 'flac-file-stream)
  47. (incf flac-count)
  48. (when func (funcall func s)))
  49. ((typep s 'mp4-file-stream)
  50. (incf mp4-count)
  51. (when func (funcall func s)))
  52. ((null s) (incf other-count)))))))
  53. (format t "~&~:d MP3s, ~:d MP4s, ~:d FLACs, ~:d Others, for a total of ~:d~%"
  54. mp3-count mp4-count flac-count other-count (+ mp3-count mp4-count flac-count other-count))))
  55. (defun time-test (&optional (dir "Queen") &key (file-system-encoding :utf-8) (do-audio-processing t))
  56. "Time parsing of DIR."
  57. (let ((audio-streams:*get-audio-info* do-audio-processing))
  58. (time (do-audio-dir dir :file-system-encoding file-system-encoding :func nil))))
  59. ;;;;;;;;;;;;;;;;;;;; Experimental multi-thread code below ;;;;;;;;;;;;;;;;;;;;
  60. (defparameter *END-THREAD* #xdeadbeef)
  61. (defparameter *MAX-THREADS* 4)
  62. (defstruct chanl-results
  63. name
  64. mp3-count
  65. flac-count
  66. mp4-count
  67. other-count)
  68. (defun mp-do-audio-dir (&optional (dir "Queen") &key (file-system-encoding :utf-8)
  69. (func #'abstract-tag:show-tags))
  70. "Walk :DIR and FUNCALL specified function for each file audio found."
  71. (set-pathname-encoding file-system-encoding)
  72. (let ((channel (make-instance 'chanl:unbounded-channel))
  73. (dead-channel (make-instance 'chanl:unbounded-channel))
  74. (mp3-count 0)
  75. (flac-count 0)
  76. (mp4-count 0)
  77. (other-count 0))
  78. (labels ((thread-reader ()
  79. (declare (special *me*))
  80. (let ((f)
  81. (results (make-chanl-results :name *me* :mp3-count 0 :flac-count 0 :mp4-count 0 :other-count 0)))
  82. (loop
  83. (with-slots (name mp3-count mp4-count flac-count other-count) results
  84. (setf f (chanl:recv channel))
  85. (when (and (typep f 'integer)
  86. (= f *END-THREAD*))
  87. (chanl:send dead-channel results)
  88. (return-from thread-reader nil))
  89. (do-audio-file f :func (lambda (s)
  90. (cond ((typep s 'mp3-file-stream)
  91. (incf mp3-count)
  92. (when func (funcall func s)))
  93. ((typep s 'flac-file-stream)
  94. (incf flac-count)
  95. (when func (funcall func s)))
  96. ((typep s 'mp4-file-stream)
  97. (incf mp4-count)
  98. (when func (funcall func s)))
  99. ((null s) (incf other-count))))))))))
  100. (cl-fad:walk-directory dir (lambda (f)
  101. (chanl:send channel f)))
  102. (dotimes (i *MAX-THREADS*)
  103. (chanl:send channel *END-THREAD*))
  104. (dotimes (i *MAX-THREADS*)
  105. (chanl:pcall #'thread-reader :initial-bindings `((*me* ,(format nil "reader-thread-~d" i)))))
  106. (block thread-reap
  107. (let ((i 0)
  108. results)
  109. (format t "Waiting on ~d threads~%" *MAX-THREADS*)
  110. (loop
  111. (force-output *standard-output*)
  112. (setf results (chanl:recv dead-channel))
  113. (format t "~4t~a died, ~:d MP3s, ~:d MP4s, ~:d FLACs, ~:d Others~%"
  114. (chanl-results-name results)
  115. (chanl-results-mp3-count results)
  116. (chanl-results-mp4-count results)
  117. (chanl-results-flac-count results)
  118. (chanl-results-other-count results))
  119. (force-output *standard-output*)
  120. (incf mp3-count (chanl-results-mp3-count results))
  121. (incf mp4-count (chanl-results-mp4-count results))
  122. (incf flac-count (chanl-results-flac-count results))
  123. (incf other-count (chanl-results-other-count results))
  124. (incf i)
  125. (when (= i *MAX-THREADS*)
  126. (return-from thread-reap *MAX-THREADS*)))))
  127. (format t "All threads done~%")
  128. (format t "~&~:d MP3s, ~:d MP4s, ~:d FLACS, ~:d Others, for a total of ~:d~%"
  129. mp3-count mp4-count flac-count other-count (+ mp3-count mp4-count flac-count other-count)))))
  130. (defun mp-time-test (&optional (dir "Queen") &key (file-system-encoding :utf-8) (do-audio-processing t))
  131. "Time parsing of DIR."
  132. (let ((audio-streams:*get-audio-info* do-audio-processing))
  133. (time (mp-do-audio-dir dir :file-system-encoding file-system-encoding :func nil))))