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.

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