Keith Hamel's Csound Course

Week 7

In this tutorial:


Index Next Week Previous Week


Csound random generators - rand, randh and randi

There are a wide range of uses for random generators in a sound synthesis language. They can be used for anything from subtle adjustments to controls (to give them a less mechanistic feel) to producing full-band white noise. Essentially, all the random generators do is produce random values within a certain range - the range is always the first argument to the random functions. What you do with the generated values depends on what the purpose of the generator is.

The random generators in Csound are rand, randh, and randi. All of these generators can be used at audio rate (a- rate) or at control rate (k- rate). They produce random numbers in the range specified by the first argument. (The actual values produced by the generator are between negative first-argument and positive first-argument - if your first argument is 20, random numbers between -20 and +20 will be generated.) randh and randi require two arguments: the first for the range of values and the second for the rate at which new values are generated. All of the generators can take an optional argument - a seed value. Different seed value are used so that random generators produce a different series of random values. If you are only using one random generator, you do not need to specify a seed value.

The difference between randh and randi is that randh retains the last calculated value until the next value is calculated, while randi interpolates new values between one calculation and the next. randi, therefore, generates continuously changing values, while randh holds the current value until the next one is generated.

In all of the random generators the first argument represents the range of values.

a1   rand 1000       ; produces values between -1000 and 1000
k1   randh 20, 5     ; produce values between -20 and 20, 5b times a second

If we want to offset the range, we can add or subtract the returned value by some constant. This constant will be the new centre value (replacing 0);

a1   rand 1000       ; produces values between -1000 and 1000
a1   =   a1 + 5000   ; shifts values to between 4000 and 6000

k1   randh 20, 5     ; produce values between -20 and 20, 5 times a second
k1   =   k1 + 100    ; shifts values to between 80 and 120

goto top Index


Using rand to Produce White Noise

Broad-band white noise can be simulated by using rand (at audio rate), and specifying the range of values. Since there will be no periodicity when the random numbers are generated, the result will be noise. The first argument of the rand will be the amplitude of the audio signal

asig   rand   10000    ; produce values between -10000 and 1000 
outs   asig, asig

Since the values we pass to rand do not really have any bearing on the resultant frequencies, this method is useful only if we want full spectrum noise. If we want more control over the frequency band of the noise we use the method described below.

goto top Index


Using randh and randi to Produce Bands of Noise

Rather than using the output from the random generator directly as an audio signal, the random values can be passed to the frequency arguments of an oscillator. As long as new (random) frequency values are generated often enough, the oscillator will produce a band of noise. Normally, randh is used to generate these random values. The first argument (the range of values) will be the 1/2 the band width of the noise (since both positive and negative values are created.) The second argument (the rate at which new values are created) must be large enough that no single frequency is around long enough to be heard. The output from the randh is added to the desired centre frequency of the noise band and used as the frequency argument in a sine wave oscillator.

kran   randh  50, 2205       ; produce values between -50 and 50
kran   =   kran + 200        ; shift values to centre freq of 200
asig   oscil  p4, kran, 1    ; generate a band of noise
outs   asig, asig            ; output sound

The maximum rate of a randh or randi which is generating a k- variable is the control rate (as set at the top of your orchestra) - usually 2205. If you want to run at the maximum rate, you can use the global variable kr as in:

kran randh 50, kr ; produce values between -50 and 50 

randi can be used instead of randh - the sound of the noise is slightly different since the frequency changes are smoother and continuous.


The advantages of sending the output from the random generator to an oscil are:

1) an envelope (or other amplitude control) can easily be applied to sound.

kenv   linseg  0, p3 * .5, 1, p3 * .5, 0  ; up-down ramp envelope
kran   randh   50, kr                     ; produce values between -50 and 50
kran   =   kran + 200                     ; shift values to centre of 200
asig   oscil   kenv * p4, kran, 1         ; generate a band of noise
outs   asig, asig                         ; output sound


2) since the bandwidth is controlled by the range argument (i.e. the first argument) of the randh, a control can be used to modify the bandwidth

kenv   linseg  0, p3 * .5, 1, p3 * .5, 0  ; up-down ramp envelope
kband  line    50, p3, 5                  ; ramp bandwidth from 100 to 10
kran   randh   kband, kr                  ; produce values between - and + kband
kran   =   kran + 200                     ; shift values to centre of 200
asig   oscil   kenv * p4, kran, 1         ; generate a band of noise
outs   asig, asig                         ; output sound


3) since the centre frequency of the band is controlled by the value added to the output from the randh (200 in the above example), the entire band can be shifted over time if a control is applied to this value. (A gliss of noise will result).

kenv   linseg  0, p3 * .5, 1, p3 * .5, 0  ; up-down ramp envelope
kran   randh   50, kr                     ; produce values between -50 and 50
kcent  line    1000, p3, 200              ; rampcentre freqency 1000 to 200
kran   =   kran + kcent                   ; shift rand values to base frequency
asig   oscil   kenv * p4, kran, 1         ; generate a band of noise
outs   asig, asig                         ; output sound


