MIDIChlorian – Accurate Web Based MIDI Payer

Have you ever wanted to play a MIDI file on a computer but then found out that there’s no simple way to do it? I know I have! It’s criminal that we don’t have access to the magic of MIDI out of the box on these supposedly super advanced 2016 computers from the future… well no more! I’m going to let you in on a little secret, dear reader, I’ve been toiling away making the world’s best online MIDI player. You can simply drag MIDIs from your computer and drop them into MIDIChlorian (that’s a Star Wars joke for you) and they will play in a beautiful 4MB SoundFont, each track playing its own beautiful notes, timing to the microsecond. Amazing! Well, what are you waiting for? Go there and remember a simpler time, before mp3s, when anybody could remix music and share among friends. Go enjoy MIDIChlorian.

chlorian

Operator Overloading in PaperScript

I was looking into how paper.js did their operator overloading and what I found was pretty clever:

var operators = {
  '+': 'add',
  '-': 'subtract',
  '*': 'multiply',
  '/': 'divide',
  '%': 'modulo',
  '==': 'equals',
  '!=': 'equals'
};

function $eval(left, operator, right) {
  var handler = operators[operator];
  if (left && left[handler]) {
    var res = left[handler](right);
    return operator == '!=' ? !res : res;
  }
  switch (operator) {
  case '+': return left + right;
  case '-': return left - right;
  case '*': return left * right;
  case '/': return left / right;
  case '%': return left % right;
  case '==': return left == right;
  case '!=': return left != right;
  default:
    throw new Error('Implement Operator: ' + operator);
  }
};

Though technically this is PaperScript and not JavaScript, so they can get around the lack of native JS operator overloading. The PaperScript code is given a minimal compile pass which replaces calls to arithmetic operators with calls to $eval().

// PaperScript
var p = new Point(10, 20);
var r = p * 5;

// JavaScript
var p = new Point(10, 20);
var r = $eval(p, "*", 5);

Related PaperScript source

So as long as Point#multiply is defined then the operator is effectively overloaded.

I’m very interested in the implications for PixieEngine and CoffeeScript.