midifile icon indicating copy to clipboard operation
midifile copied to clipboard

Documentation for writing MIDI files

Open argiepiano opened this issue 10 years ago • 4 comments

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.

argiepiano avatar Jan 07 '16 21:01 argiepiano

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.

nfroidure avatar Jan 08 '16 07:01 nfroidure

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.

1j01 avatar Feb 15 '17 00:02 1j01

@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.

nfroidure avatar Feb 15 '17 08:02 nfroidure

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;
}

mrspeaker avatar Dec 29 '17 01:12 mrspeaker