# MIDI messages sent to VirtualMidiInput ends up nowhere

**URL:** <https://forum.juce.com/t/midi-messages-sent-to-virtualmidiinput-ends-up-nowhere/57725>\
**Category:** Tracktion Engine\
**Created:** [September 5, 2023, 7:58pm UTC](https://forum.juce.com/t/midi-messages-sent-to-virtualmidiinput-ends-up-nowhere/57725 "2023-09-05T19:58:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![svenemtell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/svenemtell/32/13347_2.png) [@svenemtell](https://forum.juce.com/u/svenemtell)\
**Post date:** [September 5, 2023, 7:58pm UTC](https://forum.juce.com/t/midi-messages-sent-to-virtualmidiinput-ends-up-nowhere/57725/1 "2023-09-05T19:58:47Z")

</div>

Hi,

I have a situation where I try to send MIDI notes to a track via a VirtualMidiInput. The notes never arrives at the synth living in the track (I can verify that by logging in the synth).

```auto
track->injectLiveMidiMessage(midiMessage, 0); // This works
virtualMidiInput->handleIncomingMidiMessage(midiMessage); // This doesn't

```

I can verify that the track is indeed a target track for `virtualMidiInput` by listing the target tracks:

```auto
auto vmii = listener->getCurrentInstanceForInputDevice(virtualMidiInput);
for (auto target : vmii->getTargetTracks()) {
  juce::Logger::writeToLog(" target track " + target->getName() + " " + (target == track ? "same" : "different"));
}

```

So… any tips for where to look further?

Thanks!

---

<div class="post-metadata">

**Author:** ![RolandMR](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rolandmr/32/641_2.png) [@RolandMR](https://forum.juce.com/u/RolandMR)\
**Post date:** [September 5, 2023, 8:31pm UTC](https://forum.juce.com/t/midi-messages-sent-to-virtualmidiinput-ends-up-nowhere/57725/2 "2023-09-05T20:31:58Z")

</div>

Check the options for the input, make sure end to end is enabled and the midi filter isn’t blocking any channels.

---

<div class="post-metadata">

**Author:** ![svenemtell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/svenemtell/32/13347_2.png) [@svenemtell](https://forum.juce.com/u/svenemtell)\
**Post date:** [September 6, 2023, 4:59pm UTC](https://forum.juce.com/t/midi-messages-sent-to-virtualmidiinput-ends-up-nowhere/57725/3 "2023-09-06T16:59:28Z")

</div>

Yes, I checked now, end-to-end is enabled and the channels are allowed.  
Is there anything else I should look for?

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [September 7, 2023, 9:54am UTC](https://forum.juce.com/t/midi-messages-sent-to-virtualmidiinput-ends-up-nowhere/57725/4 "2023-09-07T09:54:46Z")

</div>

Sorry, this is a bit tricky to guess at without a working example.  
Can you step through it a bit and see where it’s getting dropped?

It should to to `MidiInputDevice::sendMessageToInstances` and then eventually `MidiInputDeviceNode::handleIncomingMidiMessage`

---

<div class="post-metadata">

**Author:** ![svenemtell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/svenemtell/32/13347_2.png) [@svenemtell](https://forum.juce.com/u/svenemtell)\
**Post date:** [September 8, 2023, 4:48pm UTC](https://forum.juce.com/t/midi-messages-sent-to-virtualmidiinput-ends-up-nowhere/57725/5 "2023-09-08T16:48:25Z")

</div>

Debugging reveals that the if statement on line 138 of `MidiInputDeviceNode::processSection` is triggered and thereby discarding the note:

```auto
    // if it's been a long time since the last block, clear the buffer because
    // it means we were muted or glitching
    if (timeNow > lastReadTime + maxExpectedMsPerBuffer)
    {
        //jassertfalse
        numMessages = 0;
    }

```

We experience the problem in a test where we first run `TransportControl::ensureContextAllocated` and soon after `virtualMidiInput->handleIncomingMidiMessage(midiMessage)`.  
If we add `juce::Thread::sleep(100)` before the `handleIncomingMidiMessage` it works as expected, so there seems to be a race condition!?

Is `ensureContextAllocated` synchronous or asynchronous?  
If asynchronous, is there way to know when things are set up and `handleIncomingMidiMessage` can be sent?

---

<div class="post-metadata">

**Author:** ![eggnog](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eggnog/32/12211_2.png) [@eggnog](https://forum.juce.com/u/eggnog)\
**Post date:** [September 11, 2023, 9:43am UTC](https://forum.juce.com/t/midi-messages-sent-to-virtualmidiinput-ends-up-nowhere/57725/6 "2023-09-11T09:43:18Z")

</div>

Might be related to [Hanging notes when hosting AUv3 plugins on iOS · Issue #143 · Tracktion/tracktion\_engine · GitHub](https://github.com/Tracktion/tracktion_engine/issues/143)

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [September 11, 2023, 11:13am UTC](https://forum.juce.com/t/midi-messages-sent-to-virtualmidiinput-ends-up-nowhere/57725/7 "2023-09-11T11:13:49Z")

</div>

@eggnog, I don’t think its related to that.

@svenemtell `ensureContextAllocated` is synchronous.  
If `lastReadTime` is initialised to `juce::Time::getApproximateMillisecondCounter()` rather than `= 0`, does your test case work? That’s probably what it should be.

---

<div class="post-metadata">

**Author:** ![svenemtell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/svenemtell/32/13347_2.png) [@svenemtell](https://forum.juce.com/u/svenemtell)\
**Post date:** [September 11, 2023, 12:15pm UTC](https://forum.juce.com/t/midi-messages-sent-to-virtualmidiinput-ends-up-nowhere/57725/8 "2023-09-11T12:15:18Z")

</div>

Yes! Initialising `lastReadTime = juce::Time::getApproximateMillisecondCounter()` in `tracktion_MidiInputDeviceNode.h` solves the problem! 🙂  
Thanks!

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [September 11, 2023, 3:29pm UTC](https://forum.juce.com/t/midi-messages-sent-to-virtualmidiinput-ends-up-nowhere/57725/9 "2023-09-11T15:29:48Z")

</div>

> <https://github.com/Tracktion/tracktion_engine/commit/e84d2e6763608b63b30fb864e2ca150c28a0cbef>
