Snd Customization and Extension Part 2


related documentationsnd.html extsnd.html clm.html sndlib.html sndscm.html libxm.html index.html

Snd Startup


Snd resources

In the Motif version, there are a few X-style resources that Snd looks for (see Snd.ad); each has a built-in default value, so you can ignore any that are already acceptable:

initFile "~/.snd"
epsFile"snd.eps"
overwriteCheck 0
autoResize 1
horizontalPanes 0


buttonFont -*-times-medium-r-*-*-14-*-*-*-*-*-iso8859-1
boldbuttonFont -*-times-bold-r-*-*-14-*-*-*-*-*-iso8859-1
peaksFont -*-times-medium-r-*-*-14-*-*-*-*-*-iso8859-1
boldpeaksFont -*-times-bold-r-*-*-14-*-*-*-*-*-iso8859-1
axisLabelFont -*-times-medium-r-normal-*-20-*-*-*-*-*-iso8859-1
axisNumbersFont -*-courier-medium-r-normal-*-14-*-*-*-*-*-iso8859-1
helpTextFont 9x15
listenerFont default


useSchemes none
highlightcolor ivory1
basiccolor ivory2
positioncolor ivory3
zoomcolor ivory4
cursorcolor red
selectioncolor lightsteelblue1
mixcolor lightgreen
mixfocuscolor yellow2
listenercolor aliceblue
envedwaveformcolor blue
filterwaveformcolor blue
mixwaveformcolor darkgray
graphcolor white
selectedgraphcolor white
datacolor black
selecteddatacolor black
markcolor red
pushedbuttoncolor lightsteelblue1
sashcolor lightgreen

You can experiment with other choices by using the -xrm switch:

  snd -xrm '*Highlightcolor: Red' oboe.snd
  snd -xrm '*AxisNumbersFont: 6x10' oboe.snd
  snd -xrm '*overwriteCheck: 1' oboe.snd
  snd -xrm '*useSchemes: all' -xrm '*scheme: Pacific'
  snd -xrm '*fontList: 9x15' oboe.snd
  snd -xrm '*listenerFont: 6x10' oboe.snd
  snd -xrm '*mixwaveformcolor: red' oboe.snd -notebook
  snd oboe.snd pistol.snd -xrm '*selectedgraphcolor: black' -xrm '*selecteddatacolor: white'
  snd oboe.snd -title hiho -display hummer.hiho:0.0 -xrm '*chn-graph*backgroundPixmap: text.xpm'

Perhaps this is most useful when your window manager assumes white text for labels (menus); since Snd defaults to an off-white background, this makes everything unreadable. You can get the black text back with:

./snd -xrm '*foreground: Black'

or put the equivalent line in your .Xdefaults file. The listener text color can be set via (set! (listener-text-color) (make-color 0 0 0)) in ~/.snd.

The colors are defined in rgb.scm. "useSchemes" refers to SGI color schemes. It's unfortunate that there is the language Scheme implemented by Guile, and the notion of an SGI color scheme -- there is no connection between the two. Other systems (Linux, Solaris) use the term "theme" instead. You can get Snd to honor your current window manager theme by setting useSchemes to all. The last example above sets the window title to "hiho", rather than "snd", displays the window on the machine hummer.hiho (presumably accessible over the net), and tiles the graph backgrounds with the contents of text.xpm. To get the -geometry argument to work, set the autoResize resource to 0:

  snd oboe.snd -geometry 800x200 -xrm '*autoResize: 0'

These resources can be set in Snd.ad or in your .Xdefaults file:

snd*buttonFont:         -adobe-times-medium-r-*-*-14-*-*-*-*-*-*-*
snd*boldbuttonFont:     -adobe-times-bold-r-*-*-14-*-*-*-*-*-*-*
snd*axisLabelFont:      -adobe-times-medium-r-normal-*-18-*-*-*-*-*-*-*
snd*axisNumbersFont:    9x15
snd*fontList:           9x15
snd*helpTextFont:       9x15

The autoResize resource determines how Snd acts when files are added to or removed from its overall display. The default (1) causes Snd to expand or contract the main window's size to accommodate the sounds; many people find this distracting. If autoResize is 0, the outer window size remains the same, and the sounds try to fit as best they can (to some extent the window manager controls this stuff). See also the variable auto-resize. If overwriteCheck is 1, Snd asks before overwriting existing files. The horizontalPanes resource is equivalent to the -h flag; if 1, sounds are layed out horizontally rather than vertically; if 2, you get a notebook widget holding the sounds.

In Gtk2, the (pango) font defaults are:

tiny-font         "Monospace 8"
button-font       "Serif 14"
bold-button-font  "Serif Bold 14"
axis-numbers-font "Monospace 10"
axis-label-font   "Serif 14"
help-text-font    "Monospace 14"

The various color resources are:

basiccolordefault background color everywherebasic-color
cursorcolor color of the cursor cursor-color
datacolor unselected data color data-color
envedwaveformcolor color of envelope editor waveform enved-waveform-color
filterwaveformcolor color of control panel filter waveformfilter-waveform-color
graphcolor unselected channels' graph background graph-color
highlightcolor highlighting here and there highlight-color
listenercolor background color of the listener listener-color
listenertextcolor text color in the listener listener-text-color
markcolor color of the mark indicator mark-color
mixcolor used for mix waveforms mix-color
selectedmixcolor selected mix waveforms selected-mix-color
positioncolor color of position sliders position-color
pushedbuttoncolor color of pushed button pushed-button-color
sashcolor color of paned window sash handles sash-color
selecteddatacolor color of the data in selected channel selected-data-color
selectedgraphcolor background of selected channel's graphselected-graph-color
selectioncolor color of an active selection selection-color
textfocuscolor color of text field with focus text-focus-color
zoomcolor color of zoom sliders zoom-color

