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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@rgglez/midi2tonejs

v0.2.2

Published

Convert MIDI files to the JSON format used by Tone.js (@tonejs/midi)

Readme

midi2tonejs

License GitHub all releases GitHub issues GitHub commit activity GitHub release npm Crates.io GitHub stars GitHub forks

midi2tonejs converts Standard MIDI Files (.mid) into the JSON format that @tonejs/midi produces with Midi.toJSON(). The JSON can go straight into new Midi().fromJSON(json) or be read directly by code built on Tone.js.

The tool works as a native command-line program and as a WebAssembly module for the browser.

Output format

{
  "header": {
    "keySignatures": [{ "key": "Eb", "scale": "minor", "ticks": 0 }],
    "meta": [{ "text": "Verse", "ticks": 0, "type": "marker" }],
    "name": "Song title",
    "ppq": 480,
    "tempos": [{ "bpm": 120, "ticks": 0 }],
    "timeSignatures": [{ "ticks": 0, "timeSignature": [4, 4], "measures": 0 }]
  },
  "tracks": [
    {
      "channel": 0,
      "controlChanges": { "7": [{ "number": 7, "ticks": 0, "time": 0, "value": 0.78 }] },
      "pitchBends": [{ "ticks": 200, "time": 0.2, "value": -0.5 }],
      "instrument": { "family": "piano", "number": 0, "name": "acoustic grand piano" },
      "name": "Piano",
      "notes": [
        { "duration": 0.5, "durationTicks": 480, "midi": 60, "name": "C4",
          "ticks": 0, "time": 0, "velocity": 0.787 }
      ],
      "endOfTrackTicks": 1920
    }
  ]
}

The conversion copies the behaviour of @tonejs/midi in detail:

  • Each source track is split into one track per (program, channel) pair.
  • A format‑1 conductor track with no notes is dropped.
  • Note‑ons are matched to note‑offs in the same order Tone.js uses.
  • Velocities and controller values are scaled to 0..1, and pitch bend to -1..1.
  • Seconds are computed with the same floating‑point operations as Tone.js.

Its output was compared with @tonejs/midi and parses to the same object. The only textual difference is that whole numbers are written as 120.0 instead of 120.

Files that use SMPTE timecode instead of PPQ timing are not supported.

Requirements

  • Rust 1.74 or newer (rustup, cargo)
  • For the web build: the wasm32-unknown-unknown target and wasm-pack

Command line

cargo build --release
./target/release/midi2tonejs --input song.mid --output song.json

| Flag | Description | |------|-------------| | -i, --input <FILE> | Source MIDI file | | -o, --output <FILE> | Destination JSON file | | -p, --pretty | Pretty‑print the JSON | | -h, --help | Show help | | -V, --version | Show version |

To install it in ~/.cargo/bin, run cargo install --path ..

WebAssembly

The crate has two Cargo features:

  • cli (enabled by default) adds clap and the binary.
  • wasm adds a wasm-bindgen export.

Build the web version without the CLI:

rustup target add wasm32-unknown-unknown
cargo install wasm-pack
wasm-pack build --target web --release -- --no-default-features --features wasm

This writes an ES module to pkg/. It exports one function:

midiToJson(bytes: Uint8Array, pretty: boolean): string   // throws on invalid MIDI

Example

<input type="file" id="file" accept=".mid,.midi">
<script type="module">
  import init, { midiToJson } from "./pkg/midi2tonejs.js";

  await init();

  document.getElementById("file").addEventListener("change", async (e) => {
    const bytes = new Uint8Array(await e.target.files[0].arrayBuffer());
    const midi = JSON.parse(midiToJson(bytes, false));
    console.log(midi.header.tempos, midi.tracks.length);
  });
</script>

To use the result with Tone.js:

import { Midi } from "@tonejs/midi";
import * as Tone from "tone";

const midi = new Midi();
midi.fromJSON(JSON.parse(midiToJson(bytes, false)));

const synth = new Tone.PolySynth().toDestination();
midi.tracks.forEach((track) =>
  track.notes.forEach((n) =>
    synth.triggerAttackRelease(n.name, n.duration, Tone.now() + n.time, n.velocity)));

Use --target bundler for webpack or Vite, or --target nodejs for Node.

As a Rust library

[dependencies]
midi2tonejs = { git = "https://github.com/rgglez/music-midi2tonejs", default-features = false }
let bytes = std::fs::read("song.mid")?;
let json: String = midi2tonejs::convert(&bytes, true)?;      // JSON text
let midi: midi2tonejs::Midi = midi2tonejs::parse(&bytes)?;  // typed structs

Development

A Makefile wraps the common tasks:

| Rule | Does | |------|------| | make / make all | cli + wasm | | make cli | Release binary in target/release/ | | make wasm | WebAssembly package in pkg/ (WASM_TARGET=bundler\|nodejs to change) | | make wasm-deps | Install the wasm32 target and wasm-pack | | make test | Run tests | | make lint | rustfmt check + clippy | | make install | Install the CLI into ~/.cargo/bin | | make clean | Remove target/ and pkg/ |

Releasing

The version is set only in Cargo.toml. wasm-pack copies it into pkg/package.json, so the npm package and the crate always have the same SemVer version.

npm login && cargo login           # once
make bump VERSION=0.2.0            # updates Cargo.toml + Cargo.lock, commits and tags v0.2.0
make publish-dry                   # optional: build and check both packages without uploading
make publish                       # publishes to npm and crates.io
git push --follow-tags

make publish runs preflight first. That step stops the release if any of these is true:

  • Tests or lint fail.
  • The working tree has uncommitted changes.
  • The vX.Y.Z tag is missing.
  • The version already exists on npm or crates.io.

Prerelease versions such as 0.2.0-beta.1 get the npm dist-tag next, so npm install does not pick them up by default. The npm package is published as @rgglez/midi2tonejs, and the crate as midi2tonejs.

Dependencies

License

Copyright © 2026 Rodolfo González González.

Released under the MIT License, the same license as @tonejs/midi.