taglib-tests.lisp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. ;;;
  14. ;;; A note re filesystem encoding: my music collection is housed on a Mac and shared via SAMBA.
  15. ;;; In order to make sure we get valid pathnames, we need to set CCL's filesystem encoding to
  16. ;;; :UTF-8
  17. (defun set-pathname-encoding (enc) (setf (ccl:pathname-encoding-name) enc))
  18. (defun set-pathname-encoding-for-osx () (set-pathname-encoding :utf-8))
  19. (defun set-pathname-encoding-for-linux () (set-pathname-encoding nil))
  20. (defun do-audio-file (&optional (file *song-m4a*) &key (func (constantly t)))
  21. "Parse one audio file (with condition handling)."
  22. (let ((foo))
  23. (unwind-protect
  24. (handler-case
  25. (progn
  26. (setf foo (make-file-stream file))
  27. (when foo
  28. (parse-audio-file foo)) ; only call parse-audio if we got back a known file type
  29. (funcall func foo)) ; call func even is foo is null so it can account for unkown file types
  30. (condition (c)
  31. (utils:warn-user "File: ~a~%Got condition: <~a>" file c)))
  32. (when foo
  33. (stream-close foo)))))
  34. (defun do-audio-dir (&optional (dir "Queen") &key (file-system-encoding :utf-8)
  35. (func #'abstract-tag:show-tags))
  36. "Walk :DIR and FUNCALL specified function for each file audio found."
  37. (set-pathname-encoding file-system-encoding)
  38. (let ((mp3-count 0)
  39. (flac-count 0)
  40. (mp4-count 0)
  41. (other-count 0))
  42. (cl-fad:walk-directory dir (lambda (f)
  43. (do-audio-file f :func (lambda (s)
  44. (cond ((typep s 'mp3-file-stream) (incf mp3-count))
  45. ((typep s 'flac-file-stream) (incf flac-count))
  46. ((typep s 'mp4-file-stream) (incf mp4-count))
  47. ((null s) (incf other-count)))
  48. (when (and (not (null s)) func) (funcall func s))))))
  49. (format t "~&~:d MP3s, ~:d MP4s, ~:d FLACs, ~:d Others, for a total of ~:d~%"
  50. mp3-count mp4-count flac-count other-count (+ mp3-count mp4-count flac-count other-count))))
  51. (defun time-test (&optional (dir "Queen") &key (file-system-encoding :utf-8) (do-audio-processing t))
  52. "Time parsing of DIR."
  53. (let ((audio-streams:*get-audio-info* do-audio-processing))
  54. (time (do-audio-dir dir :file-system-encoding file-system-encoding :func nil))))
  55. ;;;;;;;;;;;;;;;;;;;; multi-thread code below ;;;;;;;;;;;;;;;;;;;;
  56. (defparameter *END-THREAD* #xdeadbeef)
  57. (defparameter *MAX-THREADS* 4)
  58. ;;; Simple structure to hold a thread's results
  59. (defstruct chanl-results
  60. name
  61. mp3-count
  62. flac-count
  63. mp4-count
  64. other-count)
  65. (defun mp-do-audio-dir (&optional (dir "Queen") &key (file-system-encoding :utf-8)
  66. (func #'abstract-tag:show-tags))
  67. "Walk :DIR and FUNCALL specified function for each file audio found."
  68. (set-pathname-encoding file-system-encoding)
  69. (let ((channel (make-instance 'chanl:unbounded-channel))
  70. (dead-channel (make-instance 'chanl:unbounded-channel))
  71. (mp3-count 0)
  72. (flac-count 0)
  73. (mp4-count 0)
  74. (other-count 0))
  75. ;; This function is run by each thread
  76. ;; Thread sits in a loop, reading from CHAN. If that read
  77. ;; returns the integer *END-THREAD*, then thread exits; otherwise,
  78. ;; it runs DO-AUDIO-FILE on the file passed in.
  79. (labels ((thread-reader ()
  80. (declare (special *me*))
  81. (let ((f)
  82. (results (make-chanl-results :name *me* :mp3-count 0 :flac-count 0 :mp4-count 0 :other-count 0)))
  83. (loop
  84. (with-slots (name mp3-count mp4-count flac-count other-count) results
  85. (setf f (chanl:recv channel))
  86. (when (and (typep f 'integer)
  87. (= f *END-THREAD*))
  88. (chanl:send dead-channel results) ; send structure of stats back to parent
  89. (return-from thread-reader nil))
  90. (do-audio-file f :func (lambda (s)
  91. (cond ((typep s 'mp3-file-stream) (incf mp3-count))
  92. ((typep s 'flac-file-stream) (incf flac-count))
  93. ((typep s 'mp4-file-stream) (incf mp4-count))
  94. ((null s) (incf other-count)))
  95. (when (and (not (null s)) func) (funcall func s)))))))))
  96. ;; first, add all files in DIR to CHANNEL
  97. (cl-fad:walk-directory dir (lambda (f) (chanl:send channel f)))
  98. ;; At this point, CHANNEL is stuffed with files.
  99. ;; Now, send *MAX-THREADS* "ends" (at end of CHANNEL) and
  100. ;; spawn *MAX-THREADS* threads
  101. (dotimes (i *MAX-THREADS*)
  102. (chanl:send channel *END-THREAD*))
  103. (dotimes (i *MAX-THREADS*)
  104. (chanl:pcall #'thread-reader :initial-bindings `((*me* ,(format nil "reader-thread-~d" i)))))
  105. ;; sit in loop until we read *MAX-THREADS* results
  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 files~%"
  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))))