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

647 views Asked by At

I am familiar with C++ and MIDI protocol but I am a beginner with javascript. I have successfully run the example Basic.html https://github.com/mudcube/MIDI.js/blob/master/examples/Basic.html from git hub below:

<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>

I would like to load the plugin when the window loads. Then I would like to have my own javascript function send a specified note value to MIDI.noteOn() and MIDI.noteOff(). I am embarassed to ask such a simple question, but I am getting nowhere with my attempts. Thanks for any suggestions.

1

There are 1 answers

1
Brad On BEST ANSWER

In JavaScript, you can name functions and then reference them by name.

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

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