You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.1KB

  1. #pragma once
  2. #include <assert.h>
  3. #include <map>
  4. #include <vector>
  5. #include "LookupTable.h"
  6. using Spline = std::vector< std::pair<double, double> >;
  7. class NonUniformLookup
  8. {
  9. public:
  10. void add(double x, double y)
  11. {
  12. data[x] = y;
  13. }
  14. double lookup(double x)
  15. {
  16. // printf("lookup %f\n", x);
  17. auto l = data.lower_bound(x);
  18. assert(l != data.end());
  19. // printf("lower = %f, %f\n", l->first, l->second);
  20. auto p = l;
  21. p--;
  22. if (p == data.end()) {
  23. assert(l->first == x);
  24. return l->second;
  25. }
  26. assert(p != data.end());
  27. // printf("p = %f, %f\n", p->first, p->second);
  28. // construct line y = y0 + (y1 -y0)/(x1 - x0) * x-x0;
  29. // = b + a(x -b);
  30. const double b = p->second;
  31. const double a = (l->second - p->second) / (l->first - p->first);
  32. const double ret = b + a * (x - p->first);
  33. // printf("ret = %f\n", ret);
  34. return ret;
  35. }
  36. private:
  37. std::map<double, double> data;
  38. };
  39. class AsymWaveShaper
  40. {
  41. public:
  42. const static int iNumPoints = 256;
  43. const static int iSymmetryTables = 16;
  44. private:
  45. LookupTableParams<float> tables[iSymmetryTables];
  46. public:
  47. AsymWaveShaper();
  48. float lookup(float x, int index) const
  49. {
  50. float x_scaled = 0;
  51. if (x >= 1) {
  52. x_scaled = iNumPoints - 1;
  53. } else if (x < -1) {
  54. x_scaled = 0;
  55. } else {
  56. x_scaled = (x + 1) * iNumPoints / 2;
  57. }
  58. assert(index >= 0 && index < iSymmetryTables);
  59. const LookupTableParams<float>& table = tables[index];
  60. // TODO: we are going outside of domain!.
  61. const float y = LookupTable<float>::lookup(table, x_scaled, true);
  62. // printf("lookup %f -> %f ret %f\n", x, x_scaled, y);
  63. return y;
  64. }
  65. static void genTableValues(const Spline& spline, int numPoints);
  66. static void genTable(int index, double symmetry);
  67. static Spline makeSplineRight(double symmetry);
  68. static Spline makeSplineLeft(double symmetry);
  69. static std::pair<double, double> calcPoint(const Spline& spline, double t);
  70. };