<Cabbage> bounds(0, 0, 0, 0)
form caption("digital NOMAD") size(500, 500), colour(58, 110, 182), pluginid("def1")
keyboard bounds(10, 382, 381, 95)
groupbox bounds(6, 6, 102, 86) text("ARPEGGIATOR")
groupbox bounds(116, 4, 103, 90) text("LFO")
groupbox bounds(8, 98, 95, 86) text("ENV 1")
groupbox bounds(10, 194, 92, 90) text("ENV 2")
groupbox bounds(108, 98, 170, 87) text("DCO 1")
groupbox bounds(108, 194, 171, 90) text("VCO 2")
groupbox bounds(284, 98, 156, 88) text("MMF 1")
groupbox bounds(286, 194, 158, 89) text("MMF 2")
groupbox bounds(226, 4, 144, 89) text("MIX")

vslider bounds(12, 120, 15, 57) range(0.01, 1, 0.01, 1, 0.001) channel("env1_a")
vslider bounds(30, 120, 18, 57) range(0.01, 1, 0.01, 1, 0.001) channel("env1_d")
vslider bounds(52, 120, 18, 57) range(0, 1, 1, 1, 0.001) channel("env1_s")
vslider bounds(72, 120, 23, 57) range(0.01, 1, 0, 1, 0.001) channel("env1_r")

vslider bounds(16, 218, 15, 57) range(0.01, 1, 0.01, 1, 0.001) channel("env2_a")
vslider bounds(36, 218, 18, 57) range(0.01, 1, 0, 1, 0.001) channel("env2_d")
vslider bounds(58, 218, 18, 57) range(0, 1, 1, 1, 0.001) channel("env2_s")
vslider bounds(78, 218, 23, 57) range(0.01, 1, 1, 1, 0.001) channel("env2_r")

vslider bounds(176, 122, 27, 60) range(0, 1, 0, 1, 0.001) channel("dco1_sqr")
vslider bounds(208, 122, 27, 60) range(0, 1, 1, 1, 0.001) channel("dco1_saw")
vslider bounds(240, 122, 27, 60) range(0, 1, 0, 1, 0.001) channel("dco1_sub")
combobox bounds(116, 136, 52, 25) channel("dco1_env") text("ENV1", "ENV2", "LFO")

combobox bounds(116, 222, 80, 20) text("sine", "triangle", "saw", "square") channel("vco2_wave")
combobox bounds(116, 250, 80, 20) channel("vco2_env") text("ENV1", "ENV2", "LFO") value(2)
vslider bounds(200, 220, 28, 51) range(0.1, 0.9, 0.1, 1, 0.001) channel("vco2_pwm")

combobox bounds(294, 128, 52, 20) channel("mmf1_mode") text("LP", "HP", "BP", "NOTCH", "off")
combobox bounds(354, 128, 53, 20) channel("mmf1_env") text("none", "ENV 1", "ENV 2", "LFO")
hslider bounds(296, 156, 114, 18) range(0, 22000, 22000, 0.5, 0.001) channel("mmf1_freq")
vslider bounds(414, 124, 16, 50) range(1, 30, 1, 1, 0.001) channel("mmf1_res")

combobox bounds(294, 222, 52, 20) channel("mmf2_mode") text("LP", "HP", "BP", "NOTCH", "off")
combobox bounds(354, 222, 53, 20) channel("mmf2_env") text("none", "ENV 1", "ENV 2", "LFO")
hslider bounds(294, 252, 114, 18) range(0, 22000, 22000, 0.5, 0.001) channel("mmf2_freq")
vslider bounds(414, 222, 16, 50) range(1, 30, 1, 1, 0.001) channel("mmf2_res")


hslider bounds(236, 28, 81, 20) range(0, 1, 0.5, 1, 0.001) channel("pan1")
hslider bounds(236, 71, 84, 15) range(0, 1, 0.5, 1, 0.001) channel("pan2") 
vslider bounds(328, 28, 31, 59) range(0, 1, 0.5, 1, 0.001) channel("mix")

combobox bounds(126, 28, 80, 20) channel("lfo_wave") text("sine", "saw", "square")
rslider bounds(128, 52, 53, 36) range(1, 25, 5, 1, 0.001) channel("lfo_freq")
label bounds(14, 40, 83, 29) text("TODO")
label bounds(10, 294, 443, 36) text("digital NOMAD by Severák")
combobox bounds(382, 8, 80, 20), populate("*.snaps")
filebutton bounds(382, 34, 80, 40) text("Save") mode("snapshot")  populate("*.snaps")
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d -+rtmidi=NULL -M0 -m0d --midi-key-cps=4 --midi-velocity-amp=5
</CsOptions>
<CsInstruments>
; Initialize the global variables. 
ksmps = 32
nchnls = 2
0dbfs = 1


