Having trouble with an if statement

I have this simple code. What I want is this to only print hey when I press a C note on my MIDI controller. It prints hey no matter which note I play.

<Cabbage>
form caption("Untitled") size(400, 300), guiMode("queue"), pluginId("def1")
keyboard bounds(8, 158, 381, 95)
</Cabbage>
<CsoundSynthesizer>

<CsOptions>
; Select audio/midi flags here according to platform
; Audio out   Audio in    No messages
-odac           -iadc     -d         -M0  -+rtmidi=virtual ;;;RT audio I/O with MIDI in
; For Non-realtime ouput leave only the line below:
; -o midiin.wav -W ;;; for file output any platform
</CsOptions>

<CsInstruments>

sr        =  44100
ksmps     =  10
nchnls    =  1

massign         0, 130 
pgmassign       0, 130  

instr   130

kstatus, kchan, kdata1, kdata2                  midiin

if (kstatus == 144 && kdata1%12==0 ) then

prints "hey"

endif

  endin

</CsInstruments>

<CsScore>
i130 0 3600
e
</CsScore>


</CsoundSynthesizer> 


Hi @ozzylingo ,

try this:

if (kstatus == 144 && kdata1 == 60) then
prints "hey"
endif

This will print “hey” every time you press the central C in the keyboard

1 Like

I also tried this, still doesn’t work.
Edit: Even if it did work, I want to implement this to a code that makes something everytime I press a C note regardless the octave.

I think that the issue is caused by “prints”, that is being executed at init time. So every time the instrument is executed the prints also executes, printing “hey”.
The branching should work correctly but you will need another way to print. Unfortunately I’m not 100% sure on which opcode I can suggest as a replacement :sweat_smile:

1 Like

you used the print statement at init time. i think printks should work.

1 Like

Try this:

if (kdata1 == 60) then
    printks "Hey!%n", 0
endif
1 Like
if (kstatus == 144 && kdata1%12==0 ) then

printks "hey",0

endif

this seemed to do the trick but it prints 2 heys when I press the C

It works correctly here, just tried on Cabbage with its virtual midi keyboard

1 Like

can you share the code I want to compare if anything is different. And how do you open the virtual midi keyboard :joy:

<Cabbage>
form caption("Untitled") size(400, 300), guiMode("queue"), pluginId("def1")
keyboard bounds(8, 158, 381, 95)
</Cabbage>
<CsOptions>
-n -d -+rtmidi=NULL -M0 -m0d --midi-key-cps=4 --midi-velocity-amp=5
</CsOptions>
<CsInstruments>

sr        =  44100
ksmps     =  10
nchnls    =  1

massign         0, 130 
pgmassign       0, 130  

instr   130

knotelength    init    0
knoteontime    init    0

kstatus, kchan, kdata1, kdata2                  midiin

;if (kstatus == 128) then
;knoteofftime    times
;knotelength    =    knoteofftime - knoteontime
;printks "\nkstatus= %d, kchan = %d, \\tnote#  = %d, velocity = %d \\tNote OFF\\t%f %f\\n", 0, kstatus, kchan, kdata1,kdata2, knoteofftime, knotelength
;
;elseif (kstatus == 144) then
;   knoteontime    times
;   printks "\nkstatus= %d, kchan = %d, \\tnote#  = %d, velocity = %d \\tNote ON\\t%f\\n", 0, kstatus, kchan, kdata1, kdata2, knoteontime
;
;   if (kdata1%12 == 0) then
;       printks "\nHey!%n", 0
;   endif
;    
;endif

if (kstatus == 144 && kdata1%12 == 0) then

    printks "hey",0

endif

endin

</CsInstruments>

<CsScore>
i130 0 3600
e
</CsScore>


</CsoundSynthesizer>

You have also another way of doing it above, using 2 ifs, just uncomment that portion

1 Like

in general, i would not use midiin for this purpose, but something like
this:

massign 0,“GetMisi”

instr GetMidi
if notnum()%12 == 0 then
puts(“Hey!”,1)
endif
endin

not tested, but i hope it works …

2 Likes

Thanks for everyones help.