Cabbage Logo
Back to Cabbage Site

Random values for sliders in Cabbage

Hi everyone!

I’ve currently run into an issue with trying to randomize my delay. I’ve tried different things and also just tested with one of the knob-parameters. But basically what I need is a button that either generates random values of my delay-parameters and when turned off, I want to control it myself.

I assume I have to add it into my if-statement somehow, but currently nothing happens when I click the code.

This is the code!

EDIT: Now formatted!

<Cabbage> 

    form caption("Freezer") size(700, 450), guiMode("queue") pluginId("1011"), ;bundle "snare.wav"

    image bounds(0, 0, 700, 450) channel("image10013") file("distortion.png") alpha(0.24)




;REVERB + FREEZE

    groupbox bounds(0, 2, 317, 178) channel("groupbox66") colour(41, 21, 21, 255)

    label bounds(102, 2, 100, 21) channel("Reverb") text("Reverb") textColour(255, 255, 255, 255) fontColour(157, 202, 33, 255) 

    rslider bounds(14, 30, 60, 60) channel("Reverb-mix") range(0, 1, 0, 1, 0.001) text("Mix")  textColour(255, 255, 255, 255) trackerColour(157, 202, 33, 255)

    rslider bounds(114, 30, 60, 60) channel("Feedback") range(0.1, 1, 0.1, 1, 0.001) text("Feedback") textColour(255, 255, 255, 255) trackerColour (157, 202, 33, 255)

    rslider bounds(218, 30, 60, 60) channel("Frekvens") range(100, 20000, 20000, 1, 0.001) text("Low-Pass") textColour(255, 255, 255, 255) trackerColour(157, 202, 33, 255)

    checkbox bounds(2, 114, 298, 54) channel("Freeze") text("Freeze Reverb") fontColour:0(255, 0, 0, 255) fontColour:1(0, 255, 0, 255) colour:0(255, 0, 0, 255)


;DELAY

    groupbox bounds(384, 4, 316, 175) channel("groupbox68") colour(17, 14, 14, 255)

    label bounds(500, 2, 104, 19) channel("label10048") text("Delay") fontColour(255, 255, 0, 250)

    rslider bounds(624, 110, 50, 42) channel("dLowpass") range(0, 20000, 20000, 0.2, 0.001) text("LPF") textColour(255, 255, 255, 255) trackerColour(255, 255, 0, 255) 

    rslider bounds(620, 54, 59, 45) channel("dHighpass") range(0, 20000, 0, 0.2, 0.001) text("HPF") colour(255, 255, 255, 255) textColour(255, 255, 255, 255) trackerColour(255, 255, 0, 255)

    rslider bounds(410, 28, 56, 60) channel("DelayMix") range(0, 1, 0, 1, 0.001) trackerColour(255, 255, 0, 250) text("Mix") textColour(255, 255, 255, 255) 
    
    rslider bounds(478, 28, 60, 60) channel("DelayTime") range(10, 1500, 0, 1, 0.001) trackerColour(255, 255, 0, 250) text("Time in ms") textColour(255, 255, 255, 255) 

    rslider bounds(548, 28, 60, 60) channel("DFeedback") range(0, 1, 0, 1, 0.001) trackerColour(255, 255, 0, 250) text("Feedback") textColour(255, 255, 255, 255) 
    
    checkbox bounds(410, 100, 169, 39) channel("RandomDelay") text("Random Delay") fontColour:1(0, 255, 0, 255) fontColour:0(255, 0, 0, 255) colour:0(255, 0, 0, 255)

    
;MASTER & SAMPLE/DIRECT INPUT-kontrol

    groupbox bounds(1, 250, 355, 200) channel("groupbox68") colour(17, 14, 14, 255)

    label bounds(88, 250, 154, 18) channel("label10050") text("Master-controls") fontColour(197, 109, 109, 255)
    
    rslider bounds(6, 320, 75, 64) channel("Speed") range(1, 5, 1, 1, 0.125) text("Sample Speed") textColour(197, 109, 109, 255) trackerColour(0, 255, 0, 255)
    
    checkbox bounds(8, 276, 169, 39) channel("kEnableSample") text("Sample/Input") fontColour:1(0, 255, 0, 255) fontColour:0(255, 0, 0, 255) colour:0(255, 0, 0, 255)

    rslider bounds(606, 354, 91, 91), channel("Gain"), range(0, 1, 0.5, 1, 0.01), text("Master"), trackerColour(0, 255, 0, 255), outlineColour(0, 0, 0, 50), textColour(0, 17, 5, 255)
    
    rslider bounds(90, 322, 83, 63) channel("PitchScale") range(0, 2, 1, 1, 0.001) text("Scale") textColour(197, 109, 109, 255) trackerColour(0, 255, 0, 255)
    
    vslider bounds(290, 278, 50, 158) channel("Shift") range(0, 1000, 0, 0.3, 0.001) text("PShift") textColour(197, 109, 109, 255) trackerColour(0, 255, 0, 255) 
    
    hslider bounds(158, 116, 150, 50) channel("FreezeFreq") range(0, 20000, 0, 1, 0.001) text("Freq Modulation") textColour(255, 255, 255, 255) 

