Cabbage Logo
Back to Cabbage Site

Some noobie questions

Hi fellow cabbages!
I’m new to the whole csound/cabbage audio side of things and am coming across one or two problems with the synth I am building any help would be appreciated!

  1. How do I put an ADSR on a filter. Ie, I want a filter envelope.
  2. My master volume does not work for some reason.

;outs amixL, amixR
outs amixLklevel, amixRklevel

The first line of code works but when I multiply by klevel I cannot hear anything which is strange.

I do have more questions / problems but I’ll leave it there for now in the hope these can get answered first! Thanks to anyone who replies in advance!

D

<Cabbage>
form caption("Synth") size(725, 485), colour(0, 0, 0), pluginID("def1")
keyboard bounds(5, 375, 710, 100)

groupbox bounds(15,15,255,110),	text("Oscil 1"), plant("oscil1"){ 
rslider bounds(5,35,65,65), 	channel("octave1"), text("Octave"),	colour(400,100,100), 	trackercolour(0,400,0), range(-2,2,0)
rslider bounds(65,35,65,65), 	channel("semi1"),  	text("Semi"),	colour(400,100,100),	trackercolour(0,400,0), range(-12,12,0)
rslider bounds(125,35,65,65), 	channel("cent1"), 	text("Cent"),	colour(400,100,100), 	trackercolour(0,400,0), range(-1,1,0)
rslider bounds(185,35,65,65), 	channel("gain1"), 	text("Level"),	colour(400,100,100), 	trackercolour(0,400,0), range(0,1,.5)
}

groupbox bounds(15,140,255,110),text("Oscil 2"), plant("oscil2"){
rslider bounds(5,35,65,65), 	channel("octave2"),	text("Octave2"),	colour(400,100,100),	trackercolour(0,400,0), range(-2,2,0)
rslider bounds(65,35,65,65), 	channel("semi2"),	text("Semi2"),	colour(400,100,100),		trackercolour(0,400,0), range(-12,12,0)
rslider bounds(125,35,65,65), 	channel("cent2"),	text("Cent2"),	colour(400,100,100),		trackercolour(0,400,0), range(-1,1,0)
rslider bounds(185,35,65,65), 	channel("gain2"),	text("Level2"),	colour(400,100,100),		trackercolour(0,400,0), range(0,1,.5)
}
groupbox bounds(293,15,100,100),text("Drive"), plant("drive1"){
rslider bounds(20,30,55,55),  	channel("drive"),	colour(400,100,100),	trackercolour(0,400,0), range(0,1000,0,.5)
}

groupbox bounds(415,15,135,100),text("LP Filter"), plant("filer2"){
rslider bounds(5,30,65,65),		channel("cutoff2"), text("Cutoff"),			colour(400,100,100),	trackercolour(0,400,0), range(0,300,100)
rslider bounds(65,30,65,65), 	channel("res2"), 	text("Res"),			colour(400,100,100),	trackercolour(0,400,0), range(0,1,.1)
}

groupbox bounds(415,130,135,100),text("HP Filter"), plant("filer1"){
rslider bounds(5,30,65,65),		channel("cutoff1"), text("Cutoff1"),			colour(400,100,100),	trackercolour(0,400,0), range(1,20000,30,.2)
rslider bounds(65,30,65,65), 	channel("res1"), 	text("Res1"),				colour(400,100,100),	trackercolour(0,400,0), range(1,100,1)
}

groupbox bounds(350,265,250,95),text("Amp Env"), plant("amp1"){
rslider bounds(5,30,65,65), 	channel("att"), 	text("Attack"),		colour(400,100,100),	trackercolour(0,400,0), range(0.01,5,.05,.5)
rslider bounds(65,30,65,65), 	channel("dec"), 	text("Decay"),		colour(400,100,100),	trackercolour(0,400,0), range(0.0,3,1)
rslider bounds(125,30,65,65), 	channel("sus"), 	text("Sustain"),	colour(400,100,100),	trackercolour(0,400,0), range(0.0,3,1)
rslider bounds(185,30,65,65), 	channel("rel"), 	text("Release"),	colour(400,100,100),	trackercolour(0,400,0), range(0.01,3,.3,.7)
}

