# Working through the MIDI queue

**URL:** <https://forum.juce.com/t/working-through-the-midi-queue/33964>\
**Category:** General JUCE discussion\
**Created:** [June 17, 2019, 6:55pm UTC](https://forum.juce.com/t/working-through-the-midi-queue/33964 "2019-06-17T18:55:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheWaveWarden](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thewavewarden/32/13708_2.png) [@TheWaveWarden](https://forum.juce.com/u/TheWaveWarden)\
**Post date:** [June 17, 2019, 6:55pm UTC](https://forum.juce.com/t/working-through-the-midi-queue/33964/1 "2019-06-17T18:55:28Z")

</div>

I noticed that some note-off events were missing when playing “for real” leaving the note stuck until I hit it again. Now I was wondering if I am working through the MIDI queue correctly?

```auto
void OdinAudioProcessor::processBlock(AudioBuffer<float> &buffer,
                                      MidiBuffer &midiMessages) {
  MidiMessage midi_message;
  int midi_message_sample;
  MidiBuffer::Iterator midi_iterator(midiMessages);
  bool midi_message_remaining =
      midi_iterator.getNextEvent(midi_message, midi_message_sample);

  // loop over samples
  for (int sample = 0; sample < buffer.getNumSamples(); ++sample) {
  if (midi_message_remaining) {
    if (midi_message_sample <= sample) {
      doSomethingWithMIDIEvent();
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [June 17, 2019, 7:25pm UTC](https://forum.juce.com/t/working-through-the-midi-queue/33964/2 "2019-06-17T19:25:10Z")

</div>

It looks like you are only pulling one event out of the queue per call to `processBlock`. There may be more than one in there, and you need to process the ones that fall between the times of the buffer.

---

<div class="post-metadata">

**Author:** ![TheWaveWarden](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thewavewarden/32/13708_2.png) [@TheWaveWarden](https://forum.juce.com/u/TheWaveWarden)\
**Post date:** [June 17, 2019, 7:36pm UTC](https://forum.juce.com/t/working-through-the-midi-queue/33964/3 "2019-06-17T19:36:49Z")

</div>

> [@cpr](#):
>
> It looks like you are only pulling one event out of the queue per call to `processBlock`

True, although it’s one per sample. So two events on the last sample will result in one missing 🤔

Thanks I’ll fix that 🙂

---

<div class="post-metadata">

**Author:** ![TheWaveWarden](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thewavewarden/32/13708_2.png) [@TheWaveWarden](https://forum.juce.com/u/TheWaveWarden)\
**Post date:** [June 17, 2019, 8:18pm UTC](https://forum.juce.com/t/working-through-the-midi-queue/33964/4 "2019-06-17T20:18:32Z")

</div>

Ok funnyly enough I played and a note got stuck which was literally **below the range of my 88 key Midi keyboard**.

I further noticed, notes are not getting stuck in my plugin, but in Bitwig studio itself…  
I don’t know whats happening, but bitwig is somehow getting confused MIDI messages. Anyway, my plugin is innocent 😇

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [June 17, 2019, 8:21pm UTC](https://forum.juce.com/t/working-through-the-midi-queue/33964/5 "2019-06-17T20:21:49Z")

</div>

You updated your code to iterate over the midibuffer until it reaches an event past the current buffer time?

---

<div class="post-metadata">

**Author:** ![TheWaveWarden](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thewavewarden/32/13708_2.png) [@TheWaveWarden](https://forum.juce.com/u/TheWaveWarden)\
**Post date:** [June 17, 2019, 8:39pm UTC](https://forum.juce.com/t/working-through-the-midi-queue/33964/6 "2019-06-17T20:39:55Z")

</div>

> [@cpr](#):
>
> You updated your code

Ah yes I did, forgot to mension it. As said the problem persists (which doesn’t say this wasn’t causing problems as well).

I’ll be best off testing in another host now I think! (The notes are getting stuck in Bitwig even without my plugin being loaded in)

Thanks for your input! muchos appreciatos 🙂
