midiMessage questions


#1

I’m using an external sound library which has callback functions to deal with midi, I’ve setup something like this in my ProcessBlock:

MidiMessage message(0xf4, 0.0, 0, 0);
MidiBuffer::Iterator i (midiMessages);
int messageFrameRelativeTothisProcess;
while (i.getNextEvent (message, messageFrameRelativeTothisProcess))
{
IncomingMidiMessage = &message;
}

where IncomingMidiMessage midi message is a member of my class. First off is this the correct way to retrieve midi messages from a midibuffer? When I was writing this software before using the VST sdk my callback function looked like this:

int csVSTi::ReadMidiData(CSOUND *csound, void *userData,
unsigned char *mbuf, int nbytes)
{
csVSTi *midiData = (csVSTi *)userData;
if(!userData)
cout << “\n\nInvalid”;

int cnt = 0;
while (!midiData->midiEventQueue.empty() && cnt <= (nbytes - 3)) {
const VstMidiEvent &event = midiData->midiEventQueue.front();
mbuf[cnt + 0] = (unsigned char) event.midiData[0];
mbuf[cnt + 1] = (unsigned char) event.midiData[1];
mbuf[cnt + 2] = (unsigned char) event.midiData[2];
midiData->midiEventQueue.pop_front();
switch ((int) mbuf[cnt] & 0xF0) {
case 0x80: /* note off /
case 0x90: /
note on /
case 0xA0: /
polyphonic pressure /
case 0xB0: /
control change /
case 0xE0: /
pitch bend /
cnt += 3;
break;
case 0xC0: /
program change /
case 0xD0: /
channel pressure /
cnt += 2;
break;
case 0xF0:
switch ((int) mbuf[cnt]) {
case 0xF8: /
timing clock /
case 0xFA: /
start /
case 0xFB: /
continue /
case 0xFC: /
stop /
case 0xFF: /
reset /
cnt++;
}
break; /
ignore any other message */
}
}
return cnt;
}

I’m just confused as to how I do the same with Juce? Can anyone help to juce-ify the above code?


#2

I should have said that in the code I posted earlier csVSTi is a class derived from AudioEffectX and midiEventQueue is a std::list…


#3

An example how to deal with it in JUCE: http://code.google.com/p/ctrlr/source/browse/trunk/EdoController/EdoModulatorManager/EdoModulatorManager.cpp (look for the processBlock method)
and actual midi message processing: http://code.google.com/p/ctrlr/source/browse/trunk/EdoController/EdoMidiInputMapper/EdoMidiInputMapper.cpp (look for the process and match methods)


#4

Hi Atom, thanks for taking the time to reply. I’ve just taken a look over your code. I’ll go back over it again but I’m still confused however by how midi in handled in Juce, hence I hope you don’t mind answering a simple noobie question: With the VST sdk whenever a midi event is sent to the plugin processEvents() is called, is there an equivalent method in Juce, or are all midi events send in buffers to the processBlock? If so I guess if at one point in time there is no event being sent from the host then buffer will be empty?


#5

exactly the MidiBuffer passed by reference to the

processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)

method is the midi I/O for the plugin, incomming events are passed in that buffer (if not events then the buffer is empty), if you wish to send some events to the host, add them to the buffer and the host will pick them up when the method is done.


#6

Thanks Atom, that clears things up for me. Reading over you code nearly gave me a nose bleed, I haven’t concentrated that hard in a long time!


#7

is that good or bad ? :slight_smile:

it works :slight_smile: that’s all i can say, http://code.google.com/p/ctrlr/wiki/Ctrlrs


#8

Last question, do you know if most hosts send a final note-off message when a user stops playback? I’m getting a problem whereby I’ve got notes that don’t stop if I stop playback during a midi note? You see know what I mean?


#9

from what i remember they do plus they should send a CC - all notes off (number 123) http://atom.maczo.pl/~atom/stuff/tech/midispec.htm


#10

So it’s obviously something I’m doing in my callback function, like failing to deal with an all notes off message. Thanks again.


#11

[quote=“atom”]An example how to deal with it in JUCE: http://code.google.com/p/ctrlr/source/browse/trunk/EdoController/EdoModulatorManager/EdoModulatorManager.cpp (look for the processBlock method)
and actual midi message processing: http://code.google.com/p/ctrlr/source/browse/trunk/EdoController/EdoMidiInputMapper/EdoMidiInputMapper.cpp (look for the process and match methods)[/quote]

Hi Atom,

I know this was over 2 years ago but do you still have that code available? I’d like to take a look if I may.

Thanks

Dave