Jazz-Soft.net
We make it sound!
Home » Documentation » JZZ.js » Web MIDI API

Web MIDI API

JZZ.js enables Web MIDI API in Node.js and those browsers that don't support it natively.

Just include it in your project and start using navigator.requestMIDIAccess().

navigator.requestMIDIAccess()

navigator.requestMIDIAccess() - get the MIDIAccess object.

It's either the native MIDIAccess object if the browser supports Web MIDI API, or a JZZ implementation otherwise.

JZZ.requestMIDIAccess()

JZZ.requestMIDIAccess() - get the JZZ implementation of MIDIAccess object.

navigator.requestMIDIAccess() vs. JZZ.requestMIDIAccess()

In Node.js and browsers without native Web MIDI API support, both navigator.requestMIDIAccess() and JZZ.requestMIDIAccess() return the same object.

In browsers with native Web MIDI API support, the former returns the native MIDIAccess object, while the later returns the JZZ implementation.

JZZ version may offer more (virtual) MIDI ports, such as JZZ.input.Kbd or JZZ.synth.OSC.

Example

// HTML
<script src="JZZ.js"></script>
<script><!--
navigator.requestMIDIAccess().then(function(webmidi) {
    webmidi.outputs.forEach(function(port) { console.log('MIDI Out:', port.name); });
    webmidi.inputs.forEach(function(port) { console.log('MIDI In:', port.name); });
  }, function(err) {
    console.log('Cannot start WebMIDI!');
    if (err) console.log(err);
  }
);
--></script>
// Node.js
var navigator = require('jzz');
navigator.requestMIDIAccess().then(function(webmidi) {
    webmidi.outputs.forEach(function(port) { console.log('MIDI Out:', port.name); });
    webmidi.inputs.forEach(function(port) { console.log('MIDI In:', port.name); });
    navigator.close(); // Required in Node.js.
    // This will close MIDI inputs, otherwise Node.js will wait for MIDI input forever.
  }, function(err) {
    console.log('Cannot start WebMIDI!');
    if (err) console.log(err);
  }
);

See also