Cabbage Logo
Back to Cabbage Site

How can I make a fade out button?

Hi everyone
I need to make a button that when I press it, makes a fade out to the signal I’m playing.
Thank you

You could use the latched() property of the button and check when status of it is triggered. In case it is triggered, then call a line opcode…

You could also do something like this with a global variable.

form caption("Untitled") size(400, 300), colour(58, 110, 182), pluginID("def1")
button bounds(10, 10, 120, 40), text("Fade Out", "Fade In"), channel("fadeOut")
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d -+rtmidi=NULL -M0 -m0d 
</CsOptions>
<CsInstruments>
; Initialize the global variables. 
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1

gkFade init 1

instr 1
    kButton chnget "fadeOut"
    kTrig changed kButton
    if kTrig == 1 then
        if kButton == 1 then
            event "i", "APPLY_FADE", 0, 1, 0 ;pass 0 as p4 to fade down
        else
            event "i", "APPLY_FADE", 0, 1, 1 ;pass 1 as p4 to fade down
        endif
    endif

    a1 oscili gkFade, 200
    outs a1, a1
endin

instr APPLY_FADE
    if p4 == 1 then
        gkFade line 0, p3, 1
    else
        gkFade line 1, p3, 0
    endif
endin

</CsInstruments>
<CsScore>
;causes Csound to run for about 7000 years...
f0 z
;starts instrument 1 and runs it for a week
i1 0 [60*60*24*7] 
</CsScore>
</CsoundSynthesizer>

Btw, I believe there is a simple opcode that can triggering simple fade in/out envelopes, but I can never find it when I search for it. Anyone remember what it’s called?

2 Likes

Linsegr has a release segment

There was another opcode though I came across to would be perfect for this kind of thing. I’ll have another look. Each time I come across it I always think how perfect it would be for this kind of thing!

Or one could do this:

instr 1
    kButton chnget "fadeOut"
    kFade port kButton, 1
    a1 oscili kFade, 500
    outs a1, a1
endin

Still not the opcode I am trying to remember, but quite eloquent all the same.

@rorywalsh has been quicker.
Here is also a solution using the lineto opcode

<Cabbage>
form caption("Untitled") size(400, 300), colour(58, 110, 182), pluginID("def1")
keyboard bounds(8, 158, 381, 95)
button bounds(190, 32, 80, 40) channel("buttonchan3") text("Fade", "Rise") 
</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

gkFlag init  0

; This instrument manages the interface
instr 2
 if changed:k(chnget:k("buttonchan3"))==1 then
    if chnget:k("buttonchan3")==1 then
        gkFlag =0
    else 
        gkFlag = 1
    endif 
 endif
endin

;instrument will be triggered by keyboard widget
instr 1
kEnv madsr .1, .2, .6, .4
aOut vco2 p5, p4

kFade lineto gkFlag,1
aOut = aOut*kEnv*kFade
outs aOut, aOut
endin

</CsInstruments>
<CsScore>
;causes Csound to run for about 7000 years...
f0 z
i 2 0 z
</CsScore>
</CsoundSynthesizer>
1 Like

Thank you so much! This was very useful!