6

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

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

第一关图书馆工作得很好。

我试图让一个鼓声触发,但它不起作用。我可以从“acoustic_grand_piano”触发其他音符,但不仅仅是“synth_drum”。

我认为 midi note 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);
        }
    });
};
4

1 回答 1

3

Before playing the "synth_drum" sounds you must load that instrument into a channel. This is done with the programChange function. The correct method is the following.

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

MIDI standardized specification (or General MIDI) assigns a specific name and number to each instrument. Looking up "Synth Drum" in the MIDI specification gives an instrument number of 118 and thus the need to load 118 into channel 0.

You can find a list of instrument mappings in the MIDI.js source. There are also handy functions in MIDI.GeneralMIDI that will fetch instrument information byName, byId, and byCategory.

于 2013-10-03T04:09:06.920 回答