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.

132 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. // Modal synthesis voice.
  28. #ifndef ELEMENTS_DSP_VOICE_H_
  29. #define ELEMENTS_DSP_VOICE_H_
  30. #include "stmlib/stmlib.h"
  31. #include "stmlib/dsp/filter.h"
  32. #include "elements/dsp/dsp.h"
  33. #include "elements/dsp/exciter.h"
  34. #include "elements/dsp/multistage_envelope.h"
  35. #include "elements/dsp/patch.h"
  36. #include "elements/dsp/resonator.h"
  37. #include "elements/dsp/string.h"
  38. #include "elements/dsp/tube.h"
  39. #include "elements/dsp/fx/diffuser.h"
  40. namespace elements {
  41. const size_t kNumStrings = 5;
  42. enum ResonatorModel {
  43. RESONATOR_MODEL_MODAL,
  44. RESONATOR_MODEL_STRING,
  45. RESONATOR_MODEL_STRINGS,
  46. };
  47. class Voice {
  48. public:
  49. Voice() { }
  50. ~Voice() { }
  51. void Init();
  52. void Process(
  53. const Patch& patch,
  54. float frequency,
  55. float strength,
  56. const bool gate_in,
  57. const float* blow_in,
  58. const float* strike_in,
  59. float* raw,
  60. float* center,
  61. float* sides,
  62. size_t size);
  63. // For metering.
  64. inline float exciter_level() const { return exciter_level_; }
  65. void Panic() {
  66. ResetResonator();
  67. }
  68. void set_resonator_model(ResonatorModel resonator_model) {
  69. resonator_model_ = resonator_model;
  70. }
  71. private:
  72. void ResetResonator();
  73. inline uint8_t GetGateFlags(bool gate_in) {
  74. uint8_t flags = 0;
  75. if (gate_in) {
  76. if (!previous_gate_) {
  77. flags |= EXCITER_FLAG_RISING_EDGE;
  78. }
  79. flags |= EXCITER_FLAG_GATE;
  80. } else if (previous_gate_) {
  81. flags = EXCITER_FLAG_FALLING_EDGE;
  82. }
  83. previous_gate_ = gate_in;
  84. return flags;
  85. }
  86. MultistageEnvelope envelope_;
  87. Tube tube_;
  88. Exciter bow_;
  89. Exciter blow_;
  90. Exciter strike_;
  91. Diffuser diffuser_;
  92. Resonator resonator_;
  93. String string_[kNumStrings];
  94. stmlib::DCBlocker dc_blocker_;
  95. float strength_;
  96. float envelope_value_;
  97. float exciter_level_;
  98. float bow_buffer_[kMaxBlockSize];
  99. float bow_strength_buffer_[kMaxBlockSize];
  100. float blow_buffer_[kMaxBlockSize];
  101. float strike_buffer_[kMaxBlockSize];
  102. float external_buffer_[kMaxBlockSize];
  103. float diffuser_buffer_[1024];
  104. bool previous_gate_;
  105. ResonatorModel resonator_model_;
  106. float chord_index_;
  107. DISALLOW_COPY_AND_ASSIGN(Voice);
  108. };
  109. } // namespace elements
  110. #endif // ELEMENTS_DSP_VOICE_H_