Sending MIDI Messages

Once you open a MIDI output device, you can begin sending it MIDI messages using the following function:

midiOutShortMsg

Sends a MIDI message to a specified MIDI output device.

Use midiOutShortMsg to send any MIDI message (except for system-exclusive messages). This function takes an HMIDIOUT parameter specifying the MIDI output device to send the message to, and a DWORD for the MIDI message. The message is packed into the DWORD, as shown in the following illustration:

The two MIDI data bytes are optional, depending on the MIDI status byte. The following code fragment uses midiOutShortMsg to send a given MIDI event to a given MIDI output device:

/* Sends a given MIDI event to the given output device
 */
WORD sendMIDIEvent(hMidiOut, bStatus, bData1, bData2)
HMIDIOUT hMidiOut;        // handle to the output device
BYTE bStatus;                // MIDI status byte
BYTE bData1;                // first MIDI data byte
BYTE bData2;                // second MIDI data byte
{
        union {
                DWORD dwData;
                BYTE bData[4];
        } u;

        /* Construct the MIDI message */
        u.bData[0] = bStatus;
        u.bData[1] = bData1;
        u.bData[2] = bData2;
        u.bData[3] = 0;

        /* Send the message */
        return midiOutShortMsg(hMidiOut, u.dwData);
}

Note:

MIDI output drivers are not required to verify data before sending it to an output port. It is up to applications to ensure only valid data is sent using midiOutShortMsg.