Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: How to Export a MIDI File?

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.

like image 671
user1429980 Avatar asked Oct 05 '13 00:10

user1429980


People also ask

How do I export a MIDI file?

Export one or more MIDI regions as standard MIDI files In Logic Pro, select the MIDI regions to export. Choose File > Export > Selection as MIDI File. Choose the destination directory, enter a name, then click Save. The selected MIDI regions are saved as a Format 1 MIDI file.

Where is MIDI data stored?

The MIDI data is stored in separate tracks, which are additionally wrapped in containers, so it's possible to have e.g. several tracks using the same MIDI channels.


1 Answers

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>);
like image 191
yummypasta Avatar answered Sep 20 '22 05:09

yummypasta