Forráskód Böngészése

making audio-info routines more similar

Mark VandenBrink 12 éve
szülő
commit
6ceeaab2e0
7 módosított fájl, 69 hozzáadás és 49 törlés
  1. 11 9
      audio-streams.lisp
  2. 4 4
      mp3-tag.lisp
  3. 26 13
      mp4-atom.lisp
  4. 7 1
      mp4-tag.lisp
  5. 16 17
      mpeg.lisp
  6. 4 4
      packages.lisp
  7. 1 1
      taglib-tests.lisp

+ 11 - 9
audio-streams.lisp

@@ -17,10 +17,11 @@
 
 (defclass mp3-file-stream (base-file-stream)
   ((id3-header  :accessor id3-header)
-   (mpeg-info   :accessor mpeg-info :initform nil)))
+   (audio-info   :accessor audio-info :initform nil)))
 
 (defclass mp4-file-stream (base-file-stream)
-  ((mp4-atoms :accessor mp4-atoms :initform nil)))
+  ((mp4-atoms  :accessor mp4-atoms :initform nil)
+   (audio-info :accessor audio-info :initform nil)))
 
 (defun make-file-stream (class-name filename &key (read-only t))
   (let ((new-stream (make-instance (find-class class-name))))
@@ -280,30 +281,31 @@
 	(2 (stream-read-ucs-be-string instream))
 	(3 (stream-read-utf-8-string instream))))
 
