Skip to content

Instantly share code, notes, and snippets.

@tcyrus
Last active March 16, 2016 22:28
Show Gist options
  • Select an option

  • Save tcyrus/9802965b599015e88313 to your computer and use it in GitHub Desktop.

Select an option

Save tcyrus/9802965b599015e88313 to your computer and use it in GitHub Desktop.
<?php
class Rhythm {
var $length, $duration, $_prevTap, $_curTap, $_taps, $_weight;
function __construct($length = 0, $duration = 0, $_taps = array(), $_weight = 1) {
$this->length = $length;
$this->duration = $duration;
$this->_taps = $_taps;
$this->_weight = $_weight;
}
}
function compare($r1, $r2, $inelastic) {
var $error, $multiplier, $length, $dur1, $dur2, $rHi, $rLo;
if (is_numeric($r1->_tap) || is_numeric($r2->_tap)) {
throw new Exception('Can\'t compare Rhythms before calling done()');
}
$length = count($r1);
if ($length !== count($r2)) {
return false;
}
$length--;
$r1 = $r1->_taps;
$r2 = $r2->_taps;
if (!$inelastic) {
function sum($acc, $tap) {
return $acc + $tap;
}
$dur1 = array_reduce($r1, "sum");
$dur2 = array_reduce($r2, "sum");
if ($dur1 > $dur2) {
$rHi = $r1;
$rLo = $r2;
} else {
$rHi = $r2;
$rLo = $r1;
}
$multiplier = array_reduce(array_map(
function($tHi, $i) { return $rLo[$i] / $tHi; },
$rHi,
array_keys($rHi)
), "sum") / $length;
$rHi = array_map(function($tap) { return $tap * $multiplier; }, $rHi)
} else {
$rHi = $r1;
$rLo = $r2;
}
return 1 - array_reduce($rHi,
function($acc, $tap1, $i) {
$tap2 = $rLo[$i];
$tHi = max($tap1, $tap2);
$tLo = min($tap1, $tap2);
return $acc + $tHi - $tLo / $tHi;
}, array_keys($rHi)) / $length;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment