# MIDI note-on and note-off at the same time?

**URL:** <https://forum.juce.com/t/midi-note-on-and-note-off-at-the-same-time/15263>\
**Category:** General JUCE discussion\
**Created:** [August 7, 2015, 2:35pm UTC](https://forum.juce.com/t/midi-note-on-and-note-off-at-the-same-time/15263 "2015-08-07T14:35:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alkamist](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@Alkamist](https://forum.juce.com/u/Alkamist)\
**Post date:** [August 7, 2015, 2:35pm UTC](https://forum.juce.com/t/midi-note-on-and-note-off-at-the-same-time/15263/1 "2015-08-07T14:35:25Z")

</div>

There was a similar thread but the question was not answered.

In the current way I am implementing processing of MIDI messages, if a note is released and a new note started on the same position in the sample buffer, the new note-on will be eaten.

I'm just curious how you deal with this.

This is my current implementation:

```
// In processBlock
{
    MidiBuffer::Iterator MIDIMessagesIterator (midiMessages);
    MidiMessage currentMidiMessage;
    int midiMessageSamplePosition;
    for (int sample = 0; sample < buffer.getNumSamples(); ++sample)
    {
        MIDIMessagesIterator.setNextSamplePosition (sample);
        // Is there at least one more MIDI message in the buffer?
        if (MIDIMessagesIterator.getNextEvent (currentMidiMessage, midiMessageSamplePosition))
        {
            // Is the message we got a note on and is it in the
            // right place?
            if (currentMidiMessage.isNoteOn() 
                && midiMessageSamplePosition == sample)
            {
                // Do whatever here.
            }
        }
    }
}
```

Any help is appreciated!

&nbsp;

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 7, 2015, 2:38pm UTC](https://forum.juce.com/t/midi-note-on-and-note-off-at-the-same-time/15263/2 "2015-08-07T14:38:11Z")

</div>

You're horribly misusing the midi buffer iterator! You shouldn't ever set its&nbsp;sample position, just iterate it and let it tell you the sample position of each event that it gives you. The juce::Synthesiser class does all this kind of thing for you - why not have a look at how that works?

---

<div class="post-metadata">

**Author:** ![Alkamist](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@Alkamist](https://forum.juce.com/u/Alkamist)\
**Post date:** [August 7, 2015, 4:21pm UTC](https://forum.juce.com/t/midi-note-on-and-note-off-at-the-same-time/15263/3 "2015-08-07T16:21:35Z")

</div>

I'm not really making a synthesizer. I'm making an audio effect that responds to midi.

&nbsp;

I'm taking a look at the juce::Synthesiser class but I'm having a little bit of trouble understanding what's going on. I'd really like a MIDI implementation that is devoid of the concept of anything other than MIDI handling. I haven't found anything like this yet though.

&nbsp;

I would basically like to handle the incoming MIDIBuffer from processBlock and call whatever I want with it (no concept of rendering an audio block or synth voices).

---

<div class="post-metadata">

**Author:** ![Alkamist](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@Alkamist](https://forum.juce.com/u/Alkamist)\
**Post date:** [August 7, 2015, 6:13pm UTC](https://forum.juce.com/t/midi-note-on-and-note-off-at-the-same-time/15263/4 "2015-08-07T18:13:56Z")

</div>

I think the main source of my confusion right now is in the way I am handling my DSP. I'm confused what aspects of my program should be aware of what a buffer is.

&nbsp;

The way I'm currently doing it is:

- Start on the current sample

- Process each external processing aspect so their current sample is up to date

- Iterate through each channel and set the output accordingly

&nbsp;

The way I see it done in the JUCE example:

- For each processing aspect:

&nbsp; &nbsp; &nbsp; &nbsp; - Pass in the audio and MIDI buffer

&nbsp; &nbsp; &nbsp; &nbsp; - Render through the entire buffer size individually for each channel

&nbsp;

Should I be doing it this way? Should all of my aspects be capable of rendering a buffer, or should they simply be able to output the next sample?
