Coder Social home page Coder Social logo

eat-the-red-pill / midi-parser-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from colxi/midi-parser-js

0.0 0.0 0.0 604 KB

JSON Human readable MIDI sequences. Read from ArrayBuffers, Base64 encoded strings, or FileInput Element in Browsers.

License: GNU General Public License v3.0

JavaScript 93.48% HTML 6.52%

midi-parser-js's Introduction

logo

MidiParser.js

NoDependencies Browser Node

MidiParser is a Javascript Binary MIDI file reader for the browser and Node which converts a MIDI binary data structure to a JSON object, making it much easier to iterate over and interact with.

  • Tiny and dependency free
  • Browser & Node Compatible
  • Supported data input :
    • BASE64 encoded Midi data
    • UINT8 arrayBuffer, obtained when reading or fetching a .mid binary.
    • FileInput Element in the Browser
  • Custom Midi Messages

Example

A simple example parsing a MIDI file in Node ...

let midiParser  = require('midi-parser-js');
let fs = require('fs')

// read a .mid binary (as base64)
fs.readFile('./test.mid', 'base64', function (err,data) {
  // Parse the obtainer base64 string ...
  var midiArray = midiParser.parse(data);
  // done!
  console.log(midiArray);
});

Example in Browser...

<script type="module">
  import MidiParser from 'midi-parser-js'
  // select the INPUT element that will handle
  // the file selection.
  let source = document.getElementById('filereader');
  // provide the File source and a callback function
  MidiParser.parse( source, function(obj){
    console.log(obj);
  });
</script>
<input type="file" id="filereader"/>

If you want to see it in action, you can test it Here

Syntax:

  MidiParser.parse( input [, callback] );

- input : Accepts any of the supported Data Sources : FileInputElement | uint8Array | base64String

- callback : Callback to be executed when data is parsed. Only required when input is a FileInputElement.


Handle Custom messages ( sysEx, non-standard...)

By default, the library ignores the sysEx, and non-standard messages, simply converting their values to integers (when possible). However you can provide a custom hook function to be executed when any non-standard message is found, and process it by your own, returning the resulting value.

MidiParser.customInterpreter = function( msgType, arrayBuffer, metaEventLength){  /* your code */ }

- msgType : Hex value of the message type

- arrayBuffer : Dataview of the midi data. You have to extract your value/s from it, moving the pointer as needed.

- metaEventLength : A length greater than 0 indicates a received message

If you want the default action to be executed, return false

Output JSON anatomy :

The returned JSON object contains all the attributes of the MIDI file (format type, time division, track count... ) as properties. The tracks and the MIDI events related to each track are container inside the track property.

logo

The following JSON object represents a MIDI file with 3 tracks and 4 events in Track 0

outputObject{
....formatType: 0|1|2,  // Midi format type
....timeDivision: (int),  // song tempo (bpm)
....tracks: (int),  // total tracks count
....track: Array[
........[0]: Object{  // TRACK 1!
............event: Array[  // Midi events in track 1
................[0] : Object{  // EVENT 1
....................data: (string),
....................deltaTime: (int),
....................metaType: (int),
....................type: (int)
................},
................[1] : Object{...},  // EVENT 2
................[2] : Object{...}, // EVENT 3
................[3] : Object{...}  // EVENT 4
............]
........},
........[1] : Object{...},  // TRACK 2
........[2] : Object{...}  // TRACK 3
....]
}

If you want to read the data from Event 2 of Track 0 , you should use the following keypath :

outputObject.track[0].event[2].data;

Distribution & Installation :

The following distribution channels are available :

- NPM : Install using the following command :

  $ npm install midi-parser-js -s

- GIT : You can clone the repository :

  $ git clone https://github.com/colxi/midi-parser-js.git

-ZIP : Or download the package in a ZIP file from

GITHUB LATEST PACKAGE RELEASE PAGE

-CDN : Include the latest release of this library in your HTML head using the CDN :

Warning : Not recommended for production enviroments!

<script src="https://colxi.info/midi-parser-js/src/main.js"></script>

Importing

This package is shipped with support to Node CommonJS and ES6 Modules. Use the appropiate method accoordintg to your enviroment.

  // ES6 Module Import : 
  import {MidiParser} from './midi-parser.js'; 

  // CommonJS Node Import :
  let MidiParser = require('midi-parser-js');

Bonus : MIDI File Format Specifications :

MIDI Binary Encoding Specifications in https://github.com/colxi/midi-parser-js/wiki/MIDI-File-Format-Specifications

midi-parser-js's People

Contributors

colxi avatar jdevries3133 avatar micuat avatar nicolaitanes 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.