Giter Site home page Giter Site logo

adam-eden101 / midiconvert Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tonejs/midi

0.0 0.0 0.0 535 KB

Convert MIDI into Tone.js-friendly JSON

Home Page: http://tonejs.github.io/MidiConvert/

License: MIT License

HTML 10.81% JavaScript 89.19%

midiconvert's Introduction

MidiConvert

Build Status codecov

MidiConvert makes it straightforward to work with MIDI files in Javascript. It uses midi-file-parser to decode MIDI files and jsmidgen to encode MIDI files.

// load a midi file
MidiConvert.load("path/to/midi.mid", function(midi) {
  console.log(midi)
})

Format

The data parsed from the midi file looks like this:

{
  // the transport and timing data
  header: {
    name: String,                     // the name of the first empty track, 
                                      // which is usually the song name
    bpm: Number,                      // the tempo, e.g. 120
    timeSignature: [Number, Number],  // the time signature, e.g. [4, 4],
    PPQ: Number                       // the Pulses Per Quarter of the midi file
  },

  startTime: Number,                  // the time before the first note plays
  duration: Number,                   // the time until the last note finishes

  // an array of midi tracks
  tracks: [
    {
      id: Number,                     // the position of this track in the array
      name: String,                   // the track name if one was given
      notes: [
        {
          midi: Number,               // midi number, e.g. 60
          time: Number,               // time in seconds
          note: String,               // note name, e.g. "C4"
          velocity: Number,           // normalized 0-1 velocity
          duration: Number,           // duration between noteOn and noteOff
        }
      ],

      startTime: Number,              // the time before the first note plays
      duration: Number,               // the time until the last note finishes

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

      isPercussion: Boolean,          // true if this track is on a percussion
                                      // channel
      channelNumber: Number,          // the ID for this channel; 9 and 10 are
                                      // reserved for percussion

      instrumentNumber: Number,       // the ID for this instrument, as defined
                                      // by the MIDI spec
      instrumentFamily: String,       // the name of this instrument's family,
                                      // as defined by the MIDI spec
      instrument: String,             // the instrument name, as defined by the
                                      // MIDI spec
    }
  ]
}

Raw Midi Parsing

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

fs.readFile("test.mid", "binary", function(err, midiBlob) {
  if (!err) {
    var midi = MidiConvert.parse(midiBlob)
  }
})

Encoding Midi

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

// create a new midi file
var midi = MidiConvert.create()
// add a track
midi.track()
  // select an instrument by its MIDI patch number
  .patch(32)
  // chain note events: note, time, duration
  .note(60, 0, 2)
  .note(63, 1, 2)
  .note(60, 2, 2)

// write the output
fs.writeFileSync("output.mid", midi.encode(), "binary")

Tone.Part

The note data can be easily passed into Tone.Part

var synth = new Tone.PolySynth(8).toMaster()

MidiConvert.load("path/to/midi.mid", function(midi) {

  // make sure you set the tempo before you schedule the events
  Tone.Transport.bpm.value = midi.header.bpm

  // pass in the note events from one of the tracks as the second argument to Tone.Part 
  var midiPart = new Tone.Part(function(time, note) {

    //use the events to play the synth
    synth.triggerAttackRelease(note.name, note.duration, time, note.velocity)

  }, midi.tracks[0].notes).start()

  // start the transport to hear the events
  Tone.Transport.start()
})

Acknowledgment

MidiConvert uses midi-file-parser which is ported from jasmid for decoding MIDI data and and jsmidgen for encoding MIDI data.

midiconvert's People

Contributors

tambien avatar appsforartists avatar cghawthorne avatar pioug avatar adarob avatar bartoszbaczek avatar frankbaele avatar iansimon avatar jackca avatar jeanlazarou avatar kn0ll avatar thomasballinger 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.