mudcube / MIDI.js

:musical_keyboard: Making life easy to create a MIDI-app on the web. Includes a library to program synesthesia into your app for memory recognition or for creating trippy effects. Convert soundfonts for Guitar, Bass, Drums, ect. into code that can be read by the browser. Supports multiple simultaneous instruments and perfect timing.

Home Page:http://mudcu.be/midi-js/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to get data of all notes of MIDI file without playing

shivrajsa opened this issue · comments

In MIDI.js, we have listener when notes are played, which gives us below data for each note

MIDI.Player.addListener(function(data) { // set it to your own function!
    var now = data.now; // where we are now
    var end = data.end; // time when song ends
    var channel = data.channel; // channel note is playing on
    var message = data.message; // 128 is noteOff, 144 is noteOn
    var note = data.note; // the note
    var velocity = data.velocity; // the velocity of the note
    // then do whatever you want with the information!
});

How can I get data of all notes of MIDI file without playing ?

In Color Piano demo, we can observe that color bars are generated before playing the file. I want similar data without playing file.

I got some data in player.js

look for below code in the file

midi.loadMidiFile = function(onsuccess, onprogress, onerror) {
	try {
		midi.replayer = new Replayer(MidiFile(midi.currentData), midi.timeWarp, null, midi.BPM);
		midi.data = midi.replayer.getData();
		midi.endTime = getLength();
		       MIDI.loadPlugin({
// 			instruments: midi.getFileInstruments(),
			onsuccess: onsuccess,
			onprogress: onprogress,
			onerror: onerror
		});
	} catch(event) {
		onerror && onerror(event);
	}
};

And change it to

midi.loadMidiFile = function(onsuccess, onprogress, onerror) {
	try {
		midi.replayer = new Replayer(MidiFile(midi.currentData), midi.timeWarp, null, midi.BPM);
		midi.data = midi.replayer.getData();
		midi.endTime = getLength();
		var str='';
		for(var i=0;i<midi.data.length;i++)
		{
			str = str + '\n' + Object.keys(midi.data[i][0].event) + ':' + Object.values(midi.data[i][0].event) + '(' + midi.data[i][1] + ')';
		}
		document.getElementById("notes").innerHTML = str; 
		MIDI.loadPlugin({
// 			instruments: midi.getFileInstruments(),
			onsuccess: onsuccess,
			onprogress: onprogress,
			onerror: onerror
		});
	} catch(event) {
		onerror && onerror(event);
	}
};

Result is something like this

deltaTime,channel,type,noteNumber,velocity,subtype:1,1,channel,73,127,noteOn(3.90625) deltaTime,channel,type,subtype,noteNumber,velocity:240,1,channel,noteOff,73,127(937.5) deltaTime,channel,type,noteNumber,velocity,subtype:1,1,channel,76,127,noteOn(3.90625) deltaTime,channel,type,subtype,noteNumber,velocity:240,1,channel,noteOff,76,127(937.5) deltaTime,channel,type,noteNumber,velocity,subtype:1,1,channel,73,127,noteOn(3.90625) deltaTime,channel,type,subtype,noteNumber,velocity:240,1,channel,noteOff,73,127(937.5)

I think this data is returned from replayer.js
But I do not understand where is the time value for each noteOn event ?