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.

135 lines
3.5KB

  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. // Resonator.
  28. #ifndef ELEMENTS_DSP_RESONATOR_H_
  29. #define ELEMENTS_DSP_RESONATOR_H_
  30. #include "stmlib/stmlib.h"
  31. #include <cmath>
  32. #include <algorithm>
  33. #include "elements/dsp/dsp.h"
  34. #include "stmlib/dsp/filter.h"
  35. #include "stmlib/dsp/delay_line.h"
  36. namespace elements {
  37. const size_t kMaxModes = 64;
  38. const size_t kMaxBowedModes = 8;
  39. const size_t kMaxDelayLineSize = 1024;
  40. class Resonator {
  41. public:
  42. Resonator() { }
  43. ~Resonator() { }
  44. void Init();
  45. void Process(
  46. const float* bow_strength,
  47. const float* in,
  48. float* center,
  49. float* sides,
  50. size_t size);
  51. inline void set_frequency(float frequency) {
  52. frequency_ = frequency;
  53. }
  54. inline void set_geometry(float geometry) {
  55. geometry_ = geometry;
  56. }
  57. inline void set_brightness(float brightness) {
  58. brightness_ = brightness;
  59. }
  60. inline void set_damping(float damping) {
  61. damping_ = damping;
  62. }
  63. inline void set_position(float position) {
  64. position_ = position;
  65. }
  66. inline void set_resolution(size_t resolution) {
  67. resolution_ = std::min(resolution, kMaxModes);
  68. }
  69. inline void set_modulation_frequency(float modulation_frequency) {
  70. modulation_frequency_ = modulation_frequency;
  71. }
  72. inline void set_modulation_offset(float modulation_offset) {
  73. modulation_offset_ = modulation_offset;
  74. }
  75. inline float BowTable(float x, float velocity) const {
  76. x = 0.13f * velocity - x;
  77. float bow = x;
  78. bow *= 6.0f;
  79. bow = fabs(bow) + 0.75;
  80. bow *= bow;
  81. bow *= bow;
  82. bow = 0.25f / bow;
  83. if (bow < 0.0025f) bow = 0.0025f;
  84. if (bow > 0.245f) bow = 0.245f;
  85. return x * bow;
  86. }
  87. private:
  88. size_t ComputeFilters();
  89. float frequency_;
  90. float geometry_;
  91. float brightness_;
  92. float position_;
  93. float previous_position_;
  94. float damping_;
  95. float modulation_frequency_;
  96. float modulation_offset_;
  97. float lfo_phase_;
  98. float bow_signal_;
  99. size_t resolution_;
  100. stmlib::Svf f_[kMaxModes];
  101. stmlib::Svf f_bow_[kMaxBowedModes];
  102. stmlib::DelayLine<float, kMaxDelayLineSize> d_bow_[kMaxBowedModes];
  103. size_t clock_divider_;
  104. DISALLOW_COPY_AND_ASSIGN(Resonator);
  105. };
  106. } // namespace elements
  107. #endif // ELEMENTS_DSP_RESONATOR_H_