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.

94 lines
2.0KB

  1. #pragma once
  2. #include "DSPMath.hpp"
  3. #include "DSPEffect.hpp"
  4. #include "HQTrig.hpp"
  5. #define SHAPER_MAX_VOLTS 15.0
  6. #define DCBLOCK_ALPHA 0.999
  7. namespace dsp {
  8. /**
  9. * @brief Basic WaveShaper class with build-in dynamic oversampling
  10. * @tparam OVERSAMPLE
  11. */
  12. struct WaveShaper : DSPEffect {
  13. /* oversampling channel */
  14. static const int STD_CHANNEL = 0;
  15. static constexpr double MAX_BIAS_LEVEL = 5.0; // +/- 5V
  16. protected:
  17. Resampler<1> *rs;
  18. DCBlocker *dc = new DCBlocker(DCBLOCK_ALPHA);
  19. HQTanh *tanh1;
  20. bool blockDC = false;
  21. double in, gain, bias, k;
  22. double out;
  23. Vec amp;
  24. public:
  25. WaveShaper(float sr);
  26. double getIn() const;
  27. void setIn(double in);
  28. double getGain() const;
  29. void setGain(double gain);
  30. double getBias() const;
  31. void setBias(double bias);
  32. double getK() const;
  33. void setK(double k);
  34. double getOut() const;
  35. void setOut(double out);
  36. /**
  37. * @brief Returns the actual sample-rate which is used by oversampled computation
  38. * @return
  39. */
  40. double getOversampledRate() {
  41. return sr * rs->getFactor();
  42. }
  43. void setAmplitude(double kpos, double kneg) {
  44. amp = Vec(kpos, kneg);
  45. }
  46. const Vec &getAmplitude() const;
  47. bool isBlockDC() const;
  48. void setBlockDC(bool blockDC);
  49. /**
  50. * @brief Implements the oversamping of compute method
  51. */
  52. void process() override;
  53. void init() override {
  54. gain = 0;
  55. out = 0;
  56. k = 0;
  57. bias = 0;
  58. amp = Vec(0, 0);
  59. }
  60. /**
  61. * @brief To be implemented by subclass, automaticaly oversampled
  62. *
  63. * @param x Input sample
  64. * @return Output sample
  65. */
  66. virtual double compute(double x) { return x; }
  67. };
  68. }