taglib-tests.lisp 7.4 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. ;;;
  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 known file type
  27. (funcall func foo)) ; call func even is foo is null so it can account for unkown file types
  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. (func #'abstract-tag:show-tags))
  34. "Walk :DIR and FUNCALL specified function for each file (MP4/MP3) found."
  35. (set-pathname-encoding file-system-encoding)
  36. (let ((mp3-count 0)
  37. (flac-count 0)
  38. (mp4-count 0)
  39. (other-count 0))
  40. (cl-fad:walk-directory dir (lambda (f)
  41. (do-audio-file f :func (lambda (s)
  42. (cond ((typep s 'mp3-file-stream)
  43. (incf mp3-count)
  44. (when func (funcall func s)))
  45. ((typep s 'flac-file-stream)
  46. (incf flac-count)
  47. (when func (funcall func s)))
  48. ((typep s 'mp4-file-stream)
  49. (incf mp4-count)
  50. (when func (funcall func s)))
  51. ((null s) (incf other-count)))))))
  52. (format t "~&~:d MP3s, ~:d MP4s, ~:d FLACs ~:d Others, for a total of ~:d~%"
  53. mp3-count mp4-count flac-count other-count (+ mp3-count mp4-count flac-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 :func nil))))
  58. ;;;;;;;;;;;;;;;;;;;; Experimental multi-thread code below ;;;;;;;;;;;;;;;;;;;;
  59. (defparameter *END-THREAD* #xdeadbeef)
  60. (defparameter *MAX-THREADS* 4)
  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 (MP4/MP3) 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. (labels ((thread-reader ()
  78. (declare (special *me*))
  79. (let ((f)
  80. (results (make-chanl-results :name *me* :mp3-count 0 :flac-count 0 :mp4-count 0 :other-count 0)))
  81. (loop
  82. (with-slots (name mp3-count mp4-count flac-count other-count) results
  83. (setf f (chanl:recv channel))
  84. (when (and (typep f 'integer)
  85. (= f *END-THREAD*))
  86. (chanl:send dead-channel results)
  87. (return-from thread-reader nil))
  88. (do-audio-file f :func (lambda (s)
  89. (cond ((typep s 'mp3-file-stream)
  90. (incf mp3-count)
  91. (when func (funcall func s)))
  92. ((typep s 'flac-file-stream)
  93. (incf flac-count)
  94. (when func (funcall func s)))
  95. ((typep s 'mp4-file-stream)
  96. (incf mp4-count)
  97. (when func (funcall func s)))
  98. ((null s) (incf other-count))))))))))
  99. (cl-fad:walk-directory dir (lambda (f)
  100. (chanl:send channel f)))
  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. (block thread-reap
  106. (let ((i 0)
  107. results)
  108. (format t "Waiting on ~d threads~%" *MAX-THREADS*)
  109. (loop
  110. (force-output *standard-output*)
  111. (setf results (chanl:recv dead-channel))
  112. (format t "~4t~a died, ~:d MP3s, ~:d MP4s, ~:d FLACs~:d Others~%"
  113. (chanl-results-name results)
  114. (chanl-results-mp3-count results)
  115. (chanl-results-mp4-count results)
  116. (chanl-results-flac-count results)
  117. (chanl-results-other-count results))
  118. (force-output *standard-output*)
  119. (incf mp3-count (chanl-results-mp3-count results))
  120. (incf mp4-count (chanl-results-mp4-count results))
  121. (incf flac-count (chanl-results-flac-count results))
  122. (incf other-count (chanl-results-other-count results))
  123. (incf i)
  124. (when (= i *MAX-THREADS*)
  125. (return-from thread-reap *MAX-THREADS*)))))
  126. (format t "All threads done~%")
  127. (format t "~&~:d MP3s, ~:d MP4s, ~:d FLACS, ~:d Others, for a total of ~:d~%"
  128. mp3-count mp4-count flac-count other-count (+ mp3-count mp4-count flac-count other-count)))))
  129. (defun mp-time-test (&optional (dir "Queen") &key (file-system-encoding :utf-8) (do-audio-processing t))
  130. "Time parsing of DIR."
  131. (let ((audio-streams:*get-audio-info* do-audio-processing))
  132. (time (mp-do-audio-dir dir :file-system-encoding file-system-encoding :func nil))))