MATLAB Answers

How can I SEND midi commands from within Simulink?

4 ビュー (過去 30 日間)
Joel Gouker
Joel Gouker 2020 年 1 月 3 日
コメント済み: Charlie DeVane 2020 年 8 月 24 日
Hi, how can I send MIDI commands from simulink simulation to a MIDI device?
I have no problem doing this in matlab to the target device but I can't seem to figure out how to properly call the commands for midisend, midimsg, etc. from simulink at all.
I basically want functionality that is reverse of the simulink block called "Midi Control" (this existing block reads in signals from external midi devices into Simulink. I want to SEND midi messages based on what happens in a simulink simulation).
Thanks,
Joel

  0 件のコメント

サインインしてコメントする。

採用された回答

Charlie DeVane
Charlie DeVane 2020 年 1 月 7 日
Hi Joel,
You can use either the Interpreted MATLAB Function block or the MATLAB Function block to call the MATLAB MIDI functions. I have attached a simple example model using both blocks to send a counter value to move sliders on my BCF2000. The Interpreted MATLAB Function requires a MATLAB file separate from the model, whereas the MATLAB Function block has the advantage of incorporating this code directly into the model.
The Interpreted MATLAB Function block calls the following function (place it in the same folder with the model).
function sendMidiMsg(cval)
persistent md
if isempty(md)
md = mididevice('BCF2000');
end
channel = 1;
control = 82;
midisend(md, 'ControlChange', channel, control, cval);
end
The code inside the MATLAB Function block is similar, but requires coder.extrinsic (because the MIDI functions don't support codegen) and uses a different control number (to control a different slider on the BCF2000).
function sendMidiMsg(cval)
coder.extrinsic('mididevice', 'midisend');
persistent md
if isempty(md)
md = mididevice('BCF2000');
end
channel = 1;
control = 81;
midisend(md, 'ControlChange', channel, control, cval);
end
hth,
Charlie

  3 件のコメント

Joel Gouker
Joel Gouker 2020 年 1 月 7 日
Perfect. Thanks Charlie!
Multi Vac
Multi Vac 2020 年 8 月 9 日
All MIDI notes sound like piano keys. How to invoke other instruments like flute or drums?
Charlie DeVane
Charlie DeVane 2020 年 8 月 24 日
The MIDI instrument receiving your messages is using a piano patch. Send "ProgramChange" messages with different patch numbers to get different sounds.
The sound you get from a given patch number depends on the specific MIDI instrument. If it conforms to the "General MIDI v1 spec", you'll get the sounds described at https://www.midi.org/specifications-old/item/gm-level-1-sound-set.
This question is really about MIDI, and not about MATLAB or Simulink. I recommend the tutorials and other information linked at https://www.midi.org/specifications for authoritative answers.

サインインしてコメントする。

その他の回答 (1 件)

Mark McBroom
Mark McBroom 2020 年 1 月 4 日
The audio toolbox comes with Simulink blocks for midi send/receive.
Otherwise you will have to create custom block via:
  1. custom s-function
  2. MATLAB Function Block with coder.extrinsic() for midi functions
  3. MATLAB Function block with coder.ceval() to external midi APIs

  3 件のコメント

Joel Gouker
Joel Gouker 2020 年 1 月 5 日
Thanks for the response. I have the audio toolbox. However, in the examples you sent, I only see how to use the simulink blocks for midi receive (from external midi controller... to simulink environment). I'm looking for midi send (from simulink... to external midi device). Can you clarify where in the example it shows how to send commands in midi format from simulink to an external midi device?
Mark McBroom
Mark McBroom 2020 年 1 月 6 日
Hi Joel,
The MIDI Controls block can be used to send initial values to the MIDI device by setting "MIDI Controls" to "Respond to specified controls" and then checking the "Send initial values to device at start" box. Are there scenarios other than start-up where you need to send MIDI commands?
Joel Gouker
Joel Gouker 2020 年 1 月 6 日
Hi Mark. Yes. I need to send MIDI commands often. The MIDI values/control commands are being generated by the Simulink Model during the simulation - these MIDI commands need to be sent approximately every 10-60 seconds to the external MIDI device.

サインインしてコメントする。

製品


リリース

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by