使用基于javascript浏览器的midi.js创建鼓声?

Don*_*man 5 javascript midi

我一直在尝试使用midi.js http://mudcu.be/midi-js/

我一直在寻找一个地方发布关于使用它的问题,但没有找到,所以我将在这里尝试..

图书馆的第一个工作很棒.

我试图让一个鼓声只能触发,但它无法正常工作.我可以从"acoustic_grand_piano"中获取其他音符,而不是仅仅从"synth_drum"触发.

我认为midi音符35应该与"Acoustic Bass Drum"有关.

使用demo-Basic.html中的示例

window.onload = function () {
    MIDI.loadPlugin({
        soundfontUrl: "./soundfont/",
        instrument: "synth_drum",
        callback: function() {
            var delay = 0; // play one note every quarter second
            var note = 35; // 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);
        }
    });
};
Run Code Online (Sandbox Code Playgroud)

gle*_*itz 3

在播放“synth_drum”声音之前,您必须将该乐器加载到通道中。这是通过programChange函数完成的。正确的方法如下。

MIDI.loadPlugin({
    soundfontUrl: "/apps/spaceharp/static/soundfont/",
    instrument: "synth_drum",
    callback: function() {
        var delay = 0; // play one note every quarter second
        var note = 35; // the MIDI note
        var velocity = 127; // how hard the note hits
        // play the note
        MIDI.programChange(0, 118); // Load "synth_drum" (118) into channel 0
        MIDI.setVolume(0, 127);
        MIDI.noteOn(0, note, velocity, delay); // Play note on channel 0
        MIDI.noteOff(0, note, delay + 0.75); // Stop note on channel 0
    }
});
Run Code Online (Sandbox Code Playgroud)

MIDI 标准化规范(或通用 MIDI)为每个乐器分配特定的名称和编号。在 MIDI 规范中查找“Synth Drum”给出的乐器编号为 118,因此需要将 118 加载到通道 0 中。

您可以在 MIDI.js 源中找到乐器映射列表。还有一些方便的函数MIDI.GeneralMIDI可以按名称、按 ID 和按类别获取仪器信息。