[CM] To define a specific treatement

Bill Schottstaedt bil@ccrma.Stanford.EDU
Sun, 15 Feb 2004 08:42:14 -0800


 > for instance take a sound file in input (for instance piano1.aiff,
 > with blank at the begining and at the end) and apply a pre-programed
 > treatement (for instance, trimming it or adding the right loop).
 > Then apply this threatement to the whole set.

To trim silence at the start:

(define (trim-mono-silence file)
   ;; in-place trim of opening silence
   (let* ((snd (open-sound file))
	 (music-location (find (lambda (y) (not (= y 0.0))))))
     (if (and music-location
	     (> (cadr music-location) 0))
	(begin
	  (delete-samples 0 (cadr music-location))
	  (save-sound snd)))
     (close-sound snd)))

If the files might have any number of channels:

(define (trim-silence file)
   ;; in-place trim of opening silence
   (let* ((snd (open-sound file))
	 (music-location #f))
     (do ((chn 0 (1+ chn)))
	((= chn (chans snd)))
       (let* ((location-info (find (lambda (y) (not (= y 0.0))) 0 snd chn))
	     (location (and location-info (cadr location-info))))
	(if (or (not music-location)
		(< location music-location))
	    (set! music-location location))))
     (if (and music-location
	     (> music-location 0))
	(begin
	  (do ((chn 0 (1+ chn)))
	      ((= chn (chans snd)))
	    (delete-samples 0 music-location snd chn))
	  (save-sound snd)))
     (close-sound snd)))


To run this over a bunch of sounds, use the "script" stuff
described in grfsnd.html:

#!snd -b
!#
(if (= (length (script-args)) 2)
   (display "usage: script file-name...\n")
   (do ((arg (+ (script-arg) 1) (1+ arg)))
       ((= arg (length (script-args))))
     (let ((name (list-ref (script-args) arg)))
       (trim-silence name))))
(exit)