Skip to main content
This release is 2 versions behind 1.1.1 — the latest version of @midi-json-tools/json-to-midi. Jump to latest

Built and signed on GitHub Actions

Library to convert a JSON representation of a MIDI file to an encoded binary MIDI file.

This package works with Node.js, Deno, BrowsersIt is unknown whether this package works with Cloudflare Workers, Bun
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
It is unknown whether this package works with Bun
This package works with Browsers
JSR Score
100%
Published
4 months ago (1.0.9)

@midi-json-tools/json-to-midi

npm version @midi-json-tools/json-to-midi

Library to convert a JSON representation of a MIDI file to an encoded binary MIDI file.

Credit

This package and the majority of its code originates from json-midi-encoder. I've updated and removed necessary bits to make it work in a node environment.

Usage

NPM

npm i @midi-json-tools/json-to-midi

Yarn

yarn add @midi-json-tools/json-to-midi

PNPM

pnpm add @midi-json-tools/json-to-midi

JSR / Deno

import { jsonToMidi } from 'jsr:@midi-json-tools/json-to-midi'

ESM.SH

import { jsonToMidi } from "https://esm.sh/@midi-json-tools/json-to-midi@latest";

Example


import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import * as url from 'node:url';
import { jsonToMidi } from '@midi-json-tools/json-to-midi';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

const filename = path.join(__dirname, './ExampleMIDIJSON.json');
const response = await fs.readFile(filename);
const json = JSON.parse(response.toString());

const midiFile = jsonToMidi(json);

The type expected of the function is also exposed

import type { MidiFile } from '@midi-json-tools/midi-to-json';

const example: MidiFile = {
  division: 96,
  format: 2,
  tracks: [
    [
      {
        delta: 0,
        text: 'C note',
      },
      {
        delta: 0,
        endOfTrack: true,
      },
    ],
    [
      {
        delta: 0,
        text: 'c',
      },
      {
        channel: 1,
        delta: 0,
        programChange: {
          programNumber: 5,
        },
      },
      {
        channel: 1,
        delta: 0,
        noteOn: {
          noteNumber: 48,
          velocity: 121,
        },
      },
      {
        channel: 1,
        delta: 95,
        noteOn: {
          noteNumber: 48,
          velocity: 0,
        },
      },
      {
        delta: 0,
        endOfTrack: true,
      },
    ],

  ],
};

const midiFile = jsonToMidi(example);

Works with Deno as well

import { jsonToMidi } from "jsr:@midi-json-tools/json-to-midi";

const inputJSONFile = `./Example.json`;
const jsonFile = await Deno.readFile(inputFile);
const midi = jsonToMidi(jsonFile);
Built and signed on
GitHub Actions
View transparency log

Add Package

deno add jsr:@midi-json-tools/json-to-midi

Import symbol

import * as mod from "@midi-json-tools/json-to-midi";

---- OR ----

Import directly with a jsr specifier

import * as mod from "jsr:@midi-json-tools/json-to-midi";

Add Package

npx jsr add @midi-json-tools/json-to-midi

Import symbol

import * as mod from "@midi-json-tools/json-to-midi";

Add Package

yarn dlx jsr add @midi-json-tools/json-to-midi

Import symbol

import * as mod from "@midi-json-tools/json-to-midi";

Add Package

pnpm dlx jsr add @midi-json-tools/json-to-midi

Import symbol

import * as mod from "@midi-json-tools/json-to-midi";

Add Package

bunx jsr add @midi-json-tools/json-to-midi

Import symbol

import * as mod from "@midi-json-tools/json-to-midi";