Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use javascript browser based midi.js to create a drum sound?

I have been trying to use midi.js http://mudcu.be/midi-js/

I have tried looking for a place to post questions about using it but having found none so I am going to try here..

1st off the library works great.

I'm trying to get a just a drum sound to trigger but it is not working. I can get other notes to trigger from "acoustic_grand_piano" but not from just "synth_drum".

I think midi note 35 should relate to the "Acoustic Bass Drum".

Using the sample from the 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);
        }
    });
};
like image 786
Donavon Lerman Avatar asked Jul 20 '13 22:07

Donavon Lerman


1 Answers

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.

like image 123
gleitz Avatar answered Oct 23 '22 16:10

gleitz