Last active
March 16, 2016 22:28
-
-
Save tcyrus/9802965b599015e88313 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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