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.

193 lines
5.0KB

  1. // Copyright 2015 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. // Group of voices.
  28. #ifndef RINGS_DSP_PART_H_
  29. #define RINGS_DSP_PART_H_
  30. #include <algorithm>
  31. #include "stmlib/stmlib.h"
  32. #include "stmlib/dsp/cosine_oscillator.h"
  33. #include "stmlib/dsp/delay_line.h"
  34. #include "rings/dsp/dsp.h"
  35. #include "rings/dsp/fm_voice.h"
  36. #include "rings/dsp/fx/reverb.h"
  37. #include "rings/dsp/limiter.h"
  38. #include "rings/dsp/note_filter.h"
  39. #include "rings/dsp/patch.h"
  40. #include "rings/dsp/performance_state.h"
  41. #include "rings/dsp/plucker.h"
  42. #include "rings/dsp/resonator.h"
  43. #include "rings/dsp/string.h"
  44. namespace rings {
  45. enum ResonatorModel {
  46. RESONATOR_MODEL_MODAL,
  47. RESONATOR_MODEL_SYMPATHETIC_STRING,
  48. RESONATOR_MODEL_STRING,
  49. // Bonus!
  50. RESONATOR_MODEL_FM_VOICE,
  51. RESONATOR_MODEL_SYMPATHETIC_STRING_QUANTIZED,
  52. RESONATOR_MODEL_STRING_AND_REVERB,
  53. RESONATOR_MODEL_LAST
  54. };
  55. const int32_t kMaxPolyphony = 4;
  56. const int32_t kNumStrings = kMaxPolyphony * 2;
  57. class Part {
  58. public:
  59. Part() { }
  60. ~Part() { }
  61. void Init(uint16_t* reverb_buffer);
  62. void Process(
  63. const PerformanceState& performance_state,
  64. const Patch& patch,
  65. const float* in,
  66. float* out,
  67. float* aux,
  68. size_t size);
  69. inline bool bypass() const { return bypass_; }
  70. inline void set_bypass(bool bypass) { bypass_ = bypass; }
  71. inline int32_t polyphony() const { return polyphony_; }
  72. inline void set_polyphony(int32_t polyphony) {
  73. int32_t old_polyphony = polyphony_;
  74. polyphony_ = std::min(polyphony, kMaxPolyphony);
  75. for (int32_t i = old_polyphony; i < polyphony_; ++i) {
  76. note_[i] = note_[0] + i * 0.05f;
  77. }
  78. dirty_ = true;
  79. }
  80. inline ResonatorModel model() const { return model_; }
  81. inline void set_model(ResonatorModel model) {
  82. if (model != model_) {
  83. model_ = model;
  84. dirty_ = true;
  85. }
  86. }
  87. private:
  88. void ConfigureResonators();
  89. void RenderModalVoice(
  90. int32_t voice,
  91. const PerformanceState& performance_state,
  92. const Patch& patch,
  93. float frequency,
  94. float filter_cutoff,
  95. size_t size);
  96. void RenderStringVoice(
  97. int32_t voice,
  98. const PerformanceState& performance_state,
  99. const Patch& patch,
  100. float frequency,
  101. float filter_cutoff,
  102. size_t size);
  103. void RenderFMVoice(
  104. int32_t voice,
  105. const PerformanceState& performance_state,
  106. const Patch& patch,
  107. float frequency,
  108. float filter_cutoff,
  109. size_t size);
  110. inline float Squash(float x) const {
  111. if (x < 0.5f) {
  112. x *= 2.0f;
  113. x *= x;
  114. x *= x;
  115. x *= x;
  116. x *= x;
  117. x *= 0.5f;
  118. } else {
  119. x = 2.0f - 2.0f * x;
  120. x *= x;
  121. x *= x;
  122. x *= x;
  123. x *= x;
  124. x = 1.0f - 0.5f * x;
  125. }
  126. return x;
  127. }
  128. void ComputeSympatheticStringsNotes(
  129. float tonic,
  130. float note,
  131. float parameter,
  132. float* destination,
  133. size_t num_strings);
  134. bool bypass_;
  135. bool dirty_;
  136. ResonatorModel model_;
  137. int32_t num_voices_;
  138. int32_t active_voice_;
  139. uint32_t step_counter_;
  140. int32_t polyphony_;
  141. Resonator resonator_[kMaxPolyphony];
  142. String string_[kNumStrings];
  143. stmlib::CosineOscillator lfo_[kNumStrings];
  144. FMVoice fm_voice_[kMaxPolyphony];
  145. stmlib::Svf excitation_filter_[kMaxPolyphony];
  146. stmlib::DCBlocker dc_blocker_[kMaxPolyphony];
  147. Plucker plucker_[kMaxPolyphony];
  148. float note_[kMaxPolyphony];
  149. NoteFilter note_filter_;
  150. float resonator_input_[kMaxBlockSize];
  151. float sympathetic_resonator_input_[kMaxBlockSize];
  152. float noise_burst_buffer_[kMaxBlockSize];
  153. float out_buffer_[kMaxBlockSize];
  154. float aux_buffer_[kMaxBlockSize];
  155. Reverb reverb_;
  156. Limiter limiter_;
  157. static float model_gains_[RESONATOR_MODEL_LAST];
  158. DISALLOW_COPY_AND_ASSIGN(Part);
  159. };
  160. } // namespace rings
  161. #endif // RINGS_DSP_PART_H_