CLM instruments: Granular Synthesis and Sampling Rate Conversion, the Loop macro

Lecture Slides

A series of gif images of the lecture slides... (only accesible from within Stanford University)


  • some with-sound options (see the complete description in the clm manual, at file:///usr/ccrma/lisp/src/clm/clm.html:

    number of output channels
    sample rate for output soundfile
    name of the reverb instrument
    decay time of the reverb
    print some statistics about the clm run
    name of the output soundfile

  • some CLM unit generators:

    • oscil
    • env
    • outa and friends
    • locsig

  • some CLM instruments:

  • The loop macro:

    • basic syntax
    • for ... from ... by ... below
    • for ... in
    • repeat
    • collect
    • do

  • Common Music:

    • what is CM?
    • relationship with CLM
    • basic CM: processes, events
    • useful CM macros: hertz...

  • Algorithmic Techniques: within a note (generating complex envelopes)

Granular Synthesis

Overview of granular synthesis techniques.


Here's a fairly comprehensive granular synthesis instrument written in clm (grani.ins) and geared towards granulation of soundfiles.

Here is the header of the instrument and a short description of its parameters (see the source code for the parameter defaults):

(definstrument grani 
    (start-time duration amplitude file
		(grains grani-grains)
		(amp-envelope grani-amp-envelope)
		(grain-envelope grani-grain-envelope)
		(grain-envelope-end grani-grain-envelope-end)
		(grain-envelope-transition grani-grain-envelope-transition)
		(grain-duration grani-grain-duration)
		(grain-duration-spread grani-grain-duration-spread)
		(grain-duration-limit grani-grain-duration-limit)
		(srate grani-srate)
		(srate-spread grani-srate-spread)
		(srate-linear grani-srate-linear)
		(srate-base grani-srate-base)
		(srate-error grani-srate-error)
		(grain-start grani-grain-start)
		(grain-start-spread grani-grain-start-spread)
		(grain-density grani-grain-density)
		(grain-density-spread grani-grain-density-spread)
		(reverb-amount grani-reverb-amount)
		(reverse grani-reverse)
		(where-to grani-where-to)
		(where-bins grani-where-bins)
		(grain-distance grani-grain-distance)
		(grain-distance-spread grani-grain-distance-spread)
		(grain-degree grani-grain-degree)
		(grain-degree-spread grani-grain-degree-spread))

Mandatory parameters

starting time in seconds
duration of the note in seconds
amplitude of the note
the complete pathname of the soundfile you want to use as source material for the grains

Some of the optional parameters

After the name of the parameter I specify the type of parameter. Most of the key parameters can be either numbers (for a constant value) or an envelope.

amp-envelope [envelope]
amplitude envelope for the whole note
grain-envelope [envelope]
amplitude envelope for each individual grain
grain-envelope-end [envelope or nil]
a second amplitude envelope for each individual grain. If specified, grain-envelope-transition can be used to interpolate between both envelopes over the duration of the note.
grain-envelope-transition [envelope]
interpolation envelope for the grain envelope (used only if grain-envelope-end has been specified).
grain-duration [number or envelope]
duration in seconds of each individual grain
grain-duration-spread [number or envelope]
random deviation from grain-duration
grain-duration-limit [number]
minimum duration in seconds of a grain
srate [number or envelope]
sample rate conversion factor. The value for each grain is either a constant if the parameter is a number or is determined by the value of the envelope at the point in time where the grain starts. The sample rate conversion factor is a constant within the grain. If not overriden by the sampling rate conversion factor is specified in semitones (positive values transpose the sound up and negative values transpose down).
srate-spread [number or envelope]
random deviation the value of srate
srate-linear [t or nil]
defines the type of envelope used for the sampling rate conversion factor:
nil (the default): "srate" is expressed in fractional semitones above or below the original sampling rate (0 = no change in sampling rate conversion). The exponential envelope is approximated by a linear segment representation. The error bounds of the approximation can be set by srate-error. The base of the exponential curve can be changed by srate-base.
t: "srate" is the linear sampling rate conversion factor (1 = no change in sampling rate).
srate-error [number]
error bound for the exponential conversion
srate-base [number]
base for the exponential conversion. "2" will express the envelope in octaves. "(expt 2 (/ 12)" will express the envelope in semitones.
grain-start [number or envelope]
point in the input file where the samples are going to be read from. "0" represents the start of the input soundfile, "1" represents the end. An envelope can be used to produce arbitrary mappings over time.
grain-start-spread [number or envelope]
random deviation from the value of grain-start.
grain-density [number or envelope]
number of grains per second that will be created.
grain-density-spread [number or envelope]
random deviation from the value of grain-density.
reverse [t or nil]
"t" means the input soundfile will be read backwards (each grain will read backwards but of course this is independent from grain-start, for example we could be advancing forwards in the file but reading the samples backwards).
reverb-amount [number]
amount of sound to be sent to the reverb output stream.
grain-distance [number or envelope]
distance to the listener position (for locsig)
grain-distance-spread [number or envelope]
random deviation from the value of grain-distance
grain-degree [number or envelope]
angular position (for locsig)
grain-degree-spread [number or envelope]
random deviation from the value of grain-degree

Some examples...

  • Just a silly stuttering gong:
    (with-sound()(grani 0 8 2 
  • Smaller grains:
    (with-sound()(grani 0 8 3 
                        :grain-duration 0.06 :grain-density 40 
                        :grain-density-spread 5 :grain-start '(0 0.3 1 0.4)))
  • Smaller grains, downsampled by an octave:
    (with-sound()(grani 0 8 3 
                        :grain-duration 0.06 :grain-density 40 
                        :grain-density-spread 5 :grain-start '(0 0.1 1 0.22) 
                        :srate -12))
  • Smaller grains, downsampled by a 0.3x factor. Note that we change the position were we start sampling the input file and create a big spread there so that we are actually jumping around randomly:
    (with-sound()(grani 0 8 3 
                        :grain-duration 0.06 :grain-density 40 :grain-density-spread 5 
                        :grain-start '(0 0.1 1 0.22) :grain-start-spread 0.2 :srate -12))
  • Three notes spread in the stereo field:
    (with-sound(:channels 2 :statistics t)
    (grani 0 8 0.4 "/usr/ccrma/web/html/courses/220b/lectures/2/sounds/tubular-bell.snd" 
           :grain-duration 0.06 :grain-density 20 :grain-density-spread 2 :srate 0)
    (grani 0 8 0.4 "/usr/ccrma/web/html/courses/220b/lectures/2/sounds/tubular-bell.snd" 
           :grain-duration 0.08 :grain-density 20 :grain-density-spread 2 :srate 5  
           :grain-degree 0)
    (grani 0 8 0.4 "/usr/ccrma/web/html/courses/220b/lectures/2/sounds/tubular-bell.snd" 
           :grain-duration 0.05 :grain-density 35 :grain-density-spread 3
           :srate -5 :grain-degree 90))

    Try replacing the turkish-cymbal-1.snd soundfile instead of the tubular-bell...

Here's the "one-cut" instrument, a soundfile mangling machine... (cut.ins). Here are some parameter examples you can paste into a with-sound.

Some loop macro examples, from the class

So here they are, keystroke by keystroke (including typos) "what the instructor typed in" guide to the loop macro and its use in (very coarse) control of clm instruments. Some comments were added to the code to make it clearer. You will need to compile and load the fm-violin instrument to make this examples work.

If you're slightly out of your mind you can actually look at the whole thing...

Here's a pointer to the Steele's Common Lisp book chapter on the loop macro

Assignment #2 [due on January 23rd, 2002]

Use the "grani" granular synthesis instrument and the "one-cut" instrument for processing (ie: mangling) soundfiles. Create some interesting transformations on the input. There are some soundfiles available for use (or you can roll your own - I'm sure you must have materials left over from 220a). Just in case, you can find a small gong in "/usr/ccrma/web/html/courses/220b/lectures/2/sounds/small-gong.snd", a small cowbell in the same directory (file name "cencerros-1.snd"), a cymball and a tubular bell.

Your assignment has to use the loop macro for two purposes: a) to create a texture of several notes, and b) to create very complex envelopes for the grani instrument algorithmically.

CLM envelopes are simple lisp lists made of pairs of components, the first component of each pair represents relative time, the second the actual value of the quantity you want to control with the envelope. For example, a triangular amplitude envelope might be expressed as:

  '(0 0 0.5 1 1 0)

You can use the "collect" keyword in the loop macro to accumulate values in a list, a random envelope could be created with the collowing code:

    ;; ten breakpoints in the envelope
    repeat 10
    ;; x axis of the envelope
    for time from 0 by 0.1
    ;; y axis of the envelope
    for y = (+ 0.1 (random 1.0))
    collect time
    collect y)

Note the collect statements at the end of the loop, they collect values in a list that is returned when the loop finishes. Ahem, obviously a completely random envelope is not really interesting...

Have fun!

©1998,2001-2002 Fernando Lopez-Lezcano. All Rights Reserved.