【问题标题】:How to send data from a javascript function to MIDI.noteOn() in the MIDI.js API如何将数据从 javascript 函数发送到 MIDI.js API 中的 MIDI.noteOn()
【发布时间】:2015-08-10 19:33:44
【问题描述】:

我熟悉 C++ 和 MIDI 协议,但我是 javascript 的初学者。 我已经从下面的 git hub 成功运行了示例 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>

我想在窗口加载时加载插件。然后我想 让我自己的 javascript 函数将指定的音符值发送到 MIDI.noteOn() 和 MIDI.noteOff()。我不好意思问这么简单的问题,但我的尝试无济于事。感谢您的任何建议。

【问题讨论】:

    标签: javascript midi midi.js


    【解决方案1】:

    在 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
    });
    

    【讨论】:

    • 谢谢。这让我朝着写作方向前进。我必须将函数调用放在大括号中。这是为任何感兴趣的人播放半音阶的代码:
    猜你喜欢
    • 2013-09-26
    • 2021-08-23
    • 2018-04-04
    • 2019-12-04
    • 2020-10-20
    • 2013-08-10
    • 2017-06-10
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多