# @tonejs/midi

> Convert binary midi into JSON

Latest version **2.0.28** (published 2022-02-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tonejs/midi
pnpm add @tonejs/midi
yarn add @tonejs/midi
bun add @tonejs/midi
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.28 |
| Published | 2022-02-04 |
| First published | 2019-02-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 280.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1005 |
| Author | Yotam Mann |
| Maintainers | yotam-two, yotam |

## Links

- npm: https://www.npmjs.com/package/@tonejs/midi
- Repository: https://github.com/Tonejs/Midi
- Homepage: https://tonejs.github.com/Midi/
- Issues: https://github.com/Tonejs/Midi/issues
- npm.io page: https://npm.io/package/@tonejs/midi

## Dependencies (2)

- [midi-file](https://npm.io/package/midi-file.md) ^1.2.2
- [array-flatten](https://npm.io/package/array-flatten.md) ^3.0.0

## Recent versions

- 2.0.28 (latest) — 2022-02-04
- 2.0.27 — 2021-03-09
- 2.0.26 — 2021-02-15
- 2.0.25 — 2020-08-05
- 2.0.24 — 2020-08-02
- 2.0.23 — 2020-04-30
- 2.0.22 — 2020-04-15
- 2.0.21 — 2020-04-09
- 2.0.20 — 2020-03-24
- 2.0.19 — 2020-03-24
- 2.0.18 — 2020-03-24
- 2.0.17 — 2020-03-16
- 2.0.16 — 2020-03-15
- 2.0.15 — 2019-11-12
- 2.0.14 — 2019-11-12
- … 26 more at https://npm.io/package/@tonejs/midi/versions

## README

[![Build Status](https://travis-ci.org/Tonejs/Midi.svg?branch=master)](https://travis-ci.org/Tonejs/Midi)
[![codecov](https://codecov.io/gh/Tonejs/Midi/branch/master/graph/badge.svg)](https://codecov.io/gh/Tonejs/Midi)

## Installation

`npm install @tonejs/midi`

## [DEMO](https://tonejs.github.io/Midi/)

Midi makes it straightforward to read and write MIDI files with Javascript. It uses [midi-file](https://github.com/carter-thaxton/midi-file) for parsing and writing. 

## Import

Node.js:

```javascript
const { Midi } = require('@tonejs/midi')
```

Typescript / ES6

```javascript
import { Midi } from '@tonejs/midi'
```

Browser

```html
<script src="https://unpkg.com/@tonejs/midi"></script>
```
```javascript
const midi = new Midi()
```

## Basic Example


```javascript
// load a midi file in the browser
const midi = await Midi.fromUrl("path/to/midi.mid")
//the file name decoded from the first track
const name = midi.name
//get the tracks
midi.tracks.forEach(track => {
  //tracks have notes and controlChanges

  //notes are an array
  const notes = track.notes
  notes.forEach(note => {
    //note.midi, note.time, note.duration, note.name
  })

  //the control changes are an object
  //the keys are the CC number
  track.controlChanges[64]
  //they are also aliased to the CC number's common name (if it has one)
  track.controlChanges.sustain.forEach(cc => {
    // cc.ticks, cc.value, cc.time
  })

  //the track also has a channel and instrument
  //track.instrument.name
})
```

### Format

The data parsed from the midi file looks like this:

```javascript
{
  // the transport and timing data
  header: {
    name: String,                     // the name of the first empty track, 
                                      // which is usually the song name
    tempos: TempoEvent[],             // the tempo, e.g. 120
    timeSignatures: TimeSignatureEvent[],  // the time signature, e.g. [4, 4],

    PPQ: Number                       // the Pulses Per Quarter of the midi file
                                      // this is read only
  },

  duration: Number,                   // the time until the last note finishes

  // an array of midi tracks
  tracks: [
    {
      name: String,                   // the track name if one was given

      channel: Number,                // channel
                                      // the ID for this channel; 9 and 10 are
                                      // reserved for percussion
      notes: [
        {
          midi: Number,               // midi number, e.g. 60
          time: Number,               // time in seconds
          ticks: Number,              // time in ticks
          name: String,               // note name, e.g. "C4",
          pitch: String,              // the pitch class, e.g. "C",
          octave : Number,            // the octave, e.g. 4
          velocity: Number,           // normalized 0-1 velocity
          duration: Number,           // duration in seconds between noteOn and noteOff
        }
      ],

      // midi control changes
      controlChanges: {
        // if there are control changes in the midi file
        '91': [
          {
            number: Number,           // the cc number
            ticks: Number,            // time in ticks
            time: Number,             // time in seconds
            value: Number,            // normalized 0-1
          }
        ],
      },

      instrument: {                   // and object representing the program change events
        number : Number,              // the instrument number 0-127
        family: String,               // the family of instruments, read only.
        name : String,                // the name of the instrument
        percussion: Boolean,          // if the instrument is a percussion instrument
      },          
    }
  ]
}
```

### Raw Midi Parsing

If you are using Node.js or have the raw binary string from the midi file, just use the `parse` method:

```javascript
const midiData = fs.readFileSync("test.mid")
const midi = new Midi(midiData)
```

### Encoding Midi

You can also create midi files from scratch or by modifying an existing file.

```javascript
// create a new midi file
var midi = new Midi()
// add a track
const track = midi.addTrack()
track.addNote({
  midi : 60,
  time : 0,
  duration: 0.2
})
.addNote({
  name : 'C5',
  time : 0.3,
  duration: 0.1
})
.addCC({
  number : 64,
  value : 127,
  time : 0.2
})
 
// write the output
fs.writeFileSync("output.mid", new Buffer(midi.toArray()))
```

### Acknowledgment

Thank you [midi-file](https://github.com/carter-thaxton/midi-file)!

---
_Source: https://npm.io/package/@tonejs/midi · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
