Last active
May 8, 2020 06:21
-
-
Save hiromorozumi/f2e3081c3d6a247b2cbbdd40c5701934 to your computer and use it in GitHub Desktop.
An easy, silly way to create a lookup table for an exponential decay curve - useful for ADSR envelope curves
This file contains 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
#include <iomanip> | |
#include <iostream> | |
#define EXP_TABLE_LEN 100 | |
using namespace std; | |
// | |
// populates an array of doubles with values ranging from 1.0 to 0 | |
// derived from an exponential decay curve | |
// comes in handy when you get tired of linear ADSR envelope shapes | |
// | |
void fillExpDecayTable(double table[], int arraySize) | |
{ | |
double x1 = 1.0; // change x1 and x2 to get different result | |
double x2 = 3.0; // increase the x2 value and distance btw x1 and x2 | |
// to get a curve that's more intense | |
double yFloor = x1; | |
double yCeiling = x2 * x2; // for more radical curve, set this to x2 * x2 * x2 | |
// and change line 32 to: | |
// double yPos = ((xPos * xPos * xPos) - yFloor) / yRange; | |
double yRange = yCeiling - yFloor; // you can factor all these variables | |
// into formula | |
// to make the program compact | |
double xPos = x2; | |
double xDelta = (x2 - x1) / arraySize; | |
for(int i=0; i<arraySize; i++) | |
{ | |
double yPos = ((xPos * xPos) - yFloor) / yRange; | |
table[i] = yPos; | |
xPos -= xDelta; | |
} | |
} | |
// let's see how the table looks | |
int main() | |
{ | |
double expDecayTable[EXP_TABLE_LEN]; | |
fillExpDecayTable(expDecayTable, EXP_TABLE_LEN); | |
cout << "\t\t" << "expo" << "\t\t" << "linear" << endl << endl; | |
cout << setprecision(2); | |
for(int i=0; i<EXP_TABLE_LEN; i++) | |
{ | |
cout << i << "\t\t" << expDecayTable[i] << "\t\t" | |
<< 1 - (double)i / EXP_TABLE_LEN << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The first image shows linear shaping using simple linear functions for the attack, decay and release stages. The second image shows exponential shaping with the table created in the program above, with the variables x1 = 1.0 and x2 = 30.0.