midi-node
v0.1.13
Published
A midi parser and writer.
Downloads
6
Readme
MIDI for Node.js
A very basic node library to parse and write MIDI messages.
Usage
The library supports three major types of MIDI structures:
- A sequence, representing a full MIDI file with multiple tracks.
- A track, a ordered collection of messages.
- A message, the basic component for the MIDI protocol.
These can all be parsed from a Buffer.
Parsing a Message
var midi = require('midi-node');
var noteOff = new Buffer('803c00', 'hex'); // Channel 0, middle C4, 0 velocity
var message = midi.Message.fromBuffer(noteOff);
message.getStatus(); // 0x80
message.getCommand(); // "NOTE_OFF"
message.getChannel(); // 0
message.getData(); // [0x3c, 0x00]
Parsing messages from a stream
Anything that emits a 'data' event can be used as a stream.
var midi = require('midi-node');
var input = <something readable>;
var stream = new midi.Stream(input);
stream.on('startTrack', function (track) {
// do something with the track
});
stream.on('event', function (delta, message) {
message.getStatus(); // 0x80
message.getCommand(); // "NOTE_OFF"
message.getChannel(); // 0
message.getData(); // [0x3c, 0x00]
});
The following events are emitted:
startFile
, parameter is the file headerstartTrack
, parameter is the track objectevent
, parameters are delta and messageendTrack
, parameter is the track objecterror
, parameter is the error object
Writing a Message
var midi = require('midi-node');
var stream = <something writable>;
var writer = new midi.Writer(stream);
writer.startFile(0, 1, 128);
writer.startTrack();
writer.noteOn(0, 0, 0x3c, 100); // Channel 0, middle C4, 100 velocity