Cabbage Logo
Back to Cabbage Site

"Beginner Synth" Tutorial code, LFO not working

I don’t even have to paste my own code here, as it is the same problem with the code on the Tutorial “Beginner Synth”.
The LFO does not work. All it does is giving me a loud click! Please could somebody help me figure out the problem that is causing the loud clicks? Does it run on your setup?
And also: Does Cabbage or Csound have a limiter so my ears and hardware are not harmed by generating loud signals?

Here is the full code taken from http://cabbageaudio.com/docs/beginner_synth/:

`
form caption(“Simple Synth”) size(310, 310), colour(58, 110, 182), pluginID(“def1”)
keyboard bounds(12, 164, 281, 95)
rslider bounds(12, 14, 70, 70), channel(“att”), range(0, 1, 0.01, 1, .01), text(“Attack”)
rslider bounds(82, 14, 70, 70), channel(“dec”), range(0, 1, 0.5, 1, .01), text(“Decay”)
rslider bounds(152, 14, 70, 70), channel(“sus”), range(0, 1, 0.5, 1, .01), text(“Sustain”)
rslider bounds(222, 14, 70, 70), channel(“rel”), range(0, 1, 0.7, 1, .01), text(“Release”)
rslider bounds(12, 84, 70, 70), channel(“cutoff”), range(0, 22000, 2000, .5, .01), text(“Cut-Off”)
rslider bounds(82, 84, 70, 70), channel(“res”), range(0, 1, 0.7, 1, .01), text(“Resonance”)
rslider bounds(152, 84, 70, 70), channel(“LFOFreq”), range(0, 10, 0, 1, .01), text(“LFO Freq”)
rslider bounds(222, 84, 70, 70), channel(“amp”), range(0, 1, 0.7, 1, .01), text(“Amp”)



-n -d -+rtmidi=NULL -M0 -m0d --midi-key-cps=4 --midi-velocity-amp=5


; Initialize the global variables.
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1

;instrument will be triggered by keyboard widget
instr 1
iFreq = p4
iAmp = p5

iAtt chnget “att”
iDec chnget “dec”
iSus chnget “sus”
iRel chnget “rel”
kRes chnget “res”
kCutOff chnget “cutoff”
kLFOFreq chnget “LFOFreq”
kAmp chnget “amp”

kEnv madsr iAtt, iDec, iSus, iRel
aOut vco2 iAmp, iFreq
kLFO lfo 1, kLFOFreq
aLP moogladder aOut, kLFOkCutOff, kRes
outs kAmp
(aLPkEnv), kAmp(aLP*kEnv)
endin

;causes Csound to run for about 7000 years... f0 z `

Well, the click and pop occurs in this example, because of these two lines of code:

kLFO lfo 1, kLFOFreq
aLP moogladder aOut, kLFO*kCutOff, kRes

What happens here is, kLFO’s range goes from -1 to 1. We are then multiplying this with the Cut off frequency, kCutOff. The cut off frequency of a filter should always be a positive number. I think if it goes negative, it while induce some sort of aliasing. Here, it is going below zero. If you use a cut off frequency of 2000Hz, say, the LFO oscilates it from 2000 to -2000.

Simple solutions:

Move the LFO out of the negative domain. Adding this line:

kLFO *=  0.5
kLFO += 0.5

will 1) convert the range from -1 to 1 to -0.5 to +0.5
2) then shift the range to 0 to +1

When zero, it will cancel of the sound since a cut off frequency of zero would mean cutting off all the frequencies. This is a cool effect on it’s own. But if you want a continuous sound without breaks, introduce an offset frequency, so that the cutoff is always a positive number. The snippet of the code should look something this this:

iFreqOffset = 500
aLFO poscil  1, kLFOFreq
aLFO *= .5
aLFO += .5
aLP moogladder  aOut, aLFO*kCutOff + iFreqOffset, kRes

@rorywalsh You might have to fix the example!

The next part of your question, regarding whether a failsafe against loud clicks and pops exists, could be answered by somebody more experienced.

Sorry my bad. I usually use the lfo with a mode of 5, but I forgot to put it in this time. So simply changing the LFO line to:

LFO lfo 1, kLFOFreq, 5

Should fix it. I will update my code. Thanks for the heads up.

p.s. of course the fix given by @Thrifleganger will also work.