groupbox bounds(600,15,100,250),text("FilterEnv"), plant("filterenv"){
rslider bounds(20,30,65,65), 	channel("att2"), 	text("Attack2"),		colour(400,100,100),	trackercolour(0,400,0), range(0.01,5,.1)
rslider bounds(20,100,65,65), 	channel("dec2"), 	text("Decay2"),			colour(400,100,100),	trackercolour(0,400,0), range(0,3,.5)
rslider bounds(20,170,65,65), 	channel("sus2"), 	text("Sustain2"),		colour(400,100,100),	trackercolour(0,400,0), range(0,3,.5)
rslider bounds(20,200,65,65), 	channel("rel2"), 	text("Release2"),		colour(400,100,100),	trackercolour(0,400,0), range(0.01,3,.5)
}
groupbox bounds(130,265,200,95),text("Reverb"), plant("reverb1"){
rslider bounds(5,30,65,65), 	channel("drywet")	text("Dry/Wet"),colour(400,100,100),trackercolour(0,400,0), range(0,1,0)
rslider bounds(65,30,65,65), 	channel("size")		text("Size"),	colour(400,100,100),trackercolour(0,400,0), range(0,1,.1)
rslider bounds(125,30,65,65), 	channel("lp")		text("Lp"),		colour(400,100,100),trackercolour(0,400,0), range(0,1,.1)
}

groupbox bounds(15,265,95,95),text("Master"),
rslider bounds(35,295,55,55), channel ("master"), 	colour(400,100,100), trackercolour(0,400,0), range(0,1,1)

</Cabbage>
<CsoundSynthesizer>
<CsOptions>

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

</CsOptions>
<CsInstruments>

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

instr 1

kGain 	chnget 	"gain1"
ksemi	chnget	"semi1"
kdetune	chnget	"cent1"
koctave chnget 	"octave1"

kGain2 		chnget 	"gain2"
ksemi2		chnget	"semi2"
kdetune2	chnget	"cent2"
koctave2 	chnget 	"octave2"

kdrive 	chnget "drive"

kcutoff2 chnget "cutoff2"
kres2	chnget "res2"

kcutoff1 chnget "cutoff1"
kres1	chnget "res1"

iatt	chnget	"att"
idec	chnget	"dec"
isus	chnget	"sus"
irel	chnget	"rel"

iatt2	chnget	"att2"
idec2	chnget	"dec2"
isus2	chnget	"sus2"
irel2	chnget	"rel2"

kdrywet chnget "drywet"
kreverbsize chnget "size" 
kreverblp chnget "lp"

klevel chnget "master"

;MIDI SETUP
imidinn notnum 			;notnum — Get a note number from a MIDI event.
iamp	ampmidi	1		;ampmidi — Get the velocity of the current MIDI event.
kbend	pchbend 0,2  	;pchbend — Get the current pitch-bend value for this channel.
koctavesel = octave (koctave)		;octave — Calculates a factor to raise/lower a frequency by a given amount of octaves.
kfreq1 = cpsmidinn(imidinn+kbend+int(ksemi)+kdetune) ;cpsmidinn — Converts a Midi note number value to cycles-per-second. 
kfreq1 = kfreq1*koctavesel

koctavesel2 = octave (koctave2)		;octave — Calculates a factor to raise/lower a frequency by a given amount of octaves.
kfreq2 = cpsmidinn(imidinn+kbend+int(ksemi2)+kdetune2) ;cpsmidinn — Converts a Midi note number value to cycles-per-second. 
kfreq2 = kfreq2*koctavesel2


asig oscil p5, kfreq1, 1 ;Oscil 1

asig2 oscil p5, kfreq2, 2 ;Oscil 2

aout = asig*kGain + asig2*kGain2 ; Oscil 1 + Oscil 2

adrive lpf18 aout, kcutoff2, kres2, kdrive ; lpf18 — A 3-pole sweepable resonant lowpass filter.

ahp bqrez adrive, kcutoff1, kres1, 1 ;ares bqrez asig, xfco, xres [, imode] [, iskip]

