Generating and detecting a MIDI event inside a Csound instance

Dear Csounders!

How to generate and sense a MIDI event inside a Csound instance? E.g. if a MIDI event is generated using midion opcode in one instrument, how to detect it in another instrument using midiin opcode?

Here is a code example of what I mean/want to do but the midi events are never detected by instrument 2.

ksmps = 32
nchnls = 2
0dbfs = 1

massign 0,0

instr 1
    k1 metro 1
    if k1 > 0 then
        kn = 60+rnd:k(12)
        kv = 60+birnd:k(40)
        printf "NOTE %d %d\n", kn, kn, kv
        midion 1,kn,kv

instr 2
    k1,k2,k3,k4 midiin
    printf "chn:%d data1:%d data2:%d\n", k1,k2,k3,k4

i 1 0 1000
i 2 0 1000

Any ideas?

I’m not sure this approach will work as Csound won’t pick up the newly generated MIDI event. midiin only parses incoming MIDI info as far as I know, but I could be wrong.

Hi @rory ! thank you on your response. Do you know maybe any other way to generate a midi event and then catch it inside one csound instance?

I don’t, but can’t you simulate the event, or even trigger instrument 2 when an incoming MIDI note is detected?


But why do you need specifically MIDI event? Wouldn’t a normal Csound event, ie calling an instrument with schedule and parameters do?

instr 1
    k1 metro 1
    if k1 > 0 then
        kn = 60+rnd:k(12)
        kv = 60+birnd:k(40)
        printf "NOTE %d %d\n", kn, kn, kv
        schedulek 1, 0, 1, kn, kv 

instr 2
    ;k1,k2,k3,k4 midiin
     in = p4
     iv = p5 
    printf "chn:%d data1:%d data2:%d\n", in, iv

Kontakt Lovre Bogdanic via The Csound Community (<>) kirjutas kuupäeval R, 5. jaanuar 2024 kell 17:16:

I was thinking about how to implement a ASCII to MIDI converter in CSound.

My plan was to detect a pressed key with sensekey opcode and then generate some MIDI event, based on its value, that could be seen in that same csound instance as if it’s comming from a ‘normal’ MIDI keyboard.

But it seems that it is not that straight forward. :face_with_head_bandage:

Would it not be simpler to generate a score event instead?

It definitely seems that way :wink: I will probably test a bit around to see what is the best workaround

yes i think so, too.
midi events are somehow communication with the world outside of csound
(mostly to receive but also to send).
inside csound there are many ways to communicate:

  • score events as mentioned
  • software channels (chnset / chnget)
  • global variables

OSC is also possible for both, external and internal communication.
alex once did a nice example for csoundqt:

feel free to share more details of your situation, best -

Hi @joachim !! Thanks for the explanations.

“midi events are somehow communication with the world outside of csound” → this is a really good sentence. It clarifies exactly that what I was missing.

“feel free to share more details of your situation” → there are a lot of cool midi based instruments in csound examples and I wanted for my young daughter to experiment a bit with them. Since I don’t have a midi keyboard at home, I thought if I make a simple ASCII2MIDI converter she could make some noise using an old pc keyboard so she can freely channel all her energy on it :scream: :grin:

But now I know what is the problem so I will find some solution :beers:

got it!
this is a nice motivation =)

i think all frontends offer a virtual midi keyboard. for instance in
csoundqt, click on the Show Virtual MIDI Keyboard button (or View > Show
Virtual Keyboard).

once you start your .csd (with an empy score), you can trigger events by
clicking on the virtual keyboard OR BY PUSHING KEYS on the computer

i think cabbage and blue have similar features.

if you want to use commandline csound on your old PC, you can trigger
instances of another instrument via sensekey, and send the key you
pressed. something like:

kKey,kDown sensekey
if changed(kDown) && kDown==1 then

not tested, but you know what i mean …

best -

1 Like

Yes I know what you mean :wink: . Thanks for the tips. I will definitely try them out