# Generating and detecting a MIDI event inside a Csound instance

**URL:** <https://forum.csound.com/t/generating-and-detecting-a-midi-event-inside-a-csound-instance/1493>\
**Category:** General\
**Created:** [January 5, 2024, 9:45am UTC](https://forum.csound.com/t/generating-and-detecting-a-midi-event-inside-a-csound-instance/1493 "2024-01-05T09:45:35Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lovre](https://forum.csound.com/letter_avatar_proxy/v4/letter/l/bbce88/32.png) [@Lovre](https://forum.csound.com/u/Lovre)\
**Post date:** [January 5, 2024, 9:45am UTC](https://forum.csound.com/t/generating-and-detecting-a-midi-event-inside-a-csound-instance/1493/1 "2024-01-05T09:45:35Z")

</div>

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.

```csound
<CsoundSynthesizer>
<CsOptions>
-odac
</CsOptions>
<CsInstruments>
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
    endif
endin

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

</CsInstruments>
<CsScore>
i 1 0 1000
i 2 0 1000
</CsScore>
</CsoundSynthesizer>

```

Any ideas?

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [January 5, 2024, 1:44pm UTC](https://forum.csound.com/t/generating-and-detecting-a-midi-event-inside-a-csound-instance/1493/2 "2024-01-05T13:44:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Lovre](https://forum.csound.com/letter_avatar_proxy/v4/letter/l/bbce88/32.png) [@Lovre](https://forum.csound.com/u/Lovre)\
**Post date:** [January 5, 2024, 3:06pm UTC](https://forum.csound.com/t/generating-and-detecting-a-midi-event-inside-a-csound-instance/1493/3 "2024-01-05T15:06:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [January 5, 2024, 3:32pm UTC](https://forum.csound.com/t/generating-and-detecting-a-midi-event-inside-a-csound-instance/1493/4 "2024-01-05T15:32:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tarmo](https://forum.csound.com/letter_avatar_proxy/v4/letter/t/c37758/32.png) [@tarmo](https://forum.csound.com/u/tarmo)\
**Post date:** [January 5, 2024, 3:35pm UTC](https://forum.csound.com/t/generating-and-detecting-a-midi-event-inside-a-csound-instance/1493/5 "2024-01-05T15:35:11Z")

</div>

Hi!

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

```csound
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 
    endif
endin

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

```

Kontakt Lovre Bogdanic via The Csound Community (\<[noreply@forum.csound.com](mailto:noreply@forum.csound.com)\>) kirjutas kuupäeval R, 5. jaanuar 2024 kell 17:16:

---

<div class="post-metadata">

**Author:** ![Lovre](https://forum.csound.com/letter_avatar_proxy/v4/letter/l/bbce88/32.png) [@Lovre](https://forum.csound.com/u/Lovre)\
**Post date:** [January 5, 2024, 3:44pm UTC](https://forum.csound.com/t/generating-and-detecting-a-midi-event-inside-a-csound-instance/1493/6 "2024-01-05T15:44:09Z")

</div>

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. 🤕

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [January 5, 2024, 5:32pm UTC](https://forum.csound.com/t/generating-and-detecting-a-midi-event-inside-a-csound-instance/1493/7 "2024-01-05T17:32:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Lovre](https://forum.csound.com/letter_avatar_proxy/v4/letter/l/bbce88/32.png) [@Lovre](https://forum.csound.com/u/Lovre)\
**Post date:** [January 5, 2024, 6:41pm UTC](https://forum.csound.com/t/generating-and-detecting-a-midi-event-inside-a-csound-instance/1493/8 "2024-01-05T18:41:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joachim](https://forum.csound.com/letter_avatar_proxy/v4/letter/j/a87d85/32.png) [@joachim](https://forum.csound.com/u/joachim)\
**Post date:** [January 6, 2024, 8:36am UTC](https://forum.csound.com/t/generating-and-detecting-a-midi-event-inside-a-csound-instance/1493/9 "2024-01-06T08:36:56Z")

</div>

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:

> <https://github.com/CsoundQt/CsoundQt/blob/develop/src/Examples/CsoundQt/Getting%20Started/Realtime_Interaction/OpenSoundControl.csd>

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

---

<div class="post-metadata">

**Author:** ![Lovre](https://forum.csound.com/letter_avatar_proxy/v4/letter/l/bbce88/32.png) [@Lovre](https://forum.csound.com/u/Lovre)\
**Post date:** [January 6, 2024, 6:29pm UTC](https://forum.csound.com/t/generating-and-detecting-a-midi-event-inside-a-csound-instance/1493/10 "2024-01-06T18:29:32Z")

</div>

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 😱 😁

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

---

<div class="post-metadata">

**Author:** ![joachim](https://forum.csound.com/letter_avatar_proxy/v4/letter/j/a87d85/32.png) [@joachim](https://forum.csound.com/u/joachim)\
**Post date:** [January 6, 2024, 7:09pm UTC](https://forum.csound.com/t/generating-and-detecting-a-midi-event-inside-a-csound-instance/1493/11 "2024-01-06T19:09:02Z")

</div>

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  
keyboard.

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  
schedulek(“myInstr”,0,1,kKey)  
endif

not tested, but you know what i mean …

best -  
joachim

---

<div class="post-metadata">

**Author:** ![Lovre](https://forum.csound.com/letter_avatar_proxy/v4/letter/l/bbce88/32.png) [@Lovre](https://forum.csound.com/u/Lovre)\
**Post date:** [January 6, 2024, 7:41pm UTC](https://forum.csound.com/t/generating-and-detecting-a-midi-event-inside-a-csound-instance/1493/12 "2024-01-06T19:41:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![faelannm](https://forum.csound.com/user_avatar/forum.csound.com/faelannm/32/584_2.png) [@faelannm](https://forum.csound.com/u/faelannm)\
**Post date:** [September 13, 2024, 9:06am UTC](https://forum.csound.com/t/generating-and-detecting-a-midi-event-inside-a-csound-instance/1493/13 "2024-09-13T09:06:12Z")

</div>

I have the similar query but not exactly. And the solution provided by you is not working in my case.
