MIDI Maths

by amindunited

Babel + JSX

// Timesignature numberOfBeats / lengthOfMeasure
let numberOfBeats = 4;
let lengthOfMeasure = 4;
let tempo = 120;//How many beats per minute
let PPQ = 127;// Parts per quarter (tick to a quarter note) *I have read that 480 is the MIDI default...but am not sure
console.log('so...', numberOfBeats, lengthOfMeasure, tempo, PPQ);
console.log('If there is 120 bpm then there should be 2 beats per second, ...');
console.log('...so with a PPQ of 480 there will be ' + (PPQ * numberOfBeats) * (tempo/60) + ' pulses in a second');

//Reference Material:
//http://sites.uci.edu/camp2014/2014/05/19/timing-in-midi-files/
//http://www.cs.cmu.edu/~music/cmsip/readings/Standard-MIDI-file-format-updated.pdf
//http://www.mobilefish.com/tutorials/midi/midi_quickguide_specification.html

// Header:
//<Header Chunk> = <chunk type><length><format><ntrks><division>
//	As described above, <chunk type> is the four ASCII characters 'MThd';
//	<length> is a 32-bit representation of the number 6 (high byte first).
//	<format> is the midi format:
//		0-the file contains a single multi-channel track
//		1-the file contains one or more simultaneous tracks (or MIDI outputs) of a sequence
//		2-the file contains one or more sequentially independent single-track patterns
//	<ntrks>, is the number of track chunks in the file
//	<division>, the type of delta time (E250 = 30 frame timecode )

// Each chunk:
//<Track header>: MTrk
//	<Track Chunk> = <chunk type><length><MTrk event>+
//		<MTrk event> = <delta-time><event>
//			<event> = <MIDI event> | <sysex event> | <meta-event>


/**
	Checking FPS
*/
let fps = 25
let loops = 25;
let then = Date.now();
let times = [];
function animate() {
	loops--;
  if (loops > 0) {
    setTimeout(function() {
    	let now = Date.now();
      let elapsed = now - then;
      times.push(Math.round(1000 / elapsed));
      then = now;
      requestAnimationFrame(animate);
    }, 0);
  } else {
  	console.log('me times', times);
  }
}
console.log('then, ', then);
animate();