nfroidure / midifile

A MIDI file parser/writer using ArrayBuffers
http://karaoke.insertafter.com
MIT License
199 stars 30 forks source link

Documentation for writing MIDI files #9

Open argiepiano opened 8 years ago

argiepiano commented 8 years ago

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.

nfroidure commented 8 years ago

I actually didn't document the MIDIFile write functions since i think it is still experimental. Feel free to document it, i'll accept any PR doing so.

1j01 commented 7 years ago

Here's an example of transforming a MIDI file, simplified from midiflip:

var MIDIFile = require("midifile");
var MIDIEvents = require("midievents");

var midiFile = new MIDIFile(inputArrayBuffer);

for(var track_index = 0; track_index < midiFile.tracks.length; track_index++){
    var events = midiFile.getTrackEvents(track_index);
    for(var i=0; i<events.length; i++){
        var event = events[i];
        if(event.type === MIDIEvents.EVENT_MIDI){
            if(event.subtype === MIDIEvents.EVENT_MIDI_NOTE_OFF || event.subtype === MIDIEvents.EVENT_MIDI_NOTE_ON){
                event.param1 = 127 - event.param1;
            }
        }
    }
    midiFile.setTrackEvents(track_index, events);
}

var outputArrayBuffer = midiFile.getContent();

There should be documentation for individual methods though.

nfroidure commented 7 years ago

@1j01 i like midiflip, a very good use of midifile ;). I'll try it soon.

On documentation, i know, shame on me but i do not ever use midifile, just wrote it cause i wanted to know if i could.

But the good news is that i merge PRs or even add rights to people wanted to get involved on it ;).

That said, the MIDI standards aren't changing that often and except documentation/tests/refactoring i don't think there is that much work.

I'll probably make an ES6 refactoring sometime and add JSDocs but can't tell when.

mrspeaker commented 6 years ago

Just a note: if you want to make changes to event.data then you also need to update the length property, otherwise it gets truncated to the old length:

if (event.subtype === 1) {
  event.data = "updated text here!".split("").map(ch => ch.charCodeAt(0));
  event.length = event.data.length;
}