Perry Rylance

Personal & Professional blog

New MIDI Library

I’ve long been a fan of programatic composition and creating and manipulating MIDI in general. Readers may be familiar with my library MIDI.js. This library was great for quick and dirty parsing and writing back, however to work with it, it required a lot of familiarity with the MIDI specification, especially to produce valid data.

I have recently launched my new TypeScript MIDI library – @perry-rylance/midi.

The new library is much more developer friendly. A combination of TypeScript types and MIDI parameter getters / setters work together to ensure that it is impossible to create a structure in memory that would result in an invalid MIDI file. This will not only make the development experience much faster and more pleasant for developers familiar with MIDI in general, it will act as solid guide rails for newcomers.

In addition, when you wish to serialize your data, a set of validators perform additional checks such as checking for the “end of track” event and making sure that events such as “copyright” appear in the first track with a non-zero absolute time.

All this together keeps you well within the MIDI specification where the older library would fail to do so.

The library is designed to be as extensible as possible (with more factories and methods coming), and features a full Jest test suite to ensure that the library is robust at parsing files, and that it fails in the expected way when attempting to construct invalid files. My thanks go out to jazz-soft for the library of every conceivable combination of valid and invalid MIDI files.

An example of working with @perry-rylance/midi.

The motivation for this new package is my plans to start composing microtonal music, which isn’t very well supported by the MIDI specification at the moment. The plan is to extend the event system to allow for floating point key pitches in note events, automatically injecting pitch wheel events along the way.

This will, of course, have limitations when it comes to polyphonic instruments, but we’ll cross that bridge when we come to it.

Install the package now and get started:

npm install @perry-rylance/midi