taglib-tests.lisp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 #:utils))
  6. (in-package #:taglib-tests)
  7. ;;; some convenient songs to parse
  8. (defparameter *song-m4a*
  9. "/home/markv/Music/Queen/Queen I/01 Keep Yourself Alive.m4a")
  10. (defparameter *song-mp3*
  11. "/home/markv/Music/Queen/Sheer Heart Attack/07 In The Lap Of The Gods.mp3")
  12. (defparameter *song-flac*
  13. "/home/markv/Music/Frank Zappa/Baby Snakes/02. Baby Snakes.flac")
  14. ;;;
  15. ;;; Set the pathname (aka filename) encoding for appropriate platform
  16. ;;;
  17. ;;; A note re filesystem encoding: my music collection is housed on a Mac and shared via SAMBA.
  18. ;;; In order to make sure we get valid pathnames, we need to set CCL's filesystem encoding to
  19. ;;; :UTF-8
  20. (defun set-pathname-encoding (enc)
  21. #+CCL (setf (ccl:pathname-encoding-name) enc)
  22. #-CCL (declare (ignore enc))
  23. t)
  24. (defun set-pathname-encoding-for-osx () (set-pathname-encoding :utf-8))
  25. (defun set-pathname-encoding-for-linux () (set-pathname-encoding nil))
  26. (defun do-audio-file (&key (file *song-m4a*)
  27. (func #'abstract-tag:show-tags))
  28. "Parse one audio file and display the tags"
  29. (awhen (open-audio-file file)
  30. (funcall func it)))
  31. (defun do-audio-dir (&key (dir "/home/markv/Music/Queen")
  32. (file-system-encoding :utf-8)
  33. (func #'abstract-tag:show-tags))
  34. "Walk :DIR and FUNCALL specified function for each file audio 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
  41. (lambda (f)
  42. (do-audio-file :file f
  43. :func (lambda (s)
  44. (cond ((typep s 'id3-frame:mp3-file)
  45. (incf mp3-count))
  46. ((typep s 'flac-frame:flac-file)
  47. (incf flac-count))
  48. ((typep s 'mp4-atom:mp4-file)
  49. (incf mp4-count))
  50. ((null s)
  51. (incf other-count)))
  52. (when (and (not (null s)) func)
  53. (funcall func s))))))
  54. (format t "~&~:d MP3s, ~:d MP4s, ~:d FLACs, ~:d Others, for a total of ~:d~%"
  55. mp3-count mp4-count flac-count other-count
  56. (+ mp3-count mp4-count flac-count other-count))))
  57. (defun time-test (&key (dir "/home/markv/Music/Queen")
  58. (file-system-encoding :utf-8) (do-audio-processing t))
  59. "Time parsing of DIR."
  60. (set-pathname-encoding file-system-encoding)
  61. (let ((audio-streams:*get-audio-info* do-audio-processing))
  62. (time (do-audio-dir :dir dir
  63. :file-system-encoding file-system-encoding :func nil))))
  64. ;;;;;;;;;;;;;;;;;;;; multi-thread code below ;;;;;;;;;;;;;;;;;;;;
  65. (defparameter *end-thread* #xdeadbeef)
  66. (defparameter *max-threads* 4)
  67. ;;; Simple structure to hold a thread's results
  68. (defstruct chanl-results
  69. name
  70. mp3-count
  71. flac-count
  72. mp4-count
  73. other-count)
  74. (defun mp-do-audio-dir (&key (dir "/home/markv/Music/Queen")
  75. (file-system-encoding :utf-8)
  76. (func #'abstract-tag:show-tags))
  77. "Walk :DIR and FUNCALL specified function for each file audio found."
  78. (set-pathname-encoding file-system-encoding)
  79. (let ((channel (make-instance 'chanl:unbounded-channel))
  80. (dead-channel (make-instance 'chanl:unbounded-channel))
  81. (mp3-count 0)
  82. (flac-count 0)
  83. (mp4-count 0)
  84. (other-count 0))
  85. ;; This function is run by each thread
  86. ;; Thread sits in a loop, reading from CHAN. If that read
  87. ;; returns the integer *END-THREAD*, then thread exits; otherwise,
  88. ;; it runs DO-AUDIO-FILE on the file passed in.
  89. (labels ((thread-reader ()
  90. (declare (special *me*))
  91. (let ((f)
  92. (results (make-chanl-results :name *me* :mp3-count 0
  93. :flac-count 0 :mp4-count 0
  94. :other-count 0)))
  95. (loop
  96. (with-slots (name mp3-count mp4-count flac-count other-count) results
  97. (setf f (chanl:recv channel))
  98. (when (and (typep f 'integer)
  99. (= f *end-thread*))
  100. (chanl:send dead-channel results)
  101. (return-from thread-reader nil))
  102. (do-audio-file :file f
  103. :func (lambda (s)
  104. (cond ((typep s 'id3-frame:mp3-file)
  105. (incf mp3-count))
  106. ((typep s 'flac-frame:flac-file)
  107. (incf flac-count))
  108. ((typep s 'mp4-atom:mp4-file)
  109. (incf mp4-count))
  110. ((null s)
  111. (incf other-count)))
  112. (when (and (not (null s)) func)
  113. (funcall func s)))))))))
  114. ;; first, add all files in DIR to CHANNEL
  115. (cl-fad:walk-directory dir (lambda (f) (chanl:send channel f)))
  116. ;; At this point, CHANNEL is stuffed with files.
  117. ;; Now, send *MAX-THREADS* "ends" (at end of CHANNEL) and
  118. ;; spawn *MAX-THREADS* threads
  119. (dotimes (i *max-threads*)
  120. (chanl:send channel *end-thread*))
  121. (dotimes (i *max-threads*)
  122. (chanl:pcall
  123. #'thread-reader
  124. :initial-bindings `((*me* ,(format nil "reader-thread-~d" i)))))
  125. ;; sit in loop until we read *MAX-THREADS* results
  126. (block thread-reap
  127. (let ((i 0)
  128. results)
  129. (format t "Waiting on ~d threads~%" *max-threads*)
  130. (loop
  131. (force-output *standard-output*)
  132. (setf results (chanl:recv dead-channel))
  133. (format t "~4t~a died, ~:d MP3s, ~:d MP4s, ~:d FLACs, ~:d Others~%"
  134. (chanl-results-name results)
  135. (chanl-results-mp3-count results)
  136. (chanl-results-mp4-count results)
  137. (chanl-results-flac-count results)
  138. (chanl-results-other-count results))
  139. (force-output *standard-output*)
  140. (incf mp3-count (chanl-results-mp3-count results))
  141. (incf mp4-count (chanl-results-mp4-count results))
  142. (incf flac-count (chanl-results-flac-count results))
  143. (incf other-count (chanl-results-other-count results))
  144. (incf i)
  145. (when (= i *max-threads*)
  146. (return-from thread-reap *max-threads*)))))
  147. (format t "All threads done~%")
  148. (format t "~&~:d MP3s, ~:d MP4s, ~:d FLACS, ~:d Others, for a total of ~:d files~%"
  149. mp3-count mp4-count flac-count other-count
  150. (+ mp3-count mp4-count flac-count other-count)))))
  151. (defun mp-time-test (&key (dir "/home/markv/Music/Queen")
  152. (file-system-encoding :utf-8) (do-audio-processing t))
  153. "Time parsing of DIR."
  154. (set-pathname-encoding file-system-encoding)
  155. (let ((audio-streams:*get-audio-info* do-audio-processing))
  156. (time (mp-do-audio-dir :dir dir :file-system-encoding file-system-encoding :func nil))))