Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

midicallback

Call function handle when MIDI controls change value

Syntax

oldFunctionHandle = midicallback(midicontrolsObject,functionHandle)
oldFunctionHandle = midicallback(midicontrolsObject,[])
currentFunctionHandle = midicallback(midicontrolsObject)

Description

example

oldFunctionHandle = midicallback(midicontrolsObject,functionHandle) sets functionHandle as the function handle called when midicontrolsObject changes value, and returns the previous function handle, oldFunctionHandle.

example

oldFunctionHandle = midicallback(midicontrolsObject,[]) clears the function handle.

example

currentFunctionHandle = midicallback(midicontrolsObject) returns the current function handle.

Examples

collapse all

Create a default MIDI controls object. Use midicallback to associate an anonymous function with your MIDI controls object, mc.

mc = midicontrols; 
midicallback(mc,@(x)disp(midiread(x)));

Move any control on your default MIDI device to display its current normalized value on the command line.

    0.5079

    0.5000

    0.4921

    0.4841

    0.4762

    0.4683

    0.4603

    0.4683

Use midiid to identify the name of your MIDI device and a specified control. Move the MIDI control you want to identify.

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

Create an object that responds to the control you specified.

midicontrolsObject = midicontrols(controlNumber);

Define a function that plots a sinusoid with the amplitude set by your MIDI control. Make the axis constant.

axis([0,2*pi,-1,1]);
axis manual
hold on
sinePlotter = @(obj) plot(0:0.1:2*pi,midiread(obj).*sin(0:0.1:2*pi));

Use the midicallback function to associate your sinePlotter function with the control specified by your midicontrolsObject. Move your specified MIDI control. The plot updates automatically with the sinusoid amplitude specified by your MIDI control.

midicallback(midicontrolsObject,sinePlotter)

Create an object that responds to any control on the default MIDI device.

midicontrolsObject = midicontrols; 

Define an anonymous function to display the current value of the MIDI control. Use midicallback to associate your MIDI control object with the function you created. Verify that your object is associated with your function.

displayControlValue = @(object) disp(midiread(object));
midicallback(midicontrolsObject,displayControlValue);
currentFunctionHandle = midicallback(midicontrolsObject)
currentFunctionHandle = 

    @(object)disp(midiread(object))

Move any control on your default MIDI device to display its current normalized value on the command line.

    0.3095

    0.4603

    0.6746

    0.7381

    0.8175

    0.8571

    0.9048

Define an anonymous function to print the current value of the MIDI control rounded to two significant digits. Use midicallback to associate your MIDI controls object with the function you created. Return the old function handle.

displayRoundedControlValue = @(object) fprintf('%.2f\n',midiread(object));
oldFunctionHandle = midicallback(midicontrolsObject,displayRoundedControlValue)
oldFunctionHandle = 

    @(object)disp(midiread(object))

Move a control to display its current normalized value rounded to two significant digits.

0.91
0.83
0.67
0.49
0.29
0.18
0.05

Remove the association between the object and the function. Return the old function handle.

oldFunctionHandle = midicallback(midicontrolsObject,[])
oldFunctionHandle = 

    @(object)fprintf('%.2f\n',midiread(object))

Verify that no function is associated with your MIDI controls object.

currentFunctionHandle = midicallback(midicontrolsObject)
currentFunctionHandle =

     []

Define this function and save it to your current folder.

function plotSine(midicontrolsObject)

frequency = midiread(midicontrolsObject);

x = 0:0.01:10;

sinusoid = sin(2*pi*frequency.*x);

plot(x,sinusoid)
axis([0,10,-1.1,1.1]);
ylabel('Amplitude');
xlabel('Time (s)');
title('Sine Plot')
legend(sprintf('Frequency = %0.2f Hz',frequency));

end

Create a midicontrols object. Create a function handle for your plotSine function. Use midicallback to associate your midicontrolsObject with plotSineHandle.

Move any controller on your MIDI device to plot a sinusoid. The sinusoid frequency updates when you move MIDI controls.

midicontrolsObject = midicontrols;
plotSineHandle = @plotSine;
midicallback(midicontrolsObject,plotSineHandle);

Input Arguments

collapse all

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

New function handle, specified as a function handle that contains one input argument. The new function handle is called when midicontrolsObject changes value. For information on what function handles are, see Function Handles (MATLAB).

Output Arguments

collapse all

Old function handle set by the previous call to midicallback, returned as a function handle.

The function handle set by the most recent call to midicallback, returned as a function handle.

Was this topic helpful?