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.

442 lines
15KB

  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. // String synth part.
  28. #include "rings/dsp/string_synth_part.h"
  29. #include "rings/dsp/dsp.h"
  30. namespace rings {
  31. using namespace std;
  32. using namespace stmlib;
  33. void StringSynthPart::Init(uint16_t* reverb_buffer) {
  34. active_group_ = 0;
  35. acquisition_delay_ = 0;
  36. polyphony_ = 1;
  37. fx_type_ = FX_ENSEMBLE;
  38. for (int32_t i = 0; i < kStringSynthVoices; ++i) {
  39. voice_[i].Init();
  40. }
  41. for (int32_t i = 0; i < kMaxStringSynthPolyphony; ++i) {
  42. group_[i].tonic = 0.0f;
  43. group_[i].envelope.Init();
  44. }
  45. for (int32_t i = 0; i < kNumFormants; ++i) {
  46. formant_filter_[i].Init();
  47. }
  48. limiter_.Init();
  49. reverb_.Init(reverb_buffer);
  50. chorus_.Init(reverb_buffer);
  51. ensemble_.Init(reverb_buffer);
  52. note_filter_.Init(
  53. kSampleRate / kMaxBlockSize,
  54. 0.001f, // Lag time with a sharp edge on the V/Oct input or trigger.
  55. 0.005f, // Lag time after the trigger has been received.
  56. 0.050f, // Time to transition from reactive to filtered.
  57. 0.004f); // Prevent a sharp edge to partly leak on the previous voice.
  58. }
  59. const int32_t kRegistrationTableSize = 11;
  60. const float registrations[kRegistrationTableSize][kNumHarmonics * 2] = {
  61. { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f },
  62. { 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
  63. { 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
  64. { 1.0f, 0.1f, 0.0f, 0.0f, 1.0f, 0.0f },
  65. { 1.0f, 0.5f, 1.0f, 0.0f, 1.0f, 0.0f },
  66. { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
  67. { 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f },
  68. { 0.0f, 0.5f, 1.0f, 0.0f, 1.0f, 0.0f },
  69. { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f },
  70. { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f },
  71. { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f },
  72. };
  73. void StringSynthPart::ComputeRegistration(
  74. float gain,
  75. float registration,
  76. float* amplitudes) {
  77. registration *= (kRegistrationTableSize - 1.001f);
  78. MAKE_INTEGRAL_FRACTIONAL(registration);
  79. float total = 0.0f;
  80. for (int32_t i = 0; i < kNumHarmonics * 2; ++i) {
  81. float a = registrations[registration_integral][i];
  82. float b = registrations[registration_integral + 1][i];
  83. amplitudes[i] = a + (b - a) * registration_fractional;
  84. total += amplitudes[i];
  85. }
  86. for (int32_t i = 0; i < kNumHarmonics * 2; ++i) {
  87. amplitudes[i] = gain * amplitudes[i] / total;
  88. }
  89. }
  90. #ifdef BRYAN_CHORDS
  91. // Chord table by Bryan Noll:
  92. // - more compact, leaving room for a bass
  93. // - more frequent note changes between adjacent chords.
  94. // - dropped fifth.
  95. const float chords[kMaxStringSynthPolyphony][kNumChords][kMaxChordSize] = {
  96. {
  97. { -12.0f, -0.01f, 0.0f, 0.01f, 0.02f, 11.99f, 12.0f, 24.0f }, // OCT
  98. { -12.0f, -5.01f, -5.0f, 0.0f, 7.0f, 12.0f, 19.0f, 24.0f }, // 5
  99. { -12.0f, -5.0f, 0.0f, 5.0f, 7.0f, 12.0f, 17.0f, 24.0f }, // sus4
  100. { -12.0f, -5.0f, 0.0f, 0.01f, 3.0f, 12.0f, 19.0f, 24.0f }, // m
  101. { -12.0f, -5.01f, -5.0f, 0.0f, 3.0f, 10.0f, 19.0f, 24.0f }, // m7
  102. { -12.0f, -5.0f, 0.0f, 3.0f, 10.0f, 14.0f, 19.0f, 24.0f }, // m9
  103. { -12.0f, -5.01f, -5.0f, 0.0f, 3.0f, 10.0f, 17.0f, 24.0f }, // m11
  104. { -12.0f, -5.0f, 0.0f, 2.0f, 9.0f, 16.0f, 19.0f, 24.0f }, // 69
  105. { -12.0f, -5.0f, 0.0f, 4.0f, 11.0f, 14.0f, 19.0f, 24.0f }, // M9
  106. { -12.0f, -5.0f, 0.0f, 4.0f, 7.0f, 11.0f, 19.0f, 24.0f }, // M7
  107. { -12.0f, -5.0f, 0.0f, 4.0f, 7.0f, 12.0f, 19.0f, 24.0f }, // M
  108. },
  109. {
  110. { -12.0f, -0.01f, 0.0f, 0.01f, 12.0f, 12.01f }, // OCT
  111. { -12.0f, -5.01f, -5.0f, 0.0f, 7.0f, 12.0f }, // 5
  112. { -12.0f, -5.0f, 0.0f, 5.0f, 7.0f, 12.0f }, // sus4
  113. { -12.0f, -5.0f, 0.0f, 0.01f, 3.0f, 12.0f }, // m
  114. { -12.0f, -5.01f, -5.0f, 0.0f, 3.0f, 10.0f }, // m7
  115. { -12.0f, -5.0f, 0.0f, 3.0f, 10.0f, 14.0f }, // m9
  116. { -12.0f, -5.0f, 0.0f, 3.0f, 10.0f, 17.0f }, // m11
  117. { -12.0f, -5.0f, 0.0f, 2.0f, 9.0f, 16.0f }, // 69
  118. { -12.0f, -5.0f, 0.0f, 4.0f, 11.0f, 14.0f }, // M9
  119. { -12.0f, -5.0f, 0.0f, 4.0f, 7.0f, 11.0f }, // M7
  120. { -12.0f, -5.0f, 0.0f, 4.0f, 7.0f, 12.0f }, // M
  121. },
  122. {
  123. { -12.0f, 0.0f, 0.01f, 12.0f }, // OCT
  124. { -12.0f, 6.99f, 7.0f, 12.0f }, // 5
  125. { -12.0f, 5.0f, 7.0f, 12.0f }, // sus4
  126. { -12.0f, 3.0f, 11.99f, 12.0f }, // m
  127. { -12.0f, 3.0f, 9.99f, 10.0f }, // m7
  128. { -12.0f, 3.0f, 10.0f, 14.0f }, // m9
  129. { -12.0f, 3.0f, 10.0f, 17.0f }, // m11
  130. { -12.0f, 2.0f, 9.0f, 16.0f }, // 69
  131. { -12.0f, 4.0f, 11.0f, 14.0f }, // M9
  132. { -12.0f, 4.0f, 7.0f, 11.0f }, // M7
  133. { -12.0f, 4.0f, 7.0f, 12.0f }, // M
  134. },
  135. {
  136. { 0.0f, 0.01f, 12.0f }, // OCT
  137. { 0.0f, 7.0f, 12.0f }, // 5
  138. { 5.0f, 7.0f, 12.0f }, // sus4
  139. { 0.0f, 3.0f, 12.0f }, // m
  140. { 0.0f, 3.0f, 10.0f }, // m7
  141. { 3.0f, 10.0f, 14.0f }, // m9
  142. { 3.0f, 10.0f, 17.0f }, // m11
  143. { 2.0f, 9.0f, 16.0f }, // 69
  144. { 4.0f, 11.0f, 14.0f }, // M9
  145. { 4.0f, 7.0f, 11.0f }, // M7
  146. { 4.0f, 7.0f, 12.0f }, // M
  147. }
  148. };
  149. #else
  150. // Original chord table:
  151. // - wider, occupies more room in the spectrum.
  152. // - minimum number of note changes between adjacent chords.
  153. // - consistant with the chord table used for the sympathetic strings model.
  154. const float chords[kMaxStringSynthPolyphony][kNumChords][kMaxChordSize] = {
  155. {
  156. { -24.0f, -12.0f, 0.0f, 0.01f, 0.02f, 11.99f, 12.0f, 24.0f },
  157. { -24.0f, -12.0f, 0.0f, 3.0f, 7.0f, 10.0f, 19.0f, 24.0f },
  158. { -24.0f, -12.0f, 0.0f, 3.0f, 7.0f, 12.0f, 19.0f, 24.0f },
  159. { -24.0f, -12.0f, 0.0f, 3.0f, 7.0f, 14.0f, 19.0f, 24.0f },
  160. { -24.0f, -12.0f, 0.0f, 3.0f, 7.0f, 17.0f, 19.0f, 24.0f },
  161. { -24.0f, -12.0f, 0.0f, 6.99f, 7.0f, 18.99f, 19.0f, 24.0f },
  162. { -24.0f, -12.0f, 0.0f, 4.0f, 7.0f, 17.0f, 19.0f, 24.0f },
  163. { -24.0f, -12.0f, 0.0f, 4.0f, 7.0f, 14.0f, 19.0f, 24.0f },
  164. { -24.0f, -12.0f, 0.0f, 4.0f, 7.0f, 12.0f, 19.0f, 24.0f },
  165. { -24.0f, -12.0f, 0.0f, 4.0f, 7.0f, 11.0f, 19.0f, 24.0f },
  166. { -24.0f, -12.0f, 0.0f, 5.0f, 7.0f, 12.0f, 17.0f, 24.0f },
  167. },
  168. {
  169. { -24.0f, -12.0f, 0.0f, 0.01f, 12.0f, 12.01f },
  170. { -24.0f, -12.0f, 0.0f, 3.00f, 7.0f, 10.0f },
  171. { -24.0f, -12.0f, 0.0f, 3.00f, 7.0f, 12.0f },
  172. { -24.0f, -12.0f, 0.0f, 3.00f, 7.0f, 14.0f },
  173. { -24.0f, -12.0f, 0.0f, 3.00f, 7.0f, 17.0f },
  174. { -24.0f, -12.0f, 0.0f, 6.99f, 12.0f, 19.0f },
  175. { -24.0f, -12.0f, 0.0f, 4.00f, 7.0f, 17.0f },
  176. { -24.0f, -12.0f, 0.0f, 4.00f, 7.0f, 14.0f },
  177. { -24.0f, -12.0f, 0.0f, 4.00f, 7.0f, 12.0f },
  178. { -24.0f, -12.0f, 0.0f, 4.00f, 7.0f, 11.0f },
  179. { -24.0f, -12.0f, 0.0f, 5.00f, 7.0f, 12.0f },
  180. },
  181. {
  182. { -12.0f, 0.0f, 0.01f, 12.0f },
  183. { -12.0f, 3.0f, 7.0f, 10.0f },
  184. { -12.0f, 3.0f, 7.0f, 12.0f },
  185. { -12.0f, 3.0f, 7.0f, 14.0f },
  186. { -12.0f, 3.0f, 7.0f, 17.0f },
  187. { -12.0f, 7.0f, 12.0f, 19.0f },
  188. { -12.0f, 4.0f, 7.0f, 17.0f },
  189. { -12.0f, 4.0f, 7.0f, 14.0f },
  190. { -12.0f, 4.0f, 7.0f, 12.0f },
  191. { -12.0f, 4.0f, 7.0f, 11.0f },
  192. { -12.0f, 5.0f, 7.0f, 12.0f },
  193. },
  194. {
  195. { 0.0f, 0.01f, 12.0f },
  196. { 0.0f, 3.0f, 10.0f },
  197. { 0.0f, 3.0f, 7.0f },
  198. { 0.0f, 3.0f, 14.0f },
  199. { 0.0f, 3.0f, 17.0f },
  200. { 0.0f, 7.0f, 19.0f },
  201. { 0.0f, 4.0f, 17.0f },
  202. { 0.0f, 4.0f, 14.0f },
  203. { 0.0f, 4.0f, 7.0f },
  204. { 0.0f, 4.0f, 11.0f },
  205. { 0.0f, 5.0f, 7.0f },
  206. }
  207. };
  208. #endif // BRYAN_CHORDS
  209. void StringSynthPart::ProcessEnvelopes(
  210. float shape,
  211. uint8_t* flags,
  212. float* values) {
  213. float decay = shape;
  214. float attack = 0.0f;
  215. if (shape < 0.5f) {
  216. attack = 0.0f;
  217. } else {
  218. attack = (shape - 0.5f) * 2.0f;
  219. }
  220. // Convert the arbitrary values to actual units.
  221. float period = kSampleRate / kMaxBlockSize;
  222. float attack_time = SemitonesToRatio(attack * 96.0f) * 0.005f * period;
  223. // float decay_time = SemitonesToRatio(decay * 96.0f) * 0.125f * period;
  224. float decay_time = SemitonesToRatio(decay * 84.0f) * 0.180f * period;
  225. float attack_rate = 1.0f / attack_time;
  226. float decay_rate = 1.0f / decay_time;
  227. for (int32_t i = 0; i < polyphony_; ++i) {
  228. float drone = shape < 0.98f ? 0.0f : (shape - 0.98f) * 55.0f;
  229. if (drone >= 1.0f) drone = 1.0f;
  230. group_[i].envelope.set_ad(attack_rate, decay_rate);
  231. float value = group_[i].envelope.Process(flags[i]);
  232. values[i] = value + (1.0f - value) * drone;
  233. }
  234. }
  235. const int32_t kFormantTableSize = 5;
  236. const float formants[kFormantTableSize][kNumFormants] = {
  237. { 700, 1100, 2400 },
  238. { 500, 1300, 1700 },
  239. { 400, 2000, 2500 },
  240. { 600, 800, 2400 },
  241. { 300, 900, 2200 },
  242. };
  243. void StringSynthPart::ProcessFormantFilter(
  244. float vowel,
  245. float shift,
  246. float resonance,
  247. float* out,
  248. float* aux,
  249. size_t size) {
  250. for (size_t i = 0; i < size; ++i) {
  251. filter_in_buffer_[i] = out[i] + aux[i];
  252. }
  253. fill(&out[0], &out[size], 0.0f);
  254. fill(&aux[0], &aux[size], 0.0f);
  255. vowel *= (kFormantTableSize - 1.001f);
  256. MAKE_INTEGRAL_FRACTIONAL(vowel);
  257. for (int32_t i = 0; i < kNumFormants; ++i) {
  258. float a = formants[vowel_integral][i];
  259. float b = formants[vowel_integral + 1][i];
  260. float f = a + (b - a) * vowel_fractional;
  261. f *= shift;
  262. formant_filter_[i].set_f_q<FREQUENCY_DIRTY>(f / kSampleRate, resonance);
  263. formant_filter_[i].Process<FILTER_MODE_BAND_PASS>(
  264. filter_in_buffer_,
  265. filter_out_buffer_,
  266. size);
  267. const float pan = i * 0.3f + 0.2f;
  268. for (size_t j = 0; j < size; ++j) {
  269. out[j] += filter_out_buffer_[j] * pan * 0.5f;
  270. aux[j] += filter_out_buffer_[j] * (1.0f - pan) * 0.5f;
  271. }
  272. }
  273. }
  274. struct ChordNote {
  275. float note;
  276. float amplitude;
  277. };
  278. void StringSynthPart::Process(
  279. const PerformanceState& performance_state,
  280. const Patch& patch,
  281. const float* in,
  282. float* out,
  283. float* aux,
  284. size_t size) {
  285. // Assign note to a voice.
  286. uint8_t envelope_flags[kMaxStringSynthPolyphony];
  287. fill(&envelope_flags[0], &envelope_flags[polyphony_], 0);
  288. note_filter_.Process(performance_state.note, performance_state.strum);
  289. if (performance_state.strum) {
  290. group_[active_group_].tonic = note_filter_.stable_note();
  291. envelope_flags[active_group_] = ENVELOPE_FLAG_FALLING_EDGE;
  292. active_group_ = (active_group_ + 1) % polyphony_;
  293. envelope_flags[active_group_] = ENVELOPE_FLAG_RISING_EDGE;
  294. acquisition_delay_ = 3;
  295. }
  296. if (acquisition_delay_) {
  297. --acquisition_delay_;
  298. } else {
  299. group_[active_group_].tonic = note_filter_.note();
  300. group_[active_group_].chord = performance_state.chord;
  301. group_[active_group_].structure = patch.structure;
  302. envelope_flags[active_group_] |= ENVELOPE_FLAG_GATE;
  303. }
  304. // Process envelopes.
  305. float envelope_values[kMaxStringSynthPolyphony];
  306. ProcessEnvelopes(patch.damping, envelope_flags, envelope_values);
  307. copy(&in[0], &in[size], &aux[0]);
  308. copy(&in[0], &in[size], &out[0]);
  309. int32_t chord_size = min(kStringSynthVoices / polyphony_, kMaxChordSize);
  310. for (int32_t group = 0; group < polyphony_; ++group) {
  311. ChordNote notes[kMaxChordSize];
  312. float harmonics[kNumHarmonics * 2];
  313. ComputeRegistration(
  314. envelope_values[group] * 0.25f,
  315. patch.brightness,
  316. harmonics);
  317. // Note enough polyphony for smooth transition between chords.
  318. for (int32_t i = 0; i < chord_size; ++i) {
  319. float n = chords[polyphony_ - 1][group_[group].chord][i];
  320. notes[i].note = n;
  321. notes[i].amplitude = n >= 0.0f && n <= 17.0f ? 1.0f : 0.7f;
  322. }
  323. for (int32_t chord_note = 0; chord_note < chord_size; ++chord_note) {
  324. float note = 0.0f;
  325. note += group_[group].tonic;
  326. note += performance_state.tonic;
  327. note += performance_state.fm;
  328. note += notes[chord_note].note;
  329. float amplitudes[kNumHarmonics * 2];
  330. for (int32_t i = 0; i < kNumHarmonics * 2; ++i) {
  331. amplitudes[i] = notes[chord_note].amplitude * harmonics[i];
  332. }
  333. // Fold truncated harmonics.
  334. size_t num_harmonics = polyphony_ >= 2 && chord_note < 2
  335. ? kNumHarmonics - 1
  336. : kNumHarmonics;
  337. for (int32_t i = num_harmonics; i < kNumHarmonics; ++i) {
  338. amplitudes[2 * (num_harmonics - 1)] += amplitudes[2 * i];
  339. amplitudes[2 * (num_harmonics - 1) + 1] += amplitudes[2 * i + 1];
  340. }
  341. float frequency = SemitonesToRatio(note - 69.0f) * a3;
  342. voice_[group * chord_size + chord_note].Render(
  343. frequency,
  344. amplitudes,
  345. num_harmonics,
  346. (group + chord_note) & 1 ? out : aux,
  347. size);
  348. }
  349. }
  350. if (clear_fx_) {
  351. reverb_.Clear();
  352. clear_fx_ = false;
  353. }
  354. switch (fx_type_) {
  355. case FX_FORMANT:
  356. case FX_FORMANT_2:
  357. ProcessFormantFilter(
  358. patch.position,
  359. fx_type_ == FX_FORMANT ? 1.0f : 1.1f,
  360. fx_type_ == FX_FORMANT ? 25.0f : 10.0f,
  361. out,
  362. aux,
  363. size);
  364. break;
  365. case FX_CHORUS:
  366. chorus_.set_amount(patch.position);
  367. chorus_.set_depth(0.15f + 0.5f * patch.position);
  368. chorus_.Process(out, aux, size);
  369. break;
  370. case FX_ENSEMBLE:
  371. ensemble_.set_amount(patch.position * (2.0f - patch.position));
  372. ensemble_.set_depth(0.2f + 0.8f * patch.position * patch.position);
  373. ensemble_.Process(out, aux, size);
  374. break;
  375. case FX_REVERB:
  376. case FX_REVERB_2:
  377. reverb_.set_amount(patch.position * 0.5f);
  378. reverb_.set_diffusion(0.625f);
  379. reverb_.set_time(fx_type_ == FX_REVERB
  380. ? (0.5f + 0.49f * patch.position)
  381. : (0.3f + 0.6f * patch.position));
  382. reverb_.set_input_gain(0.2f);
  383. reverb_.set_lp(fx_type_ == FX_REVERB ? 0.3f : 0.6f);
  384. reverb_.Process(out, aux, size);
  385. break;
  386. default:
  387. break;
  388. }
  389. // Prevent main signal cancellation when EVEN gets summed with ODD through
  390. // normalization.
  391. for (size_t i = 0; i < size; ++i) {
  392. aux[i] = -aux[i];
  393. }
  394. limiter_.Process(out, aux, size, 1.0f);
  395. }
  396. } // namespace rings