How to send MIDI miessages independent of audio block size?


#1

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?


#2

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.