taglib-tests.lisp 7.4 KB

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