Git Product home page Git Product logo

midifile's Introduction

midifile

Read/write standard MIDI files.

NPM version Build status Dependency Status devDependency Status Coverage Status Code Climate Dependency Status

MIDIFile uses the MIDIEvents project and is part of the MIDIPlayer one. You can also check this Karaoke Player built on top of those libraries.

What it does

  • Read MIDI files
  • Check MIDI file structure (using strictMode)
  • Write MIDI files (still experimental)

What it doesn't do

Usage

// Your variable with your MIDI file as an ArrayBuffer or UInt8Array instance
var anyBuffer;

// Creating the MIDIFile instance
var midiFile = new MIDIFile(anyBuffer);

// Reading headers
midiFile.header.getFormat(); // 0, 1 or 2
midiFile.header.getTracksCount(); // n
// Time division
if(midiFile.header.getTimeDivision() === MIDIFile.Header.TICKS_PER_BEAT) {
	midiFile.header.getTicksPerBeat();
} else {
	midiFile.header.getSMPTEFrames();
	midiFile.header.getTicksPerFrame();
}

// MIDI events retriever
var events = midiFile.getMidiEvents();
events[0].subtype; // type of [MIDI event](https://github.com/nfroidure/MIDIFile/blob/master/src/MIDIFile.js#L34)
events[0].playTime; // time in ms at wich the event must be played
events[0].param1; // first parameter
events[0].param2; // second one

// Lyrics retriever
var lyrics = midiFile.getLyrics();
if ( lyrics.length ) {
	lyrics[0].playTime; // Time at wich the text must be displayed
	lyrics[0].text; // The text content to be displayed
}

// Reading whole track events and filtering them yourself
var events = midiFile.getTrackEvents(0);

events.forEach(console.log.bind(console));

// Or for a single track
var trackEventsChunk = midiFile.tracks[0].getTrackContent();
var events = MIDIEvents.createParser(trackEventsChunk);

var event;
while(event = events.next()) {
	// Printing meta events containing text only
	if(event.type === MIDIEvents.EVENT_META && event.text) {
		console.log('Text meta: '+event.text);
	}
}

Testing

Unit tests are using mocha and NodeJS. Install them and run the following command:

mocha tests/*.mocha.js

Why ArrayBuffers ?

ArrayBuffer instances are the best way to manage binary data like MIDI files.

Why not streams ?

The Standard MIDI files format isn't streamable by nature. If you want to stream MIDI file contents, you should consider transforming your files in another format (plain linearized MIDI events should do the job).

Requirements

  • ArrayBuffer, DataView or their polyfills

Contributing

  • Feel free to PR
  • If you find a MIDI File the library can't read an if it's under a free, PR the file in the sounds folder and add tests for him. I'll work on it asap.

License

MIT

midifile's People

Contributors

argiepiano avatar dobon avatar drone1 avatar exastris avatar graciano avatar lemonzi avatar mauritslamers avatar mk-pmb avatar nfroidure avatar xbenjii avatar yukulele avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

midifile's Issues

midi file unsupported (apparently)

Issue

I'm a gentledev i:

  • fully read the README recently
  • searched for existing issues
  • checked i'm up to date with the latest version of the project

Expected behavior

The console logs the result of midiFile.header.getFormat();

Actual behavior

The console returns an error: Uncaught (in promise) Error: Invalid MIDIFileTrack (0xe) : MTrk prefix not found

Steps to reproduce the behavior

Clone or download the repository here and run npm start.

Debugging informations

  • node -v result: 10.14.1
  • npm -v result: 6.4.1
    If the result is lower than 6.9.5, there is
    poor chances i even have a look to it. Please,
    use the last NodeJS LTS version.

Incorrect path to webMIDIAPIShim in tests/index.html. Incorrect handling of midiOutput

I'm very new at this, so please bear with me and let me know if I'm doing anything incorrectly.

I noticed that, in the tests/index.html, the path to the SHIM is incorrect.

Also the assignment of the output port using midiAccess.outputs is incorrectly handled - the latest SHIM and the native Web MIDI API in Chrome handle .outputs as a "maplike Interface", not a function (in fact Chrome throws an error in ln 188 in the original file saying that outputs is not a function).

I've forked the project and made a few changes to this file. I'll do a pull request. Again, let me know if this is not what I'm supposed to do :)

What playTime mean?

While calculate to get the correct duration using this formula:
ticks / (bpm * resolution / 60) = duration in seconds

The values in "playTime" which appear in console log don't seem to be correct, what this show? Is there an option to detect the tempo and the total duration? Tried reading Jazz Plugin Javascript example wasn't fruitful either.

Uncaught (in promise) TypeError: createParser is not a function

Is this sort of missing API or how do I solve this error?

var midiFile = new MIDIFile(anyBuffer);
var trackEventsChunk = midiFile.getTrackEvents(0); //I did translate midi song to arraybuffer and print Object in the console
var events = new MIDIFile.createParser(trackEventsChunk);

Documentation for writing MIDI files

Thank you for putting MIDIFile together! Is there any documentation on how to write MIDI files? The Readme says the project can write files, but the documentation only includes methods for reading them.

Reading MIDI track labels as String

Thank you for creating NPM package midifile. I read the 'documentation' and had a quick browse through the code but did not find an answer there.

How do I read the names of separate tracks of the MIDI file as a String? By names I mean the optional label assigned to the track by the creator of the file. For example drums or acoustic guitar one or vocals.

Thanks in advance.

How to read a midi file to get the ArrayBuffer?

Issue

I'm a gentledev i:

  • fully read the README recently
  • searched for existing issues
  • checked i'm up to date with the latest version of the project

Expected behavior

Actual behavior

Steps to reproduce the behavior

Debugging informations

  • node -v result:
<paste here>
  • npm -v result:
<paste here>

If the result is lower than 6.9.5, there is
poor chances i even have a look to it. Please,
use the last NodeJS LTS version.

Feature request

Feature description

Use cases

  • I will/have implement the feature

Running status not allowed

Web MIDI API (http://www.w3.org/TR/webmidi) does not allow for MIDI Running Status. JavaScript is throwing an Uncaught Type Error for running status when using the index.html in the test folder of the MIDIFile project to play a MIDI file that contains running status.

Fix dist build.

I accidentally used the JS file in your dist directory. Later, I realized you had some bug fixes that never made it into the dist.

For example:

event.subtype === type

You fixed this bug a while back, but I spent an afternoon trying to figure out why my parsing wasn't working! Haha. I traced it to the subtype filtering, and thought I found a cool bug for you to fix. Alas, it was already fixed!

Anyways, tldr: you can update your dist build when you have free time. ;-)

Getting events for large midi's (black midis) takes FOREVER

I'm a gentledev i:

  • fully read the README recently
  • searched for existing issues
  • checked i'm up to date with the latest version of the project

Expected behavior

It doesn't take 5-15 minutes to load. A reasonable time, like many other apps, is 1 minute. VanBasco's took 2 seconds.

Actual behavior

It takes FOREVER to load.

Steps to reproduce the behavior

Load up a black midi with lots of notes (insane, like 5-10 million) and run midi.getMidiEvents()

Debugging informations

  • node -v result:
9.0.0
  • npm -v result:
5.5.1

If the result is lower than 6.9.5, there is
poor chances i even have a look to it. Please,
use the last NodeJS LTS version.

stream.end is not a function running example code

I get the error stream.end is not a function when trying to run the getTrackEvents() part of the sample code in the Readme. It looks like trackEventsChunk is an array, while MIDIEvents.createParser() expects a stream and tries to call .end() on it. Is the sample code out of date?

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.