Each of these colors can be set in Guile using the second name given above ("basic-color"). Colors are defined by make-color with the three red/green/blue values, each a float between 0.0 and 1.0. (set! (basic-color) (make-color 1.0 0.0 0.0)) sets the overall background color of Snd to red. rgb.scm defines all the standard X11 color names (you probably don't want to load the whole thing; just use the names as needed).

There are several other resources that set widget sizes: zoomSliderWidth, positionSliderWidth, toggleSize, sashSize, sashIndent, channelSashSize, channelSashIndent, and envedPointSize. And several more color resources: whitecolor (list background), blackcolor (recorder VU meter text), redcolor (buttons, VU clipping, etc), greencolor (a few buttons), yellowcolor (a few envelope editor buttons), lightbluecolor (the recorder), and lighterbluecolor (the fft option panel).

In the GTK version, you can load a gtkrc file, overriding all Snd defaults, with parse-rc-file:

  (parse-rc-file "/home/bil/test/gtk+-1.2.9/gtk/testgtkrc")

I've never used this. If it's broken, please let me know!


Snd invocation flags

Snd recognizes the following switches in its command line (leaving aside all the usual Xt/X-related flags like -xrm).

-h -horizontallayout sounds as horizontal panes
-v -vertical layout sounds vertically (the default)
-notebook layout sounds in a notebook widget (Motif 2.0 or later)
-separate layout sounds each in a separate window (lisp listener in main window)
--help print some help, version info, and exit
--version print version info
-noglob don't read /etc/snd.conf
-noinit don't read ~/.snd
-nostdin don't watch for possible input from stdin
-p -preload <dir> preload sound files in directory <dir> (snd -p .)
-l -load <file> load Scheme or Ruby code in <file> (snd -l test.scm)
-e -eval expr evaluate expr
-b -batch <file> load Scheme or Ruby code in <file> as a batch (no GUI) job
-I <dir> add <dir> to the load search list

The -e switch evaluates its argument as though it had been passed to M-X. The initialization file, if any, is loaded first, then the arguments are processed in order:

snd -e "(set! (data-color) (make-color 1 0 0))" oboe.snd

reads ~/.snd, if any, then sets the (unselected) data color to red, then opens oboe.snd.

./snd -eval '(begin (display (+ 1 2)) (exit))'

prints "3" and exits. The "-title" argument works in both versions of Snd. The following adds "WAV" to the sound file extension table before preloading the directory:

snd -e '(add-sound-file-extension "WAV")' -p /home/bil/sounds

notebookcolors

The initialization file

When Snd starts, it looks for an initialization file, normally named "~/.snd" (its name can be set via the X resource mechanism, or through the environment variable SND_INIT_FILE). This optional file is supposed to be just like Emacs' .emacs file, containing any customizations or extensions that you want loaded whenever Snd starts. Say we want the Snd window to start out 800x500, want to predefine an envelope named "env1", and want the file selection box to show just sound files. We make ~/.snd and put in it:

(set! (window-width) 800)
(set! (window-height) 500)
(defvar env1 '(0 0 1 1 2 0))
(set! (just-sounds) #t)

In more complex situations, you may want an initialization file particular to a given machine, and global across users; the name of this optional global initialization file is "/etc/snd.conf". It is read before your local file; both can, of course, be absent. To override reading the global init file when Snd is invoked, include the switch -noglob. To override the local init file, use -noinit. Here's a more elaborate initialization file:

(use-modules (ice-9 debug) (ice-9 format) (ice-9 optargs))
(debug-set! stack 0)                ;turn off Guile's stack-size check
(set! snd-remember-paths #t)        ;remember paths making "load" smarter

(set! (window-width) 800)           ;these set the initial window size
(set! (window-height) 500)

(if (provided? 'snd-motif)          ;if using Motif, set some fonts
    (begin
      (set! (button-font) "-adobe-times-medium-r-*-*-14-*-*-*-*-*-*-*")
      (set! (bold-button-font) "-adobe-times-bold-r-*-*-14-*-*-*-*-*-*-*")
      (set! (axis-label-font) "-adobe-times-medium-r-normal-*-18-*-*-*-*-*-*-*")
      (set! (axis-numbers-font) "9x15")))

(set! (listener-prompt) ":")        ;change listener prompt from the default ">" to ":"
(set! (show-listener) #t)           ;include the listener window initially
(set! (show-indices) #t)            ;include sound index values with the sound name

(define beige (make-color 0.96 0.96 0.86))
(define blue (make-color 0 0 1))
(set! (selected-graph-color) beige) ;selected graph background is beige
(set! (selected-data-color) blue)   ;selected graph data is blue

(add-hook! mouse-enter-graph-hook   ;automatically focus on (activate) the widget under the mouse
  (lambda (snd chn) 
    (focus-widget (car (channel-widgets snd chn)))))
(add-hook! mouse-enter-listener-hook 
  (lambda (widget) 
    (focus-widget widget)))
(add-hook! mouse-enter-text-hook
  (lambda (w)
    (focus-widget w)))

(set! (save-dir) "/zap/snd")        ;save-state files are placed in /zap/snd
(set! (temp-dir) "/zap/tmp")        ;temp files are placed in /zap/tmp
(load "peak-env.scm")               ;large file peak env data is saved for faster 
				    ;  subsequent reads -- this makes the first
				    ;  view of a large file much snappier --
				    ;  its default directory for the data is ~/peaks.

(load "draw.scm")                   ;load some useful extensions
(load "hooks.scm")
(load "extensions.scm")

(make-current-window-display)       ;display an overview of the current window in the upper right
(if (provided? 'snd-motif)          
    (load "popup.scm")              ;context-sensitive popup menus
    (if (provided? 'snd-gtk)
        (load "/home/bil/cl/gtk-popup.scm")))

(add-hook! after-open-hook          ;if sound has many chans, use just one pane for all
  (lambda (snd)
    (if (> (chans snd) 4)
        (set! (channel-style snd) channels-combined))))

(set! (optimization) 6)             ;turn on full optimization (live dangerously...)
(set! (selection-creates-region) #f) ;turn off automatic region creation

If you loaded Snd with GSL, and have set the GSL_IEEE_MODE environment variable, it will override Snd's default arithmetic mode settings. GSL recommends the setting:

GSL_IEEE_MODE=double-precision,mask-underflow,mask-denormalized

Runtime modules and external programs

It is possible to load your own C code into Snd at run-time or use any external program from within Snd as an editing function. And, perhaps most useful, you can run Snd as an Emacs subjob.


Snd as an Emacs subjob

Snd watches stdin; any input received is evaluated as if typed in Snd's lisp listener; any subsequent output is sent to stdout; presumably any process could communicate with Snd in this manner. But the intention here is to connect to Emacs via ILISP (available at http://sourceforge.net/projects/ilisp, and possibly built into Xemacs). Once you have ILISP, put this code in your .emacs file:

(require 'ilisp)
(defdialect snd "Snd" ilisp
  (setq ilisp-block-command "(begin \n%s)")
  (setq ilisp-load-command "(load \"%s\")")
  (setq ilisp-init-hook '((lambda () (ilisp-init nil nil nil))))
  (setq comint-prompt-regexp "^>+")
  (setq ilisp-directory-command "(getcwd)")
  (setq	ilisp-set-directory-command "(chdir \"%s\")")
  (setq	ilisp-complete-command
	"(map (lambda (sym) 
                (list (symbol->string sym))) 
              (apropos-internal \"^%s\"))")
  (local-set-key "\C-x\C-h" '(lambda (s)
			       (interactive "sSnd help: ")
			       (insert "(snd-help " s ")")
			       (comint-send-input))))

  (setq snd-program "snd")

You can bind this to some key via:

  (global-set-key "\C-x\C-l" 'snd)

Now C-x C-l in Emacs starts Snd as a subjob; anything you type in the Emacs *snd* buffer is sent to Snd (very much as if you were running CLM as an Emacs subjob), and Snd's output is appended to the *snd* buffer. Also, C-x C-h prompts for some Snd entity and appends the help text for that entity in the snd buffer. The following code tries to implement Snd's name completion upon C-x C-i in the *snd* buffer:

(defun handle-snd-completion (process string)
  (let ((buffer (process-buffer process)))
    (save-excursion
      (set-buffer buffer)
      (goto-char (point-max))
      (insert (substring string 1 (- (length string) 3))))
    (goto-char (point-max))
    (display-buffer buffer)))
      
(defun snd-complete-symbol ()
  (interactive)
  (let* ((end (point))
	 (start (save-excursion (skip-syntax-backward "w_") (point)))
	 (pattern (buffer-substring-no-properties start end))
	 (proc (get-buffer-process (current-buffer)))
	 (old-filter (process-filter proc)))
    (delete-region start end)
    (set-process-filter proc 'handle-snd-completion)
    (process-send-string proc (format "(snd-completion \"%s\")\n" pattern))
    (accept-process-output proc)
    (set-process-filter proc old-filter)))

and add this to the defdialect block:

  (local-set-key "\C-x\C-i" 'snd-complete-symbol)

But for some reason it needs an open-paren? snd-program is the name of the snd image; you can include whatever startup switches you like:

(setq snd-program "snd -horizontal ~/cl/oboe.snd")

Fernando Lopez-Lezcano's DotEmacs file shows another way to set up the emacs-snd connection:

       (defdialect snd "Snd" guile
         (setq comint-prompt-regexp "^>+")
         (setq ilisp-complete-command nil)
         ;; try to autodetect the binary
         (unless snd-program
           (cond
            ((file-executable-p "/usr/bin/snd")
             (setq snd-program "/usr/bin/snd"))
            (t (setq snd-program "snd")))))

       (add-hook 'snd-hook
                 (lambda ()

                   ))
       ;; if you have the binary somewhere else:
       ;; (setq snd-program "snd")

Another, much fancier possibility is Michael Scholz's inf-snd.el.


Dynamically loaded modules

You can import shared object files into Snd at any time. You need to build Snd with -lguile (that is, load it with the guile shared library, not libguile.a); if the loader can't find libguile.so.2 (or whatever), add its directory to your LD_LIBRARY_PATH; for example, if it's on /usr/local/lib, setenv LD_LIBRARY_PATH /usr/local/lib. Next add Guile wrappers to your C code:

  /* cscm.c */
  #include <math.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <guile/gh.h>

  int hiho (int a) 
  { /* this is the function we want to call from Snd */
    return(1 + a);
  }

  SCM hiho_wrapper(SCM a) 
  { /* this tells Guile how to interpret the arguments and return value of hiho */
    return(scm_long2num(hiho(scm_num2int(a, 0, "hiho"))));
  }

  void init_hiho() 
  { /* this declares hiho within Guile calling the wrapper which calls the C function hiho */
    scm_c_define_gsubr("hiho", 1, 0, 0, hiho_wrapper);
  }

Next compile your code into a shared object (this is aimed at Linux):

  cc -c cscm.c 
  ld -shared -o cscm.so cscm.o -lguile

Now go to Snd's lisp listener and,

  (define lib (dynamic-link "/home/bil/cl/cscm.so"))
  (dynamic-call "init_hiho" lib)
  (hiho 3)

The function we actually want loaded into Guile here is "hiho". We define a wrapper for it to handle the translation between Guile (Scheme) variable types and C ("hiho_wrapper"), and a procedure to define hiho in Guile ("init_hiho"). Once loaded ("dynamic-link"), we can call the initialization function ("dynamic-call"), and thereafter treat "hiho" as though it had been defined in Guile/Snd to begin with. After both the dynamic-link and dynamic-lib calls, the listener will print "#<unspecified>" or something equally obscure to indicate in its own peculiar way that all went well. M-x (hiho 4) will print 5 in the minibuffer.

As a slightly more useful example, let's import the Bessel J0 function from GSL (Gnu Scientific Library); in this case, we need to build Snd with GSL (the easiest way is to include the --with-gsl option to configure). Then make a file (say "gsl-ex.c"):

#include <guile/gh.h>
#include <gsl/gsl_sf_bessel.h>

static SCM scm_j0(SCM x)
{ /* calls GSL function gsl_sf_bessel_J0_e */
  gsl_sf_result res;
  gsl_sf_bessel_J0_e(scm_num2dbl(x, "j0"), &res);
  return(scm_make_real(res.val));
}

void init_gsl_j0(void)
{ /* links scm_j0 into Snd under the name j0 */
  scm_c_define_gsubr("j0", 1, 0, 0, scm_j0);
}

Now the usual compile, load, link into Snd sequence:

/home/bil/snd-4/ cc gsl-ex.c -c -Wall
/home/bil/snd-4/ ld -shared gsl-ex.o -o gsl-ex.so -lguile
/home/bil/snd-4/ ./snd

>(define lib (dynamic-link "/home/bil/snd-4/gsl-ex.so"))
#<unspecified>
>(dynamic-call "init_gsl_j0" lib)
#<unspecified>
>(j0 0.0)
1.0
>(j0 2.0)
0.223890779141236
>(define (bes-fm dur freq amp ratio index)
 ;; bessel-FM from CLM
   (let* ((car-ph 0.0)
	  (mod-ph 0.0)
 	  (car-incr (hz->radians freq))
	  (mod-incr (* ratio car-incr))
	  (ampenv (make-env :envelope '(0 0 25 1 75 1 100 0) :scaler amp :end dur))
	  (output (make-vct dur)))
     (vct-map! output
       (lambda ()
         (let ((val (* (env ampenv) (j0 car-ph))))
           (set! car-ph (+ car-ph car-incr (* index (j0 mod-ph))))
           (set! mod-ph (+ mod-ph mod-incr))
	   val)))
     (vct->samples 0 dur output)))
#<unspecified>
>(bes-fm 22050 440 10.0 1.0 8.0)

To call internal Snd functions, you can do something like the following: declare an SCM variable to hold the procedure variable, in the init function set the variable to the value of scm_symbol_value0("function-name"), and in the rest of the code call it via scm_apply. The following is a sketch using the Snd internal "srate" function:

  #include <guile/gh.h>
  static SCM g_srate;
  static SCM srate_wrapper(SCM a) {return(scm_call_1(g_srate, a));}
  void init_srate(void) 
  { 
    scm_c_define_gsubr("my-srate", 1, 0, 0, srate_wrapper);
    g_srate = scm_symbol_value0("srate");
  }

Alternatively, you can simply use scm_eval_str0:

  scm_eval_str0("(recorder-dialog)");
  scm_eval_str0("(open-sound \"oboe.snd\")");
  srate = scm_num2int(scm_eval_str0("(srate)"), 0, "");

External Programs

Any external program that knows about sound files can be used to perform editing operations in Snd. You get Snd's display, analysis, header and format conversion, and edit-tree support, and can concentrate on the actual sound effect you're developing. The original impetus for Snd came from CLM, a large lisp-listener based program which normally runs without a graphical user interface, and without any simple way to move around in what Snd calls the edit history. Since interprocess communication proved problematic in this case, the communication path was simplified to consist of little more than shared files, with CLM treated as a batch program.

Say we have a sound processing CLM instrument we like; it takes two sound file names as its arguments, reading the first and writing the second. In Snd we write the current edited state to a temporary file (save-sound-as), start CLM, call the instrument passing it the input filename (just written by Snd), then pass CLM's output back to Snd. Snd replaces (via set-samples) the current data with the data our instrument wrote, as if it had incorporated that instrument as an editing operation from the beginning. We then delete the Snd output (the input to CLM).


Snd as a Widget

To include the entire Snd editor as a widget in some other program, first compile it with -DSND_AS_WIDGET. Then load it into your program, using the procedure snd_as_widget to fire it up. See saw.c included with Snd.

  void snd_as_widget(int argc, char **argv, XtAppContext app, Widget parent, Arg *caller_args, int caller_argn)

starts up the Snd editor in the widget parent, passing the outer Snd form widget the arguments caller_args and caller_argn. The enclosing application context is app. parent needs to be realized at the time of the call, since Snd uses it to set up graphics contexts and so on. argc and argv can be passed to simulate a shell invocation of Snd. Remember that in this case, the first string argument is expected to be the application name, and is ignored by Snd. In Gtk, the arguments are different, but the basic idea is the same.


Snd and the CLM module

The files clm.c, clm.h, and clm2xen.c implement CLM (a Common Lisp Music V implementation described in clm.html, available in clm-2.tar.gz at ccrma-ftp) as a Guile-loadable module. They are loaded into Snd when it is built. You can see what a generator does, or a group of generators, by running them in the lisp listener, and using the graph and spectrum functions. Say we have these declarations in ~/.snd:

(define data-size 1024)
(define data (make-vct data-size))

(define run 
  (lambda (fun)
    (do ((i 0 (1+ i))) 
        ((= i data-size))
      (vct-set! data i (fun)))
    (graph data)))

(define runf
  (lambda (fun)
    (do ((i 0 (1+ i))) 
        ((= i data-size))
      (vct-set! data i (fun)))
    (graph (snd-spectrum data blackman2-window data-size #t))))

Now we can open the listener, and type:

(define hi (make-oscil))
(run (lambda () (oscil hi)))
(define ho (make-oscil))
(runf (lambda () (oscil hi (* .5 (oscil ho)))))

Obviously, any CLM instrument or function can be used in this way to edit sounds. Say we want an echo effect:

(define echo 
  (lambda (scaler secs)
    (let ((del (make-delay (round (* secs (srate))))))
      (lambda (inval)
        (+ inval (delay del (* scaler (+ (tap del) inval))))))))

Here scaler sets how loud subsequent echos are, and secs sets how far apart they are in seconds. echo uses the secs argument to create a delay line (make-delay) using the current sound's sampling rate to turn the secs parameter into samples. echo then returns a closure, that is, a function with associated variables (in this case del and scaler); the returned function (the second lambda) takes one argument (inval) and returns the result of passing that value to the delay with scaling. The upshot of all this is that we can use:

(map-chan (echo .5 .75) 0 44100)

to take the current active channel and return 44100 samples of echos, each echo half the amplitude of the previous, and spaced by .75 seconds. map-chan's first argument is a function of one argument, the current sample; when we pass it (echo ...), it evaluates the echo call, which returns the function that actually runs the delay line, producing the echo. The CLM (common lisp) version might be something like:

(definstrument echo (beg dur scaler secs file)
  (let ((del (make-delay (round (* secs *srate*))))
	(inf (open-input file))
	(j 0))
    (run
     (loop for i from beg below (+ beg dur) do
       (let ((inval (ina j inf)))
	 (outa i (+ inval (delay del (* scaler (+ (tap del) inval)))))
	 (incf j))))
    (close-input inf)))

;;; (with-sound () (echo 0 60000 .5 1.0 "pistol.snd"))

CLM functions

See clm.html for full details. Optional args are in italics.


all-pass(gen input pm)all-pass filter
all-pass?(gen)#t if gen is all-pass filter
amplitude-modulate(carrier in1 in2)amplitude modulation
array-interp(arr x)interpolated array lookup
array->file(filename vct len srate channels)
write the contents of vct to the newly created sound file filename, giving the new file channels channels (data assumed to be interleaved in vct), sampling rate srate, and len samples (not frames).
asymmetric-fm(gen index fm)asymmetric-fm generator
asymmetric-fm?(gen)#t if gen is asymmetric-fm generator
buffer->frame(gen frame)buffer generator returning frame
buffer->sample(gen)buffer generator returning sample
buffer-empty?(gen)#t if buffer has no data
buffer-full?(gen)#t if buffer has no room for more data
buffer?(gen)#t if gen is buffer generator
clear-array(arr)set all elements of arr to 0.0
comb(gen input pm)comb filter
comb?(gen)#t if gen is comb filter
continue-sample->file(file)reopen file for more output
contrast-enhancement(input (index 1.0))a kind of phase modulation or companding
convolution(sig1 sig2 n)convolve sig1 with sig2 (size n), returning new sig1
convolve(gen input-function)convolve generator
convolve?(gen)#t if gen is convolve generator
convolve-files(f1 f2 maxamp outf)convolve f1 with f2, normalize to maxamp, write outf
db->linear(db)translate dB value to linear
degrees->radians(deg)translate degrees to radians
delay(gen input pm)delay line

In Scheme delay is a built-in syntactic form; it is renamed %delay here.

delay?(gen)#t if gen is delay line
dot-product(sig1 sig2)return dot-product of sig1 with sig2
env(gen)envelope generator
env-interp(x env (base 1.0))return value of env at x
env?(gen)#t if gen is env (from make-env)
mus-fft(rl im n sign)fft of rl and im (sign = -1 for ifft), result in rl
file->array(filename chan start len vct) load len samples of filename into vct starting at frame start in channel chan.
file->frame(gen loc frame)return frame from file at loc
file->frame?(gen)#t if gen is file->frame generator
file->sample(gen loc (chan 0))return sample from file at loc
file->sample?(gen)#t if gen is file->sample generator
filter(gen input)filter
filter?(gen)#t if gen is filter
fir-filter(gen input)FIR filter
fir-filter?(gen)#t if gen is fir filter
formant(gen input)formant generator
formant-bank(scls gens inval)
formant?(gen)#t if gen is formant generator
frame*(fr1 fr2 outfr)element-wise multiply
frame+(fr1 fr2 outfr)element-wise add
frame->buffer(buf frame)add frame to buffer
frame->file(gen loc frame)write (add) frame to file at loc
frame->file?(gen)#t if gen is frame->file generator
frame->frame(mixer frame outfr)pass frame through mixer
frame->list(frame)return list of frame contents
frame-ref(frame chan)return frame[chan]
frame->sample(frmix frame)pass frame through frame or mixer to produce sample
frame-set!(frame chan val)frame[chan]=val
frame?(gen)#t if gen is frame object

In Guile frame? is part of the debugger; it is renamed %frame? here.

granulate(gen input-function)granular synthesis generator
granulate?(gen)#t if gen is granulate generator
hz->radians(freq)translate freq to radians/sample
iir-filter(gen input)IIR filter
iir-filter?(gen)#t if gen is iir-filter
in-any(loc chan stream)return sample in stream at loc and chan
in-hz(freq)translate freq to radians/sample
ina(loc stream)return sample in stream at loc, chan 0
inb(loc stream)return sample in stream at loc, chan 1
linear->db(val)translate linear val to dB
locsig(gen loc input)place input in output channels at loc
locsig-ref(gen chan)locsig-scaler[chan]
locsig-reverb-ref(gen chan)locsig-reverb-scaler[chan]
locsig-set!(gen chan val)locsig-scaler[chan] = val
locsig-reverb-set!(gen chan val)locsig-reverb-scaler[chan] = val
locsig?(gen)#t if gen is locsig generator

;; all the make function arguments are optional-key args
make-all-pass(feedback feedforward size initial-contents initial-element max-size)
make-asymmetric-fm(frequency initial-phase r ratio)
make-buffer(size fill-time)
make-comb(scaler size initial-contents initial-element max-size)
make-convolve(input filter fft-size filter-size)
make-delay(size initial-contents initial-element max-size)
make-env(envelope scaler duration offset base end start)
make-fft-window(type size beta)
make-file->frame(name)
make-file->sample(name)
make-filter(order xcoeffs ycoeffs)
make-fir-filter(order xcoeffs)
make-formant(radius frequency gain)
make-frame(chans &rest vals)
make-frame->file(name chans format type)
make-granulate(input expansion length scaler hop ramp jitter max-size)
make-iir-filter(order ycoeffs)
make-locsig(degree distance reverb output revout channels type)
make-mixer(chans &rest vals)
make-notch(scaler size initial-contents initial-element max-size)
make-one-pole(a0 b1)
make-one-zero(a0 a1)
make-oscil(frequency initial-phase)
make-phase-vocoder(fftsize overlap interp pitch analyze edit synthesize)
make-ppolar(radius frequency)
make-pulse-train(frequency amplitude initial-phase)
make-rand(frequency amplitude)
make-rand-interp(frequency amplitude)
make-readin(file channel start)
make-sample->file(name chans format type comment)
make-sawtooth-wave(frequency amplitude initial-phase)
make-sine-summation(frequency initial-phase n a ratio)
make-square-wave(frequency amplitude initial-phase)
make-src(input srate width)
make-sum-of-cosines(frequency initial-phase cosines)
make-table-lookup(frequency initial-phase wave size)
make-triangle-wave(frequency amplitude initial-phase)
make-two-pole(a0 b1 b2)
make-two-zero(a0 a1 a2)
make-wave-train(frequency initial-phase wave size)
make-waveshape(frequency partials wave size)
make-zpolar(radius frequency)
mixer*(mix1 mix2 outmx)matrix multiply of mix1 and mix2
mixer-ref(mix in out)mix-scaler[in,out]
mixer-set!(mix in out val)mix-scaler[in,out] = val
mixer?(gen)#t if gen is mixer object
move-locsig(gen degree distance)
multiply-arrays(arr1 arr2)arr1[i] *= arr2[i]
mus-a0(gen)a0 field (simple filters)
mus-a1(gen)a1 field (simple filters)
mus-a2(gen)a2 field (simple filters)
mus-array-print-length()how many array (vct) elements to print in mus_describe
mus-b1(gen)b1 field (simple filters)
mus-b2(gen)b2 field (simple filters)
mus-bank(gens amps args1 args2)
mus-channel(gen)channel of gen
mus-channels(gen)channels of gen
mus-cosines(gen)cosines of sum-of-cosines gen
mus-data(gen)data array of gen
mus-feedback(gen)feedback term of gen (simple filters)
mus-feedforward(gen)feedforward term of gen (all-pass)
mus-file-buffer-size()size of input/ouput buffers (default 8192)
mus-file-name(gen)name of file assoicated with gen
mus-formant-radius(gen)formant radius
mus-frequency(gen)frequency of gen (Hz)
mus-hop(gen)hop amount of gen (granulate)
mus-increment(gen)increment of gen (src, readin, granulate)
mus-input?(gen)#t if gen is input source
mus-length(gen)length of gen
mus-location(gen)location (read point) of gen
mus-mix(outfile infile (outloc 0) frames (inloc 0) mixer envs)

mix infile into outfile starting at outloc in outfile and inloc in infile mixing frames frames of infile. frames defaults to the length of infile. If mixer, use it to scale the various channels; if envs (an array of envelope generators), use it in conjunction with mixerto scale/envelope all the various ins and outs. outfile can be a frame->file generator, and infile can be a file->frame generator.

mus-order(gen)order of gen (filters)
mus-output?(gen)#t if gen is output generator
mus-phase(gen)phase of gen (radians)
mus-ramp(gen)ramp time of gen (granulate)
mus-random(val)random numbers bewteen -val and val
mus-run(gen arg1 arg2)apply gen to args
mus-scaler(gen)scaler of gen
mus-rand-seed(val)random number generator seed (settable via set!)
mus-srate()current sampling rate
mus-xcoeffs(gen)feedforward (FIR) coeffs of filter
mus-ycoeffs(gen)feedback (IIR) coeefs of filter
notch(gen input pm)notch filter
notch?(gen)#t if gen is notch filter
one-pole(gen input)one-pole filter
one-pole?(gen)#t if gen is one-pole filter
one-zero(gen input)one-zero filter
one-zero?(gen)#t if gen is one-zero filter
oscil(gen fm pm)sine wave generator
oscil-bank(scls gens invals)bank of oscils
oscil?(gen)#t if gen is oscil generator
out-any(loc samp chan stream)write (add) samp to stream at loc in channel chan
outa(loc samp stream)write (add) samp to stream at loc in chan 0
outb(loc samp stream)write (add) samp to stream at loc in chan 1
outc(loc samp stream)write (add) samp to stream at loc in chan 2
outd(loc samp stream)write (add) samp to stream at loc in chan 3
partials->polynomial(partials kind)create waveshaping polynomial from partials
partials->wave(synth-data table norm)load table from synth-data
partials->waveshape(partials norm size)create waveshaping table from partials
phase-partials->wave(synth-data table norm)load table from synth-data
phase-vocoder(pv input)phase vocoder generator
phase-vocoder?(pv)#t if pv is phase vocoder generator
polar->rectangular(rl im)translate from polar to rectangular coordinates
polynomial(coeffs x)evaluate polynomial at x
pulse-train(gen fm)pulse-train generator
pulse-train?(gen)#t if gen is pulse-train generator
radians->degrees(rads)convert radians to degrees
radians->hz(rads)convert radians/sample to Hz
rand(gen fm)random number generator

In Ruby rand is a built-in method; it is aliased to kernel_rand here.

rand-interp(gen fm)interpolating random number generator
rand-interp?(gen)#t if gen is interpolating random number generator
rand?(gen)#t if gen is random number generator
readin(gen)read one value from associated input stream
readin?(gen)#t if gen is readin generator
rectangular->polar(rl im)translate from rectangular to polar coordinates
restart-env(env)return to start of env
ring-modulate(sig1 sig2)sig1 * sig2 (element-wise)
sample->buffer(buf samp)store samp in buffer
sample->file(gen loc chan val)store val in file at loc in channel chan
sample->file?(gen)#t if gen is sample->file generator
sample->frame(frmix samp outfr)convert samp to frame
sawtooth-wave(gen fm)sawtooth-wave generator
sawtooth-wave?(gen)#t if gen is sawtooth-wave generator
sine-summation(gen fm)sine-summation generator
sine-summation?(gen)#t if gen is sine-summation generator
spectrum(rl im win type)produce spectrum of data in rl (return rl)
square-wave(gen fm)square-wave generator
square-wave?(gen)#t if gen is square-wave generator
src(gen fm input-function)sample rate converter
src?(gen)#t if gen is sample-rate converter
sum-of-cosines(gen fm)sum-of-cosines (pulse-train) generator
sum-of-cosines?(gen)#t if gen is sum-of-cosines generator
sum-of-sines(amps phases)additive synthesis
table-lookup(gen fm)table-lookup generator
table-lookup?(gen)#t if gen is table-lookup generator
tap(gen pm)delay line tap
triangle-wave(gen fm)triangle-wave generator
triangle-wave?(gen)#t if gen is triangle-wave generator
two-pole(gen input)two-pole filter
two-pole?(gen)#t if gen is two-pole filter
two-zero(gen input)two-zero filter
two-zero?(gen)#t if gen is two-zero filter
wave-train(gen fm)wave-train generator
wave-train?(gen)#t if gen is wave-train generator
waveshape(gen index fm)waveshaping generator
waveshape?(gen)#t if gen is waveshape generator

In Snd, the default locsig interpolation type is handled via locsig-type, rather than CLM's global variable *clm-locsig-type*.

formant-bank and oscil-bank are optimizations for situations like the phase vocoder or cross synthesis (see examp.scm). It is assumed that you have a vector of generators, all summing their outputs into a single float. The amplitude scalers (the first argument to the bank function) can be a float, a vector of floats, a vct of floats, or a function that returns a float each time it is called; similarly for the inputs (the third argument); the bank of generators (the second argument) is assumed to be a vector full of generators.

  (formant-bank amps gens inval)

is the same as (but 30 times faster than)

  (do ((sum 0.0)
       (i 0 (1+ i))) 
      ((= i (vct-length gens)) sum)
    (set! sum (+ sum (* (vector-ref amps i)
                        (formant (vector-ref gens i) inval)))))

in the all-vector case. mus-bank is the general case. Here are a few more examples, taken from examp.scm.



(define comb-filter 
  (lambda (scaler size)
    (let ((cmb (make-comb scaler size)))
      (lambda (x) (comb cmb x)))))

; (map-chan (comb-filter .8 32))

;;; by using filters at harmonically related sizes, we can get chords:

(define comb-chord
  (lambda (scaler size amp)
    (let ((c1 (make-comb scaler size))
	  (c2 (make-comb scaler (* size .75)))
	  (c3 (make-comb scaler (* size 1.2))))
      (lambda (x)
        (* amp (+ (comb c1 x) (comb c2 x) (comb c3 x)))))))

; (map-chan (comb-chord .95 60 .3))

;;; or change the comb length via an envelope:

(define max-envelope
  (lambda (e mx)
    (if (null? e)
	mx
      (max-envelope (cddr e) (max mx (abs (cadr e)))))))

(define zcomb
  (lambda (scaler size pm)
    (let ((cmb (make-comb scaler size :max-size (+ size 1 (max-envelope pm 0))))
	  (penv (make-env :envelope pm :end (frames))))
      (lambda (x) (comb cmb x (env penv))))))

; (map-chan (zcomb .8 32 '(0 0 1 10)))

;;; to impose several formants, just add them in parallel:

(define formants
  (lambda (r1 f1 r2 f2 r3 f3)
    (let ((fr1 (make-formant r1 f1))
	  (fr2 (make-formant r2 f2))
	  (fr3 (make-formant r3 f3)))
      (lambda (x)
	(+ (formant fr1 x)
	   (formant fr2 x)
	   (formant fr3 x))))))

; (map-chan (formants .01 900 .02 1800 .01 2700))

;;; to get a moving formant:

(define moving-formant
  (lambda (radius move)
    (let ((frm (make-formant radius (cadr move)))
	  (menv (make-env :envelope move :end (frames))))
      (lambda (x)
        (let ((val (formant frm x)))
	  (set! (mus-frequency frm) (env menv))
	  val)))))

; (map-chan (moving-formant .01 '(0 1200 1 2400)))

;;; various "Forbidden Planet" sound effects:

(define fp
  (lambda (sr osamp osfrq)
    (let* ((os (make-oscil osfrq))
	   (sr (make-src :srate sr))
	   (len (frames))
	   (sf (make-sample-reader))
	   (out-data (make-vct len)))
      (vct-map! out-data
		  (lambda () 
		    (src sr (* osamp (oscil os))
			 (lambda (dir)
			   (if (> dir 0)
			       (next-sample sf)
			       (previous-sample sf))))))
      (free-sample-reader sf)
      (vct->samples 0 len out-data))))

; (fp 1.0 .3 20)


;;; -------- shift pitch keeping duration constant
;;;
;;; both src and granulate take a function argument to get input whenever it is needed.
;;; in this case, src calls granulate which reads the currently selected file.

(define expsrc
  (lambda (rate)
    (let* ((gr (make-granulate :expansion rate))
	   (sr (make-src :srate rate))
	   (vsize 1024)
	   (vbeg 0)
	   (v (samples->vct 0 vsize))
	   (inctr 0))
      (lambda (inval)
        (src sr 0.0
	  (lambda (dir)
	    (granulate gr
	      (lambda (dir)
		(let ((val (vct-ref v inctr)))
		  (set! inctr (+ inctr dir))
		  (if (>= inctr vsize)
		      (begin
			(set! vbeg (+ vbeg inctr))
			(set! inctr 0)
			(samples->vct vbeg vsize 0 0 v)))
		  val)))))))))

Geez, I haven't had this much fun in a long time! Check out examp.scm and snd-test.scm for more. CLM-in-CL users will be disappointed with the CLM-in-Scheme performance; my tests indicate that interpreted Scheme (as in Snd currently) is about 30 to 100 times slower than CLM instruments using the run macro. That macro now exists in Snd; see the optimization variable for a brief discussion. Currently, I can speed up most CLM-related Scheme code by factors between 8 and 20. This is still slightly slower than CLM, but it's certainly an improvement!

You can load Rick Taube's CM into Snd as Scheme code:

snd -l /home/bil/test/cm-2.4.0/src/cm.scm

and all of CM is at your disposal!


Instruments

It's hard to decide what's an "instrument" in this context, but I think I'll treat it as something that can be called as a note in a notelist (say in with-sound) and generate its own sound. Test 23 in snd-test.scm has a with-sound that includes most of these instruments.

InstrumentSourcesDescription
anoiclm-ins.scmnoise reduction based on P Cook's Scrubber.m
attractclm-ins.scmJames McCarthy's choatic brass
bes-fmclm-ins.scmFM analog using Bessel j0 rather than sin
bigbirdbird.scm, bird.rbmore bird songs
birdbird.scm, bird.rbbird songs
bowstrad.scm, strad.rbbowed string physical model (Juan Reyes, Michael Scholz)
bowstrprc95.scmbowed string pysical model
brassprc95.scmbrass pysical model
cellonclm-ins.scmS Krupowicz's feedback FM
chain-dspsexamp.scminstrument gets gen patch from its args
clarinetprc95.scmclarinet pysical model
drone, canterclm-ins.scmPeter Common's bagpipe
exp-sndclm-ins.scmgranulate to the max
expfilclm-ins.scmgranular synthesis interleaving two files
fluteprc95.scmflute pysical model
fm-bellclm-ins.scm, examp.rbMike McNabb's FM bell
fm-drumclm-ins.scmJan Mattox's FM dum
fm-insectclm-ins.scmFM insect (katydid)
fm-noisenoise.scm, noise.rbnoise maker
fm-trumpetclm-ins.scmDexter Morrill's FM trumpet
fm-violinv.scm, v.rb, fmv.scm, sndscm.htmlviolin-like sounds via FM
fofinsclm-ins.scmFOF synthesis
fullmixclm-ins.scmcomplicated mixer
freeverbfreeverb.scma reverberator
gongclm-ins.scmPaul Weineke's FM gong
gran-synthclm-ins.scmsimple granular synthesis
graphEqclm-ins.scmMarco Trevisani's semi-graphical equalizer
jc-reverbjcrev.scm, examp.rbChowning's old reverb
jl-reverbclm-ins.scmcavernous version of jc-reverb
lbj-pianoclm-ins.scmadditive synthesis piano (Doug Fulton)
maraca, big-maracamaraca.scmPerry Cook's maraca physical model
metalclm-ins.scmmore FM by Perry
nrevclm-ins.scmMike McNabb's reverb
ppiano.scm, piano.rbScott van Duyne's piano physical model
pinsclm-ins.scmXavier Serra SMS-style spectral modeling
pluckclm-ins.scmDavid Jaffe's Karplus-Strong (physical model) plucked string
pluckyprc95.scmplucked string pysical model
pqw-voxclm-ins.scmvocal sounds from phase-quadrature waveshaping
pqwclm-ins.scmphase-quadrature waveshaping
resfltclm-ins.scmtwo-poles for resonances (Xavier Serra)
resonclm-ins.scmparallel FM creating resonances
rhodey, wurley, hammondoidclm-ins.scmFM stuff from Perry Cook
scm-simpclm.htmlsimple sine wave instrument
scratchclm-ins.scmmove around in sound via list of turn points and src
singersinger.scmPerry Cook's vocal tract physical model
spectraclm-ins.scmsimple additive synthesis
stereo-fluteclm-ins.scmNicky Hind's flute physical model
touch-toneclm-ins.scmtouch-tone telephone
tubebellclm-ins.scmPerry Cook's FM tubular bell
two-tabclm-ins.scminterpolate between two spectra
voxclm-ins.scmvoice using "leapfrog" FM-generated formants
zc, zn, zaclm-ins.scminterpolating delay effects




Snd and Motif

It is possible to add your own user-interface elements using the xm module included with Snd. 'make xm' should create a shared library named xm.so; you can load this at any time into Snd:

> (define hxm (dlopen "/home/bil/snd-6/xm.so"))
#<unspecified>
> (dlinit hxm "init_xm")
#t

and now we have access to all of X and Motif. Alternatively, use the configure switch --with-static-xm, and the xm module will be included in the base Snd image. Here's dialog window with a slider:

(define scale-dialog #f)
(define current-scaler 1.0)

(define (create-scale-dialog parent)
  (if (not (Widget? scale-dialog))
      (let ((xdismiss (XmStringCreate "Dismiss" XmFONTLIST_DEFAULT_TAG))
	    (xhelp (XmStringCreate "Help" XmFONTLIST_DEFAULT_TAG))
	    (titlestr (XmStringCreate "Scaling" XmFONTLIST_DEFAULT_TAG)))
	(set! scale-dialog 
	      (XmCreateTemplateDialog parent "Scaling"
                (list XmNcancelLabelString   xdismiss
		      XmNhelpLabelString     xhelp
		      XmNautoUnmanage        #f
		      XmNdialogTitle         titlestr
		      XmNresizePolicy        XmRESIZE_GROW
	              XmNnoResize            #f
		      XmNtransient           #f)))
	(XtAddCallback scale-dialog 
		       XmNcancelCallback (lambda (w context info)
					   (XtUnmanageChild scale-dialog)))
	(XtAddCallback scale-dialog 
		       XmNhelpCallback (lambda (w context info)
					 (snd-print "move the slider to affect the volume")))
	(XmStringFree xhelp)
	(XmStringFree xdismiss)
	(XmStringFree titlestr)

	(let* ((mainform 
		(XtCreateManagedWidget "formd" xmFormWidgetClass scale-dialog
                  (list XmNleftAttachment    XmATTACH_FORM
		        XmNrightAttachment   XmATTACH_FORM
		        XmNtopAttachment     XmATTACH_FORM
		        XmNbottomAttachment  XmATTACH_WIDGET
		        XmNbottomWidget      (XmMessageBoxGetChild scale-dialog XmDIALOG_SEPARATOR))))
	       (scale
		(XtCreateManagedWidget "" xmScaleWidgetClass mainform
		  (list XmNorientation XmHORIZONTAL
			XmNshowValue   #t
			XmNvalue       100
			XmNmaximum     500
			XmNdecimalPoints 2))))

      (XtAddCallback scale XmNvalueChangedCallback (lambda (w context info)
					     (set! current-scaler (/ (.value info) 100.0))))
      (XtAddCallback scale XmNdragCallback (lambda (w context info)
					     (set! current-scaler (/ (.value info) 100.0)))))))
  (XtManageChild scale-dialog))

(create-scale-dialog (cadr (main-widgets)))

In Ruby, this is:

$scale_dialog = false
$current_scaler = 1.0

def create_scale_dialog(parent)
  if !RWidget?($scale_dialog) 
    then
      xdismiss = RXmStringCreate("Dismiss", RXmFONTLIST_DEFAULT_TAG)
      xhelp = RXmStringCreate("Help", RXmFONTLIST_DEFAULT_TAG)
      titlestr = RXmStringCreate("Scaling", RXmFONTLIST_DEFAULT_TAG)
      $scale_dialog = RXmCreateTemplateDialog(parent, "Scaling",
                   	[RXmNcancelLabelString,  xdismiss,
                      	 RXmNhelpLabelString,    xhelp,
                      	 RXmNautoUnmanage,       false,
                      	 RXmNdialogTitle,        titlestr,
                      	 RXmNresizePolicy,       RXmRESIZE_GROW,
                      	 RXmNnoResize,           false,
                       	 RXmNtransient,          false])
      RXtAddCallback($scale_dialog, RXmNcancelCallback, 
                     Proc.new { |w, context, info| RXtUnmanageChild($scale_dialog)})
      RXtAddCallback($scale_dialog, RXmNhelpCallback, 
                     Proc.new { |w, context, info| snd_print "move the slider to affect the volume"})
      RXmStringFree xhelp
      RXmStringFree xdismiss
      RXmStringFree titlestr
      mainform = RXtCreateManagedWidget("formd", RxmFormWidgetClass, $scale_dialog,
                  	[RXmNleftAttachment,      RXmATTACH_FORM,
                         RXmNrightAttachment,     RXmATTACH_FORM,
                         RXmNtopAttachment,       RXmATTACH_FORM,
                         RXmNbottomAttachment,    RXmATTACH_WIDGET,
                         RXmNbottomWidget,        RXmMessageBoxGetChild($scale_dialog, RXmDIALOG_SEPARATOR)])
      scale = RXtCreateManagedWidget("", RxmScaleWidgetClass, mainform,
                  	[RXmNorientation, RXmHORIZONTAL,
                         RXmNshowValue,   true,
                         RXmNvalue,       100,
                         RXmNmaximum,     500,
                         RXmNdecimalPoints, 2])
      RXtAddCallback(scale, RXmNvalueChangedCallback, 
                     Proc.new { |w, context, info| $current_scaler = Rvalue(info) / 100.0})
      RXtAddCallback(scale, RXmNdragCallback, 
                     Proc.new { |w, context, info| $current_scaler = Rvalue(info) / 100.0})
      RXtManageChild $scale_dialog
    end
end

$Snd_widgets = main_widgets()
create_scale_dialog $Snd_widgets[1]

All of Snd is at your disposal once this module is loaded. The next function installs our own file filtering procedure into the File:Open dialog (it uses match-sound-files from extensions.scm):

(define (install-searcher proc)
  (define (XmString->string str)
    (cadr (XmStringGetLtoR str XmFONTLIST_DEFAULT_TAG)))
  (define (XmStringTable->list st len)
    (XmStringTableUnparse st len #f XmCHARSET_TEXT XmCHARSET_TEXT #f 0 XmOUTPUT_ALL))
  (define (list->XmStringTable strs)
    (XmStringTableParseStringArray strs (length strs) #f XmCHARSET_TEXT #f 0 #f))
  (XtSetValues (let ((m (open-file-dialog #f)))
                         ; make sure the dialog exists
		  (list-ref (dialog-widgets) 6))
		(list XmNfileSearchProc                             ; set dialog file search procedure
		       (lambda (widget info)
			 (let* ((dir (XmString->string (dir info))) ; directory string
				(files (match-sound-files proc dir)) ; list of matching files
				(fileTable (list->XmStringTable      ; XmStringTable for XmNfileListItems
                                             (map (lambda (n)        ; every file needs prepended dir
                                                    (string-append dir n)) 
                                                  files))))
			   (XtSetValues widget                      ; change the list of files
					 (list XmNfileListItems fileTable
					       XmNfileListItemCount (length files)
					       XmNlistUpdated #t)))))))

;(install-searcher (lambda (file) (= (mus-sound-srate file) 44100)))
;(install-searcher (lambda (file) (= (mus-sound-chans file) 4)))

Now click the 'Filter' button to see only those files that fit the procedure in the dialog's files list. See snd-motif.scm and popup.scm.


Snd and Gtk+

Here's the scale-dialog in xg/gtk:

(define scale-dialog #f)
(define current-scaler 1.0)

(define* (g_signal_connect obj name func #:optional data)
  ;; slightly obsolete ...
  (g_signal_connect_closure_by_id 
   (list 'gpointer (cadr obj))
   (g_signal_lookup name (G_OBJECT_TYPE (GTK_OBJECT obj)))
   0
   (g_cclosure_new func data (list 'GClosureNotify 0))
   #f))

(define (create-scale-dialog parent)
  (if (not scale-dialog)
      (begin
	(set! scale-dialog (gtk_dialog_new))
	(g_signal_connect scale-dialog "delete-event"
			     (lambda (w ev info)
			       (gtk_widget_hide w)))
	(gtk_window_set_title (GTK_WINDOW scale-dialog) "Scale")
	(gtk_widget_realize scale-dialog)
	(let ((dismiss (gtk_button_new_with_label "Dismiss"))
	      (help (gtk_button_new_with_label "Help")))
	  (gtk_box_pack_start (GTK_BOX (.action_area (GTK_DIALOG scale-dialog))) dismiss #t #t 4)
	  (gtk_box_pack_end (GTK_BOX (.action_area (GTK_DIALOG scale-dialog))) help #t #t 4)	
	  (g_signal_connect dismiss "clicked"
			       (lambda (w info)
				 (gtk_widget_hide scale-dialog)))
	  (g_signal_connect help "clicked"
			       (lambda (w info)
				 (help-dialog "Scaler Dialog" "move the slider to affect the volume")))
	  (gtk_widget_show dismiss)
	  (gtk_widget_show help)
	  (let* ((adj (gtk_adjustment_new 0.0 0.0 1.01 0.01 0.01 .01))
		 (scale (gtk_hscale_new (GTK_ADJUSTMENT adj))))
	    (gtk_range_set_update_policy (GTK_RANGE (GTK_SCALE scale)) GTK_UPDATE_CONTINUOUS)
	    (gtk_scale_set_draw_value (GTK_SCALE scale) #t)
	    (gtk_scale_set_digits (GTK_SCALE scale) 2)
	    (g_signal_connect adj "value_changed"
				 (lambda (wadj info)
				   (set! current-scaler (.value (GTK_ADJUSTMENT wadj)))))
	    (gtk_box_pack_start (GTK_BOX (.vbox (GTK_DIALOG scale-dialog))) scale #f #f 6)
	    (gtk_widget_show scale)))))
  (gtk_widget_show scale-dialog))

(create-scale-dialog (cadr (main-widgets)))

The only change from the C code was the addition of GTK_ADJUSTMENT in the scale value_changed callback -- currently the xg module assumes the first argument to the two-argument callback is a GtkWidget, so we have to cast a GtkAdjustment back to its original type. Once I figure out how the "marshaller" works in Gtk, I may be able to fix this.


Snd with no GUI and scripting

If Snd is built without a graphical user interface (either by specifying --with-no-gui to configure, or by setting the USE_NO_GUI compile-time flag), it runs Guile's "repl" (read-eval-print loop), or Ruby's equivalent, with input from stdin. All the non-interface related functions are available, so you can do things like:

snd> (new-sound "new.snd")
0
snd> (load "v.scm")
snd> (fm-violin 0 1 440 .1)
-1
snd> (frames 0)
22050
snd> (play)
#t
snd> (exit)

Guile's repl has its own error handlers, different from the normal Snd handlers; name completion, if it exists at all, won't complete Snd names; there are undoubtedly other differences that I haven't noticed.

Since this version of Snd is the same as the guile program with Snd loaded, you can treat it as a scripting engine. For example, if you have an executable file with:

#!/home/bil/test/snd-6/snd -l
!#
(define a-test 32)
(display "hiho")
(newline)

it can be executed just like any such script.

/home/bil/test/snd-6/ script
hiho
:a-test
32
:(exit)
/home/bil/test/snd-6/ 

The difference between this use of Snd, and using guile itself for scripts is that Snd uses the -l switch where guile would use -s. As noted above, you can use the -e switch to use Snd as a pure command-line program, and, of course, (exit) to drop back to the shell. Here's a script that doubles every sample in "oboe.snd" and writes the result as "test.snd":

#!/home/bil/test/snd-6/snd -l
!#
(open-sound "oboe.snd")
(scale-by 2.0)
(save-sound-as "test.snd")
(exit)

The functions script-args and script-arg can be used to access the script's arguments, and if necessary (if not exiting) tell Snd to ignore arguments. script-args returns a list of strings giving the arguments. The first two are always "-l" and the script file name. The current argument is (script-arg). If you set this to a higher value, Snd will subsequently ignore the intevening arguments as it scans the startup arguments (see snd-test.scm).

#!/home/bil/test/snd-6/snd -l
!#
(if (= (length (script-args)) 2) ;i.e. ("-l" "script")
  (display "usage: script file-name...\n")
  (begin
    (open-sound (list-ref (script-args) (+ (script-arg) 1)))
    (scale-by 2.0)
    (save-sound-as "test.snd")))
(exit)

This either grumbles if no argument is given, or scales its argument sound by 2.0:

script pistol.snd

And obviously we can run through the entire argument list, doubling all the sounds or whatever by using a do loop -- the following displays all the comments it finds:

#!/home/bil/cl/snd -l
!#
(use-modules (ice-9 format))
(if (= (length (script-args)) 2) ;i.e. ("-l" "script")
  (display "usage: script file-name...\n")
  (do ((arg (+ (script-arg) 1) (1+ arg)))
      ((= arg (length (script-args))))
    (let ((name (list-ref (script-args) arg)))
      (display (format #f "~A: ~A~%" name (mus-sound-comment name))))))
(exit)

Say we save this as the file "comments".

/home/bil/cl/comments *.snd

If you like, you can use env:

#!/usr/bin/env snd
!#

But if that works, so will:

#!snd -l
!#

This scripting mechanism actually will work in any version of Snd; to keep the Snd window from popping up, use the -b (-batch) switch in place of -l. Here's another script; it looks for any sounds that are longer than 40 seconds in duration, and truncates them to 40 seconds:

#!/usr/local/bin/snd -l
!#
(if (= (length (script-args)) 2)
  (display "usage: trunc.scm file-name...\n")
  (do ((arg (+ (script-arg) 1) (1+ arg)))
      ((= arg (length (script-args))))
    (let* ((name (list-ref (script-args) arg)))
      (if (> (mus-sound-duration name) 40.0)
	  (let* ((ind (open-sound name)))
	    (set! (frames ind) (* 40 (srate ind)))
	    (save-sound ind)
	    (close-sound ind))))))
(exit)

Here's a sndplay replacement script:

#!snd -b
!#
(play-and-wait (list-ref (script-args) (+ (script-arg) 1)))
(exit)

And here's a script that splits a multi-channel file into a bunch of mono files:

#!snd -b
!#
(if (= (length (script-args)) 2)
  (display "usage: split.scm filename\n")
  (let* ((name (list-ref (script-args) (1+ (script-arg))))
	 (chns (mus-sound-chans name)))
    (if (> chns 1)
	(let ((ind (open-sound name)))
	  (do ((i 0 (1+ i)))
	      ((= i chns))
	    (display (format #f "~A.~D " name i))
	    (save-sound-as (format #f "~A.~D" name i)
			   ind
			   (header-type ind)
			   (data-format ind)
			   (srate ind)
			   i))
	  (close-sound ind)))))
(exit)

Snd with Ruby

Ruby is an extension language described as an "object-oriented Perl". It provides a different syntax from that of Guile/Scheme. In Ruby, all the "-" are "_", "->" is "2", hooks and memo_sound have "$" prepended (since they are global variables from Ruby's point of view), and all the constants are capitalized (e.g. Autocorrelation). The generalized set! functions are replaced by "set_" plus the base name (e.g. set_window_width), with arguments reordered in some cases to place the optional values after the new value. That is, (set! (sync snd) 1) becomes set_sync(1, snd). Hooks in Ruby (which have little or nothing to do with Ruby's "hookable variables") are just procedures or nil, not lists of procedures as in Guile. Here's the Ruby version of the init file given above:

set_window_width 800
set_window_height 500

set_listener_font "9x15"
set_help_text_font "9x15"
set_axis_numbers_font "9x15"

set_show_mix_waveforms true
set_trap_segfault false
set_show_backtrace true
set_show_indices true

set_listener_prompt ":"
show_listener

beige = make_color 0.96, 0.96, 0.86
blue = make_color 0, 0, 1
set_selected_graph_color beige
set_selected_data_color blue

Procedures are created via Proc.new, so to set the open-hook to print the file name,

>$open_hook = Proc.new { |name| snd_print name }
#<Proc:0x40221b84>
>open_sound "oboe.snd"
/home/bil/cl/oboe.snd
0

(The trailing "0" is the result of open_sound). The Guile hook list support procedures aren't included in Ruby -- simply set the variable to the procedure you want, or false to clear it.

Vcts and sound-data objects mixin "Comparable" and "Enumerable", and respond to various array-like methods:

>v1 = make_vct 4
#<vct[len=4]: 0.000 0.000 0.000 0.000>
>v1[3] = 1.0
1.0
>v1.sort
0.00.00.01.0 # I don't know why it prints this way but ...
>v1
#<vct[len=4]: 0.000 0.000 0.000 1.000>
>v1.max
1.0

I'm thinking about making classes for things like sounds; you could then have sound + sound to mix, or sound * 2 to scale it, a given channel could be treated as an array, accessed via sound[0, 12345] and so on, These are extremely easy to add, but I'd like to coordinate this with Guile's object system. Keywords, CLM generic functions, and optional arguments work as in Scheme:

>osc = make_oscil(:frequency, 440)
oscil freq: 440.000Hz, phase: 0.000
>oscil osc
0.0
>oscil osc
0.1250506192
>osc.frequency
440.0

Lists (from the Scheme point of view) are arrays (vectors) in Ruby, and various built-in Scheme functions such as car aren't predefined, so to set up the focusing hooks as described in mouse-enter-graph-hook we need to do something along these lines:

def car(v)
  v[0]
end

$mouse_enter_graph_hook = Proc.new {|snd, chn| 
			            if sound? snd then
			               focus_widget car channel_widgets snd, chn
                                    end 
                                   }

$mouse_enter_listener_hook = Proc.new { |widget| 
                                        focus_widget widget 
                                      }

Here's one more example, a translation of display-energy in draw.scm:

def display_energy(snd, chn)
  ls = left_sample
  rs = right_sample
  data1 = make_graph_data(snd, chn)
  data = data1
  if not vct? data
    data = data1[1]
  end
  len = vct_length data
  sr = srate snd
  y_max = y_zoom_slider(snd, chn)
  vct_multiply!(data, data)
  graph(data, "energy", ls / sr, rs / sr, 0.0, y_max * y_max, snd, chn, false)
  end

# $lisp_graph_hook = Proc.new {|snd, chn| display_energy(snd, chn)}

In Ruby you make a symbol by prepending ":", so Guile's

(list 'KeySym (char->integer #\F))

becomes

[:KeySym, ?F]

In the listener, everything is line-oriented (that is, I'm not trying to catch incomplete expressions). And it appears that in Ruby, variables defined within a file are considered local to that file(?). The save state mechanism is incomplete, but the basic stuff works. Unimplemented (or untested) are: backtrace upon error, env lookup from the env name, and many cute methods. The RubyInline module might serve as a run-macro replacement. I'm slowly translating snd-test.scm to Ruby: see snd_test.rb. bird.rb is the Ruby version of bird.scm. My very informal timing tests indicate that Guile and Ruby run at the same speed. Michael Scholz has translated many of the Scheme files to Ruby -- see any of the .rb files.


Snd and LADSPA

  init-ladspa
  list-ladspa
  analyse-ladspa library type
  apply-ladspa reader data duration origin

Richard Furse has provided a module to support LADSPA plugins in Snd. Here is his documentation:

Supporting functions are:

	(init-ladspa)

	Performs a search of LADSPA_PATH for plugins, doesn't need to be called 
as LADSPA automatically initialises on first use however can be used to 
reinitialise if new plugins have arrived.

	(list-ladspa)

	Returns a list of lists where each inner list contains a string to 
identify the plugin library and a string to identify the plugin type within 
the library.

	(analyse-ladspa plugin-library plugin-type)

	Returns a list of assorted data about a particular plugin including a 
list of port descriptions. plugin-library and plugin-type are as provided 
by list-ladspa.

The main function is:

	(apply-ladspa reader (plugin-library plugin-type [param1 [param2 ...]]) samples origin)

	Applies a LADSPA plugin to a block of samples. 
An example call to apply the low-pass-filter in the CMT plugin library is 
(apply-ladspa (make-sample-reader 0) (list "cmt" "lpf" 1000) 10000 "origin").

Dave Phillips in Linux Audio Plug-Ins: A Look Into LADSPA adds this:

  (apply-ladspa (make-sample-reader 57264) (list "cmt" "delay_5s" .3 .5) 32556 "ibm.wav")

"This sequence tells Snd to read a block of 32556 samples from the ibm.wav file, starting at sample number 57264, and apply the delay_5s LADSPA plug-in (Richard Furse's delay plug-in, also found in cmt.so) with a delay time of .3 seconds and a 50/50 dry/wet balance."

To help Snd find the plugin library, set either the Snd variable ladspa-dir or the environment variable LADSPA_PATH to the directory. If, for example, cmt.so is in /usr/local/lib/ladspa, (and you're using tcsh), then

  setenv LADSPA_PATH /usr/local/lib/ladspa

or

  (set! (ladspa-dir) "/usr/local/lib/ladspa")

Snd plugins may have any number of inputs and outputs; if more than one input is required, the first argument to apply-ladspa should be a list of readers:

  (apply-ladspa (list (make-sample-reader 0 0 0)  ;chan 0
                      (make-sample-reader 0 0 1)) ;chan 1
                (list "cmt" "freeverb3" 0 .5 .5 .5 .5 .5) 
                100000 "freeverb")

The "regularized" version of apply-ladspa could be defined:

(define* (ladspa-channel ladspa-data #:optional nbeg ndur nsnd nchn nedpos)
  (let* ((beg (or nbeg 0)) 
	 (snd (or nsnd (selected-sound) (car (sounds))))
	 (chn (or nchn (selected-channel)))
	 (dur (or ndur (- (frames snd chn) beg)))
	 (edpos (or nedpos current-edit-position))
	 (reader (make-sample-reader beg snd chn 1 edpos)))
    (apply-ladspa reader ladspa-data dur "apply-ladspa")
    (free-sample-reader reader)))

Driving Snd remotely

It is possible to send Snd arbitrary Scheme or Ruby code from any other program; see the program sndctrl.c. Snd has two X window properties: "SND_VERSION" and "SND_COMMAND"; the former is the Snd version (a date), and the latter is the communication path for other programs. Any time such a program changes the SND_COMMAND property, Snd notices and evaluates the new value (as a string, as if typed in the Snd listener). To get a response from Snd, use the function change-window-property(consat,name,command) where consat is the property name Snd should search for, name is the property to change, and command is the string that replaces the current property value. CLM's communication with Snd function sends Snd this string:

"(change-window-property \"CLM_VERSION\" \"CLM_COMMAND\" " str ")"

where str is the form to be evaluated within Snd. It then waits for a change to the CLM_COMMAND property, returning its value to the user. The send-snd function itself, similarly, looks for SND_VERSION and sets SND_COMMAND to str, which Snd subsequently notices.


Snd and OpenGL

Snd can be used in conjunction with OpenGL. If it is compiled with the switch -DHAVE_GL, and loaded with -lGLU -lGL (normally this can be handled via the configure switch --with-gl), the top level Snd shell is setup to handle OpenGL graphics; the GLXContext is (snd-glx-context). The GL-to-Scheme bindings are in gl.c, and follow the same name and type conventions of the Motif bindings in xm.c. Any of the Snd drawing area widgets (or your own) can receive GL graphics commands. Here is a translation of the SGI/xjournal glxmotif program:

(define (draw-it)
  (glXMakeCurrent (XtDisplay (cadr (main-widgets))) 
		  (XtWindow (car (channel-widgets)))
		  (snd-glx-context))
  (glEnable GL_DEPTH_TEST)
  (glDepthFunc GL_LEQUAL)
  (glClearDepth 1.0)
  (glClearColor 0.0 0.0 0.0 0.0)
  (glLoadIdentity)
  (gluPerspective 40.0 1.0 10.0 200.0)
  (glTranslatef 0.0 0.0 -50.0)
  (glRotatef -58.0 0.0 1.0 0.0)
  (let ((vals (XtVaGetValues (car (channel-widgets)) (list XmNwidth 0 XmNheight 0))))
    (glViewport 0 0 (list-ref vals 1) (list-ref vals 3)))
  (glClear (logior GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
  (glBegin GL_POLYGON)
  (glColor3f 0.0 0.0 0.0)   (glVertex3f -10.0 -10.0 0.0)
  (glColor3f 0.7 0.7 0.7)   (glVertex3f 10.0 -10.0 0.0)
  (glColor3f 1.0 1.0 1.0)   (glVertex3f -10.0 10.0 0.0)
  (glEnd)
  (glBegin GL_POLYGON)
  (glColor3f 1.0 1.0 0.0)   (glVertex3f 0.0 -10.0 -10.0)
  (glColor3f 0.0 1.0 0.7)   (glVertex3f 0.0 -10.0 10.0)
  (glColor3f 0.0 0.0 1.0)   (glVertex3f 0.0 5.0 -10.0)
  (glEnd)
  (glBegin GL_POLYGON)
  (glColor3f 1.0 1.0 0.0)   (glVertex3f -10.0 6.0 4.0)
  (glColor3f 1.0 0.0 1.0)   (glVertex3f -10.0 3.0 4.0)
  (glColor3f 0.0 0.0 1.0)   (glVertex3f 4.0 -9.0 -10.0)
  (glColor3f 1.0 0.0 1.0)   (glVertex3f 4.0 -6.0 -10.0)
  (glEnd)
  (glXSwapBuffers (XtDisplay (cadr (main-widgets))) 
		  (XtWindow (car (channel-widgets))))
  (glFlush))

See snd-gl.scm.


GL spectrogram
now.snd GL spectrogram

Snd and gdb

Here are some gdb functions (for your ~/.gbdinit file) that might come in handy:

define gp
set gdb_print($arg0)
print gdb_output
end
document gp
Executes (object->string arg): gp memo_sound => #f
end

define ge
call gdb_read($arg0)
call gdb_eval(gdb_result)
set gdb_print(gdb_result)
print gdb_output
end
document ge
Executes (print (eval (read arg))): ge "(+ 1 2)" => 3
end

define gh
call g_help(scm_str2symbol($arg0), 20)
set gdb_print($1)
print gdb_output
end
document gh
Prints help string for arg: gh "enved-target"
end

SCM values are displayed as integers in gdb, so say Snd halts and you notice it's loading some unknown file:

#32 0x081ae8f4 in scm_primitive_load (filename=1112137128) at load.c:129

You can get the file name with gp:

(gdb) gp 1112137128
$1 = 0
$2 = 0x40853fac "\"/home/bil/test/share/guile/1.5.0/ice-9/session.scm\""

If you have a pointer to a CLM generator, you can use:

p mus_describe(arg)  --  show the outsider's view of arg
p mus_inspect(arg)  --  show every internal field of arg

The ge function can show current Snd state:

(gdb) ge "(eps-file)"
$5 = 0
$6 = 0
$7 = 0
$8 = 0x8296cf0 "\"snd.eps\""

related documentationsnd.html extsnd.html clm.html sndlib.html sndscm.html libxm.html index.html