</Cabbage>
<CsoundSynthesizer>

<CsOptions>
-n -d
</CsOptions>

<CsInstruments>

ksmps = 32
nchnls = 2
0dbfs = 1

;GLOBALE VÆRDIER

    gaRevL      init        0
    gaRevR      init        0
    gaDel       init        0
    gaSend      init        0

instr 1

;Cabbage-værdier i instrumentet
;______________________________________

    kGain cabbageGetValue "Gain"

    kSpeed cabbageGetValue "Speed"

    kEnable cabbageGetValue "kEnableSample"

    kPitchShiftRandom cabbageGetValue "kPitchShiftRandom"

    kShift cabbageGetValue "Shift"

    kScale cabbageGetValue "PitchScale"

    kPitchShiftRandom randh 8, 0.8

    kShift += kPitchShiftRandom

;Sample eller stemme som bliver analyseret af PVS
;______________________________________


    ;Gjør at når sample-button er på, bruger den sample og når den er slået fra bruger den mikrofon-input

    if kEnable == 0 then // sample

    asig1, asig2 diskin2 "sampletest.wav", 1*kSpeed, 0, 1        

    elseif kEnable == 1 then // mikrofon

    aIn1 inch 1
    aIn2 inch 2
    
    asig1 = aIn1
    asig2 = aIn2    

    endif

;Værdier som skal ind for selve PVS-analysen

    ifftsize	=	1024
    ioverlap	=	ifftsize/4
    iwinsize	=	1024
    iwintype	=	0
    inbins      =   ifftsize/2 + 1


;Trækker ut parametere for henvoldvis at skalere og pitch-shifte signalet
;___________

    fSig1    pvsanal asig1, ifftsize, ioverlap, iwinsize, iwintype
    fSig2    pvsanal asig2, ifftsize, ioverlap, iwinsize, iwintype

    fScale1  pvscale fSig1, kScale
    fScale2  pvscale fSig2, kScale

    fShift1  pvshift fScale1, kShift, 0
    fShift2  pvshift fScale2, kShift, 0
    
    aOut1    pvsynth fShift1
    aOut2    pvsynth fShift2


    gaSend += (aOut1 +aOut2) * kGain

endin

;REVERB
;________________________________________________________
instr 2
    
    kFeedback cabbageGetValue "Feedback"

    kFrekvens cabbageGetValue "Frekvens"

    kFreeze chnget "Freeze"

    kFreezeFreq chnget "FreezeFreq"

    gaRevL, gaRevR reverbsc gaSend, gaSend, kFeedback, kFrekvens 

;FREEZE-REVERB
;_____________________

    ifftsize	=	1024
    ioverlap	=	ifftsize/4
    iwinsize	=	1024
    iwintype	=	0

    fSigL   pvsanal gaRevL,ifftsize, ioverlap, iwinsize, iwintype

    fSigR   pvsanal gaRevR, ifftsize, ioverlap, iwinsize, iwintype

    fOutL   pvsfreeze fSigL, kFreeze, kFreezeFreq

    fOutR   pvsfreeze fSigR, kFreeze, kFreezeFreq

    aOutL   pvsynth fOutL

    aOutR   pvsynth fOutR

;Gør at Freeze-signalet er lig med reverb signalet, så det kommer ind, når jeg summerer i mastervolumen.

    gaRevL = aOutL 
    gaRevR = aOutR 

endin 

;DELAY
;________________________________________________________
instr 3
        
    aFeed init 0

    kDelayTime      cabbageGetValue "DelayTime"
    kFeedback       cabbageGetValue "DFeedback"
    
    kRandomDelay   cabbageGetValue "RandomDelay"
    
    if kRandomDelay == 1 then
        kDelayTime randh 10, 1500, 1  ;
        kFeedback randh 0, 1, 1       
        kDelayMix randh 10, 100, 1     
    endif
        
      
    aTime           interp kDelayTime
    aTime           tone aTime, 1

    aDelay          vdelay      gaSend + aFeed, aTime, 1500

    kLowpass        cabbageGetValue "dLowpass"
    kHighpass       cabbageGetValue "dHighpass"

    aLowpass        butterlp    aDelay, kLowpass
    aHighpass       butterhp    aDelay, kHighpass

    aFeed = 0.5*(aHighpass + aLowpass) * kFeedback
    
    

    gaDel           +=      aDelay 
    

endin


;MASTER-INSTRUMENT
;_____________________
    instr 50

    kRevMix        cabbageGetValue "Reverb-mix"
    kDelayMix      cabbageGetValue "DelayMix"

;Mixslider for Delay
    aDel = gaDel * kDelayMix

;Summering af signaler i effekt
    aMixL = gaSend * (1-kRevMix) + gaRevL*kRevMix + aDel

    aMixR = gaSend * (1-kRevMix) + gaRevR*kRevMix + aDel

outs aMixL, aMixR


;GLOBAL VALUES 
    gaDel  = 0
    gaRevL = 0
    gaRevR = 0 
    gaSend = 0

endin

</CsInstruments>
<CsScore>
f0 z
i1 0 z
i2 0 z
i3 0 z
i50 0 z

</CsScore>
</CsoundSynthesizer>

This working for me here, well kind of. I think you have the parameters of the randh opcodes in the wrong order. The first parameter is the amplitude of the output signal. The second if the frequency. Also, you should probably pass the outputs through an abs to remove the negative parts, because randh will returns a bi-polar signal.

p.s. thanks for formatting the code, people here will tell you, I get really paranoid about that :rofl:

1 Like

Thanks for the answer. I don’t know why it isn’t for me then :open_mouth: If I don’t touch the sliders, nothing happens for me when the random button is on. I just want the sliders to generate random values between the range of the sliders whenever the button is on without touching the sliders. Only if I turn off the random button I can the customize the sliders as I want for desired effect. It was just to generate random delay.

And I thought that I should add in the desired range of the slider in the randH to make sure it varied between the entire value of the sliders.

I’ve tried with the adding the abs and changing the parameters, but I still hear no difference whatsoever.

I tried adding the outs in the if-statement as well, but this didn’t do anything either for the button.

You should read the Csound manual on randh/i to get the correct parameters. Anyway, I had to change kDelayMix to hear it the randomised sounds. If you want to change the sliders you can just do something like:

cabbageSetValue "DelayTime", random:k(10, 1500), metro(10)

that will cause your slider to move. The value of it will then be picked up as if you have changed it yourself.

1 Like

So now it works like I want it to by randomly generating numbers to whatever I tell the metro to be. But the sliders are not moving inside the GUI itself when resetting with my button even though the reset is properly working when clicking the button. The sliders are moving as the the random checkbox is on accordingly to the metro.

I tried to follow another tutorial on here about slider resetting but just adjusted to my code. Still the slider values doesn’t change even though they sonically reset.

Cs_testResetSlider.csd (813 Bytes)

instr 4
    kRandomDelay chnget "RandomDelay"
    kResetButton chnget "ResetButton"
    printk2 kRandomDelay
    printk2 kResetButton


kResetButton changed chnget:k("ResetButton")
	if kResetButton==1 then 
		chnset k(0),"DelayTime"
		chnset k(0),"DFeedBack"
		chnset k(0),"DelayMix"
		
		cabbageSet "DelayTime", "value", 10
        cabbageSet "DFeedback", "value", 0
        cabbageSet "DelayMix", "value", 0
	endif

    ; Generate random values if RandomDelay is on
    if kRandomDelay == 1 then
        if metro(0.2) == 1 then  ; Adjust the frequency as needed
            ; Generate random values
            kRandomTime random 10, 1500
            kRandomFeedback random 0.1, 1
            kRandomMix random 0, 1

            ; Set the generated random values
            cabbageSetValue "DelayTime", kRandomTime
            cabbageSetValue "DFeedback", kRandomFeedback
            cabbageSetValue "DelayMix", kRandomMix
        endif
    endif
endin

Here’s the updated test code.

<Cabbage>
form caption("Untitled") size(400, 300), colour(58, 110, 182), pluginID("def1")
button bounds(16, 14, 80, 40) channel("buttonchan") text("Push me", "Push me") latched(0)
vslider bounds(220, 8, 50, 150) channel("vslider") range(0, 1, 0, 1, 0.001) 
</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
	kButton, kButtonTrig cabbageGetValue "buttonchan"
    cabbageSetValue "vslider", 0, kButtonTrig
endin

</CsInstruments>
<CsScore>
i 1 0 3600
</CsScore>
</CsoundSynthesizer>

I added a guiMode(“queue”) to the form declaration - this allows you to ditch the identChannels and use the cabbagedSet/Get opcodes. Cabbage widgets don’t have any reset mechanism, but there value can be updated at any time

1 Like

Thanks for the answer

The test code worked properly before though. In my code I don’t use the identchannels either. I use the guiMode “queue”.

I’ve tried with the same logic in my code, but I’m just not getting it right :slight_smile: It resets actually, but the sliders don’t go back to 0.

Be sure to use the k-rate variants of the cabbageSet opcodes. I see a lot of i-time versions in your code.

I fixed it, thanks :slight_smile: