Main Content

midiread

Return most recent value of MIDI controls

Description

controlValues = midiread(midicontrolsObject) returns the most recent value of the MIDI controls associated with the specified midicontrolsObject. To create this object, use the midicontrols function.

example

Examples

collapse all

midicontrolsObject = midicontrols;
controlValue = midiread(midicontrolsObject);

Identify two MIDI controls on your MIDI device.

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

controlOne =

        1081

Move the control you wish to identify; type ^C to abort.
Waiting for control message... done

controlTwo =

        1082

Create a MIDI controls object that listens to both controls you identified.

controlNumbers = [controlOne,controlTwo];
midicontrolsObject = midicontrols(controlNumbers);

Move your specified MIDI controls and return their values. The values are returned as a vector that corresponds to your control numbers vector, controlNumbers.

tic
while toc < 5
    controlValues = midiread(midicontrolsObject)
end
controlValues =

    0.0397    0.0556

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... done

Create a MIDI controls object. The value associated with your MIDI controls object cannot be determined until you move the MIDI control. Specify an initial value associated with your MIDI control. The midiread function returns the initial value until the MIDI control is moved.

initialControlValue = 1;
midicontrolsObject = midicontrols(controlNumber,initialControlValue);

Create a dsp.AudioFileReader System object™ with default settings. Create an audioDeviceWriter System object and specify the sample rate.

fileReader = dsp.AudioFileReader('RockDrums-44p1-stereo-11secs.mp3');
deviceWriter = audioDeviceWriter(...
    'SampleRate',fileReader.SampleRate);

In an audio stream loop, read an audio signal frame from the file, apply gain specified by the control on your MIDI device, and then write the frame to your audio output device. By default, the control value returned by midiread is normalized.

while ~isDone(fileReader)
    audioData = step(fileReader);
    
    controlValue = midiread(midicontrolsObject);
    
    gain = controlValue*2;
    audioDataWithGain = audioData*gain;
    
    play(deviceWriter,audioDataWithGain);
end

Close the input file and release your output device.

release(fileReader);
release(deviceWriter);

Input Arguments

collapse all

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

Output Arguments

collapse all

Most recent values of MIDI controls, returned as normalized values in the range [0,1], or as integer values in the range [0,127]. The output values depend on the OutputMode specified when your midicontrols object is created.

  • If OutputMode was specified as 'normalized', then midiread returns values in the range [0,1]. The default OutputMode is 'normalized'.

  • If OutputMode was specified as 'rawmidi', then midiread returns integer values in the range [0,127], and no quantization is required.

Version History

Introduced in R2016a