[cl-wav-synth-devel] newbie question

Lui Fungsin fungsin.lui at gmail.com
Mon Oct 23 03:55:57 UTC 2006


On 10/9/06, Philippe Brochard <hocwp at free.fr> wrote:
> Here is how I write this (load it from slime or the clim repl):
>

Hi Philippe,

This works well for me. Thanks!

BTW, during the course of parsing the pronounciation files I have, I
enhance the wav header parsing method a bit to skip other misc header
fields.

With this patch I'm able to read all of the 10000+ wav samples I have.

Attached is the diff.

-- fungsin
-------------- next part --------------
Index: cl-wav-synth.lisp
===================================================================
--- cl-wav-synth.lisp	(revision 910)
+++ cl-wav-synth.lisp	(working copy)
@@ -353,9 +353,10 @@
 
 (defgeneric read-header (filename header))
 (defmethod read-header (filename (header header))
+  "Read wav header info. See http://www.sonicspot.com/guide/wavefiles.html"
   (labels ((expected (read-str orig-str)
 	     (assert (string= read-str orig-str) ()
-		     "error reading header: ~S is not a wav file" filename)))
+		     "error reading header: ~S is not a wav file. Expected ~A Got ~A" filename orig-str read-str)))
     (with-slots (n-samples-per-sec
 		 n-channels n-bits-per-sample
 		 n-block-align n-avg-bytes-per-sec
@@ -365,16 +366,25 @@
 	(expected (read-id stream 4) "RIFF")
 	(read-32 stream)
 	(expected (read-id stream  4) "WAVE")
-	(expected (read-id stream 4) "fmt ")
-	(read-32 stream)
-	(read-16 stream)
-	(setf n-channels (read-16 stream))
-	(setf n-samples-per-sec (read-32 stream))
-	(setf n-avg-bytes-per-sec (read-32 stream))
-	(setf n-block-align (read-16 stream))
-	(setf n-bits-per-sample (read-16 stream))
-	(expected (read-id stream 4) "data")
-	(setf total-byte (read-32 stream)))))
+        (loop
+         (let* ((next-header (read-id stream 4))
+                (bytes (read-32 stream)))
+           (cond ((string= next-header "fmt ")
+                  (read-16 stream) ;; compression code
+                  (setf n-channels (read-16 stream)) 
+                  (setf n-samples-per-sec (read-32 stream))
+                  (setf n-avg-bytes-per-sec (read-32 stream))
+                  (setf n-block-align (read-16 stream))
+                  (setf n-bits-per-sample (read-16 stream))
+                  ;; possible extra format bytes
+                  (dotimes (i (- bytes 16)) (read-byte stream)))
+                 ((string= next-header "data")
+                  (setf total-byte bytes)
+                  (return))
+                 (t
+                  ;; There're a lot of headers that we don't
+                  ;; care. For instance, bext minf elmo, etc
+                  (dotimes (i bytes) (read-byte stream)))))))))
   header)
 
 (defgeneric print-header (header &optional comment))


More information about the Cl-wav-synth-devel mailing list