/midifile-ts

TypeScript MIDI file parser forked from jasmid https://github.com/gasman/jasmid

Primary LanguageTypeScriptMIT LicenseMIT

npm version Actions Status License: MIT

midifile-ts

TypeScript MIDI file parser forked from jasmid https://github.com/gasman/jasmid

Installation

npm install midifile-ts --save

Usage

import { read } from "midifile-ts"

const midi = read(data) // { header, tracks }

Parse MIDI on node.js

import { read } from "midifile-ts"

const data = fs.readFileSync("song.mid")
const midi = read(data)

Parse MIDI on the browser

import { read, MidiFile } from "midifile-ts"

const changeFileInput = (
  input: HTMLInputElement,
  callback: (midi: MidiFile|null) => void
) => {
  if (input.files === null || input.files.length === 0) {
    return
  }

  const file = input.files[0]
  const reader = new FileReader()

  reader.onload = e => {
    if (e.target == null) {
      callback(null)
      return
    }
    const buf = e.target.result as ArrayBuffer
    const midi = read(buf)
    callback(midi)
  }

  reader.readAsArrayBuffer(file)
}