如何将数据从 javascript 函数发送到 MIDI.js API 中的 MIDI.noteOn()

How to send data from a javascript function to MIDI.noteOn() in the MIDI.js API

我熟悉 C++ 和 MIDI 协议,但我是 javascript 的初学者。 我已成功 运行 来自 git 中心的示例 Basic.html https://github.com/mudcube/MIDI.js/blob/master/examples/Basic.html

<body>
<script type="text/javascript">
window.onload = function () {
MIDI.loadPlugin({
    soundfontUrl: "./soundfont/",
    instrument: "acoustic_grand_piano",
    onprogress: function(state, progress) {
        console.log(state, progress);
    },
    onsuccess: function() {
        var delay = 0; // play one note every quarter second
        var note = 50; // the MIDI note
        var velocity = 127; // how hard the note hits
        // play the note
        MIDI.setVolume(0, 127);
        MIDI.noteOn(0, note, velocity, delay);
        MIDI.noteOff(0, note, delay + 0.75);
    }
});
};
</script>
</body>

我想在 window 加载时加载插件。那么我想 让我自己的 javascript 函数将指定的音符值发送到 MIDI.noteOn() 和 MIDI.noteOff()。我很尴尬问这样一个简单的问题,但我的尝试毫无进展。感谢您的任何建议。

在JavaScript中,您可以命名函数,然后通过名称引用它们。

function playNote() {
  MIDI.noteOn(0, 50, 127, 0);
  MIDI.noteOff(0, 50, 0.75);
}

MIDI.loadPlugin({
  soundfontUrl: './soundfont/',
  instrument: 'acoustic_grand_piano',
  onsuccess: playNote
});