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.

134 lines
3.9KB

  1. // Copyright 2014 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // Ensemble FX.
  28. #ifndef RINGS_DSP_FX_ENSEMBLE_H_
  29. #define RINGS_DSP_FX_ENSEMBLE_H_
  30. #include "stmlib/stmlib.h"
  31. #include "stmlib/dsp/dsp.h"
  32. #include "rings/dsp/fx/fx_engine.h"
  33. #include "rings/resources.h"
  34. namespace rings {
  35. class Ensemble {
  36. public:
  37. Ensemble() { }
  38. ~Ensemble() { }
  39. void Init(uint16_t* buffer) {
  40. engine_.Init(buffer);
  41. phase_1_ = 0;
  42. phase_2_ = 0;
  43. }
  44. void Process(float* left, float* right, size_t size) {
  45. typedef E::Reserve<2047, E::Reserve<2047> > Memory;
  46. E::DelayLine<Memory, 0> line_l;
  47. E::DelayLine<Memory, 1> line_r;
  48. E::Context c;
  49. while (size--) {
  50. engine_.Start(&c);
  51. float dry_amount = 1.0f - amount_ * 0.5f;
  52. // Update LFO.
  53. phase_1_ += 1.57e-05f;
  54. if (phase_1_ >= 1.0f) {
  55. phase_1_ -= 1.0f;
  56. }
  57. phase_2_ += 1.37e-04f;
  58. if (phase_2_ >= 1.0f) {
  59. phase_2_ -= 1.0f;
  60. }
  61. int32_t phi_1 = (phase_1_ * 4096.0f);
  62. float slow_0 = lut_sine[phi_1 & 4095];
  63. float slow_120 = lut_sine[(phi_1 + 1365) & 4095];
  64. float slow_240 = lut_sine[(phi_1 + 2730) & 4095];
  65. int32_t phi_2 = (phase_2_ * 4096.0f);
  66. float fast_0 = lut_sine[phi_2 & 4095];
  67. float fast_120 = lut_sine[(phi_2 + 1365) & 4095];
  68. float fast_240 = lut_sine[(phi_2 + 2730) & 4095];
  69. float a = depth_ * 1.0f;
  70. float b = depth_ * 0.1f;
  71. float mod_1 = slow_0 * a + fast_0 * b;
  72. float mod_2 = slow_120 * a + fast_120 * b;
  73. float mod_3 = slow_240 * a + fast_240 * b;
  74. float wet = 0.0f;
  75. // Sum L & R channel to send to chorus line.
  76. c.Read(*left, 1.0f);
  77. c.Write(line_l, 0.0f);
  78. c.Read(*right, 1.0f);
  79. c.Write(line_r, 0.0f);
  80. c.Interpolate(line_l, mod_1 + 1024, 0.33f);
  81. c.Interpolate(line_l, mod_2 + 1024, 0.33f);
  82. c.Interpolate(line_r, mod_3 + 1024, 0.33f);
  83. c.Write(wet, 0.0f);
  84. *left = wet * amount_ + *left * dry_amount;
  85. c.Interpolate(line_r, mod_1 + 1024, 0.33f);
  86. c.Interpolate(line_r, mod_2 + 1024, 0.33f);
  87. c.Interpolate(line_l, mod_3 + 1024, 0.33f);
  88. c.Write(wet, 0.0f);
  89. *right = wet * amount_ + *right * dry_amount;
  90. left++;
  91. right++;
  92. }
  93. }
  94. inline void set_amount(float amount) {
  95. amount_ = amount;
  96. }
  97. inline void set_depth(float depth) {
  98. depth_ = depth * 128.0f;
  99. }
  100. private:
  101. typedef FxEngine<4096, FORMAT_16_BIT> E;
  102. E engine_;
  103. float amount_;
  104. float depth_;
  105. float phase_1_;
  106. float phase_2_;
  107. DISALLOW_COPY_AND_ASSIGN(Ensemble);
  108. };
  109. } // namespace rings
  110. #endif // RINGS_DSP_FX_ENSEMBLE_H_