-
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; FILES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-(defun parse-mp4-file (filename)
+(defun parse-mp4-file (filename &key (get-audio-info *get-audio-info*))
   (let (stream)
 	(handler-case
 		(progn
 		  (setf stream (make-file-stream 'mp4-file-stream filename))
-		  (mp4-atom:find-mp4-atoms stream))
+		  (mp4-atom:find-mp4-atoms stream)
+		  (when get-audio-info
+			(setf (audio-info stream) (mp4-atom:get-mp4-audio-info stream))))
 	  (mp4-atom:mp4-atom-condition (c)
 		(warn-user "make-mp4-stream got condition: ~a" c)
 		(when stream (stream-close stream))
 		(setf stream nil)))
 	stream))
 
-(defvar *get-mpeg-info* nil)
+(defvar *get-audio-info* nil)
 
-(defun parse-mp3-file (filename &key (get-mpeg-info *get-mpeg-info*))
+(defun parse-mp3-file (filename &key (get-audio-info *get-audio-info*))
   (let (stream)
 	  (handler-case
 		  (progn
 			(setf stream (make-file-stream 'mp3-file-stream filename))
 			(id3-frame:find-id3-frames stream)
-			(when get-mpeg-info
-			  (setf (mpeg-info stream) (mpeg:get-mpeg-info stream))))
+			(when get-audio-info
+			  (setf (audio-info stream) (mpeg:get-mpeg-audio-info stream))))
 		(id3-frame:id3-frame-condition (c)
 		  (warn-user "make-mp3-stream got condition: ~a" c)
 		  (when stream (stream-close stream))

+ 4 - 4
mp3-tag.lisp

@@ -348,8 +348,8 @@
   (if raw
 	  (format t "~a: ~a~%" (stream-filename me)
 			  (with-output-to-string (s)
-				(when (mpeg-info me)
-				  (vpprint (mpeg-info me) s)
+				(when (audio-info me)
+				  (mpeg::vpprint (audio-info me) s)
 				  (format s "~%"))
 				(vpprint (id3-header me) s)))
 	  (let ((album (album me))
@@ -372,8 +372,8 @@
 			(writer (writer me))
 			(year (year me)))
 		(format t "~a: ~a~%" (stream-filename me) 
-				(if (mpeg-info me)
-					(vpprint (mpeg-info me) nil) ""))
+				(if (audio-info me)
+					(mpeg::vpprint (audio-info me) nil) ""))
 		(when album (format t "~4talbum: ~a~%" album))
 		(when album-artist (format t "~4talbum-artist: ~a~%" album-artist))
 		(when artist (format t "~4tartist: ~a~%" artist))

+ 26 - 13
mp4-atom.lisp

@@ -598,20 +598,33 @@ return trak.mdia.mdhd and trak.mdia.minf.stbl.stsd"
 														(traverse track (list +mp4-atom-mdia+ +mp4-atom-minf+ +mp4-atom-stbl+ +audioprop-mp4a+ +audioprop-esds+)))))))
   nil)
 
-;;; song length is seconds is (float duration) / (float scale)
-(defun get-audio-properties (mp4-file)
-  (let ((time))
+(defclass audio-info ()
+  ((seconds         :accessor seconds :initform nil)
+   (channels        :accessor channels :initform nil)
+   (bits-per-sample :accessor bits-per-sample :initform nil)
+   (sample-rate     :accessor sample-rate :initform nil)
+   (max-bit-rate    :accessor max-bit-rate :initform nil)
+   (avg-bit-rate    :accessor avg-bit-rate :initform nil)))
+
+(defmethod vpprint ((me audio-info) stream)
+  (with-slots (seconds channels bits-per-sample sample-rate max-bit-rate avg-bit-rate) me
+	(format stream "sample rate: ~:d Hz, # channels: ~d, bits-per-sample: ~:d, max bit-rate: ~:d, avg bit-rate: ~:d, duration: ~:d:~2,'0d"
+			sample-rate channels bits-per-sample max-bit-rate avg-bit-rate
+			(floor (/ seconds 60)) (round (mod seconds 60)))))
+
+(defun get-mp4-audio-info (mp4-file)
+  (let ((info (make-instance 'audio-info)))
 	(multiple-value-bind (mdhd mp4a esds) (get-audio-properties-atoms mp4-file)
-	  (when mdhd
-		(setf time (/ (float (duration mdhd)) (float (scale mdhd))))
-		(format t "~a seconds~%" time))
+	  (with-slots (seconds channels bits-per-sample sample-rate max-bit-rate avg-bit-rate) info
+		(when mdhd
+		  (setf seconds (/ (float (duration mdhd)) (float (scale mdhd)))))
 		(when mp4a
-		  (format t "channels: ~d~%" (num-chans mp4a))
-		  (format t "bits/sample: ~:d~%" (samp-size mp4a))
+		  (setf channels (num-chans mp4a))
+		  (setf bits-per-sample (samp-size mp4a))
 		  (let* ((upper (ash (samp-rate mp4a) -16))
-				 (lower (logand (samp-rate mp4a) #xffff))
-				 (rate (+ (float upper) (/ (float lower) 1000))))
-		  (format t "sample rate: ~:d~%" rate)))
+				 (lower (logand (samp-rate mp4a) #xffff)))
+			(setf sample-rate (+ (float upper) (/ (float lower) 1000))))
 		(when esds
-		  (format t "average bit-rate = ~:d, max bit-rate = ~:d~%"
-				  (avg-bit-rate esds) (max-bit-rate esds))))))
+		  (setf avg-bit-rate (avg-bit-rate esds))
+		  (setf max-bit-rate (max-bit-rate esds))))))
+	info))

+ 7 - 1
mp4-tag.lisp

@@ -40,7 +40,10 @@
   "Show the tags for an MP4-FILE. If RAW is non-nil, dump the DATA atoms; else show subset of DATA atoms"
   (format t "~a~%" (stream-filename me))
   (if raw
-	  (mp4-atom:mp4-show-raw-tag-atoms me)
+	  (progn
+		(mp4-atom:mp4-show-raw-tag-atoms me)
+		(if (audio-info me)
+		  (mp4-atom:vpprint (audio-info me) t)))
 	  (let ((album (album me))
 			(album-artist (album-artist me))
 			(artist (artist me))
@@ -60,6 +63,9 @@
 			(track (track me))
 			(writer (writer me))
 			(year (year me)))
+
+		(if (audio-info me)
+		  (mp4-atom:vpprint (audio-info me) t))
 		(when album (format t "~4talbum: ~a~%" album))
 		(when album-artist (format t "~4talbum-artist: ~a~%" album-artist))
 		(when artist (format t "~4tartist: ~a~%" artist))

+ 16 - 17
mpeg.lisp

@@ -412,16 +412,15 @@
 		  (values t br len))
 		(values nil nil nil))))
 
-(defclass mpeg-info ()
-  ((is-vbr      :accessor is-vbr :initarg :is-vbr)
-   (bit-rate    :accessor bit-rate :initarg :bit-rate)
-   (sample-rate :accessor sample-rate :initarg :sample-rate)
-   (len         :accessor len :initarg :len)
-   (version     :accessor version :initarg :version)
-   (layer       :accessor layer :initarg :layer))
-  (:default-initargs :is-vbr nil :bit-rate nil :sample-rate nil :len nil :version nil :layer nil))
-
-(defmethod vpprint ((me mpeg-info) stream)
+(defclass mpeg-audio-info ()
+  ((is-vbr      :accessor is-vbr :initarg :is-vbr :initform nil)
+   (bit-rate    :accessor bit-rate :initarg :bit-rate :initform nil)
+   (sample-rate :accessor sample-rate :initarg :sample-rate :initform nil)
+   (len         :accessor len :initarg :len :initform nil)
+   (version     :accessor version :initarg :version :initform nil)
+   (layer       :accessor layer :initarg :layer :initform nil)))
+
+(defmethod vpprint ((me mpeg-audio-info) stream)
   (with-slots (is-vbr sample-rate  bit-rate len version layer) me
 	(format stream "~a, ~a, ~:[CBR,~;VBR,~] sample rate: ~:d Hz, bit rate: ~:d Kbps, duration: ~:d:~2,'0d"
 			(get-mpeg-version-string version)
@@ -430,17 +429,17 @@
 			sample-rate
 			(round (/ bit-rate 1000))
 			(floor (/ len 60)) (round (mod len 60)))))
-  
-(defun get-mpeg-info (in &key (max-frames nil))
+
+(defun get-mpeg-audio-info (in &key (max-frames nil))
   "Get MPEG Layer 3 audio information."
-  (log5:with-context "get-mpeg-info"
+  (log5:with-context "get-mpeg-audio-info"
 	(let ((pos (stream-seek in 0 :current))
 		  (first-frame (find-first-sync in))
-		  (info (make-instance 'mpeg-info)))
+		  (info (make-instance 'mpeg-audio-info)))
 
 	  (log-mpeg-frame "search for first frame yielded ~a" first-frame)
 	  (when (null first-frame)
-		(return-from get-mpeg-info nil))
+		(return-from get-mpeg-audio-info nil))
 
 	  (with-slots (is-vbr sample-rate bit-rate len version layer) info
 		(setf version (version first-frame))
@@ -472,8 +471,8 @@
 						  :read-payload nil :max max-frames)
 			  (if (or (< n-frames 10) (zerop bit-rate-total))
 				  (progn
-					(log-mpeg-frame "couldn't get mpeg-info: only got ~d frames" n-frames)
-					(return-from get-mpeg-info nil))
+					(log-mpeg-frame "couldn't get audio-info: only got ~d frames" n-frames)
+					(return-from get-mpeg-audio-info nil))
 				  (progn
 					(setf is-vbr vbr)
 					(setf len total-len)

+ 4 - 4
packages.lisp

@@ -11,9 +11,9 @@
   (:use #:common-lisp))
 
 (defpackage #:audio-streams
-  (:export #:octets #:make-octets *get-mpeg-info*
+  (:export #:octets #:make-octets *get-mpeg-audio-info*
 		   #:mp3-file-stream #:mp4-file-stream #:base-mem-stream
-		   #:id3-header #:mpeg-info #:mp4-atoms
+		   #:id3-header #:audio-info #:mp4-atoms
 		   #:parse-mp3-file #:parse-mp4-file
 		   #:make-mem-stream #:stream-filename
 		   #:stream-read-u8 #:stream-read-u16 #:stream-read-u24 #:stream-read-u32 #:stream-read-u64 #:stream-read-octets
@@ -31,7 +31,7 @@
   (:export #:mp4-atom #:map-mp4-atom #:find-mp4-atoms #:traverse #:mp4-atom-condition
 		   #:atom-file-position #:atom-children #:atom-size #:atom-of-interest #:atom-decoded
 		   #:atom-type #:vpprint #:*tag-path* #:tag-get-value #:mp4-atom-condition
-		   #:mp4-show-raw-tag-atoms
+		   #:mp4-show-raw-tag-atoms #:get-mp4-audio-info
 		   #:+itunes-album+
 		   #:+itunes-album-artist+
 		   #:+itunes-artist+
@@ -77,5 +77,5 @@
   (:use #:common-lisp #:utils))
 
 (defpackage #:mpeg
-  (:export #:get-mpeg-info #:vpprint)
+  (:export #:get-mpeg-audio-info #:vpprint)
   (:use #:common-lisp #:audio-streams #:utils))

+ 1 - 1
taglib-tests.lisp

@@ -53,7 +53,7 @@
 								 (let ((file (mp4-test0 f)))
 								   (when file 
 									 (mp4-tag:show-tags file :raw raw)
-									 (mp4-atom::get-audio-properties file)))))))
+									 (mp4-atom::get-audio-info file)))))))
 
 ;;;;;;;;;;;;;;;;;;;; MP3 Tests ;;;;;;;;;;;;;;;;;;;;
 (defun mp3-test0 (file)