Main Content

midisync

Send values to MIDI controls for synchronization

Description

midisync(midicontrolsObject) sends the initial values of controls to your MIDI device, as specified by your MIDI controls object. To create this object, use the midicontrols function. If your MIDI device can receive and respond to messages, it adjusts its controls as specified.

Note

Many MIDI devices are not bidirectional. Calling midisync with a unidirectional device has no effect. midisync cannot tell whether a value is successfully sent to a device or even whether the device is bidirectional. If sending a value fails, no errors or warnings are generated.

example

midisync(midicontrolsObject,controlValues) sends controlValues to the MIDI controls associated with the specified midicontrolsObject.

example

Examples

collapse all

Use midiid to identify a control on your default MIDI device.

[controlNumber,~] = midiid;
Move the control you wish to identify; type ^C to abort.
Waiting for control message... done

Create a MIDI controls object. Specify an initial value for your control. Call midisync to set the specified control on your device to the initial value.

initialValue = 0.5;
midicontrolsObject = midicontrols(controlNumber,initialValue);
midisync(midicontrolsObject);

Use midiid to identify three controls on your default MIDI device.

[controlNumber1,~] = midiid;
[controlNumber2,~] = midiid;
[controlNumber3,~] = midiid;
controlNumbers = [controlNumber1,controlNumber2,controlNumber3];
Move the control you wish to identify; type ^C to abort.
Waiting for control message... done
Move the control you wish to identify; type ^C to abort.
Waiting for control message... done
Move the control you wish to identify; type ^C to abort.
Waiting for control message... done

Create a MIDI controls object. Specify initial values for your controls. Call midisync to set the specified control on your device to the initial value.

controlValues = [0,0,1];
midicontrolsObject = midicontrols(controlNumbers,controlValues);
midisync(midicontrolsObject);

Create a loop that updates your control values and synchronizes those values to the physical controls on your device.

for i = 1:100
    controlValues = controlValues + [0.006,0.008,-0.008];
    midisync(midicontrolsObject,controlValues);
    pause(0.1)
end

Define this function and save it to your current folder.

function trivialmidigui(controlNumber,deviceName)

    slider = uicontrol('Style','slider');
    mc = midicontrols(controlNumber,'MIDIDevice',deviceName);
    midisync(mc);
    set(slider,'Callback',@slidercb);
    midicallback(mc, @mccb);
    
    function slidercb(slider,~)
        val = get(slider,'Value');
        midisync(mc, val);
        disp(val);
    end

    function mccb(mc)
        val = midiread(mc);
        set(slider,'Value',val);
        disp(val);
    end

end

Use midiid to identify a control number and device name. Call the function you created, specifying the control number and device name as inputs.

[controlNumber,deviceName] = midiid;
trivialmidigui(controlNumber,deviceName)

The slider on the user interface is synchronized with the specified control on your device. Move one to see the other respond.

Input Arguments

collapse all

Object that listens to the controls on a MIDI device, specified as an object created by midicontrols.

Values sent to MIDI device, specified as a scalar or an array the same size as controlNumbers of the associated midicontrols object. If you do not specify controlValues, the default value is the initialValues of the associated midicontrols object.

The possible range for controlValues depends on the OutputMode of the associated midicontrols object.

  • If OutputMode is specified as 'normalized', then controlValues must consist of values in the range [0,1]. The default OutputMode is 'normalized'.

  • If OutputMode is specified as 'rawmidi', then controlValues must consist of integer values in the range [0,127].

Example: 0.3

Example: [0,0.3,0.6]

Example: 5

Example: [5;15;20]

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Version History

Introduced in R2016a