; MIDILOG
; (c) Severak 2019
; MIT licensed

; TODO - kontrola zda jsme nenechali nějakou stistknutou klávesu (na tlačítko)

<Cabbage>
form caption("MIDI log") size(600, 220), colour(58, 110, 182), pluginid("mlog") bounds(0, 0, 600, 220)
csoundoutput bounds(6, 4, 590, 194) text("MIDI log")
checkbox bounds(6, 208, 6, 6) widgetarray("pressed", 128)
</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

;instrument will be triggered by MIDI
instr 1
    kpressed[] init 128

    kstatus, kchan, kdata1, kdata2 midiin
    if changed(kstatus, kchan, kdata1, kdata2)==1 then
        ;printks "= %d %d %d %d\n", 0, kstatus, kchan, kdata1, kdata2
        if kstatus==144 then
            printks "CH %d: ON  %d vel %d\n", 0, kchan, kdata1, kdata2
            kpressed[i(kdata1)] = 1
            Snam sprintfk "pressed_ident%d", kdata1
            chnset "value(1)", Snam
        elseif kstatus==128 then
            printks "CH %d: OFF %d vel %d\n", 0, kchan, kdata1, kdata2
            kpressed[i(kdata1)] = 0
            Snam sprintfk "pressed_ident%d", kdata1
            chnset "value(0)", Snam
        elseif kstatus==176 then
            printks "CH %d: CC %d val %d\n", 0.05, kchan, kdata1, kdata2
        elseif kstatus==224 then
            printks "CH %d: pitch bend %d val %d\n", 0.1, kchan, kdata1, kdata2
        endif
    endif
endin

instr 2
    iCnt init 21
    kMetro metro 1
    until iCnt > 108 do
        printks "%d", 0, iCnt
        Sval sprintfk "pos(%d, 208)", (iCnt-21)*6+6
        Snam sprintfk "pressed_ident%d", iCnt
        chnset Sval, Snam
        iCnt=iCnt+1
    enduntil
    ;if kMetro==1 then
    ;    event "i", 2, 0, .01
    ;endif       
endin

</CsInstruments>
<CsScore>
i 1 0 [3600*24*7]
i 2 0 0
</CsScore>
</CsoundSynthesizer>
