npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

midi-file

v1.2.4

Published

Parse and write MIDI files

Downloads

10,578

Readme

midi-file

Install with yarn add midi-file or npm install midi-file.

The parser is loosely based on midi-file-parser and jasmid, but totally rewritten to use arrays instead of strings for portability.

Typescript Usage

import * as fs from 'fs';
import * as midiManager from 'midi-file';

// Read MIDI file into a buffer
const input = fs.readFileSync('star_wars.mid');

// Convert buffer to midi object
const parsed = midiManager.parseMidi(input);

// Convert object to midi buffer
const output = midiManager.writeMidi(parsed);

// Write into file
const outputBuffer = Buffer.from(output);
fs.writeFileSync('copy_star_wars.mid', outputBuffer);

Raw Javascript Usage

var fs = require('fs')
var parseMidi = require('midi-file').parseMidi
var writeMidi = require('midi-file').writeMidi

// Read MIDI file into a buffer
var input = fs.readFileSync('star_wars.mid')

// Parse it into an intermediate representation
// This will take any array-like object.  It just needs to support .length, .slice, and the [] indexed element getter.
// Buffers do that, so do native JS arrays, typed arrays, etc.
var parsed = parseMidi(input)

// Turn the intermediate representation back into raw bytes
var output = writeMidi(parsed)

// Note that the output is simply an array of byte values.  writeFileSync wants a buffer, so this will convert accordingly.
// Using native Javascript arrays makes the code portable to the browser or non-node environments
var outputBuffer = Buffer.from(output)

// Write to a new MIDI file.  it should match the original
fs.writeFileSync('copy_star_wars.mid', outputBuffer)

The intermediate representation has a 'header' and 'tracks', and each track is an array of events.

Explicit Formatting

Options are provided to writeMidi to control various ambiguities in the MIDI file format.

The following will use byte 0x09 for noteOff messages with velocity zero. (Typically such messages use 0x08). It will also use running status bytes to compress consecutive events when possible.

var output = writeMidi(parsed, { useByte9ForNoteOff: true, running: true })

When parsing the file with readMidi, each compressed event using running status bytes will have a running flag set on it. Similarly, each noteOff event that was encoded using 0x09 will have a byte9 property set on it.

By default, writeMidi will defer to each event to indicate the behavior it should use for encoding such ambiguities, which will produce an exact copy of the original file read with parseMidi. However, these options to writeMidi allow the behavior to be overridden at the top-level, which may be relevant if you are generating the MIDI events, rather than just reading them from a file.