gkLFO init 0


;instrument will be triggered by keyboard widget
instr 1

; ENV 1
ienv1_a chnget "env1_a"
ienv1_d chnget "env1_d"
ienv1_s chnget "env1_s"
ienv1_r chnget "env1_r"

; ENV 2

kEnv1 madsr ienv1_a, ienv1_d, ienv1_s, ienv1_r

ienv2_a chnget "env2_a"
ienv2_d chnget "env2_d"
ienv2_s chnget "env2_s"
ienv2_r chnget "env2_r"

kEnv2 madsr ienv2_a, ienv2_d, ienv2_s, ienv2_r


; DCO 1

inotnum notnum

kDcoEnv init 0

idco1_env chnget "dco1_env"

if idco1_env == 1 then
    kDcoEnv = kEnv1
elseif idco1_env == 2 then
    kDcoEnv = kEnv2    
else
    kDcoEnv = gkLFO
endif

kdco1_sqr chnget "dco1_sqr"
kdco1_saw chnget "dco1_saw"
kdco1_sub chnget "dco1_sub"

aDcoSaw vco2 0.8, p4, 0
aDcoSqr vco2 0.8, p4, 10
aDcoSub vco2 0.8, cpsmidinn(inotnum-12), 10

aDcoOut = (aDcoSaw*kdco1_saw + aDcoSqr*kdco1_sqr + aDcoSub* kdco1_sub) * kDcoEnv

; VCO 2

ivco2_env chnget "vco2_env"

if ivco2_env == 1 then
    kVcoEnv = kEnv1
elseif ivco2_env == 2 then
    kVcoEnv = kEnv2    
else
    kVcoEnv = gkLFO
endif

iwaves[] fillarray -1, -1, 12, 0, 10
ivco2_wave chnget "vco2_wave"
kvco2_pwm chnget "vco2_pwm"

if iwaves[ivco2_wave] == -1 then
    aVcoOut poscil 0.8, p4
else
    aVcoOut vco2 0.8, p4, iwaves[ivco2_wave], kvco2_pwm 
endif

aVcoOut = aVcoOut * kVcoEnv

; MMF1

kmmf1_freq chnget "mmf1_freq"
kmmf1_res chnget "mmf1_res"
immf1_mode chnget "mmf1_mode"
immf1_env chnget "mmf1_env"

kMmf1Env init 0

if immf1_env == 1 then
    kMmf1Env = 1
elseif immf1_env == 2 then
    kMmf1Env = kEnv1
elseif immf1_env == 3 then
    kMmf1Env = kEnv2
elseif immf1_env == 4 then
    kMmf1Env = gkLFO
endif

aDcoOut bqrez aDcoOut, kmmf1_freq * kMmf1Env, kmmf1_res, immf1_mode - 1
aDcoOut clip aDcoOut, 0, 1

; MMF1

kmmf2_freq chnget "mmf2_freq"
kmmf2_res chnget "mmf2_res"
immf2_mode chnget "mmf2_mode"
immf2_env chnget "mmf2_env"

kMmf2Env init 0

if immf2_env == 1 then
    kMmf2Env = 1
elseif immf2_env == 2 then
    kMmf2Env = kEnv1
elseif immf2_env == 3 then
    kMmf2Env = kEnv2
elseif immf2_env == 4 then
    kMmf2Env = gkLFO
endif

aVcoOut bqrez aVcoOut, kmmf2_freq * kMmf2Env, kmmf2_res, immf2_mode - 1
aVcoOut clip aVcoOut, 0, 1

; MIX

kpan1 chnget "pan1"
aPan1L, aPan1R pan2 aDcoOut, kpan1

kpan2 chnget "pan2"
aPan2L, aPan2R pan2 aVcoOut, kpan2

kmix chnget "mix"
aMixL = (aPan1L * kmix) + (aPan2L * (1-kmix))
aMixR = (aPan1R * kmix) + (aPan2R * (1-kmix))

aMixL clip aMixL, 0, 1
aMixR clip aMixR, 0, 1

outs aMixL, aMixR
endin

; LFO
instr 3
klfo_freq chnget "lfo_freq"
ilfo_mode chnget "lfo_mode"

; TODO - LFO

;if ilfo_mode == 1 then
    kLFO lfo 0.5, klfo_freq, 0
;elseif ilfo_mode == 2 then
;    kLFO lfo 0.5, klfo_freq, 4
;else
;    kLFO lfo 0.5, klfo_freq, 2
;endif
gkLFO = kLFO + 0.5

endin

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