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.

118 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 PEAKS_DRUMS_SVF_H_
  29. #define PEAKS_DRUMS_SVF_H_
  30. #include "stmlib/stmlib.h"
  31. #include "stmlib/utils/dsp.h"
  32. #include "peaks/resources.h"
  33. namespace peaks {
  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. inline void Init() {
  44. lp_ = 0;
  45. bp_ = 0;
  46. frequency_ = 33 << 7;
  47. resonance_ = 16384;
  48. dirty_ = true;
  49. punch_ = 0;
  50. }
  51. inline void set_frequency(int16_t frequency) {
  52. dirty_ = dirty_ || (frequency_ != frequency);
  53. frequency_ = frequency;
  54. }
  55. inline void set_resonance(int16_t resonance) {
  56. resonance_ = resonance;
  57. dirty_ = true;
  58. }
  59. inline void set_punch(uint16_t punch) {
  60. punch_ = (static_cast<uint32_t>(punch) * punch) >> 24;
  61. }
  62. template<SvfMode mode>
  63. inline int32_t Process(int32_t in) {
  64. if (dirty_) {
  65. f_ = stmlib::Interpolate824(lut_svf_cutoff, frequency_ << 17);
  66. damp_ = stmlib::Interpolate824(lut_svf_damp, resonance_ << 17);
  67. dirty_ = false;
  68. }
  69. int32_t f = f_;
  70. int32_t damp = damp_;
  71. if (punch_) {
  72. int32_t punch_signal = lp_ > 4096 ? lp_ : 2048;
  73. f += ((punch_signal >> 4) * punch_) >> 9;
  74. damp += ((punch_signal - 2048) >> 3);
  75. }
  76. int32_t notch = in - (bp_ * damp >> 15);
  77. lp_ += f * bp_ >> 15;
  78. CLIP(lp_)
  79. int32_t hp = notch - lp_;
  80. bp_ += f * hp >> 15;
  81. CLIP(bp_)
  82. return mode == SVF_MODE_BP ? bp_ : (mode == SVF_MODE_HP ? hp : lp_);
  83. }
  84. private:
  85. bool dirty_;
  86. int16_t frequency_;
  87. int16_t resonance_;
  88. int32_t punch_;
  89. int32_t f_;
  90. int32_t damp_;
  91. int32_t lp_;
  92. int32_t bp_;
  93. SvfMode mode_;
  94. DISALLOW_COPY_AND_ASSIGN(Svf);
  95. };
  96. } // namespace peaks
  97. #endif // PEAKS_DRUMS_SVF_H_