kadsr	mxadsr	iatt,idec,isus,irel ;mxadsr — Calculates the classical ADSR envelope using the expsegr mechanism.

ahp = ahp*kadsr ;Apply amp adsr to output

denorm ahp ;denorm — Mixes low level noise to a list of a-rate signals

aoutL, aoutR freeverb  ahp, ahp, kreverbsize, kreverblp ;Reverb


amixL		ntrpol		ahp, aoutL, kdrywet	;CREATE A DRY/WET MIX BETWEEN THE DRY AND THE REVERBERATED SIGNAL
amixR		ntrpol		ahp, aoutR, kdrywet	;CREATE A DRY/WET MIX BETWEEN THE DRY AND THE REVERBERATED SIGNAL



outs amixL, amixR
;outs amixL*klevel, amixR*klevel

endin

</CsInstruments>
<CsScore>
;causes Csound to run for about 7000 years...

f1 0 1024 10 1 ; Sin wave
f2 0 16384 10 1 0.5 0.3 0.25 0.2 0.167 0.14 0.125 .111 ; Saw wave
f0 2000


</CsScore>
</CsoundSynthesizer>

You’ve created an ADSR with this line of code:

kadsr mxadsr iatt,idec,isus,irel ;mxadsr — Calculates the classical ADSR envelope using the expsegr mechanism.

Now all you need to do is use this variable to affect your filter cutoff. For example, if I wanted to use it to control a lowpass filter I might do something like this:

aOut moogladder aSignal, kadsr*10000, 1

The 10000 represents the cutoff frequency. This is required as madsr only moves between 0 and 1. You can take a look at this simple instrument:


It also uses ADSR envelopes to affect filters.

Thanks for the really quick reply Rory much appreciated!!
I will try this now and get back to you with how things have gone.
Thanks again for the help!

Cheers that seemed to do the job!
Thanks again

Glad it worked out. Let us know if you have any further issues.

Hey Rory,

I’m having another bit of a problem with some Cabbage code.

Basically my filter envelope controls are effecting my amp envelope and not only my filter envelope. Sorry to bother but I have spend 2 weeks trying to fix this with no luck!.I was wondering if you could take a quick look whenever you have a chance no rush it’d be much appreciated!

Thanks a mill

form caption("Synth") size(800, 485), colour(0, 0, 0), pluginID("def1") keyboard bounds(5, 375, 790, 100)

groupbox bounds(15,15,255,110), text(“Oscil 1”), plant(“oscil1”){
rslider bounds(5,35,65,65), channel(“octave1”), text(“Octave”), colour(400,100,100), trackercolour(0,400,0), range(-2,2,0)
rslider bounds(65,35,65,65), channel(“semi1”), text(“Semi”), colour(400,100,100), trackercolour(0,400,0), range(-12,12,0)
rslider bounds(125,35,65,65), channel(“cent1”), text(“Cent”), colour(400,100,100), trackercolour(0,400,0), range(-1,1,0)
rslider bounds(185,35,65,65), channel(“gain1”), text(“Level”), colour(400,100,100), trackercolour(0,400,0), range(0,.15,.075)
}

groupbox bounds(15,140,255,110),text(“Oscil 2”), plant(“oscil2”){
rslider bounds(5,35,65,65), channel(“octave2”), text(“Octave2”),colour(400,100,100), trackercolour(0,400,0), range(-2,2,0)
rslider bounds(65,35,65,65), channel(“semi2”), text(“Semi2”), colour(400,100,100), trackercolour(0,400,0), range(-12,12,0)
rslider bounds(125,35,65,65), channel(“cent2”), text(“Cent2”), colour(400,100,100), trackercolour(0,400,0), range(-1,1,0)
rslider bounds(185,35,65,65), channel(“gain2”), text(“Level2”), colour(400,100,100), trackercolour(0,400,0), range(0,.15,.075)
}
groupbox bounds(293,15,100,110),text(“Drive”), plant(“drive1”){
rslider bounds(15,35,65,65), channel(“drive”), text(“Drive”), colour(400,100,100), trackercolour(0,400,0), range(0.00,1.5,0,.5)
}

