As a side project, I am implementing a MIDI matrix to connect several MIDI keyboards to audio sources. The key requirement here is the echo of the MIDI data received on the input port to the selected output port.
Having created the necessary P / Invoke declarations and wrappers, I notice that the Win32 documentation for MidiInProcsays: "Applications should not call any multimedia functions inside the callback function, as this can cause a deadlock."
Given that it is not safe to call midiOutShortMsgfrom the inside MidiInProc, my current solution is to write MIDI data to the queue and set up the event. A worker thread waits for an event and calls midiOutShortMsg. The general idea is: -
static void InputCallback( int hMidiIn, uint wMsg, uint dwInstance, uint dwParam1, uint dwParam2 )
{
if( wMsg == MM_MIM_DATA )
{
data.EnQueue( dwParam1 );
dataReady.Set();
}
}
void ThreadProc
{
while( !_done )
{
dataReady.WaitOne();
midiOutShortMsg( hMidiOut, data.DeQueue() );
}
}
, , , dataReady.Set() InputCallBack, , preemption midiOutShortMsg ( ).
?