Tutorial 3



Resonant filters with note envelopes

In this tutorial we'll refine the last instrument, by placing a resonant low-pass filter, which varies in time. The filter will decrease at the start of the note and then goes back up again when the note is released.

We will also use another f-table, this time an approximation of a square wave which looks like:






Here it goes:



<CsoundSynthesizer>

<CsOptions>

-+P12 -+K0 -m0 -+O

;I've added two more flags which disable messages for better realtime performance

</CsOptions>

<CsInstruments>

sr=44100

kr=441

ksmps=100

nchnls=2

;The usual headers

massign 1,3

;Assign MIDI channel 1 to instrument 3

instr 3

icps cpsmidi

ivel veloc

;The usual note and velocity assignments



kenv linsegr 0,0.1,10000* (ivel/128), 0.5, 0

;I've modified the attack and release time of the envelope for this instrument
;This time we have a faster attack (0.1)and release(0.5).

kfilt expsegr 1,0.7,0.001,0.5,0.5

;The opcode expsegr is another envelope generator, but this time it will control
;The filter cutoff frequency. Expsegr generates an envelope composed not of straight
;line segments but of exponential segments. See the image at the bottom
;to see what this envelope looks like. In Csound envelope generators which end in 'r'
;depend on a midi note off message for their final release segment. In this case,
;the value 0.001 is held until a note off message is received, and the it rises back to 0.5
;in 0.5 seconds, the same time used in the other envelope generator for amplitude decay.



kcutoff = (kfilt*icps)

;The cutoff frequency should depend on the actual frequency of the note to create a similar
;effect for each note, that's why the actual cutoff frequency is the value of kfilt times
;the frequency of the note.



asig oscil3 1, icps, 1

;The oscillator oscil3 is the same as oscil and oscili but performs
;cubic interpolation, a better interpolation, but again more processor consuming.
;f-table 1 now contains a square wave.



aout lowres asig, kcutoff, 1.2

;The opcode lowres is a resonant low-pass filter (the one usually found on analog synths)
;The first parameter in this opcode is the signal to be processed, next the cutoff frequency
;which in this case is the variable envelope stored in kcutoff, and has a resonance of 1.2
;This resonance value was chosen by ear. Notice how the filter goes down during the attack of the note.
;It then stays constant after reaching a certain point, and then goes back up again
;when the note is released.

outs aout*kenv , aout*kenv

;Sends the oscillator's scaled signal to the outputs.



endin



</CsInstruments>



<CsScore>



f 1 0 4096 10 1 0 0.5 0 0.25 0 0.125 0 0.0625

;Generates a curve approximating a square wave, from a sum of sinusoids.



f 0 120

</CsScore>

</CsoundSynthesizer>



The curve for the filter envelope looks something like:




The flat part in the middle doesn't have a definite duration, it depends on how long the note is held down.

You may want to play around using other f-tables, or changing the resonance value.

Also change oscil3 to oscili or oscil and notice the big difference in sound for each.



Back to the MIDI tutorials page