Notoriously Psychedelic Modules
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

jzz-midi-smf

1.0.6 • Public • Published

JZZ-midi-SMF

Standard MIDI Files: read / write / play

Node.js Firefox Chrome Opera Safari Internet Explorer
npm npm Build Status Coverage Status

Install

npm install jzz-midi-smf --save
or bower install jzz-midi-smf
or yarn add jzz-midi-smf
or get the full development version and minified scripts from GitHub

Usage

Plain HTML
<script src="JZZ.js"></script>
<script src="JZZ.midi.SMF.js"></script>
//...
CDN (always the latest version!)
<script src="https://cdn.jsdelivr.net/npm/jzz"></script>
<script src="https://cdn.jsdelivr.net/npm/jzz-midi-smf"></script>
//...
CommonJS (Browserify and Node.js command line applications)
var JZZ = require('jzz');
require('jzz-midi-smf')(JZZ);
//...
AMD
require(['JZZ', 'JZZ.midi.SMF'], function(JZZ, dummy) {
  // ...
});

MIDI files

Supported file formats: .mid, .kar, .rmi

Please check the API Reference !

Playing MIDI file
var midiout = JZZ().openMidiOut();
var data = require('fs').readFileSync('file.mid', 'binary');
var smf = new JZZ.MIDI.SMF(data);
var player = smf.player();
player.connect(midiout);
player.play();
Creating MIDI file from scratch
var smf = new JZZ.MIDI.SMF(0, 96); // type 0, 96 ticks per quarter note
var trk = new JZZ.MIDI.SMF.MTrk;
smf.push(trk);
// add contents:
trk.add(0, JZZ.MIDI.smfSeqName('This is a sequence name'))
   .add(0, JZZ.MIDI.smfBPM(90)) // tempo 90 bpm
   .add(96, JZZ.MIDI.noteOn(0, 'C6', 127))
   .add(96, JZZ.MIDI.noteOn(0, 'Eb6', 127))
   .add(96, JZZ.MIDI.noteOn(0, 'G6', 127))
   .add(192, JZZ.MIDI.noteOff(0, 'C6'))
   .add(192, JZZ.MIDI.noteOff(0, 'Eb6'))
   .add(192, JZZ.MIDI.noteOff(0, 'G6'))
   .add(288, JZZ.MIDI.smfEndOfTrack());
// or an alternative way:
trk.smfSeqName('This is a sequence name').smfBPM(90).tick(96)
   .noteOn(0, 'C6', 127).noteOn(0, 'Eb6', 127).noteOn(0, 'G6', 127)
   .tick(96).noteOff(0, 'C6').noteOff(0, 'Eb6').noteOff(0, 'G6')
   .tick(96).smfEndOfTrack();
// or even shorter:
trk.smfSeqName('This is a sequence name').smfBPM(90).tick(96)
   .ch(0).note('C6', 127, 96).note('Eb6', 127, 96).note('G6', 127, 96)
   .tick(192).smfEndOfTrack();
Transposing MIDI file
for (var i = 0; i < smf.length; i++) {
  if (smf[i] instanceof JZZ.MIDI.SMF.MTrk) {
    for (var j = 0; j < smf[i].length; j++) {
      var note = smf[i][j].getNote();
      if (typeof note != 'undefined') {
        if (smf[i][j].getChannel() != 9) { // skip the percussion channel
          smf[i][j].setNote(note + 12);    // transpose one octave up
        }
      }
    }
  }
}
Saving MIDI file
require('fs').writeFileSync('out.mid', smf.dump(), 'binary');

More information

Please visit https://jazz-soft.net for more information.
Your questions and comments are welcome here.

We would really appreciate your support!

install

npm i jzz-midi-smf

Downloadsweekly downloads

104

version

1.0.6

license

MIT

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability