Skip to content

Instantly share code, notes, and snippets.

@davedarko
Last active February 5, 2025 11:53
Show Gist options
  • Save davedarko/568e55cfd76b4405e9957328f5b8e799 to your computer and use it in GitHub Desktop.
Save davedarko/568e55cfd76b4405e9957328f5b8e799 to your computer and use it in GitHub Desktop.
Eurorack modules like euclidean, this is a php gist to generate euclidean steps / truth tables
<?php
// this will probably be arduino code at one point
// but it's still trash atm
function gcd2($k, $n, $steps) {
if($n<2) return $steps;
else {
// $steps
$cnt = count($steps);
if ($cnt < 2*$n) return $steps;
$f = (int)(($k-$n)/$n);
if ($f == 0) $f = 1;
for ($i=0; $i<$n; $i++)
{
for ($j=1; $j<=$f; $j++) {
$steps[$i] = array_merge($steps[$i], $steps[$cnt-($j*$n)+$i]);
unset($steps[$cnt-($j*$n)+$i]);
}
}
// pp($f ." " . $k ." " . $n ." " . $k%$n);
// pp($steps);
return gcd2($n, $k%$n, $steps);
}
}
function printFlat($a) {
foreach ($a as $k=>$i) {
if ($i) echo 'x ';
else echo '. ';
}
echo "<br>";
}
function euclidean($k, $n){
$steps = array(array());
for ($i=0; $i<$k; $i++) {
if ($i<$n) $steps[$i][] = true;
else $steps[$i][] = false;
}
$steps = gcd2($k, $n, $steps);
$combined = array();
for ($i=0; $i<count($steps); $i++) {
$combined = array_merge($combined, $steps[$i]);
}
echo "$k|$n ";
printFlat($combined);
}
echo '<pre>';
for($i=3; $i<16; $i++) {
for($j=1; $j<=$i/2; $j++) {
euclidean($i, $j);
}
}
// euclidean(10, 2);
echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment