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.

143 lines
3.8KB

  1. /* *\
  2. ** __ ___ ______ **
  3. ** / / / _ \/_ __/ **
  4. ** / /__/ , _/ / / Lindenberg **
  5. ** /____/_/|_| /_/ Research Tec. **
  6. ** **
  7. ** **
  8. ** https://github.com/lindenbergresearch/LRTRack **
  9. ** heapdump@icloud.com **
  10. ** **
  11. ** Sound Modules for VCV Rack **
  12. ** Copyright 2017/2018 by Patrick Lindenberg / LRT **
  13. ** **
  14. ** For Redistribution and use in source and binary forms, **
  15. ** with or without modification please see LICENSE. **
  16. ** **
  17. \* */
  18. #pragma once
  19. namespace dsp {
  20. class RateConverter {
  21. public:
  22. RateConverter(void);
  23. virtual ~RateConverter(void);
  24. // buffer for the impulse response h[n] of the FIR
  25. float *irBuffer;
  26. // buffers for the transversal delay lines, one for each input
  27. float *inputBuffer;
  28. // float *m_pRightInputBuffer;
  29. // read index for delay lines (input x buffers)
  30. int delayPos;
  31. // read index for impulse response buffers
  32. int irBufferPos;
  33. // write index for input x buffer
  34. int inputPos;
  35. int ratio; // OS value, 4 = 4X Oversampling
  36. // counters and index values for the convolutions
  37. int osPos;
  38. int length;
  39. // initializer - creates the buffers and loads the FIR IR
  40. void init(int _ratio, int _length, float *pIRBuffer);
  41. // flush buffers
  42. void reset();
  43. // overrides for derived objects
  44. //
  45. // interpolateSamples: take one pair of L/R samples and produce L-length buffers of samples
  46. virtual void interpolateSamples(float xnL, float *pLeftInterpBuffer) {};
  47. // inner loop function that processes just one pair of inputs
  48. virtual void interpolateNextOutputSample(float xnL, float &fLeftOutput) {};
  49. // decimateSamples: take one pai rL-length buffers of samples and decimate down to just one pair of output samples
  50. virtual void decimateSamples(float *pLeftDeciBuffer, float &ynL) {};
  51. // inner loop function that processes just one pair of inputs
  52. virtual bool decimateNextOutputSample(float xnL, float &fLeftOutput) { return true; };
  53. };
  54. class Decimator : public RateConverter {
  55. public:
  56. Decimator(void);
  57. ~Decimator(void);
  58. bool decimateNextOutputSample(float x, float &out) override;
  59. void decimateSamples(float *buffer, float &out) override;
  60. };
  61. class Interpolator : public RateConverter {
  62. public:
  63. Interpolator(void);
  64. ~Interpolator(void);
  65. void interpolateNextOutputSample(float x, float &fLeftOutput) override;
  66. void interpolateSamples(float x, float *buffer) override;
  67. };
  68. /**
  69. * @brief New oversampling class that uses polyphase
  70. */
  71. class NeoOversampler {
  72. private:
  73. int irSize;
  74. float irBuffer[1024];
  75. int ratio = 4;
  76. void init();
  77. Interpolator interpolator;
  78. Decimator decimator;
  79. float *interpBuffer;
  80. float *decimBuffer;
  81. public:
  82. bool enabled;
  83. NeoOversampler();
  84. virtual float process(float x) { return x; };
  85. float compute(float x);
  86. };
  87. /**
  88. * @brief Just for test!
  89. */
  90. struct TanhOS : NeoOversampler {
  91. float gain = 0.f;
  92. TanhOS();
  93. virtual float process(float x) override;
  94. };
  95. }