groupbox bounds(415,15,135,110),text(“LP Filter”), plant(“filer2”){
rslider bounds(5,35,65,65), channel(“cutoff2”), text(“Cutoff”), colour(400,100,100), trackercolour(0,400,0), range(0,18000,18000,.2)
rslider bounds(65,35,65,65), channel(“res2”), text(“Res”), colour(400,100,100), trackercolour(0,400,0), range(0,1,.1)
}

groupbox bounds(415,140,135,110),text(“HP Filter”), plant(“filer1”){
rslider bounds(5,35,65,65), channel(“cutoff1”), text(“Cutoff1”), colour(400,100,100), trackercolour(0,400,0), range(1,20000,1,.2)
rslider bounds(65,35,65,65), channel(“res1”), text(“Res1”), colour(400,100,100), trackercolour(0,400,0), range(0.01,1,.1)
}

groupbox bounds(350,265,250,95),text(“Amp Env”), plant(“amp1”){
rslider bounds(5,30,65,65), channel(“att”), text(“Attack”), colour(400,100,100), trackercolour(0,400,0), range(0.01,5,.01,.5)
rslider bounds(65,30,65,65), channel(“dec”), text(“Decay”), colour(400,100,100), trackercolour(0,400,0), range(0.01,3,1)
rslider bounds(125,30,65,65), channel(“sus”), text(“Sustain”), colour(400,100,100), trackercolour(0,400,0), range(0.01,3,1)
rslider bounds(185,30,65,65), channel(“rel”), text(“Release”), colour(400,100,100), trackercolour(0,400,0), range(0.01,3,.1,.7)
}

groupbox bounds(570,15,100,150),text(“Lfo”), plant(“FilterLfo”){
rslider bounds(20,30,55,55), channel(“lfo”), colour(400,100,100), trackercolour(0,400,0), range(0,100,.5)
rslider bounds(20,90,55,55), channel(“lforate”), colour(400,100,100), trackercolour(0,400,0), range(0,100,.5,.4)
}

groupbox bounds(690,15,100,345),text(“FilterEnv”), plant(“filterenv”){
rslider bounds(20,30,65,65), channel(“att2”), text(“Attack2”), colour(400,100,100), trackercolour(0,400,0), range(0.01,5,.05,.5)
rslider bounds(20,100,65,65), channel(“dec2”), text(“Decay2”), colour(400,100,100), trackercolour(0,400,0), range(0.00,3,1)
rslider bounds(20,170,65,65), channel(“sus2”), text(“Sustain2”), colour(400,100,100), trackercolour(0,400,0), range(0.001,3,1)
rslider bounds(20,240,65,65), channel(“rel2”), text(“Release2”), colour(400,100,100), trackercolour(0,400,0), range(0.01,3,.3,.7)
}
groupbox bounds(130,265,200,95),text(“Reverb”), plant(“reverb1”){
rslider bounds(5,30,65,65), channel(“drywet”) text(“Dry/Wet”),colour(400,100,100),trackercolour(0,400,0), range(0,1,0)
rslider bounds(65,30,65,65), channel(“size”) text(“Size”), colour(400,100,100),trackercolour(0,400,0), range(0,1,.5)
rslider bounds(125,30,65,65), channel(“lp”) text(“Lp”), colour(400,100,100),trackercolour(0,400,0), range(0,1,.5)
}

groupbox bounds(293,140,100,110),text(“Stereo”), plant(“spread”){
rslider bounds(20,35,65,65), channel(“spread”), text(“Spread”), colour(400,100,100), trackercolour(0,400,0), range(0,1,.5)
}

groupbox bounds(15,265,95,95),text(“Master”),
rslider bounds(35,295,55,55), channel(“master”), colour(400,100,100), trackercolour(0,400,0), range(0,1,1)

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

sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1

garvbsig init 0 ; global “a” variable initialized to 0

instr 1

kGain chnget “gain1”
ksemi chnget “semi1”
kdetune chnget “cent1”
koctave chnget “octave1”

kGain2 chnget “gain2”
ksemi2 chnget “semi2”
kdetune2 chnget “cent2”
koctave2 chnget “octave2”

kdrive chnget “drive”

