9

I am new to working with MIDI in my javascript code, and I would like to create an application that simply generates a MIDI file based on the user's input. I have looked at using MIDI.js, but I'm not sure if I need any of the library. Ideally, I would like to be able to work with note values rather than hexadecimal MIDI codes... would I need to use the converter within plugin.js?

My current research has indicated that generated soundfonts are the suggested way to go; however, I would like to export/generate a MIDI file for use in a professional DAW.

Thanks for any help.

Community
  • 1
  • 1
user1429980
  • 6,872
  • 2
  • 43
  • 53
  • What exactly are you asking for? Do you want to know how to generate a MIDI file in your own code? Questions asking to recommend or find a library are off-topic for Stack Overflow as they tend to attract opinionated answers. Instead, [describe the problem](http://meta.stackexchange.com/q/139399/) and what has been done so far to solve it. – CL. Oct 05 '13 at 10:20
  • 1
    The former: how to generate a MIDI file using javascript. – user1429980 Oct 06 '13 at 04:14
  • Read the [specification](http://www.music.mcgill.ca/~ich/classes/mumt306/midiformat.pdf) (PDF), write the appropriate code. What specific problem do you have with that? – CL. Oct 06 '13 at 07:46
  • Can you give a sample of the input you'd like to see and the output? It'd help me to see if there is something I know about that's already out there. – Michael Scott Asato Cuthbert Oct 09 '13 at 00:37
  • Looking here: https://github.com/mudcube/MIDI.js (the MIDI.js samples), there is a method, `MIDI.Player.loadFile(file, callback)` to load a MIDI file, but I cannot find one to save a MIDI file. (I found this SO while searching how to save a MIDI file based off a graphically defined loop) – Nick.Mc Jan 06 '15 at 06:09

1 Answers1

2

You can use a library I found called MidiWriterJS.

It is very easy to use:

Step 1: Install

The easiest way to insall is to use npm.

$ npm install midi-writer-js

Then

var MidiWriter = require('midi-writer-js');

Step 2: Create an array of tracks and add notes

var tracks = [];
for(t of <your existing track array>){
    var notes = [];
    for(n of <your existing note array for the track>){
        notes.push(new MidiWriter.NoteEvent({pitch: <array of all notes in the chord>, duration: <how long the chord should last>});
    }
    var newTrack = new MidiWriter.Track();
    newTrack.addEvent(notes, function(event, index){ return {sequential: true}; });
    tracks.push(newTrack);
}

Step 3: Export the MIDI Data

var writer = new MidiWriter.Writer(tracks);
writer.saveMIDI(<filename>);
yummypasta
  • 1,398
  • 2
  • 17
  • 36