Skip to content

Instantly share code, notes, and snippets.

@iccir
Created August 3, 2017 01:52
Show Gist options
  • Save iccir/a5e62f528226f0245b30bb2a99d2b6e1 to your computer and use it in GitHub Desktop.
Save iccir/a5e62f528226f0245b30bb2a99d2b6e1 to your computer and use it in GitHub Desktop.
Envelopes
// Fade Ins:
// Linear:
for (let i = 0; i < length; i++) {
array[i] = i / length;
}
// Exponential, Concave Up:
let silence = Math.pow(10.0, -120.0 / 20.0); // Silence is -120dB
let fromValue = silence;
let multiplier = Math.pow(1.0 / silence, 1.0 / length);
for (let i = 0; i < length; i++) {
result[i] = fromValue;
fromValue *= multiplier;
}
// Quadratic, Concave Up:
for (let i = 0; i < length; i++) {
array[i] = Math.pow(, 2);
}
// Cubic, Concave Up
for (let i = 0; i < length; i++) {
array[i] = Math.pow(i / length, 3);
}
// Quadratic, Concave Down
for (let i = 0; i < length; i++) {
array[i] = 1.0 + Math.pow((i / length) - 1.0, 2);
}
// Cubic, Concave Down
for (let i = 0; i < length; i++) {
array[i] = 1.0 + Math.pow((i / length) - 1.0, 3);
}
// Fade Outs:
// Linear
for (let i = 0; i < length; i++) {
array[i] = 1.0 - (i / length);
}
// Exponential, Concave Up:
let silence = Math.pow(10.0, -120.0 / 20.0); // Silence is -120dB
let fromValue = 1.0;
let multiplier = Math.pow(silence, 1.0 / length);
for (let i = 0; i < length; i++) {
result[i] = fromValue;
fromValue *= multiplier;
}
// Quadratic, Concave Up
for (let i = 0; i < length; i++) {
array[i] = Math.pow(1.0 - (i / length), 2);
}
// Cubic, Concave Up
for (let i = 0; i < length; i++) {
array[i] = Math.pow(1.0 - (i / length), 3);
}
// Quadratic, Concave Down
for (let i = 0; i < length; i++) {
array[i] = 1.0 - Math.pow(1.0 - (i / length), 2);
}
// Cubic, Concave Down
for (let i = 0; i < length; i++) {
array[i] = 1.0 - Math.pow(1.0 - (i / length), 3);
}
// Exponential (Both):
let silence = Math.pow(10.0, -120.0 / 20.0); // Silence is -120dB
let fromValue = isFadeIn ? silence : 1.0;
let toValue = isFadeIn ? 1.0 : silence;
let multiplier = Math.pow(toValue / fromValue, 1.0 / length);
for (let i = 0; i < length; i++) {
result[i] = fromValue;
fromValue *= multiplier;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment