taglib-tests.lisp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. (defstruct file-counts
  32. (mp3-count 0 :type fixnum)
  33. (flac-count 0 :type fixnum)
  34. (mp4-count 0 :type fixnum)
  35. (other-count 0 :type fixnum))
  36. (defmethod print-object ((me file-counts) stream)
  37. (with-slots (mp3-count flac-count mp4-count other-count) me
  38. (format stream
  39. "~&~:d MP3s, ~:d MP4s, ~:d FLACs, ~:d Others, for a total of ~:d files~%"
  40. mp3-count mp4-count flac-count other-count
  41. (+ mp3-count mp4-count flac-count other-count))))
  42. (defun do-audio-dir (&key (dir "/home/markv/Music/Queen")
  43. (file-system-encoding :utf-8)
  44. (func #'abstract-tag:show-tags))
  45. "Walk :DIR and FUNCALL specified function for each file audio found."
  46. (set-pathname-encoding file-system-encoding)
  47. (let ((file-counts (make-file-counts)))
  48. (with-slots (mp3-count flac-count mp4-count other-count) file-counts
  49. (cl-fad:walk-directory dir
  50. (lambda (f)
  51. (do-audio-file :file f
  52. :func (lambda (s)
  53. (cond ((typep s 'id3:mp3-file)
  54. (incf mp3-count))
  55. ((typep s 'flac:flac-file)
  56. (incf flac-count))
  57. ((typep s 'm4a:mp4-file)
  58. (incf mp4-count))
  59. ((null s)
  60. (incf other-count)))
  61. (when (and (not (null s)) func)
  62. (funcall func s)))))))
  63. file-counts))
  64. (defun time-test (&key (dir "/home/markv/Music/Queen")
  65. (file-system-encoding :utf-8) (do-audio-processing t))
  66. "Time parsing of DIR."
  67. (set-pathname-encoding file-system-encoding)
  68. (let ((audio-streams:*get-audio-info* do-audio-processing))
  69. (time (format t "~a~%"
  70. (do-audio-dir :dir dir
  71. :file-system-encoding file-system-encoding :func nil)))))
  72. (defun insert-into-ht (id ht)
  73. (multiple-value-bind (value foundp)
  74. (gethash id ht)
  75. (setf (gethash id ht)
  76. (if foundp
  77. (1+ value)
  78. 1))))
  79. (defun get-stats (&key (dir "/home/markv/Music/Queen")
  80. (file-system-encoding :utf-8))
  81. "Gen up some interesting statistics on DIR"
  82. (let ((m4-ht (make-hash-table :test #'equalp))
  83. (m3-ht (make-hash-table :test #'equalp)))
  84. (do-audio-dir
  85. :dir dir
  86. :file-system-encoding file-system-encoding
  87. :func (lambda (s)
  88. (cond ((typep s 'id3:mp3-file)
  89. (id3:map-id3-frames s
  90. :func (lambda (f)
  91. (insert-into-ht (id3:id f) m3-ht))))
  92. ((typep s 'm4a:mp4-file)
  93. (m4a:map-mp4-atoms s :func (lambda (f)
  94. (insert-into-ht (m4a:atom-type f) m4-ht)))))))
  95. (format t "MP3 Stats:~%")
  96. (loop for key being the hash-keys of m3-ht
  97. using (hash-value value)
  98. do (format t "~2t~a:~:d~%" key value))
  99. (format t "~%M4A Stats:~%")
  100. (loop for key being the hash-keys of m4-ht
  101. using (hash-value value)
  102. do (format t "~2t~a:~:d~%" key value))
  103. (values m3-ht m4-ht)))
  104. ;;;; multi-thread code below
  105. #+(or :ccl :sbcl :abcl)
  106. (progn
  107. (defparameter *end-thread* #xdeadbeef)
  108. (defparameter *max-threads* 4)
  109. ;;; Simple structure to hold a thread's results
  110. (defstruct chanl-results
  111. name
  112. mp3-count
  113. flac-count
  114. mp4-count
  115. other-count)
  116. (defun mp-do-audio-dir (&key (dir "/home/markv/Music/Queen")
  117. (file-system-encoding :utf-8)
  118. (func nil))
  119. "Walk :DIR and FUNCALL specified function for each file audio found."
  120. (set-pathname-encoding file-system-encoding)
  121. (let ((channel (make-instance 'chanl:unbounded-channel))
  122. (dead-channel (make-instance 'chanl:unbounded-channel))
  123. (mp3-count 0)
  124. (flac-count 0)
  125. (mp4-count 0)
  126. (other-count 0))
  127. ;; This function is run by each thread
  128. ;; Thread sits in a loop, reading from CHAN. If that read
  129. ;; returns the integer *END-THREAD*, then thread exits; otherwise,
  130. ;; it runs DO-AUDIO-FILE on the file passed in.
  131. (labels ((thread-reader ()
  132. (declare (special *me*))
  133. (let ((f)
  134. (results (make-chanl-results :name *me* :mp3-count 0
  135. :flac-count 0 :mp4-count 0
  136. :other-count 0)))
  137. (loop
  138. (with-slots (name mp3-count mp4-count flac-count other-count) results
  139. (setf f (chanl:recv channel))
  140. (when (and (typep f 'integer)
  141. (= f *end-thread*))
  142. (chanl:send dead-channel results)
  143. (return-from thread-reader nil))
  144. (do-audio-file :file f
  145. :func (lambda (s)
  146. (cond ((typep s 'id3:mp3-file)
  147. (incf mp3-count))
  148. ((typep s 'flac:flac-file)
  149. (incf flac-count))
  150. ((typep s 'm4a:mp4-file)
  151. (incf mp4-count))
  152. ((null s)
  153. (incf other-count)))
  154. (when (and (not (null s)) func)
  155. (funcall func s)))))))))
  156. ;; first, add all files in DIR to CHANNEL
  157. (cl-fad:walk-directory dir (lambda (f) (chanl:send channel f)))
  158. ;; At this point, CHANNEL is stuffed with files.
  159. ;; Now, send *MAX-THREADS* "ends" (at end of CHANNEL) and
  160. ;; spawn *MAX-THREADS* threads
  161. (dotimes (i *max-threads*)
  162. (chanl:send channel *end-thread*))
  163. (dotimes (i *max-threads*)
  164. (chanl:pcall
  165. #'thread-reader
  166. :initial-bindings `((*me* ,(format nil "reader-thread-~d" i)))))
  167. ;; sit in loop until we read *MAX-THREADS* results
  168. (block thread-reap
  169. (let ((i 0)
  170. results)
  171. (format t "Waiting on ~d threads~%" *max-threads*)
  172. (loop
  173. (force-output *standard-output*)
  174. (setf results (chanl:recv dead-channel))
  175. (format t "~4t~a died, ~:d MP3s, ~:d MP4s, ~:d FLACs, ~:d Others~%"
  176. (chanl-results-name results)
  177. (chanl-results-mp3-count results)
  178. (chanl-results-mp4-count results)
  179. (chanl-results-flac-count results)
  180. (chanl-results-other-count results))
  181. (force-output *standard-output*)
  182. (incf mp3-count (chanl-results-mp3-count results))
  183. (incf mp4-count (chanl-results-mp4-count results))
  184. (incf flac-count (chanl-results-flac-count results))
  185. (incf other-count (chanl-results-other-count results))
  186. (incf i)
  187. (when (= i *max-threads*)
  188. (return-from thread-reap *max-threads*)))))
  189. (format t "All threads done~%")
  190. (format t "~&~:d MP3s, ~:d MP4s, ~:d FLACS, ~:d Others, for a total of ~:d files~%"
  191. mp3-count mp4-count flac-count other-count
  192. (+ mp3-count mp4-count flac-count other-count)))))
  193. (defun mp-time-test (&key (dir "/home/markv/Music/Queen")
  194. (file-system-encoding :utf-8) (do-audio-processing t))
  195. "Time parsing of DIR."
  196. (set-pathname-encoding file-system-encoding)
  197. (let ((audio-streams:*get-audio-info* do-audio-processing))
  198. (time (mp-do-audio-dir :dir dir :file-system-encoding file-system-encoding :func nil))))
  199. )