Home Banner image
Home

Project MIDI Connections

Project MIDI provides late-binding connections between assemblies of events and event handlers.

Reflection is used to identify those events and actions that are intended to be used for this purpose.

Project Midi Events

Events which happen within an assembly can be exposed to the rest of the assemblies by giving them the [ProjectMidiEventAttribute] attribute. This attribute informs Project MIDI that this event can be connected to Project Midi Actions. This event must conform to the following signature:


   public delegate void ProjectMidiEventHandler(object sender, EventArgs e);
   
   public event ProjectMidiEventHandler EventName;

Refer to the AssemblyInterfaces.cs file for the definition of of classes derived from EventArgs which may be used within Project MIDI.

ProjectMidiEventAttribute

This attribute identifies an event as a Project MIDI event. It can optionally specify the following parameters:

Dynamic Events

Some events may need to be created dynamically. For example, Midi Input Port events will be created depending on the particular MIDI hardware installed.

Classes which need to implement dynamic events must implement the IProjectMidiDynamicEvent interface.

To create a dynamic event, use the IProjectMidiConnection interface to add a new PMEventInfo object to the IProjectMidiConnection.AllEvents collection.

Refer to the example code below.

Project Midi Actions

Actions which are implemented within an assembly are exposed to the rest of the assemblies by giving them the [ProjectMidiActionAttribute] attribute. This attribute informs Project MIDI that Project Midi Events can be connected to them. These actions must conform to the following signature:


   public void HandlerName(object sender, EventArgs e);

As you can see, this signature matches the ProjectMidiEventHandler delegate signature listed above it.

Refer to the sample source code on the Downloads page for actual code listings and comments.

Dynamic Actions

Some actions may need to be created dynamically. For example, Midi Output Port actions will be created depending on the particular MIDI hardware installed.

Classes which need to implement dynamic actions must implement the IProjectMidiDynamicActions interface.

To create a dynamic action, use the IProjectMidiConnection interface to add a new PMActionInfo object to the IProjectMidiConnection.AllActions collection.

Refer to the example code below.

Automatic Connections

Some connections will need to be connected automatically when ProjectMIDI starts. For example, connecting a source of trace messages to the Trace Message assembly, which then displays the messages. This is accomplished by specifying a common connection name in the attributes for both the events and the actions.

The first time that Project MIDI loads an assembly, it will search for events and actions that specify the same autoconnect name. When found, they will be automatically connected.

Existing connections will be saved to connections.xml. When Project MIDI starts, it will load connections.xml and reconnect events and actions previously connected.

Example of creating dynamic events and actions

The following example is extracted from the midi assembly. It demonstrates how to create dynamic actions and events.

It is implemented in an All Assemblies Loaded handler. Use the IProjectMidiParent interface to receive a AllAssembliesLoaded event notification.


   private void AllAssembliesLoadedHandler( object sender, EventArgs e )
   {
      foreach(IProjectMidi iproj in projectMidiParent.IProjectMidis)
      {
         iconnections = iproj as IProjectMidiConnections;
         if(iconnections != null) break;
      }
      // Create a dynamic connection for each port
      if(iconnections!=null)
      {
         foreach( MidiInputPort iport in midiInputPorts)
         {
            PMEventInfo pmevent = new PMEventInfo(iport.Name, 
                                                  "Midi", 
                                                  1, 
                                                  iport.Name, 
                                                  ConnectionType.Normal, 
                                                  iport, 
                                                  this );
            iconnections.AllEvents.Add( pmevent );
         }
         foreach( MidiOutputPort oport in midiOutputPorts)
         {
            // Dynamically create an Action for this output port
            PMActionInfo pmaction = new PMActionInfo(oport.Name, 
                                                     "Midi", 
                                                     1, 
                                                     oport.Name, 
                                                     oport, 
                                                     this );
            iconnections.AllActions.Add( pmaction );
         }
      }
   }