How Do I ... ?

Control a MIDI Device

 

 

MIDI Musical Instrument Digital Interface: an industry-standard communications protocol that enables electronic musical instruments and other equipment to communicate, control and synchronize with each other in real time. is an industry-standard serial protocol that enables real-time control of electronic musical instruments and audio equipment, and can be used to transmit musical notes, parametric and effects signals, configuration and timecode data.

 

Online Resources

There is a multimedia walk-through of these techniques in the online Technical Resources.

 

You Will Need...

To send MIDI control commands to an external device, you will need a sound card in your control PC that has a MIDI Out port.

 

You can usually test MIDI music playback without the need for any external MIDI devices connected, as Windows provides a software synthesizer which can play basic notes and emulate instruments.

 

Configuring MIDI Devices

Stardraw Control uses Windows Multimedia (MCI) drivers for its MIDI support: when Stardraw Control sends and receives MIDI commands, it communicates using a built-in Windows driver, which in turn communicates with your hardware's driver.  This means that there is very little you need to configure within Stardraw Control to work with your MIDI equipment.

 

To add support for sending MIDI messages, the first step is to add a generic MIDI Device to your project.

 

To add a MIDI Device

  1. In the Topology View, locate the MIDI Device in the Generic Control category of the Stardraw Control Products List.

  2. Click and drag the device to add it to your topology.

  3. Add a connection from your Computer's MIDI Out port to the MIDI In port of the new device.

 

To configure your control PC

  1. Select the Computer device in the topology.

  2. Change the MIDI Out DeviceID property to the Device ID of your sound card's MIDI Out port.

 

Notes

If you only have one MIDI Output port available on your PC, the Device ID will probably be 0 (zero).  If additional MIDI Output ports are installed in your PC they might be assigned a Device ID of 1 or more.

Additional configuration using the Windows MIDI Mapper (in the Windows Control Panel), or your sound card's configuration software, such as patch assignments or channel maps, may also affect the behavior of your external devices.

 

Controlling a MIDI Device

The generic MIDI Device is a virtual representation of your external device, and supports basic Note On, Note Off, Pitch Wheel and Program Change commands, and can set Velocity, Channel and Controller parameters.

 

More advanced MIDI support is possible by modifying the generic MIDI Device adding any additional commands you need.  See Modifying MIDI Device Drivers below for more information.

 

As a basic example, we'll create a slider control on a form that plays a MIDI note: the value of the slider will be used to set the pitch of the note played:

 

To create an Action that plays a MIDI note

  1. In the Forms View, from the Controls tab of the Toolbox on the left side of the screen, click and drag a Level Slider control onto the form.

  2. Double-click the level slider control to display the Actions Editor.
    Note that the selected event is levelSlider1.ValueChanged: this is the event that we wish to capture to play a MIDI note.

  3. Add the following Action to set the SendNoteOn property:
    Devices – MIDI Device (1) – MIDI In – Set SendNoteOn to

  4. Set the SendNoteOn property to:
    Controls – levelSlider1 – Value

  5. Click Ok to close the Actions dialog.

 

Run the project: when you change the slider control, you should hear notes being played from your MIDI device; the note's pitch corresponds to the level of the slider.  Note that the slider control has a range of values from 0 to 100, which is within the acceptable range of values for the SendNoteOn property.

 

The controllable properties of the generic MIDI Device are:

 

Property

Description

ChannelNumber

Gets or sets the MIDI channel that the next MIDI message will use: values are in the range 0 to 127 and the default value is 0 (zero).

ControllerID

Gets or sets the controller number that the next SendController message will use: values are in the range 0 to 127 and the default value is 0 (zero).  Note: The ControllerID value could be a standard defined controller number or determined by the MIDI device.

Velocity

Gets or sets the Velocity that the next NoteOn or NoteOff message will use: values are in the range 0 to 127, and specifies with how much force the note should be played.  The default value is 127.

SendNoteOn

Sends a NoteOn message using the current ChannelNumber and Velocity: values are in the range 0 to 127 representing which note should be played, where a value of 60 represents Middle C (or C4).

SendNoteOff

Sends a NoteOff message for the current ChannelNumber and Velocity: values as per SendNoteOn.

SendPitchWheel

Sends a PitchWheel message using the current ChannelNumber: values are in the range 0 to 16,383 representing ± 2 half steps transposition, where a value of 8,192 represents the Pitch Wheel in the centre position, i.e. no pitch change.

SendProgramChange

Sends a Program Change message using the current ChannelNumber: values are in the range 0 to 127 representing the program (i.e. patch, instrument or preset, etc.)

SendController

Sends a Controller message using the current ChannelNumber and ControllerID: values are in the range 0 to 127 representing the value to which the controller should be set.

 

Modifying MIDI Device Drivers

This example shows you how to modify the generic MIDI Device to add support for the Volume controller value.  It illustrates how to extend the existing driver using the functionality of the MidiInPortInstance base class.

 

The MIDI protocol specifies a controller number of 7 for Coarse Volume and 39 for Fine Volume.  In this example, we will implement Coarse Volume, whose value is in the range 0 to 127.

 

To modify the generic MIDI Device

  1. In the Topology View, double-click the MIDI Device (1) device.

  2. In the Product Properties dialog, select the Midi In port and click Edit, then click Edit again: the Script Editor will open.

 

In the script, the driver's class derives from the MidiInPortInstance base class.  This base class includes a Controller method that we can use to set a specific controller value.

 

 

public class MyClass : MidiInPortInstance

 

 

In the script editor, add the following code fragment to the MyClass class:

 

 

[Controllable]

public void SendVolume( byte volume )

{

   // send a Coarse Volume controller value to the current channel

   Controller( channelNumber, 7, volume );

}

 

 

This creates a new method called SendVolume, which sends a controller message by calling the base class's Controller method with the current channel number, which is a member variable of the MyClass class, a controller number of 7 representing Coarse Volume, and the volume value itself.

 

Tip

You can apply these changes directly to the generic MIDI Device in the Product list, which means you can reuse your new driver features in other Stardraw Control projects.  To do this, double-click the MIDI Device in the Generic Control category of the Stardraw Control Products List, and edit the script for the Midi In port as above.  Because you are attempting to edit a master product, when you save your changes, Stardraw Control will ask you whether to copy the new device into My Libraries.

 

To test the new function, we can add a second slider to the form created in the previous example.

 

To create an Action that changes the MIDI volume

  1. In the Forms View, add a second Level Slider control to the form.

  2. Double-click the new slider control to display the Actions Editor.

  3. Add the following Action to set the SendVolume property:
    Devices – MIDI Device (1) – MIDI In – Set SendVolume to

  4. Set the SendVolume property to:
    Controls – levelSlider2 – Value

  5. Click Ok to close the Actions dialog.

 

Run the project: change the level of the second slider: when you move the first slider the note's volume corresponds to the level of the second slider.

 

Please refer to the Class Library section for full details of the MidiInPortInstance base class.

 

See Also

Class Library