[CM] snd question - new format

Bill Schottstaedt bil@ccrma.Stanford.EDU
Sun, 14 Jul 2002 04:22:30 -0700


> How do I determine whether I'm reading or writing from in the hook?

it's reading.

> Also, it appears to make a temporary .raw file on disk... does it have to?

yes.  Here is code to read/write OGG files:

(use-modules (ice-9 format))

(define (read-ogg filename)
  ;; check for "OggS" first word, if found, translate to something Snd can read
  (if (call-with-input-file filename
	(lambda (fd)
	  (and (char=? (read-char fd) #\O)
	       (char=? (read-char fd) #\g)
	       (char=? (read-char fd) #\g)
	       (char=? (read-char fd) #\S))))
      (let ((aufile (string-append filename ".au")))
	(if (file-exists? aufile) (delete-file aufile))
	(system (format #f "ogg123 -d au -f ~A ~A" aufile filename))
	aufile)
      #f))

(add-hook! open-hook
           (lambda (filename)
             (if (= (mus-sound-header-type filename) mus-raw)
                 (read-ogg filename)
		 #f)))

(define (write-ogg snd)
  ;; write snd data in OGG format
  (if (or (> (car (edits snd)) 0)
	  (not (= (header-type snd) mus-riff)))
      (let ((file (string-append (file-name snd) ".tmp")))
	(save-sound-as file snd mus-riff)
	(system (format #f "oggenc ~A" file))
	(delete-file file))
      (system (format #f "oggenc ~A" (file-name snd)))))