Managing MIDI Thru

You can connect a MIDI input device directly to a MIDI output device so that when the input device receives an MIM_DATA message, the system sends a message with the same MIDI event data to the output device driver. To connect a MIDI output device to a MIDI input device, use the midiConnect function.

To achieve the best possible performance with multiple outputs, an application can choose to supply a special form of MIDI output driver, called a thru driver. Although the system allows only one MIDI output device to be connected to a MIDI input device, multiple MIDI output devices can be connected to a thru driver. An application on such a system could connect the MIDI input device to this thru device and connect the MIDI thru device to as many MIDI output devices as needed. For more information about thru drivers, see the Windows device-driver documentation.

Using Messages to Manage MIDI Recording

The following messages can be sent to a window or thread callback procedure for managing MIDI recording.

Value Meaning
MM_MIM_CLOSE Sent when a MIDI input device is closed by using the midiInClose function.
MM_MIM_DATA Sent when a complete MIDI message is received. (This message is used for all MIDI messages except system-exclusive messages.)
MM_MIM_ERROR Sent when an invalid MIDI message is received. (This message is used for all MIDI messages except system-exclusive messages.)
MM_MIM_LONGDATA Sent when either a complete MIDI system-exclusive message is received or when a buffer has been filled with system-exclusive data.
MM_MIM_LONGERROR Sent when an invalid MIDI system-exclusive message is received.
MM_MIM_MOREDATA Sent when an application is not processing MIM_DATA messages fast enough to keep up with the input device driver.
MM_MIM_OPEN Sent when a MIDI input device is opened by using the midiInOpen function.

 

A wParam parameter and an lParam parameter are associated with each of these messages. The wParam parameter always specifies the handle of an open MIDI device. The lParam parameter is unused for the MM_MIM_CLOSE and MM_MIM_OPEN messages.

For the MM_MIM_LONGDATA message, lpMidiHdr specifies an address of a MIDIHDR structure that identifies the buffer for system-exclusive messages. The buffer may not be completely filled, because the size of the system-exclusive messages is usually not known before being recorded and because buffers whose total size can contain the largest expected message must be allocated. To determine the amount of valid data present in the buffer, use the dwBytesRecorded member of the MIDIHDR structure.

Using a Callback Function to Manage MIDI Recording

You can define your own callback function to manage recording for MIDI input devices. The callback function is documented as MidiInProc.

The following messages can be sent to the wMsg parameter of the MidiInProc callback function.

Value Meaning
MIM_CLOSE Sent when the device is closed by using the midiInClose function.
MIM_DATA Sent when a complete MIDI message is received (this message is used for all MIDI messages except system-exclusive messages).
MIM_ERROR Sent when an invalid MIDI message is received (this message is used for all MIDI messages except system-exclusive messages).
MIM_LONGDATA Sent when either a complete MIDI system-exclusive message is received or when a buffer has been filled with system-exclusive data.
MIM_LONGERROR Sent when an invalid MIDI system-exclusive message is received.
MIM_MOREDATA Sent when an application is not processing MIM_DATA messages fast enough to keep up with the input device driver.
MIM_OPEN Sent when the MIDI input device is opened by using the midiInOpen function.

 

These messages are similar to those sent to window procedure functions, but the parameters are different. A handle of the open MIDI device is passed as a parameter to the callback function, along with the doubleword of instance data that was passed by using midiInOpen.

For the MIM_LONGDATA message, lpMidiHdr specifies an address of a MIDIHDR structure that identifies the buffer for system-exclusive messages. The buffer might not be completely filled. To determine the amount of valid data present in the buffer, use the dwBytesRecorded member of the MIDIHDR structure.

After the device driver is finished with a data block, you can clean up and free the data block.

Recording MIDI Audio