Skip to content

Instantly share code, notes, and snippets.

@geraintluff
Last active June 6, 2022 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geraintluff/b7ad635b031cb4c7aa4bffe77246316e to your computer and use it in GitHub Desktop.
Save geraintluff/b7ad635b031cb4c7aa4bffe77246316e to your computer and use it in GitHub Desktop.
A map from [0-1] to an arbitrary range, specified using low/mid/high values. Pulled out from some Signalsmith Audio internal code.
class UnitRangeMapReciprocal {
double vMin, vTopFactor, vBottomFactor;
public:
UnitRangeMapReciprocal() : UnitRangeMapReciprocal(0, 0.5, 1) {}
UnitRangeMapReciprocal(double min, double mid, double max) {
vMin = min;
double k = (mid - min)/(max - mid);
vTopFactor = max*k - min;
vBottomFactor = k - 1;
}
double toUnitRange(double value) const {
return (value - vMin)/(vTopFactor - value*vBottomFactor);
}
double fromUnitRange(double unit) const {
return (vMin + unit*vTopFactor)/(1 + unit*vBottomFactor);
}
};
@geraintluff
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment