Skip to content

Instantly share code, notes, and snippets.

@pinebit
Last active September 8, 2023 06:34
Show Gist options
  • Save pinebit/07e10862dba33c60873b to your computer and use it in GitHub Desktop.
Save pinebit/07e10862dba33c60873b to your computer and use it in GitHub Desktop.
Playing MIDI using raw WIN32 API
// Step 1. Open default MIDI-device or die
int rc = midiOutOpen(&midi_device, 0, 0, 0, CALLBACK_NULL);
if (rc != MMSYSERR_NOERROR) {
return FALSE;
}
// Step 2. Set instrument for a channel (0..15)
DWORD command = (0x000000C0 | CHANNEL) | (INSTRUMENT << 8);
midiOutShortMsg(midi_device, command);
// Step 3. "Touch" a key, sleep some, and "Release" then.
DWORD command = (0x00000090 | CHANNEL) | (KEY << 8) | (VOLUME << 16);
midiOutShortMsg(midi_device, command);
Sleep(100);
command &= 0x0000FFFF;
midiOutShortMsg(midi_device, command);
// Step 4. Reset and Close the MIDI device as no longer needed
midiOutReset(midi_device);
midiOutClose(midi_device);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment