node package manager
Easy sharing. Manage teams and permissions with one click. Create a free org ยป

jazz-midi

jazz-midi

Low-level MIDI support.

nodejs windows mocos linux raspberry pi

Note: Although jazz-midi can be used stand-alone, it is best to use via jzz or web-midi-api.

This module shares the MIDI API with the browser Jazz-Plugin and allows using the same JavaScript on both browser and Node.js sides.

API reference is available here.

Questions and comments are welcome here.

Install

npm install jazz-midi

Dependencies

None

MIDI-Out example

var jazz = require('jazz-midi');
var midi = new jazz.MIDI();

var name = midi.MidiOutOpen(0);
if(name){
  console.log('Default MIDI-Out port:', name);
  midi.MidiOut(0x90,60,100); midi.MidiOut(0x90,64,100); midi.MidiOut(0x90,67,100);
  setTimeout(function(){
    midi.MidiOut(0x80,60,0); midi.MidiOut(0x80,64,0); midi.MidiOut(0x80,67,0);
    midi.MidiOutClose();
    console.log('Thank you for using Jazz-MIDI!');
  }, 3000);
} else {
  console.log('Cannot open default MIDI-Out port!');
}

MIDI-In callback example

var jazz = require('jazz-midi');
var midi = new jazz.MIDI();

var name = midi.MidiInOpen(0, function(t, msg){
  console.log(msg);
});
if(name){
  console.log('Default MIDI-In port:', name);
  setTimeout(function(){
    midi.MidiInClose();
    console.log('Thank you for using Jazz-MIDI!');
  }, 5000);
} else {
  console.log('Cannot open default MIDI-In port!');
}

MIDI-In polling example

var jazz = require('jazz-midi');
var midi = new jazz.MIDI();

var out_name;
var in_name;
var current;
var delta;

function start_recording(){
  out_name = midi.MidiOutOpen(0);
  if(out_name==''){ console.log('No default MIDI-Out port!'); return;}
  in_name = midi.MidiInOpen(0);
  if(in_name==''){ console.log('No default MIDI-In port!'); return;}
  console.log('Recording from', in_name, '...');
  setTimeout(start_playing, 5000);
}

function start_playing(){
  midi.MidiInClose();
  console.log('Playing to', out_name, '...');
  current = midi.QueryMidiIn();
  if(!current){
    cleanup();
    return;
  }
  delta = midi.Time() - current[0];
  next_msg();
}

function next_msg(){
  if(!current){
    setTimeout(cleanup, 1000);
    return;
  }
  var wait = current[0] + delta - midi.Time();
  if(wait<=0){
    midi.MidiOutLong(current.slice(1, current.length));
    current = midi.QueryMidiIn();
    next_msg();
  }
  else setTimeout(next_msg, wait);
}

function cleanup(){
  midi.MidiOutClose();
  console.log('Hope you have enjoyed!');
}

start_recording();

License

The MIT License (MIT)

Copyright (c) 2014-now Sema, http://jazz-soft.net

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.