Last active
January 8, 2025 20:39
-
-
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.
This file contains hidden or 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
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); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interactive version: https://www.desmos.com/calculator/dj2xiomjux