]> git.sesse.net Git - nageru/blobdiff - piecewise_interpolator.h
Split out the piecewise linear interpolation code into its own class.
[nageru] / piecewise_interpolator.h
diff --git a/piecewise_interpolator.h b/piecewise_interpolator.h
new file mode 100644 (file)
index 0000000..17a9d8b
--- /dev/null
@@ -0,0 +1,27 @@
+#ifndef _PIECEWISE_INTERPOLATOR_H
+#define _PIECEWISE_INTERPOLATOR_H
+
+// A class to do piecewise linear interpolation of one scale to another
+// (and back). Typically used to implement nonlinear dB mappings for sliders
+// or meters, thus the nomenclature.
+
+#include <vector>
+
+class PiecewiseInterpolator {
+public:
+       // Both dB and fraction values must go from high to low.
+       struct ControlPoint {
+               double db_value;
+               double fraction;
+       };
+       PiecewiseInterpolator(const std::vector<ControlPoint> &control_points)
+               : control_points(control_points) {}
+
+       double fraction_to_db(double db) const;
+       double db_to_fraction(double x) const;
+
+private:
+       const std::vector<ControlPoint> control_points;
+};
+
+#endif  // !defined(_PIECEWISE_INTERPOLATOR_H)