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.

92 lines
2.4KB

  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. /** Why does this class exist when we also have NonUniformLookupTable?
  8. */
  9. class NonUniformLookup
  10. {
  11. public:
  12. void add(double x, double y)
  13. {
  14. data[x] = y;
  15. }
  16. double lookup(double x)
  17. {
  18. // printf("lookup %f\n", x);
  19. auto l = data.lower_bound(x);
  20. assert(l != data.end());
  21. // printf("lower = %f, %f\n", l->first, l->second);
  22. auto p = l;
  23. if (p == data.begin()) {
  24. // mac was crashing decrementing past begin
  25. assert(l->first == x);
  26. return l->second;
  27. }
  28. p--;
  29. if (p == data.end()) {
  30. assert(false); // should never happen
  31. assert(l->first == x);
  32. return l->second;
  33. }
  34. assert(p != data.end());
  35. // printf("p = %f, %f\n", p->first, p->second);
  36. // construct line y = y0 + (y1 -y0)/(x1 - x0) * x-x0;
  37. // = b + a(x -b);
  38. const double b = p->second;
  39. const double a = (l->second - p->second) / (l->first - p->first);
  40. const double ret = b + a * (x - p->first);
  41. // printf("ret = %f\n", ret);
  42. return ret;
  43. }
  44. private:
  45. std::map<double, double> data;
  46. };
  47. class AsymWaveShaper
  48. {
  49. public:
  50. const static int iNumPoints = 256;
  51. const static int iSymmetryTables = 16;
  52. private:
  53. LookupTableParams<float> tables[iSymmetryTables];
  54. public:
  55. AsymWaveShaper();
  56. float lookup(float x, int index) const
  57. {
  58. float x_scaled = 0;
  59. if (x >= 1) {
  60. x_scaled = iNumPoints - 1;
  61. } else if (x < -1) {
  62. x_scaled = 0;
  63. } else {
  64. x_scaled = (x + 1) * iNumPoints / 2;
  65. }
  66. assert(index >= 0 && index < iSymmetryTables);
  67. const LookupTableParams<float>& table = tables[index];
  68. // TODO: we are going outside of domain!.
  69. const float y = LookupTable<float>::lookup(table, x_scaled, true);
  70. // printf("lookup %f -> %f ret %f\n", x, x_scaled, y);
  71. return y;
  72. }
  73. static void genTableValues(const Spline& spline, int numPoints);
  74. static void genTable(int index, double symmetry);
  75. static Spline makeSplineRight(double symmetry);
  76. static Spline makeSplineLeft(double symmetry);
  77. static std::pair<double, double> calcPoint(const Spline& spline, double t);
  78. };