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.

121 lines
3.0KB

  1. // Copyright 2013 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. // SVF used for modeling the bridged T-networks.
  28. #ifndef BRAIDS_SVF_H_
  29. #define BRAIDS_SVF_H_
  30. #include "stmlib/stmlib.h"
  31. #include "braids/resources.h"
  32. #include "stmlib/utils/dsp.h"
  33. namespace braids {
  34. enum SvfMode {
  35. SVF_MODE_LP,
  36. SVF_MODE_BP,
  37. SVF_MODE_HP
  38. };
  39. class Svf {
  40. public:
  41. Svf() { }
  42. ~Svf() { }
  43. void Init() {
  44. lp_ = 0;
  45. bp_ = 0;
  46. frequency_ = 33 << 7;
  47. resonance_ = 16384;
  48. dirty_ = true;
  49. punch_ = 0;
  50. mode_ = SVF_MODE_BP;
  51. }
  52. void set_frequency(int16_t frequency) {
  53. dirty_ = dirty_ || (frequency_ != frequency);
  54. frequency_ = frequency;
  55. }
  56. void set_resonance(int16_t resonance) {
  57. resonance_ = resonance;
  58. dirty_ = true;
  59. }
  60. void set_punch(uint16_t punch) {
  61. punch_ = (static_cast<uint32_t>(punch) * punch) >> 24;
  62. }
  63. void set_mode(SvfMode mode) {
  64. mode_ = mode;
  65. }
  66. inline int32_t Process(int32_t in) {
  67. if (dirty_) {
  68. f_ = stmlib::Interpolate824(lut_svf_cutoff, frequency_ << 17);
  69. damp_ = stmlib::Interpolate824(lut_svf_damp, resonance_ << 17);
  70. dirty_ = false;
  71. }
  72. int32_t f = f_;
  73. int32_t damp = damp_;
  74. if (punch_) {
  75. int32_t punch_signal = lp_ > 4096 ? lp_ : 2048;
  76. f += ((punch_signal >> 4) * punch_) >> 9;
  77. damp += ((punch_signal - 2048) >> 3);
  78. }
  79. int32_t notch = in - (bp_ * damp >> 15);
  80. lp_ += f * bp_ >> 15;
  81. CLIP(lp_)
  82. int32_t hp = notch - lp_;
  83. bp_ += f * hp >> 15;
  84. CLIP(bp_)
  85. return mode_ == SVF_MODE_BP ? bp_ : (mode_ == SVF_MODE_HP ? hp : lp_);
  86. }
  87. private:
  88. bool dirty_;
  89. int16_t frequency_;
  90. int16_t resonance_;
  91. int32_t punch_;
  92. int32_t f_;
  93. int32_t damp_;
  94. int32_t lp_;
  95. int32_t bp_;
  96. SvfMode mode_;
  97. DISALLOW_COPY_AND_ASSIGN(Svf);
  98. };
  99. } // namespace braids
  100. #endif // BRAIDS_SVF_H_