Giter VIP home page Giter VIP logo

midi-file's Introduction

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.

midi-file's People

Contributors

asmanp2012 avatar carter-thaxton avatar harlos0517 avatar pasqlisena avatar tetsuo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.