Factorial router
Inspired from Module to Switch Cable Routes.
This uses input 6 as a CV control to choose the permutation number. The CV is split into 120 intervals, each corresponding to a permutation of inputs 1 through 5.
{
"plugin": "VCV-Prototype",
"version": "1.1.0",
"model": "Prototype",
"data": {
"path": "/home/vortico/src/vcv/Rack-v1/plugins/VCV-Prototype/examples/factorial_router.js",
"script": "\nfunction permutation(arr, k) {\n\tvar n = arr.length\n\tvar factorial = 1\n\tfor (var i = 2; i <= n; i++) {\n\t\tfactorial *= i\n\t}\n\tvar perm = []\n\n\tfor (var i = n; i >= 1; i--) {\n\t\tfactorial /= i\n\t\tvar j = Math.floor(k / factorial)\n\t\tk %= factorial\n\t\tvar el = arr[j]\n\t\tarr.splice(j, 1)\n\t\tperm.push(el)\n\t}\n\n\treturn perm\n}\n\nconfig.bufferSize = 32\n\nfunction process(block) {\n\t// Get factorial index from input 6\n\tvar k = Math.floor(block.inputs[5][0] / 10 * 120)\n\tk = Math.min(Math.max(k, 0), 120 - 1)\n\t// Get index set permutation\n\tvar perm = permutation([0, 1, 2, 3, 4], k)\n\tdisplay(perm)\n\tfor (var i = 0; i < 5; i++) {\n\t\t// Permute input i\n\t\tfor (var j = 0; j < block.bufferSize; j++) {\n\t\t\tblock.outputs[i][j] = block.inputs[perm[i]][j]\n\t\t}\n\t}\n}\n"
}
}