This project has moved and is read-only. For the latest updates, please go here.

How to send a program change message?

Topics: Midi Message Classes
Dec 15, 2013 at 9:18 PM
Can you help by telling me if I have this right (or at least am on the right track) ? I want to send a patch change to an instrument. I think I can do it like this...

public void SendPatch(int outPortId, int channel, int patchnumber)


// Define the Midi Status Byte for a ProgramChange
byte ProgramChange = 0xC0;

//Set the Channel that it is going out on. (Why do we & 0x0F?)
byte outchannel = (byte)((channel - 1) & 0x0F);

// Create the outgoing status byte; combination of channel and ProgramChange
byte _outPatchChange;
_outPatchChange = (byte)(ProgramChange | channel);

//Declare a new MidiData
MidiData eventData = new MidiData(patchnumber);

//Set the status byte = to our (Channel & ProgramChange)
eventData.Status = _outPatchChange;

// Set the first data byte equal to the patchnumber passed in
eventData.Parameter1 = (byte)patchnumber;

//Open the MidiPort
MidiOutPort _outPort = new MidiOutPort();

//Send the patchChange to the Midi Port

//All done; close the Midi Port

Dec 16, 2013 at 6:34 AM
The MidiData structure was made to help you with this. So you can also write:
MidiData pc = new MidiData();
pc.Status = 0xC0;
pc.Channel = channel;
pc.Parameter1 = patchNumber;
This you can send to the output port.

Note that you are closing the output port right after you send the data. Although the methods may seem synchronous, the driver may be asynchronous. That could mean that you close the port before the midi was sent...

Hope it helps.
Dec 16, 2013 at 4:19 PM
Thank you! That approach is much simpler. I'll give it a try... my current approach didn't seem to work although I could step through it without error.
Dec 17, 2013 at 12:02 AM
Using the approach shown above, should channel (as in "pc.Channel = channel; ") be zero-based? For Midi Channel #1, should the value of channel = 0 ?
Dec 17, 2013 at 6:19 AM
Yes. The MidiData struct just helps you shuffling the parts of a short Midi message around. It does not change the value of these parts. So you use the values you wan't to see in the resulting Midi message. So channels would be zero-based. Parameters range from 0-127.