; WAGGONBAU
;
; simple sampler for industrial style drums
;
; (s) Severák 2019
; licensed with MIT license
<Cabbage> 
form caption("WaggonBAU") size(400, 180), colour(58, 110, 182), pluginid("wagb")
keyboard bounds(4, 72, 381, 95)
filebutton bounds(6, 4, 40, 40) text("C", "C") channel("C_path") identchannel("C_button") populate("*.wav") fontcolour:0(128, 128, 128, 255)
filebutton bounds(48, 4, 40, 40) text("D", "D") channel("D_path") identchannel("D_button") populate("*.wav","") fontcolour:0(128,128,128, 255)
filebutton bounds(90, 4, 40, 40) text("E", "E") channel("E_path") identchannel("E_button") populate("*.wav","") fontcolour:0(128,128,128, 255)

filebutton bounds(218, 4, 40, 40) text("F", "F") channel("F_path") identchannel("F_button") populate("*.wav","") fontcolour:0(128,128,128, 255)
filebutton bounds(260, 4, 40, 40) text("G", "G") channel("G_path") identchannel("G_button") populate("*.wav","") fontcolour:0(128,128,128, 255)
filebutton bounds(302, 4, 40, 40) text("A", "A") channel("A_path") identchannel("A_button") populate("*.wav","") fontcolour:0(128,128,128, 255)
filebutton bounds(344, 4, 40, 40) text("H", "H") channel("H_path") identchannel("H_button") populate("*.wav","") fontcolour:0(128,128,128, 255)



label bounds(134, 4, 80, 16) text("Waggon")
label bounds(134, 20, 80, 23) text("BAU")
label bounds(4, 50, 379, 16) text("SET sample for each note and play it via keyboard")
</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

giSampleC init 0
giSampleD init 0
giSampleE init 0

giSampleF init 0
giSampleG init 0
giSampleA init 0
giSampleH init 0

massign 0,0	;disable triggering of all instruments on all channels

; always on
instr MIDI_IN
    kSampleC = giSampleC
    kSampleD = giSampleD
    kSampleE = giSampleE
    
    kSampleF = giSampleF
    kSampleG = giSampleG
    kSampleA = giSampleA
    kSampleH = giSampleH

    kstatus, kchan, kdata1, kdata2 midiin
    if changed(kstatus, kchan, kdata1, kdata2)==1 then
        if kstatus==144 then
            knote = kdata1 % 12
            ; 0 2 4 ; 5 7 9 11
            if knote == 0 && kSampleC > 0 then
                event "i", "PLAY_SAMPLE_C", 0, 1, kdata2
            endif
            if knote == 2 && kSampleD > 0 then
                event "i", "PLAY_SAMPLE_D", 0, 1, kdata2
            endif
            if knote == 4 && kSampleE > 0 then
                event "i", "PLAY_SAMPLE_E", 0, 1, kdata2
            endif
            if knote == 5 && kSampleF > 0 then
                event "i", "PLAY_SAMPLE_F", 0, 1, kdata2
            endif
            if knote == 7 && kSampleG > 0 then
                event "i", "PLAY_SAMPLE_G", 0, 1, kdata2
            endif
            if knote == 9 && kSampleA > 0 then
                event "i", "PLAY_SAMPLE_A", 0, 1, kdata2
            endif
            if knote == 11 && kSampleH > 0 then
                event "i", "PLAY_SAMPLE_H", 0, 1, kdata2
            endif
            ;printks "this will trigger note %d\n", 0, kdata1%12
        endif
    endif
endin

instr GUI
    SampleCPath chnget "C_path"       
    if changed:k(SampleCPath) == 1 then
        event "i", "LOAD_SAMPLE_C", 0, 0
    endif
    
    SampleDPath chnget "D_path"       
    if changed:k(SampleDPath) == 1 then
        event "i", "LOAD_SAMPLE_D", 0, 0
    endif
    
    SampleEPath chnget "E_path"       
    if changed:k(SampleEPath) == 1 then
        event "i", "LOAD_SAMPLE_E", 0, 0
    endif
    
    SampleFPath chnget "F_path"       
    if changed:k(SampleFPath) == 1 then
        event "i", "LOAD_SAMPLE_F", 0, 0
    endif
    
    SampleGPath chnget "G_path"       
    if changed:k(SampleGPath) == 1 then
        event "i", "LOAD_SAMPLE_G", 0, 0
    endif
    
    SampleAPath chnget "A_path"       
    if changed:k(SampleAPath) == 1 then
        event "i", "LOAD_SAMPLE_A", 0, 0
    endif
    
    SampleHPath chnget "H_path"       
    if changed:k(SampleHPath) == 1 then
        event "i", "LOAD_SAMPLE_H", 0, 0
    endif
endin

; loading & saving is triggered as needed

instr LOAD_SAMPLE_C
    SampleCPath chnget "C_path"
    if filevalid(SampleCPath) == 1 then
        giSampleC ftgen 0,0,0,1,SampleCPath,0,0,0
        chnset "fontcolour:0(255,255,0, 255)", "C_button"
    endif
endin

instr PLAY_SAMPLE_C
    ; p4
    kVol = p4 / 127
    ichnls = ftchnls(giSampleC)
    
    if (ichnls == 1) then
        asigL loscil3 kVol, 1, giSampleC, 1
        asigR = 	asigL
    elseif (ichnls == 2) then
        asigL, asigR loscil3 kVol, 1, giSampleC, 1
        ;safety precaution if not mono or stereo
    else
        asigL = 0
        asigR = 0
    endif
    outs asigL, asigR
endin

instr LOAD_SAMPLE_D
    SampleDPath chnget "D_path"
    if filevalid(SampleDPath) == 1 then
        giSampleD ftgen 0,0,0,1,SampleDPath,0,0,0
        chnset "fontcolour:0(255,255,0, 255)", "D_button"
    endif
endin

instr PLAY_SAMPLE_D
    ; p4
    kVol = p4 / 127
    ichnls = ftchnls(giSampleD)
    
    if (ichnls == 1) then
        asigL loscil3 kVol, 1, giSampleD, 1
        asigR = 	asigL
    elseif (ichnls == 2) then
        asigL, asigR loscil3 kVol, 1, giSampleD, 1
        ;safety precaution if not mono or stereo
    else
        asigL = 0
        asigR = 0
    endif
    outs asigL, asigR
endin

instr LOAD_SAMPLE_E
    SampleEPath chnget "E_path"
    if filevalid(SampleEPath) == 1 then
        giSampleE ftgen 0,0,0,1,SampleEPath,0,0,0
        chnset "fontcolour:0(255,255,0, 255)", "E_button"
    endif
endin

instr PLAY_SAMPLE_E
    ; p4
    kVol = p4 / 127
    ichnls = ftchnls(giSampleE)
    
    if (ichnls == 1) then
        asigL loscil3 kVol, 1, giSampleE, 1
        asigR = 	asigL
    elseif (ichnls == 2) then
        asigL, asigR loscil3 kVol, 1, giSampleE, 1
        ;safety precaution if not mono or stereo
    else
        asigL = 0
        asigR = 0
    endif
    outs asigL, asigR
endin

instr LOAD_SAMPLE_F
    SampleFPath chnget "F_path"
    if filevalid(SampleFPath) == 1 then
        giSampleF ftgen 0,0,0,1,SampleFPath,0,0,0
        chnset "fontcolour:0(255,255,0, 255)", "F_button"
    endif
endin

instr PLAY_SAMPLE_F
    ; p4
    kVol = p4 / 127
    ichnls = ftchnls(giSampleF)
    
    if (ichnls == 1) then
        asigL loscil3 kVol, 1, giSampleF, 1
        asigR = 	asigL
    elseif (ichnls == 2) then
        asigL, asigR loscil3 kVol, 1, giSampleF, 1
        ;safety precaution if not mono or stereo
    else
        asigL = 0
        asigR = 0
    endif
    outs asigL, asigR
endin

instr LOAD_SAMPLE_G
    SampleGPath chnget "G_path"
    if filevalid(SampleGPath) == 1 then
        giSampleG ftgen 0,0,0,1,SampleGPath,0,0,0
        chnset "fontcolour:0(255,255,0, 255)", "G_button"
    endif
endin

instr PLAY_SAMPLE_G
    ; p4
    kVol = p4 / 127
    ichnls = ftchnls(giSampleG)
    
    if (ichnls == 1) then
        asigL loscil3 kVol, 1, giSampleG, 1
        asigR = 	asigL
    elseif (ichnls == 2) then
        asigL, asigR loscil3 kVol, 1, giSampleG, 1
        ;safety precaution if not mono or stereo
    else
        asigL = 0
        asigR = 0
    endif
    outs asigL, asigR
endin

instr LOAD_SAMPLE_A
    SampleAPath chnget "A_path"
    if filevalid(SampleAPath) == 1 then
        giSampleA ftgen 0,0,0,1,SampleAPath,0,0,0
        chnset "fontcolour:0(255,255,0, 255)", "A_button"
    endif
endin

instr PLAY_SAMPLE_A
    ; p4
    kVol = p4 / 127
    ichnls = ftchnls(giSampleA)
    
    if (ichnls == 1) then
        asigL loscil3 kVol, 1, giSampleA, 1
        asigR = 	asigL
    elseif (ichnls == 2) then
        asigL, asigR loscil3 kVol, 1, giSampleA, 1
        ;safety precaution if not mono or stereo
    else
        asigL = 0
        asigR = 0
    endif
    outs asigL, asigR
endin

instr LOAD_SAMPLE_H
    SampleHPath chnget "H_path"
    if filevalid(SampleHPath) == 1 then
        giSampleH ftgen 0,0,0,1,SampleHPath,0,0,0
        chnset "fontcolour:0(255,255,0, 255)", "H_button"
    endif
endin

instr PLAY_SAMPLE_H
    ; p4
    kVol = p4 / 127
    ichnls = ftchnls(giSampleH)
    
    if (ichnls == 1) then
        asigL loscil3 kVol, 1, giSampleH, 1
        asigR = 	asigL
    elseif (ichnls == 2) then
        asigL, asigR loscil3 kVol, 1, giSampleH, 1
        ;safety precaution if not mono or stereo
    else
        asigL = 0
        asigR = 0
    endif
    outs asigL, asigR
endin




</CsInstruments>
<CsScore>
;causes Csound to run for about 7000 years...
f0 z
; instr 1 listens
i "GUI" 0 z
; MIDI listens too
i "MIDI_IN" 0 z
</CsScore>
</CsoundSynthesizer>