goto top Index


Some Uses of Noise

Noise can be used to add a percussive attack to an instrument. To do this, place a short, sharp envelope on the noise, and add the noise to the other audio signal:

kenv1  linseg  1, .1, 0, p3 - .1, 0    ; short sharp envelope
kran   randh   1000, kr                ; produce range of 1000
kran   =   kran + 2000                 ; centre freq is 2000
anoise oscil   kenv1 * p4, kran, 1     ; noise oscillator

kenv2  expon   1, p3, .0001            ; exponential decay
asig   foscili p4*kenv2, cpspch(p5), 1, 1.414, 2, 1 ; bell foscil 
aout   =   (asig + anoise) * .5
outs   aout, aout                      ; output mix of sounds


A thin, band of noise can add a breathy quality to a tone. A very thin band will sound like a whistle.

iptch  =   cpspch(p5)                ; centrepitch
iwidth =   iptch * .01               ; width = 1 /100 of cps
kenv1  linseg  1, p3, 0              ; ramp envelope
kran   randh   iwidth, kr            ; generate range of iwidth
kran   =   kran + iptch              ; centre on iptch
asig   oscil   kenv1 * p4, kran, 1   ; noise oscillator
outs   asig, asig


goto top Index


Other Uses of Random Generators

If the rates at which randh and randi are generated are reduced, they can be used in many other ways. For example, an interpolating random generator (randi) with a very small range and a very slow rate can be applied to a constant value to create slight fluctuations. This can have the effect of making the sound appear less predictable and thus sound more natural.

krand  randi   1, .5                     ; range is -1 to +1, rate is every 2 seconds
asig   oscil   p4, cpspch(p5) + krand, 1 ; oscillator with 2 cps freq deviation
outs   asig, asig

This type of slight fluctuation could be applied to almost anything: a pitch, a vibrato speed, a vibrato depth, a modulation ratio, a modulation index, etc...


In following example, the position of the sound moves randomly in stereo space. Since stereo space can be viewed as a continuum between 0 and 1 (left - right channels), the range of values generated by the randi will be .5 (giving us -.5 to .5). This value will then be shifted by .5 to give us values of 0 to 1. The speed can be any constant or changing (or random) value.

asig   oscil p4, cpspch(p5), 1      ; simple oscillator
kpan   randi .5, 5                  ; range is .5, rate is 5 / sec
kpan   =   kpan + .5
outs   asig * kpan, asig * (1-kpan) ; move randomly in stereo space

goto top Index


Soundin

Soundin is a very simple, but powerful generator. Simply put, it allows already existing sound files to be read back into an instrument. The arguments to soundin are the number associated with the soundfile to be read, and (optionally) the time (in seconds) in the file where the reading begins. The length of the reading is determined by the value of p3 in the score.

In order to access an existing file it must:

  1. be an AIFF (Audio Interchange File Format) soundfile in stereo or mono
  2. be located in either your Samples or Sounds directory (in the Csound folder).
  3. have been generated at the same sampling rate as the orchestra you are reading the file into.
  4. must be renamed as soundin.# (eg, soundin.1, soundin.2)
In order to use one of your existing soundfiles, copy it into the Samples directory, by dragging the file's icon, then change the name of the file to soundin.#

Since we are using a sampling rate of 22050 in stereo, this should be the format of all sound files you access with soundin. If the file is not currently at this rate, you can use the Change Format feature of SoundHack to change the sampling rate and number of channels. (If you try to read an incompatible format, a warning message will be printed and the soundfile will either be compressed or stretched in time).

Unlike the operators examined so far, soundin can return values into 2 variables separated by a comma (these are the values of left and right channels in the stereo soundfile).

asigl, asigr  soundin  1, 1.5   ; soundin file 1 starting at 1.5 seconds

Once the values have been read from the soundfile, they can be processed in any way you desire. However, if they are already complex sounds (such as speech) there is only a limited amount of processing that can be done without destroying the original sound. (Filtering, one of the most useful processing methods will be discussed later in the course).

Example: read sound called soundin.1 and oscillate it quickly between channels

asigl, asigr  soundin 1, 1.5             ; soundin file 1 starting at 1.5 seconds 
kpan   oscil .5, 30, 1                   ; oscillate -.5 to .5, 30 times a second
kpan   =   kpan + .5                     ; offset to 0 - 1 (30 times a second)
outs1  asigl * kpan + asigr * (1 - kpan) ; shift sound in stereo
outs2  asigr * kpan + asigl * (1 - kpan) ; shift sound in stereo

goto top Index


Suggested Assignment

1) Create an orchestra with at least 3 percussive instruments. One of the instruments should be drum-like, one should be woodblock-like, and the other(s) can be of your own choice. (You should use a random generator in at least two of the instruments). Create a score (c 30") to demonstate the instruments.

2) (Optional). Record a short text fragment and save this sound in your Samples directory (as soundin.1). Create a soundin instrument which accesses short fragments of the text (with or without processing).


goto top Index Next Week Previous Week