# How to send MIDI miessages independent of audio block size?

**URL:** <https://forum.juce.com/t/how-to-send-midi-miessages-independent-of-audio-block-size/20548>\
**Category:** General JUCE discussion\
**Created:** [January 10, 2017, 4:16pm UTC](https://forum.juce.com/t/how-to-send-midi-miessages-independent-of-audio-block-size/20548 "2017-01-10T16:16:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![horigome](https://avatars.discourse-cdn.com/v4/letter/h/4da419/32.png) [@horigome](https://forum.juce.com/u/horigome)\
**Post date:** [January 10, 2017, 4:16pm UTC](https://forum.juce.com/t/how-to-send-midi-miessages-independent-of-audio-block-size/20548/1 "2017-01-10T16:16:14Z")

</div>

I’m writing a VST plugin that is supposed to process MIDI in as fast a manner as possible (\<10 ms). How can I send MIDI notes in JUCE independent of the audio block size determined by the host?

One idea I had is to keep track of time stamps and at each processBlock call I would send multiple midi messages at one, each with different time stamp. Another idea I had is to run another timer that would send midi messages independent of the block size. The little intuition/knowledge I have about threads tells me that the latter solution is not ideal. What do you think?

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [January 12, 2017, 11:45am UTC](https://forum.juce.com/t/how-to-send-midi-miessages-independent-of-audio-block-size/20548/2 "2017-01-12T11:45:58Z")

</div>

> [@horigome](#):
>
> One idea I had is to keep track of time stamps and at each processBlock call I would send multiple midi messages at one, each with different time stamp.

Yes this is exactly what you want to be doing. For example, if you wanted to output a large midi buffer, you would do something like this (Warning: untested):

```
	void processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
	{
		const int n = buffer.getNumSamples();
		MidiBuffer::Iterator it (*midiBufferToPlay);
		it.setNextSamplePosition (midiPosition);
	
		int position = midiPosition;
		MidiMessage msg;
	
		while (it.getNextEvent (msg, position) && position < (midiPosition + n))
			midiMessages.addEvent (msg, position - midiPosition);
		
		midiPosition += n;
	}
.
.
.

private:
	int midiPosition;
	MidiBuffer midiBufferToPlay;

```

…where the midi event time-stamps are assumed to be in samples.
