# Trouble understanding how to send generated midi messages

**URL:** <https://forum.juce.com/t/trouble-understanding-how-to-send-generated-midi-messages/50204>\
**Category:** General JUCE discussion\
**Created:** [February 16, 2022, 5:04am UTC](https://forum.juce.com/t/trouble-understanding-how-to-send-generated-midi-messages/50204 "2022-02-16T05:04:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jozangui](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Jozangui](https://forum.juce.com/u/Jozangui)\
**Post date:** [February 16, 2022, 5:04am UTC](https://forum.juce.com/t/trouble-understanding-how-to-send-generated-midi-messages/50204/1 "2022-02-16T05:04:29Z")

</div>

Greetings,

Having as base the _plugin_ JUCE template from projucer i want to generate a midi message in PluginEditor (_on a button click for example_) and send it out to wherever my plugin is connected to, making it sort of like the built in juce::MidiKeyboard.

From what i understood all messages pass in PluginAudioProcessor::processBlock, but i wasn’t able to see any message.

Generating midi message

```auto
auto message = juce::MidiMessage::noteOn (10, 60, (juce::uint8) 100);
midibuffer.addEvent(message);

```

Declared in Editor header:

```auto
juce::MidiBuffer midibuffer;
PluginAudioProcessor& audioProcessor;

```

My processBlock:

```auto
void PluginAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{

    for (const auto metadata : midiMessages)
    {
        const auto msg = metadata.getMessage();
        if (msg.isNoteOn()){
            DBG("note on!");
        };
    }
}

```

My questions are:

- how does processBlock know a message was generated in a child component
- if i should use the midibuffer where should it be declared
- how does juce output these internally generated midi messages since it has no definite “sendmidi(message)” type of command.

I am not concerned about the duration or handling, my goal with this example is to just send a midi note of type ‘on’ and be able to pass it to somewhere (ie. using Audio Plugin Host)

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [February 16, 2022, 11:23am UTC](https://forum.juce.com/t/trouble-understanding-how-to-send-generated-midi-messages/50204/2 "2022-02-16T11:23:24Z")

</div>

> [@Jozangui](#):
>
> how does processBlock know a message was generated in a child component

It doesn’t, unless you pass that information yourself. The normal way to do this is to have something like a separate midi buffer in the processor that the editor adds to. This can be a bit tricky to write, because the `processBlock` call will happen on a different thread to callbacks from the editor, so you’ll need to think carefully about thread safety.

> [@Jozangui](#):
>
> if i should use the midibuffer where should it be declared

Whatever you decide to use, you will probably need to add it to the AudioProcessor. The Editor will have a reference back to the Processor, and so will be able to access the buffer (or alternative) too.

> [@Jozangui](#):
>
> how does juce output these internally generated midi messages since it has no definite “sendmidi(message)” type of command.

Any messages left in the midi buffer after the call to `processBlock` finishes are assumed to be the AudioProcessor’s MIDI output.

* * *

I’d recommend looking at the AudioPluginDemo to see one approach for sending MIDI messages from a plugin’s editor. This demo’s editor contains a MIDI keyboard that can be used to send MIDI events.

---

<div class="post-metadata">

**Author:** ![Jozangui](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Jozangui](https://forum.juce.com/u/Jozangui)\
**Post date:** [February 16, 2022, 2:38pm UTC](https://forum.juce.com/t/trouble-understanding-how-to-send-generated-midi-messages/50204/3 "2022-02-16T14:38:12Z")

</div>

Hello reuk thank you so much for the reply.  
I indeed looked at the Demo you referenced and i was left wondering if i can use only the juce::MidiKeyboardState to handle these inputs with the noteOn() and addEvent().

I guess i have to study a little bit more, even tough the Demo is detailed is filled with stuff i dont need and it gets hard to track how everything works on a single overwhelming header file.

Kudos