kcutoff2 chnget “cutoff2”
kres2 chnget “res2”

kcutoff1 chnget “cutoff1”
kres1 chnget “res1”

klfoamp2 chnget “lfo”
klforate chnget “lforate”

iatt chnget “att”
idec chnget “dec”
isus chnget “sus”
irel chnget “rel”

iatt2 chnget “att2”
idec2 chnget “dec2”
isus2 chnget “sus2”
irel2 chnget “rel2”

kspread chnget “spread”

klevel chnget “master”

;MIDI SETUP
imidinn notnum ;notnum — Get a note number from a MIDI event.
iamp ampmidi 1 ;ampmidi — Get the velocity of the current MIDI event.
kbend pchbend 0,2 ;pchbend — Get the current pitch-bend value for this channel.
koctavesel = octave (koctave) ;octave — Calculates a factor to raise/lower a frequency by a given amount of octaves.
kfreq1 = cpsmidinn(imidinn+kbend+int(ksemi)+kdetune) ;cpsmidinn — Converts a Midi note number value to cycles-per-second.
kfreq1 = kfreq1*koctavesel

koctavesel2 = octave (koctave2) ;octave — Calculates a factor to raise/lower a frequency by a given amount of octaves.
kfreq2 = cpsmidinn(imidinn+kbend+int(ksemi2)+kdetune2) ;cpsmidinn — Converts a Midi note number value to cycles-per-second.
kfreq2 = kfreq2*koctavesel2

asig oscil p5, kfreq1, 1 ;Oscil 1
asig2 oscil p5, kfreq2, 2 ;Oscil 2

aout = asigkGain + asig2kGain2 ; Oscil 1 + Oscil 2

aadsr mxadsr iatt, idec, isus, irel ;mxadsr — Calculates the classical ADSR envelope using the expsegr mechanism.
kadsrfilter mxadsr iatt2, idec2, isus2, irel2 ; Filter Envelope

adrive lpf18 aout, kcutoff2*kadsrfilter, kres2, kdrive ; Low pass filter with envelope

ahp = adrive*aadsr ;Apply amp adsr to output

outs ahpklevel, ahpklevel

garvbsig = garvbsig + (ahp*klevel)

endin

instr 99

kreverbsize chnget “size”
kreverblp chnget “lp”

kdrywet chnget “drywet”
kreverbsize chnget “size”
kreverblp chnget “lp”

kspread chnget “spread”

asigL, asigR freeverb garvbsig, garvbsig, kreverbsize, kreverblp ; put global sig into reverb

amixLeft ntrpol garvbsig, asigL, kdrywet ;CREATE A DRY/WET MIX BETWEEN THE DRY AND THE REVERBERATED SIGNAL
amixRight ntrpol garvbsig, asigR, kdrywet ;CREATE A DRY/WET MIX BETWEEN THE DRY AND THE REVERBERATED SIGNAL

aoutLL = ((amixLeft * kspread) + (amixRight * (1 - kspread))) *.5 ; Stereo Reverb
aoutRR = ((amixLeft * (1-kspread)) + (amixRight * kspread)) *.5 ; Stereo Reverb

outs aoutLL, aoutRR

garvbsig = 0 ; then clear it

      endin
;causes Csound to run for about 7000 years...

f1 0 16384 10 1 ; Sin wave
f2 0 16384 10 1 0.5 0.3 0.25 0.2 0.167 0.14 0.125 .111 ; Saw wave
f0 2000

i99 0 10000 ; Keeps reverb running?

All good Rory issue solved. Thanks none the less!

I believe they are working as expected, although it may not sound like that. Your main filter envelope is acting, at times, as an amplitude envelope. The filter’s frequency will start at 0 and move upward towards cut-off. So it will only be letting through a very small amount of frequencies at the start. This will affect the amplitude of the overall output. This is why your filter envelope is also controlling the gain of the output.

p.s. I was just sending this when your reply came in!

Your completely right! Everything was working all along, however what seemed to be throwing me was that when the drive was down on the lp18f it made it seem like it was not working. I’ve decided to use another Lp filter opcode as a work around and then to add a separate opcode for the drive.

Thanks a mill for the reply and info much appreciated!