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.

254 lines
7.9KB

  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. // Group of voices.
  28. #include "elements/dsp/part.h"
  29. #include "elements/resources.h"
  30. namespace elements {
  31. using namespace std;
  32. using namespace stmlib;
  33. void Part::Init(uint16_t* reverb_buffer) {
  34. patch_.exciter_envelope_shape = 1.0f;
  35. patch_.exciter_bow_level = 0.0f;
  36. patch_.exciter_bow_timbre = 0.5f;
  37. patch_.exciter_blow_level = 0.0f;
  38. patch_.exciter_blow_meta = 0.5f;
  39. patch_.exciter_blow_timbre = 0.5f;
  40. patch_.exciter_strike_level = 0.8f;
  41. patch_.exciter_strike_meta = 0.5f;
  42. patch_.exciter_strike_timbre = 0.5f;
  43. patch_.exciter_signature = 0.0f;
  44. patch_.resonator_geometry = 0.2f;
  45. patch_.resonator_brightness = 0.5f;
  46. patch_.resonator_damping = 0.25f;
  47. patch_.resonator_position = 0.3f;
  48. patch_.resonator_modulation_frequency = 0.5f / kSampleRate;
  49. patch_.resonator_modulation_offset = 0.1f;
  50. patch_.reverb_diffusion = 0.625f;
  51. patch_.reverb_lp = 0.7f;
  52. patch_.space = 0.5f;
  53. previous_gate_ = false;
  54. active_voice_ = 0;
  55. fill(&silence_[0], &silence_[kMaxBlockSize], 0.0f);
  56. fill(&note_[0], &note_[kNumVoices], 69.0f);
  57. for (size_t i = 0; i < kNumVoices; ++i) {
  58. voice_[i].Init();
  59. ominous_voice_[i].Init();
  60. }
  61. reverb_.Init(reverb_buffer);
  62. scaled_exciter_level_ = 0.0f;
  63. scaled_resonator_level_ = 0.0f;
  64. resonator_level_ = 0.0f;
  65. bypass_ = false;
  66. resonator_model_ = RESONATOR_MODEL_MODAL;
  67. }
  68. void Part::Seed(uint32_t* seed, size_t size) {
  69. // Scramble all bits from the serial number.
  70. uint32_t signature = 0xf0cacc1a;
  71. for (size_t i = 0; i < size; ++i) {
  72. signature ^= seed[i];
  73. signature = signature * 1664525L + 1013904223L;
  74. }
  75. float x;
  76. x = static_cast<float>(signature & 7) / 8.0f;
  77. signature >>= 3;
  78. patch_.resonator_modulation_frequency = (0.4f + 0.8f * x) / kSampleRate;
  79. x = static_cast<float>(signature & 7) / 8.0f;
  80. signature >>= 3;
  81. patch_.resonator_modulation_offset = 0.05f + 0.1f * x;
  82. x = static_cast<float>(signature & 7) / 8.0f;
  83. signature >>= 3;
  84. patch_.reverb_diffusion = 0.55f + 0.15f * x;
  85. x = static_cast<float>(signature & 7) / 8.0f;
  86. signature >>= 3;
  87. patch_.reverb_lp = 0.7f + 0.2f * x;
  88. x = static_cast<float>(signature & 7) / 8.0f;
  89. signature >>= 3;
  90. patch_.exciter_signature = x;
  91. }
  92. void Part::Process(
  93. const PerformanceState& performance_state,
  94. const float* blow_in,
  95. const float* strike_in,
  96. float* main,
  97. float* aux,
  98. size_t size) {
  99. // Copy inputs to outputs when bypass mode is enabled.
  100. if (bypass_ || panic_) {
  101. if (panic_) {
  102. // If the resonator is blowing up (this has been observed once before
  103. // corrective action was taken), reset the state of the filters to 0
  104. // to prevent the module to freeze with resonators' state blocked at NaN.
  105. for (size_t i = 0; i < kNumVoices; ++i) {
  106. voice_[i].Panic();
  107. }
  108. resonator_level_ = 0.0f;
  109. panic_ = false;
  110. }
  111. copy(&blow_in[0], &blow_in[size], &aux[0]);
  112. copy(&strike_in[0], &strike_in[size], &main[0]);
  113. return;
  114. }
  115. // When a new note is played, cycle to the next voice.
  116. if (performance_state.gate && !previous_gate_) {
  117. ++active_voice_;
  118. if (active_voice_ >= kNumVoices) {
  119. active_voice_ = 0;
  120. }
  121. }
  122. previous_gate_ = performance_state.gate;
  123. note_[active_voice_] = performance_state.note;
  124. fill(&main[0], &main[size], 0.0f);
  125. fill(&aux[0], &aux[size], 0.0f);
  126. // Compute the raw signal gain, stereo spread, and reverb parameters from
  127. // the "space" metaparameter.
  128. float space = patch_.space >= 1.0f ? 1.0f : patch_.space;
  129. float raw_gain = space <= 0.05f ? 1.0f :
  130. (space <= 0.1f ? 2.0f - space * 20.0f : 0.0f);
  131. space = space >= 0.1f ? space - 0.1f : 0.0f;
  132. float spread = space <= 0.7f ? space : 0.7f;
  133. float reverb_amount = space >= 0.5f ? 1.0f * (space - 0.5f) : 0.0f;
  134. float reverb_time = 0.35f + 1.2f * reverb_amount;
  135. // Render each voice.
  136. for (size_t i = 0; i < kNumVoices; ++i) {
  137. float midi_pitch = note_[i] + performance_state.modulation;
  138. if (easter_egg_) {
  139. ominous_voice_[i].Process(
  140. patch_,
  141. midi_pitch,
  142. performance_state.strength,
  143. i == active_voice_ && performance_state.gate,
  144. (i == active_voice_) ? blow_in : silence_,
  145. (i == active_voice_) ? strike_in : silence_,
  146. raw_buffer_,
  147. center_buffer_,
  148. sides_buffer_,
  149. size);
  150. } else {
  151. // Convert the MIDI pitch to a frequency.
  152. int32_t pitch = static_cast<int32_t>((midi_pitch + 48.0f) * 256.0f);
  153. if (pitch < 0) {
  154. pitch = 0;
  155. } else if (pitch >= 65535) {
  156. pitch = 65535;
  157. }
  158. voice_[i].set_resonator_model(resonator_model_);
  159. // Render the voice signal.
  160. voice_[i].Process(
  161. patch_,
  162. lut_midi_to_f_high[pitch >> 8] * lut_midi_to_f_low[pitch & 0xff],
  163. performance_state.strength,
  164. i == active_voice_ && performance_state.gate,
  165. (i == active_voice_) ? blow_in : silence_,
  166. (i == active_voice_) ? strike_in : silence_,
  167. raw_buffer_,
  168. center_buffer_,
  169. sides_buffer_,
  170. size);
  171. }
  172. // Mixdown.
  173. for (size_t j = 0; j < size; ++j) {
  174. float side = sides_buffer_[j] * spread;
  175. float r = center_buffer_[j] - side;
  176. float l = center_buffer_[j] + side;;
  177. main[j] += r;
  178. aux[j] += l + (raw_buffer_[j] - l) * raw_gain;
  179. }
  180. }
  181. // Pre-clipping
  182. if (!easter_egg_) {
  183. for (size_t i = 0; i < size; ++i) {
  184. main[i] = SoftLimit(main[i]);
  185. aux[i] = SoftLimit(aux[i]);
  186. }
  187. }
  188. // Metering.
  189. float exciter_level = voice_[active_voice_].exciter_level();
  190. float resonator_level = resonator_level_;
  191. for (size_t i = 0; i < size; ++i) {
  192. float error = main[i] * main[i] - resonator_level;
  193. resonator_level += error * (error > 0.0f ? 0.05f : 0.0005f);
  194. }
  195. resonator_level_ = resonator_level;
  196. if (resonator_level >= 200.0f) {
  197. panic_ = true;
  198. }
  199. if (easter_egg_) {
  200. float l = (patch_.exciter_blow_level + patch_.exciter_strike_level) * 0.5f;
  201. scaled_exciter_level_ = l * (2.0f - l);
  202. } else {
  203. exciter_level *= 16.0f;
  204. scaled_exciter_level_ = exciter_level > 0.1f ? 1.0f : exciter_level;
  205. }
  206. resonator_level *= 16.0f;
  207. scaled_resonator_level_ = resonator_level < 1.0f ? resonator_level : 1.0f;
  208. // Apply reverb.
  209. reverb_.set_amount(reverb_amount);
  210. reverb_.set_diffusion(patch_.reverb_diffusion);
  211. bool freeze = patch_.space >= 1.75f;
  212. if (freeze) {
  213. reverb_.set_time(1.0f);
  214. reverb_.set_input_gain(0.0f);
  215. reverb_.set_lp(1.0f);
  216. } else {
  217. reverb_.set_time(reverb_time);
  218. reverb_.set_input_gain(0.2f);
  219. reverb_.set_lp(patch_.reverb_lp);
  220. }
  221. reverb_.Process(main, aux, size);
  222. }
  223. } // namespace elements