Index | Next Week | Previous Week |
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
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.
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
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
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
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...
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
In order to access an existing file it must:
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
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).
Index | Next Week | Previous Week |