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.7KB

  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. // Exciter.
  28. #ifndef ELEMENTS_DSP_EXCITER_H_
  29. #define ELEMENTS_DSP_EXCITER_H_
  30. #include "stmlib/stmlib.h"
  31. #include "stmlib/dsp/dsp.h"
  32. #include "stmlib/dsp/filter.h"
  33. #include "stmlib/utils/random.h"
  34. namespace elements {
  35. enum ExciterModel {
  36. EXCITER_MODEL_GRANULAR_SAMPLE_PLAYER,
  37. EXCITER_MODEL_SAMPLE_PLAYER,
  38. EXCITER_MODEL_MALLET,
  39. EXCITER_MODEL_PLECTRUM,
  40. EXCITER_MODEL_PARTICLES,
  41. EXCITER_MODEL_FLOW,
  42. EXCITER_MODEL_NOISE
  43. };
  44. enum ExciterFlags {
  45. EXCITER_FLAG_RISING_EDGE = 1,
  46. EXCITER_FLAG_FALLING_EDGE = 2,
  47. EXCITER_FLAG_GATE = 4
  48. };
  49. class Exciter {
  50. public:
  51. typedef void (Exciter::*ProcessFn)(const uint8_t, float*, size_t);
  52. Exciter() { }
  53. ~Exciter() { }
  54. void Init();
  55. inline void set_signature(float signature) {
  56. signature_ = signature;
  57. }
  58. inline void set_model(ExciterModel model) {
  59. model_ = model;
  60. }
  61. inline void set_parameter(float parameter) {
  62. parameter_ = parameter;
  63. }
  64. inline void set_timbre(float timbre) {
  65. timbre_ = timbre;
  66. }
  67. inline void set_meta(float meta, ExciterModel first, ExciterModel last) {
  68. meta *= static_cast<float>(last - first + 1);
  69. MAKE_INTEGRAL_FRACTIONAL(meta);
  70. model_ = static_cast<ExciterModel>(first + meta_integral);
  71. parameter_ = meta_fractional;
  72. if (model_ > EXCITER_MODEL_NOISE) {
  73. model_ = EXCITER_MODEL_NOISE;
  74. }
  75. }
  76. inline float damping() const {
  77. return damping_;
  78. }
  79. inline const stmlib::Svf& filter() const { return lp_; }
  80. void Process(const uint8_t flags, float* out, size_t n);
  81. void ProcessGranularSamplePlayer(const uint8_t, float*, size_t);
  82. void ProcessSamplePlayer(const uint8_t, float*, size_t);
  83. void ProcessMallet(const uint8_t, float*, size_t);
  84. void ProcessPlectrum(const uint8_t, float*, size_t);
  85. void ProcessParticles(const uint8_t, float*, size_t);
  86. void ProcessFlow(const uint8_t, float*, size_t);
  87. void ProcessNoise(const uint8_t, float*, size_t);
  88. private:
  89. float GetPulseAmplitude(float cutoff);
  90. inline float RandomSample() const {
  91. return static_cast<float>(stmlib::Random::GetWord()) / 4294967296.0f;
  92. }
  93. ExciterModel model_;
  94. float parameter_;
  95. float timbre_;
  96. stmlib::Svf lp_;
  97. float damp_state_;
  98. float particle_state_;
  99. float particle_range_;
  100. float damping_;
  101. float signature_;
  102. uint32_t phase_;
  103. uint32_t delay_;
  104. uint32_t plectrum_delay_;
  105. static ProcessFn fn_table_[];
  106. DISALLOW_COPY_AND_ASSIGN(Exciter);
  107. };
  108. } // namespace elements
  109. #endif // ELEMENTS_DSP_EXCITER_H_