Skip to content

Instantly share code, notes, and snippets.

@jzellis
Last active August 18, 2016 22:44
Show Gist options
  • Save jzellis/011411b13f7261478081409bd8a34b61 to your computer and use it in GitHub Desktop.
Save jzellis/011411b13f7261478081409bd8a34b61 to your computer and use it in GitHub Desktop.
Given an array of numeric arrays and a decimal value between 0 and max, returns a "morph" of the arrays at a point on the continuum of the arrays at that point. Used to emulate the waveform knob on a Moog synthesizer.
// These are just examples.
var arrays = [
[0.64, 0.02, 0.1, 0, 0.9, 0, 0.09, 0, 0.07, 0, 0.06, 0, 0.05, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0.81, 0, -0.09, 0, 0.03, 0, -0.02, 0, 0.01, 0, -0.01, 0, 0, 0],
[-0.32, -0.16, -0.11, -0.08, -0.06, -0.05, -0.05, -0.04, -0.04, -0.03, -0.03, -0.02, -0.02]
];
var arrayMorph = function(value, max, arrays) {
current = [];
//Get the number of arrays so we can divide the input value equally between them
numArrays = arrays.length;
// Get the current input value
// Get the input's max value
// Get the current input value as a spectrum between the values
absMorph = value / max * (numArrays - 1);
// This is the "hard" previous preset index
prev = Math.floor(absMorph);
// This is the "hard" next preset index
next = Math.ceil(absMorph);
// This is the decimal difference between the prev and next
diff = absMorph - prev;
prevArray = arrays[prev];
nextArray = arrays[next];
nextArray.forEach(function(v, i, a) {
thisDiff = nextArray[i] - prevArray[i];
thisVal = prevArray[i] + (thisDiff * diff);
if (!isFinite(thisVal)) thisVal = 0;
current.push(thisVal);
});
return current;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment