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.

2569 lines
80KB

  1. // Copyright 2012 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. // Oscillator - digital style waveforms.
  28. #include "braids/digital_oscillator.h"
  29. #include <algorithm>
  30. #include <cstdio>
  31. #include "stmlib/utils/dsp.h"
  32. #include "stmlib/utils/random.h"
  33. #include "braids/parameter_interpolation.h"
  34. #include "braids/resources.h"
  35. namespace braids {
  36. using namespace stmlib;
  37. static const uint16_t kHighestNote = 140 * 128;
  38. static const uint16_t kPitchTableStart = 128 * 128;
  39. static const uint16_t kOctave = 12 * 128;
  40. static const uint32_t kFIR4Coefficients[4] = { 10530, 14751, 16384, 14751 };
  41. static const uint32_t kFIR4DcOffset = 28208;
  42. uint32_t DigitalOscillator::ComputePhaseIncrement(int16_t midi_pitch) {
  43. if (midi_pitch >= kPitchTableStart) {
  44. midi_pitch = kPitchTableStart - 1;
  45. }
  46. int32_t ref_pitch = midi_pitch;
  47. ref_pitch -= kPitchTableStart;
  48. size_t num_shifts = 0;
  49. while (ref_pitch < 0) {
  50. ref_pitch += kOctave;
  51. ++num_shifts;
  52. }
  53. uint32_t a = lut_oscillator_increments[ref_pitch >> 4];
  54. uint32_t b = lut_oscillator_increments[(ref_pitch >> 4) + 1];
  55. uint32_t phase_increment = a + \
  56. (static_cast<int32_t>(b - a) * (ref_pitch & 0xf) >> 4);
  57. phase_increment >>= num_shifts;
  58. return phase_increment;
  59. }
  60. uint32_t DigitalOscillator::ComputeDelay(int16_t midi_pitch) {
  61. if (midi_pitch >= kHighestNote - kOctave) {
  62. midi_pitch = kHighestNote - kOctave;
  63. }
  64. int32_t ref_pitch = midi_pitch;
  65. ref_pitch -= kPitchTableStart;
  66. size_t num_shifts = 0;
  67. while (ref_pitch < 0) {
  68. ref_pitch += kOctave;
  69. ++num_shifts;
  70. }
  71. uint32_t a = lut_oscillator_delays[ref_pitch >> 4];
  72. uint32_t b = lut_oscillator_delays[(ref_pitch >> 4) + 1];
  73. uint32_t delay = a + (static_cast<int32_t>(b - a) * (ref_pitch & 0xf) >> 4);
  74. delay >>= 12 - num_shifts;
  75. return delay;
  76. }
  77. void DigitalOscillator::Render(
  78. const uint8_t* sync,
  79. int16_t* buffer,
  80. size_t size) {
  81. // Quantize parameter for FM.
  82. if (shape_ >= OSC_SHAPE_FM &&
  83. shape_ <= OSC_SHAPE_CHAOTIC_FEEDBACK_FM) {
  84. uint16_t integral = parameter_[1] >> 8;
  85. uint16_t fractional = parameter_[1] & 255;
  86. int16_t a = lut_fm_frequency_quantizer[integral];
  87. int16_t b = lut_fm_frequency_quantizer[integral + 1];
  88. parameter_[1] = a + ((b - a) * fractional >> 8);
  89. }
  90. RenderFn fn = fn_table_[shape_];
  91. if (shape_ != previous_shape_) {
  92. Init();
  93. previous_shape_ = shape_;
  94. init_ = true;
  95. }
  96. phase_increment_ = ComputePhaseIncrement(pitch_);
  97. delay_ = ComputeDelay(pitch_);
  98. if (pitch_ > kHighestNote) {
  99. pitch_ = kHighestNote;
  100. } else if (pitch_ < 0) {
  101. pitch_ = 0;
  102. }
  103. (this->*fn)(sync, buffer, size);
  104. }
  105. void DigitalOscillator::RenderTripleRingMod(
  106. const uint8_t* sync,
  107. int16_t* buffer,
  108. size_t size) {
  109. uint32_t phase = phase_ + (1L << 30);
  110. uint32_t increment = phase_increment_;
  111. uint32_t modulator_phase = state_.vow.formant_phase[0];
  112. uint32_t modulator_phase_2 = state_.vow.formant_phase[1];
  113. uint32_t modulator_phase_increment = ComputePhaseIncrement(
  114. pitch_ + ((parameter_[0] - 16384) >> 2)
  115. );
  116. uint32_t modulator_phase_increment_2 = ComputePhaseIncrement(
  117. pitch_ + ((parameter_[1] - 16384) >> 2)
  118. );
  119. while (size--) {
  120. phase += increment;
  121. if (*sync++) {
  122. phase = 0;
  123. modulator_phase = 0;
  124. modulator_phase_2 = 0;
  125. }
  126. modulator_phase += modulator_phase_increment;
  127. modulator_phase_2 += modulator_phase_increment_2;
  128. int16_t result = Interpolate824(wav_sine, phase);
  129. result = result * Interpolate824(wav_sine, modulator_phase) >> 16;
  130. result = result * Interpolate824(wav_sine, modulator_phase_2) >> 16;
  131. result = Interpolate88(ws_moderate_overdrive, result + 32768);
  132. *buffer++ = result;
  133. }
  134. phase_ = phase - (1L << 30);
  135. state_.vow.formant_phase[0] = modulator_phase;
  136. state_.vow.formant_phase[1] = modulator_phase_2;
  137. }
  138. void DigitalOscillator::RenderSawSwarm(
  139. const uint8_t* sync,
  140. int16_t* buffer,
  141. size_t size) {
  142. int32_t detune = parameter_[0] + 1024;
  143. detune = (detune * detune) >> 9;
  144. uint32_t increments[7];
  145. for (int16_t i = 0; i < 7; ++i) {
  146. int32_t saw_detune = detune * (i - 3);
  147. int32_t detune_integral = saw_detune >> 16;
  148. int32_t detune_fractional = saw_detune & 0xffff;
  149. int32_t increment_a = ComputePhaseIncrement(pitch_ + detune_integral);
  150. int32_t increment_b = ComputePhaseIncrement(pitch_ + detune_integral + 1);
  151. increments[i] = increment_a + \
  152. (((increment_b - increment_a) * detune_fractional) >> 16);
  153. }
  154. if (strike_) {
  155. for (size_t i = 0; i < 6; ++i) {
  156. state_.saw.phase[i] = Random::GetWord();
  157. }
  158. strike_ = false;
  159. }
  160. int32_t hp_cutoff = pitch_;
  161. if (parameter_[1] < 10922) {
  162. hp_cutoff += ((parameter_[1] - 10922) * 24) >> 5;
  163. } else {
  164. hp_cutoff += ((parameter_[1] - 10922) * 12) >> 5;
  165. }
  166. if (hp_cutoff < 0) {
  167. hp_cutoff = 0;
  168. } else if (hp_cutoff > 32767) {
  169. hp_cutoff = 32767;
  170. }
  171. int32_t f = Interpolate824(lut_svf_cutoff, hp_cutoff << 17);
  172. int32_t damp = lut_svf_damp[0];
  173. int32_t bp = state_.saw.bp;
  174. int32_t lp = state_.saw.lp;
  175. while (size--) {
  176. if (*sync++) {
  177. for (size_t i = 0; i < 6; ++i) {
  178. state_.saw.phase[i] = 0;
  179. }
  180. }
  181. int32_t notch, hp, sample;
  182. phase_ += increments[0];
  183. state_.saw.phase[0] += increments[1];
  184. state_.saw.phase[1] += increments[2];
  185. state_.saw.phase[2] += increments[3];
  186. state_.saw.phase[3] += increments[4];
  187. state_.saw.phase[4] += increments[5];
  188. state_.saw.phase[5] += increments[6];
  189. // Compute a sample.
  190. sample = -28672;
  191. sample += phase_ >> 19;
  192. sample += state_.saw.phase[0] >> 19;
  193. sample += state_.saw.phase[1] >> 19;
  194. sample += state_.saw.phase[2] >> 19;
  195. sample += state_.saw.phase[3] >> 19;
  196. sample += state_.saw.phase[4] >> 19;
  197. sample += state_.saw.phase[5] >> 19;
  198. sample = Interpolate88(ws_moderate_overdrive, sample + 32768);
  199. notch = sample - (bp * damp >> 15);
  200. lp += f * bp >> 15;
  201. CLIP(lp)
  202. hp = notch - lp;
  203. bp += f * hp >> 15;
  204. int32_t result = hp;
  205. CLIP(result)
  206. *buffer++ = result;
  207. }
  208. state_.saw.lp = lp;
  209. state_.saw.bp = bp;
  210. }
  211. void DigitalOscillator::RenderComb(
  212. const uint8_t* sync,
  213. int16_t* buffer,
  214. size_t size) {
  215. // Filter the delay time to avoid clicks/glitches.
  216. int32_t pitch = pitch_ + ((parameter_[0] - 16384) >> 1);
  217. int32_t filtered_pitch = state_.ffm.previous_sample;
  218. filtered_pitch = (15 * filtered_pitch + pitch) >> 4;
  219. state_.ffm.previous_sample = filtered_pitch;
  220. int16_t* dl = delay_lines_.comb;
  221. uint32_t delay = ComputeDelay(filtered_pitch);
  222. if (delay > (kCombDelayLength << 16)) {
  223. delay = kCombDelayLength << 16;
  224. }
  225. uint32_t delay_integral = delay >> 16;
  226. int32_t delay_fractional = delay & 0xffff;
  227. // Warp the resonance curve to have a more precise adjustment in the extrema.
  228. int16_t resonance = (parameter_[1] << 1) - 32768;
  229. resonance = Interpolate88(ws_moderate_overdrive, resonance + 32768);
  230. uint32_t delay_ptr = phase_;
  231. delay_ptr = delay_ptr % kCombDelayLength;
  232. while (size--) {
  233. int32_t in = *buffer;
  234. uint32_t offset = delay_ptr + 2 * kCombDelayLength - delay_integral;
  235. int32_t a = dl[offset % kCombDelayLength];
  236. int32_t b = dl[(offset - 1) % kCombDelayLength];
  237. int32_t delayed_sample = a + (((b - a) * (delay_fractional >> 1)) >> 15);
  238. int32_t feedback = (delayed_sample * resonance >> 15) + (in >> 1);
  239. CLIP(feedback)
  240. dl[delay_ptr] = feedback;
  241. int32_t out = (in + (delayed_sample << 1)) >> 1;
  242. CLIP(out)
  243. *buffer++ = out;
  244. delay_ptr = (delay_ptr + 1) % kCombDelayLength;
  245. }
  246. phase_ = delay_ptr;
  247. }
  248. void DigitalOscillator::RenderToy(
  249. const uint8_t* sync,
  250. int16_t* buffer,
  251. size_t size) {
  252. // 4 times oversampling.
  253. phase_increment_ >>= 2;
  254. uint32_t phase_increment = phase_increment_;
  255. uint32_t phase = phase_;
  256. uint16_t decimation_counter = state_.toy.decimation_counter;
  257. uint16_t decimation_count = 512 - (parameter_[0] >> 6);
  258. uint8_t held_sample = state_.toy.held_sample;
  259. while (size--) {
  260. int32_t filtered_sample = 0;
  261. if (*sync++) {
  262. phase = 0;
  263. }
  264. for (size_t tap = 0; tap < 4; ++tap) {
  265. phase += phase_increment;
  266. if (decimation_counter >= decimation_count) {
  267. uint8_t x = parameter_[1] >> 8;
  268. held_sample = (((phase >> 24) ^ (x << 1)) & (~x)) + (x >> 1);
  269. decimation_counter = 0;
  270. }
  271. filtered_sample += kFIR4Coefficients[tap] * held_sample;
  272. ++decimation_counter;
  273. }
  274. *buffer++ = (filtered_sample >> 8) - kFIR4DcOffset;
  275. }
  276. state_.toy.held_sample = held_sample;
  277. state_.toy.decimation_counter = decimation_counter;
  278. phase_ = phase;
  279. }
  280. const uint32_t kPhaseReset[] = {
  281. 0,
  282. 0x80000000,
  283. 0x40000000,
  284. 0x80000000
  285. };
  286. void DigitalOscillator::RenderDigitalFilter(
  287. const uint8_t* sync,
  288. int16_t* buffer,
  289. size_t size) {
  290. int16_t shifted_pitch = pitch_ + ((parameter_[0] - 2048) >> 1);
  291. if (shifted_pitch > 16383) {
  292. shifted_pitch = 16383;
  293. }
  294. uint32_t modulator_phase = state_.res.modulator_phase;
  295. uint32_t square_modulator_phase = state_.res.square_modulator_phase;
  296. int32_t square_integrator = state_.res.integrator;
  297. uint8_t filter_type = shape_ - OSC_SHAPE_DIGITAL_FILTER_LP;
  298. uint32_t modulator_phase_increment = state_.res.modulator_phase_increment;
  299. uint32_t target_increment = ComputePhaseIncrement(shifted_pitch);
  300. uint32_t modulator_phase_increment_increment =
  301. modulator_phase_increment < target_increment
  302. ? (target_increment - modulator_phase_increment) / size
  303. : ~((modulator_phase_increment - target_increment) / size);
  304. while (size--) {
  305. phase_ += phase_increment_;
  306. modulator_phase_increment += modulator_phase_increment_increment;
  307. modulator_phase += modulator_phase_increment;
  308. uint16_t integrator_gain = (modulator_phase_increment >> 14);
  309. if (*sync++) {
  310. state_.res.polarity = 1;
  311. phase_ = 0;
  312. modulator_phase = 0;
  313. square_modulator_phase = 0;
  314. square_integrator = 0;
  315. }
  316. square_modulator_phase += modulator_phase_increment;
  317. if (phase_ < phase_increment_) {
  318. modulator_phase = kPhaseReset[filter_type];
  319. }
  320. if ((phase_ << 1) < (phase_increment_ << 1)) {
  321. state_.res.polarity = !state_.res.polarity;
  322. square_modulator_phase = kPhaseReset[(filter_type & 1) + 2];
  323. }
  324. int32_t carrier = Interpolate824(wav_sine, modulator_phase);
  325. int32_t square_carrier = Interpolate824(wav_sine, square_modulator_phase);
  326. uint16_t saw = ~(phase_ >> 16);
  327. uint16_t double_saw = ~(phase_ >> 15);
  328. uint16_t triangle = (phase_ >> 15) ^ (phase_ & 0x80000000 ? 0xffff : 0x0000);
  329. uint16_t window = parameter_[1] < 16384 ? saw : triangle;
  330. int32_t pulse = (square_carrier * double_saw) >> 16;
  331. if (state_.res.polarity) {
  332. pulse = -pulse;
  333. }
  334. square_integrator += (pulse * integrator_gain) >> 16;
  335. CLIP(square_integrator)
  336. int16_t saw_tri_signal;
  337. int16_t square_signal;
  338. if (filter_type & 2) {
  339. saw_tri_signal = (carrier * window) >> 16;
  340. square_signal = pulse;
  341. } else {
  342. saw_tri_signal = (window * (carrier + 32768) >> 16) - 32768;
  343. square_signal = square_integrator;
  344. if (filter_type == 1) {
  345. square_signal = (pulse + square_integrator) >> 1;
  346. }
  347. }
  348. uint16_t balance = (parameter_[1] < 16384 ?
  349. parameter_[1] : ~parameter_[1]) << 2;
  350. *buffer++ = Mix(saw_tri_signal, square_signal, balance);
  351. }
  352. state_.res.modulator_phase = modulator_phase;
  353. state_.res.square_modulator_phase = square_modulator_phase;
  354. state_.res.integrator = square_integrator;
  355. state_.res.modulator_phase_increment = modulator_phase_increment;
  356. }
  357. void DigitalOscillator::RenderVosim(
  358. const uint8_t* sync,
  359. int16_t* buffer,
  360. size_t size) {
  361. for (size_t i = 0; i < 2; ++i) {
  362. state_.vow.formant_increment[i] = ComputePhaseIncrement(parameter_[i] >> 1);
  363. }
  364. while (size--) {
  365. phase_ += phase_increment_;
  366. if (*sync++) {
  367. phase_ = 0;
  368. }
  369. int32_t sample = 16384 + 8192;
  370. state_.vow.formant_phase[0] += state_.vow.formant_increment[0];
  371. sample += Interpolate824(wav_sine, state_.vow.formant_phase[0]) >> 1;
  372. state_.vow.formant_phase[1] += state_.vow.formant_increment[1];
  373. sample += Interpolate824(wav_sine, state_.vow.formant_phase[1]) >> 2;
  374. sample = sample * (Interpolate824(lut_bell, phase_) >> 1) >> 15;
  375. if (phase_ < phase_increment_) {
  376. state_.vow.formant_phase[0] = 0;
  377. state_.vow.formant_phase[1] = 0;
  378. sample = 0;
  379. }
  380. sample -= 16384 + 8192;
  381. *buffer++ = sample;
  382. }
  383. }
  384. struct PhonemeDefinition {
  385. uint8_t formant_frequency[3];
  386. uint8_t formant_amplitude[3];
  387. };
  388. static const PhonemeDefinition vowels_data[9] = {
  389. { { 27, 40, 89 }, { 15, 13, 1 } },
  390. { { 18, 51, 62 }, { 13, 12, 6 } },
  391. { { 15, 69, 93 }, { 14, 12, 7 } },
  392. { { 10, 84, 110 }, { 13, 10, 8 } },
  393. { { 23, 44, 87 }, { 15, 12, 1 } },
  394. { { 13, 29, 80 }, { 13, 8, 0 } },
  395. { { 6, 46, 81 }, { 12, 3, 0 } },
  396. { { 9, 51, 95 }, { 15, 3, 0 } },
  397. { { 6, 73, 99 }, { 7, 3, 14 } }
  398. };
  399. static const PhonemeDefinition consonant_data[8] = {
  400. { { 6, 54, 121 }, { 9, 9, 0 } },
  401. { { 18, 50, 51 }, { 12, 10, 5 } },
  402. { { 11, 24, 70 }, { 13, 8, 0 } },
  403. { { 15, 69, 74 }, { 14, 12, 7 } },
  404. { { 16, 37, 111 }, { 14, 8, 1 } },
  405. { { 18, 51, 62 }, { 14, 12, 6 } },
  406. { { 6, 26, 81 }, { 5, 5, 5 } },
  407. { { 6, 73, 99 }, { 7, 10, 14 } },
  408. };
  409. void DigitalOscillator::RenderVowel(
  410. const uint8_t* sync,
  411. int16_t* buffer,
  412. size_t size) {
  413. size_t vowel_index = parameter_[0] >> 12;
  414. uint16_t balance = parameter_[0] & 0x0fff;
  415. uint16_t formant_shift = (200 + (parameter_[1] >> 6));
  416. if (strike_) {
  417. strike_ = false;
  418. state_.vow.consonant_frames = 160;
  419. uint16_t index = (Random::GetSample() + 1) & 7;
  420. for (size_t i = 0; i < 3; ++i) {
  421. state_.vow.formant_increment[i] = \
  422. static_cast<uint32_t>(consonant_data[index].formant_frequency[i]) * \
  423. 0x1000 * formant_shift;
  424. state_.vow.formant_amplitude[i] = consonant_data[index].formant_amplitude[i];
  425. }
  426. state_.vow.noise = index >= 6 ? 4095 : 0;
  427. }
  428. if (state_.vow.consonant_frames) {
  429. --state_.vow.consonant_frames;
  430. } else {
  431. for (size_t i = 0; i < 3; ++i) {
  432. state_.vow.formant_increment[i] =
  433. (vowels_data[vowel_index].formant_frequency[i] * (0x1000 - balance) + \
  434. vowels_data[vowel_index + 1].formant_frequency[i] * balance) * \
  435. formant_shift;
  436. state_.vow.formant_amplitude[i] =
  437. (vowels_data[vowel_index].formant_amplitude[i] * (0x1000 - balance) + \
  438. vowels_data[vowel_index + 1].formant_amplitude[i] * balance) >> 12;
  439. }
  440. state_.vow.noise = 0;
  441. }
  442. int32_t noise = state_.vow.noise;
  443. while (size--) {
  444. phase_ += phase_increment_;
  445. size_t phaselet;
  446. int16_t sample = 0;
  447. state_.vow.formant_phase[0] += state_.vow.formant_increment[0];
  448. phaselet = (state_.vow.formant_phase[0] >> 24) & 0xf0;
  449. sample += wav_formant_sine[phaselet | state_.vow.formant_amplitude[0]];
  450. state_.vow.formant_phase[1] += state_.vow.formant_increment[1];
  451. phaselet = (state_.vow.formant_phase[1] >> 24) & 0xf0;
  452. sample += wav_formant_sine[phaselet | state_.vow.formant_amplitude[1]];
  453. state_.vow.formant_phase[2] += state_.vow.formant_increment[2];
  454. phaselet = (state_.vow.formant_phase[2] >> 24) & 0xf0;
  455. sample += wav_formant_square[phaselet | state_.vow.formant_amplitude[2]];
  456. sample *= 255 - (phase_ >> 24);
  457. int32_t phase_noise = Random::GetSample() * noise;
  458. if ((phase_ + phase_noise) < phase_increment_) {
  459. state_.vow.formant_phase[0] = 0;
  460. state_.vow.formant_phase[1] = 0;
  461. state_.vow.formant_phase[2] = 0;
  462. sample = 0;
  463. }
  464. sample = Interpolate88(ws_moderate_overdrive, sample + 32768);
  465. *buffer++ = sample;
  466. }
  467. }
  468. static const int16_t formant_f_data[kNumFormants][kNumFormants][kNumFormants] = {
  469. // bass
  470. {
  471. { 9519, 10738, 12448, 12636, 12892 }, // a
  472. { 8620, 11720, 12591, 12932, 13158 }, // e
  473. { 7579, 11891, 12768, 13122, 13323 }, // i
  474. { 8620, 10013, 12591, 12768, 13010 }, // o
  475. { 8324, 9519, 12591, 12831, 13048 } // u
  476. },
  477. // tenor
  478. {
  479. { 9696, 10821, 12810, 13010, 13263 }, // a
  480. { 8620, 11827, 12768, 13228, 13477 }, // e
  481. { 7908, 12038, 12932, 13263, 13452 }, // i
  482. { 8620, 10156, 12768, 12932, 13085 }, // o
  483. { 8324, 9519, 12852, 13010, 13296 } // u
  484. },
  485. // countertenor
  486. {
  487. { 9730, 10902, 12892, 13085, 13330 }, // a
  488. { 8832, 11953, 12852, 13085, 13296 }, // e
  489. { 7749, 12014, 13010, 13330, 13483 }, // i
  490. { 8781, 10211, 12852, 13085, 13296 }, // o
  491. { 8448, 9627, 12892, 13085, 13363 } // u
  492. },
  493. // alto
  494. {
  495. { 10156, 10960, 12932, 13427, 14195 }, // a
  496. { 8620, 11692, 12852, 13296, 14195 }, // e
  497. { 8324, 11827, 12852, 13550, 14195 }, // i
  498. { 8881, 10156, 12956, 13427, 14195 }, // o
  499. { 8160, 9860, 12708, 13427, 14195 } // u
  500. },
  501. // soprano
  502. {
  503. { 10156, 10960, 13010, 13667, 14195 }, // a
  504. { 8324, 12187, 12932, 13489, 14195 }, // e
  505. { 7749, 12337, 13048, 13667, 14195 }, // i
  506. { 8881, 10156, 12956, 13609, 14195 }, // o
  507. { 8160, 9860, 12852, 13609, 14195 } // u
  508. }
  509. };
  510. static const int16_t formant_a_data[kNumFormants][kNumFormants][kNumFormants] = {
  511. // bass
  512. {
  513. { 16384, 7318, 5813, 5813, 1638 }, // a
  514. { 16384, 4115, 5813, 4115, 2062 }, // e
  515. { 16384, 518, 2596, 1301, 652 }, // i
  516. { 16384, 4617, 1460, 1638, 163 }, // o
  517. { 16384, 1638, 411, 652, 259 } // u
  518. },
  519. // tenor
  520. {
  521. { 16384, 8211, 7318, 6522, 1301 }, // a
  522. { 16384, 3269, 4115, 3269, 1638 }, // e
  523. { 16384, 2913, 2062, 1638, 518 }, // i
  524. { 16384, 5181, 4115, 4115, 821 }, // o
  525. { 16384, 1638, 2314, 3269, 821 } // u
  526. },
  527. // countertenor
  528. {
  529. { 16384, 8211, 1159, 1033, 206 }, // a
  530. { 16384, 3269, 2062, 1638, 1638 }, // e
  531. { 16384, 1033, 1033, 259, 259 }, // i
  532. { 16384, 5181, 821, 1301, 326 }, // o
  533. { 16384, 1638, 1159, 518, 326 } // u
  534. },
  535. // alto
  536. {
  537. { 16384, 10337, 1638, 259, 16 }, // a
  538. { 16384, 1033, 518, 291, 16 }, // e
  539. { 16384, 1638, 518, 259, 16 }, // i
  540. { 16384, 5813, 2596, 652, 29 }, // o
  541. { 16384, 4115, 518, 163, 10 } // u
  542. },
  543. // soprano
  544. {
  545. { 16384, 8211, 411, 1638, 51 }, // a
  546. { 16384, 1638, 2913, 163, 25 }, // e
  547. { 16384, 4115, 821, 821, 103 }, // i
  548. { 16384, 4617, 1301, 1301, 51 }, // o
  549. { 16384, 2596, 291, 163, 16 } // u
  550. }
  551. };
  552. int16_t DigitalOscillator::InterpolateFormantParameter(
  553. const int16_t table[][kNumFormants][kNumFormants],
  554. int16_t x,
  555. int16_t y,
  556. uint8_t formant) {
  557. uint16_t x_index = x >> 13;
  558. uint16_t x_mix = x << 3;
  559. uint16_t y_index = y >> 13;
  560. uint16_t y_mix = y << 3;
  561. int16_t a = table[x_index][y_index][formant];
  562. int16_t b = table[x_index + 1][y_index][formant];
  563. int16_t c = table[x_index][y_index + 1][formant];
  564. int16_t d = table[x_index + 1][y_index + 1][formant];
  565. a = a + ((b - a) * x_mix >> 16);
  566. c = c + ((d - c) * x_mix >> 16);
  567. return a + ((c - a) * y_mix >> 16);
  568. }
  569. void DigitalOscillator::RenderVowelFof(
  570. const uint8_t* sync,
  571. int16_t* buffer,
  572. size_t size) {
  573. // The original implementation used FOF but we live in the future and it's
  574. // less computationally expensive to render a proper bank of 5 SVF.
  575. int16_t amplitudes[kNumFormants];
  576. int32_t svf_lp[kNumFormants];
  577. int32_t svf_bp[kNumFormants];
  578. int16_t svf_f[kNumFormants];
  579. for (size_t i = 0; i < kNumFormants; ++i) {
  580. int32_t frequency = InterpolateFormantParameter(
  581. formant_f_data,
  582. parameter_[1],
  583. parameter_[0],
  584. i) + (12 << 7);
  585. svf_f[i] = Interpolate824(lut_svf_cutoff, frequency << 17);
  586. amplitudes[i] = InterpolateFormantParameter(
  587. formant_a_data,
  588. parameter_[1],
  589. parameter_[0],
  590. i);
  591. if (init_) {
  592. svf_lp[i] = 0;
  593. svf_bp[i] = 0;
  594. } else {
  595. svf_lp[i] = state_.fof.svf_lp[i];
  596. svf_bp[i] = state_.fof.svf_bp[i];
  597. }
  598. }
  599. if (init_) {
  600. init_ = false;
  601. }
  602. uint32_t phase = phase_;
  603. int32_t previous_sample = state_.fof.previous_sample;
  604. int32_t next_saw_sample = state_.fof.next_saw_sample;
  605. uint32_t increment = phase_increment_ << 1;
  606. while (size) {
  607. int32_t this_saw_sample = next_saw_sample;
  608. next_saw_sample = 0;
  609. phase += increment;
  610. if (phase < increment) {
  611. uint32_t t = phase / (increment >> 16);
  612. if (t > 65535) {
  613. t = 65535;
  614. }
  615. this_saw_sample -= static_cast<int32_t>(t * t >> 18);
  616. t = 65535 - t;
  617. next_saw_sample -= -static_cast<int32_t>(t * t >> 18);
  618. }
  619. next_saw_sample += phase >> 17;
  620. int32_t in = this_saw_sample;
  621. int32_t out = 0;
  622. for (int32_t i = 0; i < 5; ++i) {
  623. int32_t notch = in - (svf_bp[i] >> 6);
  624. svf_lp[i] += svf_f[i] * svf_bp[i] >> 15;
  625. CLIP(svf_lp[i])
  626. int32_t hp = notch - svf_lp[i];
  627. svf_bp[i] += svf_f[i] * hp >> 15;
  628. CLIP(svf_bp[i])
  629. out += svf_bp[i] * amplitudes[0] >> 17;
  630. }
  631. CLIP(out);
  632. *buffer++ = (out + previous_sample) >> 1;
  633. *buffer++ = out;
  634. previous_sample = out;
  635. size -= 2;
  636. }
  637. phase_ = phase;
  638. state_.fof.next_saw_sample = next_saw_sample;
  639. state_.fof.previous_sample = previous_sample;
  640. for (size_t i = 0; i < kNumFormants; ++i) {
  641. state_.fof.svf_lp[i] = svf_lp[i];
  642. state_.fof.svf_bp[i] = svf_bp[i];
  643. }
  644. }
  645. void DigitalOscillator::RenderFm(
  646. const uint8_t* sync,
  647. int16_t* buffer,
  648. size_t size) {
  649. uint32_t modulator_phase = state_.modulator_phase;
  650. uint32_t modulator_phase_increment = ComputePhaseIncrement(
  651. (12 << 7) + pitch_ + ((parameter_[1] - 16384) >> 1)) >> 1;
  652. BEGIN_INTERPOLATE_PARAMETER_0
  653. while (size--) {
  654. INTERPOLATE_PARAMETER_0
  655. phase_ += phase_increment_;
  656. if (*sync++) {
  657. phase_ = modulator_phase = 0;
  658. }
  659. modulator_phase += modulator_phase_increment;
  660. uint32_t pm = (
  661. Interpolate824(wav_sine, modulator_phase) * parameter_0) << 2;
  662. *buffer++ = Interpolate824(wav_sine, phase_ + pm);
  663. }
  664. END_INTERPOLATE_PARAMETER_0
  665. previous_parameter_[0] = parameter_[0];
  666. state_.modulator_phase = modulator_phase;
  667. }
  668. void DigitalOscillator::RenderFeedbackFm(
  669. const uint8_t* sync,
  670. int16_t* buffer,
  671. size_t size) {
  672. int16_t previous_sample = state_.ffm.previous_sample;
  673. uint32_t modulator_phase = state_.ffm.modulator_phase;
  674. int32_t attenuation = pitch_ - (72 << 7) + ((parameter_[1] - 16384) >> 1);
  675. attenuation = 32767 - attenuation * 4;
  676. if (attenuation < 0) attenuation = 0;
  677. if (attenuation > 32767) attenuation = 32767;
  678. uint32_t modulator_phase_increment = ComputePhaseIncrement(
  679. (12 << 7) + pitch_ + ((parameter_[1] - 16384) >> 1)) >> 1;
  680. BEGIN_INTERPOLATE_PARAMETER_0
  681. while (size--) {
  682. INTERPOLATE_PARAMETER_0
  683. phase_ += phase_increment_;
  684. if (*sync++) {
  685. phase_ = modulator_phase = 0;
  686. }
  687. modulator_phase += modulator_phase_increment;
  688. int32_t pm;
  689. int32_t p = parameter_0 * attenuation >> 15;
  690. pm = previous_sample << 14;
  691. pm = (
  692. Interpolate824(wav_sine, modulator_phase + pm) * p) << 1;
  693. previous_sample = Interpolate824(wav_sine, phase_ + pm);
  694. *buffer++ = previous_sample;
  695. }
  696. END_INTERPOLATE_PARAMETER_0
  697. state_.ffm.previous_sample = previous_sample;
  698. state_.ffm.modulator_phase = modulator_phase;
  699. }
  700. void DigitalOscillator::RenderChaoticFeedbackFm(
  701. const uint8_t* sync,
  702. int16_t* buffer,
  703. size_t size) {
  704. uint32_t modulator_phase_increment = ComputePhaseIncrement(
  705. (12 << 7) + pitch_ + ((parameter_[1] - 16384) >> 1)) >> 1;
  706. int16_t previous_sample = state_.ffm.previous_sample;
  707. uint32_t modulator_phase = state_.ffm.modulator_phase;
  708. BEGIN_INTERPOLATE_PARAMETER_0
  709. while (size--) {
  710. INTERPOLATE_PARAMETER_0
  711. phase_ += phase_increment_;
  712. if (*sync++) {
  713. phase_ = modulator_phase = 0;
  714. }
  715. int32_t pm;
  716. pm = (Interpolate824(wav_sine, modulator_phase) * parameter_0) << 1;
  717. previous_sample = Interpolate824(wav_sine, phase_ + pm);
  718. *buffer++ = previous_sample;
  719. modulator_phase += (modulator_phase_increment >> 8) * \
  720. (129 + (previous_sample >> 9));
  721. }
  722. END_INTERPOLATE_PARAMETER_0
  723. state_.ffm.previous_sample = previous_sample;
  724. state_.ffm.modulator_phase = modulator_phase;
  725. }
  726. static const int16_t kBellPartials[] = {
  727. -1284, -1283, -184, -183, 385, 1175, 1536, 2233, 2434, 2934, 3110
  728. };
  729. static const int16_t kBellPartialAmplitudes[] = {
  730. 8192, 5488, 8192, 14745, 21872, 13680, 11960, 10895, 10895, 6144, 10895
  731. };
  732. static const uint16_t kBellPartialDecayLong[] = {
  733. 65533, 65533, 65533, 65532, 65531, 65531, 65530, 65529, 65527, 65523, 65519
  734. };
  735. static const uint16_t kBellPartialDecayShort[] = {
  736. 65308, 65283, 65186, 65123, 64839, 64889, 64632, 64409, 64038, 63302, 62575
  737. };
  738. static const int16_t kDrumPartials[] = {
  739. 0, 0, 1041, 1747, 1846, 3072
  740. };
  741. static const int16_t kDrumPartialAmplitude[] = {
  742. 16986, 2654, 3981, 5308, 3981, 2985
  743. };
  744. static const uint16_t kDrumPartialDecayLong[] = {
  745. 65533, 65531, 65531, 65531, 65531, 65516
  746. };
  747. static const uint16_t kDrumPartialDecayShort[] = {
  748. 65083, 64715, 64715, 64715, 64715, 62312
  749. };
  750. void DigitalOscillator::RenderStruckBell(
  751. const uint8_t* sync,
  752. int16_t* buffer,
  753. size_t size) {
  754. // To save some CPU cycles, do not refresh the frequency of all partials at
  755. // the same time. This create a kind of "arpeggiation" with high frequency
  756. // CV though...
  757. size_t first_partial = state_.add.current_partial;
  758. size_t last_partial = std::min(
  759. state_.add.current_partial + 3,
  760. kNumBellPartials);
  761. state_.add.current_partial = (first_partial + 3) % kNumBellPartials;
  762. if (strike_) {
  763. for (size_t i = 0; i < kNumBellPartials; ++i) {
  764. state_.add.partial_amplitude[i] = kBellPartialAmplitudes[i];
  765. state_.add.partial_phase[i] = (1L << 30);
  766. }
  767. strike_ = false;
  768. first_partial = 0;
  769. last_partial = kNumBellPartials;
  770. }
  771. for (size_t i = first_partial; i < last_partial; ++i) {
  772. int16_t partial_pitch = pitch_ + kBellPartials[i];
  773. if (i & 1) {
  774. partial_pitch += parameter_[1] >> 7;
  775. } else {
  776. partial_pitch -= parameter_[1] >> 7;
  777. }
  778. state_.add.partial_phase_increment[i] = \
  779. ComputePhaseIncrement(partial_pitch) << 1;
  780. }
  781. // Allow a "droning" bell with no energy loss when the parameter is set to
  782. // its maximum value
  783. if (parameter_[0] < 32000) {
  784. for (size_t i = 0; i < kNumBellPartials; ++i) {
  785. int32_t decay_long = kBellPartialDecayLong[i];
  786. int32_t decay_short = kBellPartialDecayShort[i];
  787. int16_t balance = (32767 - parameter_[0]) >> 8;
  788. balance = balance * balance >> 7;
  789. int32_t decay = decay_long - ((decay_long - decay_short) * balance >> 7);
  790. state_.add.partial_amplitude[i] = \
  791. state_.add.partial_amplitude[i] * decay >> 16;
  792. }
  793. }
  794. int16_t previous_sample = state_.add.previous_sample;
  795. while (size--) {
  796. int32_t out = 0;
  797. for (size_t i = 0; i < kNumBellPartials; ++i) {
  798. state_.add.partial_phase[i] += state_.add.partial_phase_increment[i];
  799. int32_t partial = Interpolate824(wav_sine, state_.add.partial_phase[i]);
  800. out += partial * state_.add.partial_amplitude[i] >> 17;
  801. }
  802. CLIP(out)
  803. *buffer++ = (out + previous_sample) >> 1;
  804. *buffer++ = out; size--;
  805. previous_sample = out;
  806. }
  807. state_.add.previous_sample = previous_sample;
  808. }
  809. void DigitalOscillator::RenderHarmonics(
  810. const uint8_t* sync,
  811. int16_t* buffer,
  812. size_t size) {
  813. uint32_t phase = phase_;
  814. int16_t previous_sample = state_.add.previous_sample;
  815. uint32_t phase_increment = phase_increment_ << 1;
  816. int32_t target_amplitude[kNumAdditiveHarmonics];
  817. int32_t amplitude[kNumAdditiveHarmonics];
  818. int32_t peak = (kNumAdditiveHarmonics * parameter_[0]) >> 7;
  819. int32_t second_peak = (peak >> 1) + kNumAdditiveHarmonics * 128;
  820. int32_t second_peak_amount = parameter_[1] * parameter_[1] >> 15;
  821. int32_t sqrtsqrt_width = parameter_[1] < 16384
  822. ? parameter_[1] >> 6 : 511 - (parameter_[1] >> 6);
  823. int32_t sqrt_width = sqrtsqrt_width * sqrtsqrt_width >> 10;
  824. int32_t width = sqrt_width * sqrt_width + 4;
  825. int32_t total = 0;
  826. for (size_t i = 0; i < kNumAdditiveHarmonics; ++i) {
  827. int32_t x = i << 8;
  828. int32_t d, g;
  829. d = (x - peak);
  830. g = 32768 * 128 / (128 + d * d / width);
  831. d = (x - second_peak);
  832. g += second_peak_amount * 128 / (128 + d * d / width);
  833. total += g;
  834. target_amplitude[i] = g;
  835. }
  836. int32_t attenuation = 2147483647 / total;
  837. for (size_t i = 0; i < kNumAdditiveHarmonics; ++i) {
  838. if ((phase_increment >> 16) * (i + 1) > 0x4000) {
  839. target_amplitude[i] = 0;
  840. } else {
  841. target_amplitude[i] = target_amplitude[i] * attenuation >> 16;
  842. }
  843. amplitude[i] = state_.hrm.amplitude[i];
  844. }
  845. while (size) {
  846. int32_t out;
  847. phase += phase_increment;
  848. if (*sync++ || *sync++) {
  849. phase = 0;
  850. }
  851. out = 0;
  852. for (size_t i = 0; i < kNumAdditiveHarmonics; ++i) {
  853. out += Interpolate824(wav_sine, phase * (i + 1)) * amplitude[i] >> 15;
  854. amplitude[i] += (target_amplitude[i] - amplitude[i]) >> 8;
  855. }
  856. CLIP(out)
  857. *buffer++ = (out + previous_sample) >> 1;
  858. *buffer++ = out;
  859. previous_sample = out;
  860. size -= 2;
  861. }
  862. state_.add.previous_sample = previous_sample;
  863. phase_ = phase;
  864. for (size_t i = 0; i < kNumAdditiveHarmonics; ++i) {
  865. state_.hrm.amplitude[i] = amplitude[i];
  866. }
  867. }
  868. void DigitalOscillator::RenderStruckDrum(
  869. const uint8_t* sync,
  870. int16_t* buffer,
  871. size_t size) {
  872. if (strike_) {
  873. bool reset_phase = state_.add.partial_amplitude[0] < 1024;
  874. for (size_t i = 0; i < kNumDrumPartials; ++i) {
  875. state_.add.target_partial_amplitude[i] = kDrumPartialAmplitude[i];
  876. if (reset_phase) {
  877. state_.add.partial_phase[i] = (1L << 30);
  878. }
  879. }
  880. strike_ = false;
  881. } else {
  882. if (parameter_[0] < 32000) {
  883. for (size_t i = 0; i < kNumDrumPartials; ++i) {
  884. int32_t decay_long = kDrumPartialDecayLong[i];
  885. int32_t decay_short = kDrumPartialDecayShort[i];
  886. int16_t balance = (32767 - parameter_[0]) >> 8;
  887. balance = balance * balance >> 7;
  888. int32_t decay = decay_long - ((decay_long - decay_short) * balance >> 7);
  889. state_.add.target_partial_amplitude[i] = \
  890. state_.add.partial_amplitude[i] * decay >> 16;
  891. }
  892. }
  893. }
  894. for (size_t i = 0; i < kNumDrumPartials; ++i) {
  895. int16_t partial_pitch = pitch_ + kDrumPartials[i];
  896. state_.add.partial_phase_increment[i] = ComputePhaseIncrement(partial_pitch) << 1;
  897. }
  898. int16_t previous_sample = state_.add.previous_sample;
  899. int32_t cutoff = (pitch_ - 12 * 128) + (parameter_[1] >> 2);
  900. if (cutoff < 0) {
  901. cutoff = 0;
  902. } else if (cutoff > 32767) {
  903. cutoff = 32767;
  904. }
  905. int32_t f = Interpolate824(lut_svf_cutoff, cutoff << 16);
  906. int32_t lp_state_0 = state_.add.lp_noise[0];
  907. int32_t lp_state_1 = state_.add.lp_noise[1];
  908. int32_t lp_state_2 = state_.add.lp_noise[2];
  909. int32_t harmonics_gain = parameter_[1] < 12888 ? (parameter_[1] + 4096) : 16384;
  910. int32_t noise_mode_gain = parameter_[1] < 16384 ? 0 : parameter_[1] - 16384;
  911. noise_mode_gain = noise_mode_gain * 12888 >> 14;
  912. int32_t fade_increment = 65536 / size;
  913. int32_t fade = 0;
  914. while (size--) {
  915. fade += fade_increment;
  916. int32_t harmonics = 0;
  917. int32_t noise = Random::GetSample();
  918. if (noise > 16384) {
  919. noise = 16384;
  920. }
  921. if (noise < -16384) {
  922. noise = -16384;
  923. }
  924. lp_state_0 += (noise - lp_state_0) * f >> 15;
  925. lp_state_1 += (lp_state_0 - lp_state_1) * f >> 15;
  926. lp_state_2 += (lp_state_1 - lp_state_2) * f >> 15;
  927. int32_t partials[kNumDrumPartials];
  928. for (size_t i = 0; i < kNumDrumPartials; ++i) {
  929. AdditiveState* a = &state_.add;
  930. a->partial_phase[i] += a->partial_phase_increment[i];
  931. int32_t partial = Interpolate824(wav_sine, a->partial_phase[i]);
  932. int32_t amplitude = a->partial_amplitude[i] + \
  933. (((a->target_partial_amplitude[i] - a->partial_amplitude[i]) * fade) >> 15);
  934. partial = partial * amplitude >> 16;
  935. harmonics += partial;
  936. partials[i] = partial;
  937. }
  938. int32_t sample = partials[0];
  939. int32_t noise_mode_1 = partials[1] * lp_state_2 >> 8;
  940. int32_t noise_mode_2 = partials[3] * lp_state_2 >> 9;
  941. sample += noise_mode_1 * (12288 - noise_mode_gain) >> 14;
  942. sample += noise_mode_2 * noise_mode_gain >> 14;
  943. sample += harmonics * harmonics_gain >> 14;
  944. CLIP(sample)
  945. //sample = Interpolate88(ws_moderate_overdrive, sample + 32768);
  946. *buffer++ = (sample + previous_sample) >> 1;
  947. *buffer++ = sample; size--;
  948. previous_sample = sample;
  949. }
  950. state_.add.previous_sample = previous_sample;
  951. state_.add.lp_noise[0] = lp_state_0;
  952. state_.add.lp_noise[1] = lp_state_1;
  953. state_.add.lp_noise[2] = lp_state_2;
  954. for (size_t i = 0; i < kNumBellPartials; ++i) {
  955. AdditiveState* a = &state_.add;
  956. a->partial_amplitude[i] = a->target_partial_amplitude[i];
  957. }
  958. }
  959. void DigitalOscillator::RenderPlucked(
  960. const uint8_t* sync,
  961. int16_t* buffer,
  962. size_t size) {
  963. phase_increment_ <<= 1;
  964. if (strike_) {
  965. ++active_voice_;
  966. if (active_voice_ >= kNumPluckVoices) {
  967. active_voice_ = 0;
  968. }
  969. // Find the optimal oversampling rate.
  970. PluckState* p = &state_.plk[active_voice_];
  971. int32_t increment = phase_increment_;
  972. p->shift = 0;
  973. while (increment > (2 << 22)) {
  974. increment >>= 1;
  975. ++p->shift;
  976. }
  977. p->size = 1024 >> p->shift;
  978. p->mask = p->size - 1;
  979. p->write_ptr = 0;
  980. p->max_phase_increment = phase_increment_ << 1;
  981. p->phase_increment = phase_increment_;
  982. int32_t width = parameter_[1];
  983. width = (3 * width) >> 1;
  984. p->initialization_ptr = p->size * (8192 + width) >> 16;
  985. strike_ = false;
  986. }
  987. PluckState* current_string = &state_.plk[active_voice_];
  988. // Update the phase increment of the latest note, but do not transpose too
  989. // high above the original pitch.
  990. current_string->phase_increment = std::min(
  991. phase_increment_,
  992. current_string->max_phase_increment);
  993. // Compute loss and stretching factors.
  994. uint32_t update_probability = parameter_[0] < 16384
  995. ? 65535
  996. : 131072 - (parameter_[0] >> 3) * 31;
  997. int16_t loss = 4096 - (phase_increment_ >> 14);
  998. if (loss < 256) {
  999. loss = 256;
  1000. }
  1001. if (parameter_[0] < 16384) {
  1002. loss = loss * (16384 - parameter_[0]) >> 14;
  1003. } else {
  1004. loss = 0;
  1005. }
  1006. int16_t previous_sample = state_.plk[0].previous_sample;
  1007. while (size) {
  1008. int32_t sample = 0;
  1009. for (size_t i = 0; i < kNumPluckVoices; ++i) {
  1010. PluckState* p = &state_.plk[i];
  1011. int16_t* dl = delay_lines_.ks + i * 1025;
  1012. // Initialization: Just use a white noise sample and fill the delay
  1013. // line.
  1014. if (p->initialization_ptr) {
  1015. --p->initialization_ptr;
  1016. int32_t excitation_sample = (dl[p->initialization_ptr] + \
  1017. 3 * Random::GetSample()) >> 2;
  1018. dl[p->initialization_ptr] = excitation_sample;
  1019. sample += excitation_sample;
  1020. } else {
  1021. p->phase += p->phase_increment;
  1022. size_t read_ptr = ((p->phase >> (22 + p->shift)) + 2) & p->mask;
  1023. size_t write_ptr = p->write_ptr;
  1024. size_t num_loops = 0;
  1025. while (write_ptr != read_ptr) {
  1026. ++num_loops;
  1027. size_t next = (write_ptr + 1) & p->mask;
  1028. int32_t a = dl[write_ptr];
  1029. int32_t b = dl[next];
  1030. uint32_t probability = Random::GetWord();
  1031. if ((probability & 0xffff) <= update_probability) {
  1032. int32_t sum = (a + b);
  1033. sum = sum < 0 ? -(-sum >> 1) : (sum >> 1);
  1034. if (loss) {
  1035. sum = sum * (32768 - loss) >> 15;
  1036. }
  1037. dl[write_ptr] = sum;
  1038. }
  1039. if (write_ptr == 0) {
  1040. dl[p->size] = dl[0];
  1041. }
  1042. write_ptr = next;
  1043. }
  1044. p->write_ptr = write_ptr;
  1045. sample += Interpolate1022(dl, p->phase >> p->shift);
  1046. }
  1047. }
  1048. CLIP(sample);
  1049. *buffer++ = (previous_sample + sample) >> 1;
  1050. *buffer++ = sample;
  1051. previous_sample = sample;
  1052. size -= 2;
  1053. }
  1054. state_.plk[0].previous_sample = previous_sample;
  1055. }
  1056. static const int32_t kBridgeLPGain = 14008;
  1057. static const int32_t kBridgeLPPole1 = 18022;
  1058. static const int32_t kBiquadGain = 6553;
  1059. static const int32_t kBiquadPole1 = 6948;
  1060. static const int32_t kBiquadPole2 = -2959;
  1061. void DigitalOscillator::RenderBowed(
  1062. const uint8_t* sync,
  1063. int16_t* buffer,
  1064. size_t size) {
  1065. int8_t* dl_b = delay_lines_.bowed.bridge;
  1066. int8_t* dl_n = delay_lines_.bowed.neck;
  1067. if (strike_) {
  1068. memset(dl_b, 0, sizeof(delay_lines_.bowed.bridge));
  1069. memset(dl_n, 0, sizeof(delay_lines_.bowed.neck));
  1070. memset(&state_, 0, sizeof(state_));
  1071. strike_ = false;
  1072. }
  1073. int16_t parameter_0 = 172 - (parameter_[0] >> 8);
  1074. int16_t parameter_1 = 6 + (parameter_[1] >> 9);
  1075. uint16_t delay_ptr = state_.phy.delay_ptr;
  1076. uint16_t excitation_ptr = state_.phy.excitation_ptr;
  1077. int32_t lp_state = state_.phy.lp_state;
  1078. int32_t biquad_y0 = state_.phy.filter_state[0];
  1079. int32_t biquad_y1 = state_.phy.filter_state[1];
  1080. // Setup delay times and interpolation coefficients.
  1081. uint32_t delay = (delay_ >> 1) - (2 << 16); // Compensation for 1-pole delay
  1082. uint32_t bridge_delay = (delay >> 8) * parameter_1;
  1083. // Transpose one octave up when the note is too low to fit in the delays.
  1084. while ((delay - bridge_delay) > ((kWGNeckLength - 1) << 16)
  1085. || bridge_delay > ((kWGBridgeLength - 1) << 16)) {
  1086. delay >>= 1;
  1087. bridge_delay >>= 1;
  1088. }
  1089. uint16_t bridge_delay_integral = bridge_delay >> 16;
  1090. uint16_t bridge_delay_fractional = bridge_delay & 0xffff;
  1091. uint32_t neck_delay = delay - bridge_delay;
  1092. uint32_t neck_delay_integral = neck_delay >> 16;
  1093. uint16_t neck_delay_fractional = neck_delay & 0xffff;
  1094. int16_t previous_sample = state_.phy.previous_sample;
  1095. // Rendered at half the sample rate (for avoiding big rounding error in
  1096. // coefficients of body IIR filter).
  1097. while (size) {
  1098. phase_ += phase_increment_;
  1099. int32_t new_velocity, friction;
  1100. uint16_t bridge_delay_ptr = delay_ptr + 2 * kWGBridgeLength \
  1101. - bridge_delay_integral;
  1102. uint16_t neck_delay_ptr = delay_ptr + 2 * kWGNeckLength \
  1103. - neck_delay_integral;
  1104. int16_t bridge_dl_a = dl_b[bridge_delay_ptr % kWGBridgeLength];
  1105. int16_t bridge_dl_b = dl_b[(bridge_delay_ptr - 1) % kWGBridgeLength];
  1106. int16_t nut_dl_a = dl_n[neck_delay_ptr % kWGNeckLength];
  1107. int16_t nut_dl_b = dl_n[(neck_delay_ptr - 1) % kWGNeckLength];
  1108. int32_t bridge_value = Mix(
  1109. bridge_dl_a, bridge_dl_b, bridge_delay_fractional) << 8;
  1110. int32_t nut_value = Mix(nut_dl_a, nut_dl_b, neck_delay_fractional) << 8;
  1111. lp_state = (bridge_value * kBridgeLPGain + lp_state * kBridgeLPPole1) >> 15;
  1112. int32_t bridge_reflection = -lp_state;
  1113. int32_t nut_reflection = -nut_value;
  1114. int32_t string_velocity = bridge_reflection + nut_reflection;
  1115. int32_t bow_velocity = lut_bowing_envelope[excitation_ptr >> 1];
  1116. bow_velocity += lut_bowing_envelope[(excitation_ptr + 1) >> 1];
  1117. bow_velocity >>= 1;
  1118. int32_t velocity_delta = bow_velocity - string_velocity;
  1119. friction = velocity_delta * parameter_0 >> 5;
  1120. if (friction < 0) {
  1121. friction = -friction;
  1122. }
  1123. if (friction >= (1 << 17)) {
  1124. friction = (1 << 17) - 1;
  1125. }
  1126. //friction = Interpolate824(lut_bowing_friction, friction << 15);
  1127. friction = lut_bowing_friction[friction >> 9];
  1128. new_velocity = friction * velocity_delta >> 15;
  1129. dl_n[delay_ptr % kWGNeckLength] = (bridge_reflection + new_velocity) >> 8;
  1130. dl_b[delay_ptr % kWGBridgeLength] = (nut_reflection + new_velocity) >> 8;
  1131. ++delay_ptr;
  1132. int32_t temp = bridge_value * kBiquadGain >> 15;
  1133. temp += biquad_y0 * kBiquadPole1 >> 12;
  1134. temp += biquad_y1 * kBiquadPole2 >> 12;
  1135. int32_t out = temp - biquad_y1;
  1136. biquad_y1 = biquad_y0;
  1137. biquad_y0 = temp;
  1138. CLIP(out)
  1139. *buffer++ = (out + previous_sample) >> 1;
  1140. *buffer++ = out;
  1141. previous_sample = out;
  1142. ++excitation_ptr;
  1143. size -= 2;
  1144. }
  1145. if ((excitation_ptr >> 1) >= LUT_BOWING_ENVELOPE_SIZE - 32) {
  1146. excitation_ptr = (LUT_BOWING_ENVELOPE_SIZE - 32) << 1;
  1147. }
  1148. state_.phy.delay_ptr = delay_ptr % kWGNeckLength;
  1149. state_.phy.excitation_ptr = excitation_ptr;
  1150. state_.phy.lp_state = lp_state;
  1151. state_.phy.filter_state[0] = biquad_y0;
  1152. state_.phy.filter_state[1] = biquad_y1;
  1153. state_.phy.previous_sample = previous_sample;
  1154. }
  1155. static const uint16_t kBreathPressure = 26214;
  1156. static const int16_t kReflectionCoefficient = -3891;
  1157. static const int16_t kReedSlope = -1229;
  1158. static const int16_t kReedOffset = 22938;
  1159. void DigitalOscillator::RenderBlown(
  1160. const uint8_t* sync,
  1161. int16_t* buffer,
  1162. size_t size) {
  1163. uint16_t delay_ptr = state_.phy.delay_ptr;
  1164. int32_t lp_state = state_.phy.lp_state;
  1165. int16_t* dl = delay_lines_.bore;
  1166. if (strike_) {
  1167. memset(dl, 0, sizeof(delay_lines_.bore));
  1168. strike_ = false;
  1169. }
  1170. uint32_t delay = (delay_ >> 1) - (1 << 16);
  1171. while (delay > ((kWGBoreLength - 1) << 16)) {
  1172. delay >>= 1;
  1173. }
  1174. uint16_t bore_delay_integral = delay >> 16;
  1175. uint16_t bore_delay_fractional = delay & 0xffff;
  1176. uint16_t parameter = 28000 - (parameter_[0] >> 1);
  1177. int16_t filter_state = state_.phy.filter_state[0];
  1178. int16_t normalized_pitch = (pitch_ - 8192 + (parameter_[1] >> 1)) >> 7;
  1179. if (normalized_pitch < 0) {
  1180. normalized_pitch = 0;
  1181. } else if (normalized_pitch > 127) {
  1182. normalized_pitch = 127;
  1183. }
  1184. uint16_t filter_coefficient = lut_flute_body_filter[normalized_pitch];
  1185. while (size--) {
  1186. phase_ += phase_increment_;
  1187. int32_t breath_pressure = Random::GetSample() * parameter >> 15;
  1188. breath_pressure = breath_pressure * kBreathPressure >> 15;
  1189. breath_pressure += kBreathPressure;
  1190. uint16_t bore_delay_ptr = delay_ptr + 2 * kWGBoreLength \
  1191. - bore_delay_integral;
  1192. int16_t dl_a = dl[bore_delay_ptr % kWGBoreLength];
  1193. int16_t dl_b = dl[(bore_delay_ptr - 1) % kWGBoreLength];
  1194. int32_t dl_value = Mix(dl_a, dl_b, bore_delay_fractional);
  1195. int32_t pressure_delta = (dl_value >> 1) + lp_state;
  1196. lp_state = dl_value >> 1;
  1197. pressure_delta = kReflectionCoefficient * pressure_delta >> 12;
  1198. pressure_delta -= breath_pressure;
  1199. int32_t reed = (pressure_delta * kReedSlope >> 12) + kReedOffset;
  1200. CLIP(reed)
  1201. int32_t out = pressure_delta * reed >> 15;
  1202. out += breath_pressure;
  1203. CLIP(out)
  1204. dl[delay_ptr++ % kWGBoreLength] = out;
  1205. filter_state = (filter_coefficient * out + \
  1206. (4096 - filter_coefficient) * filter_state) >> 12;
  1207. *buffer++ = filter_state;
  1208. }
  1209. state_.phy.filter_state[0] = filter_state;
  1210. state_.phy.delay_ptr = delay_ptr % kWGBoreLength;
  1211. state_.phy.lp_state = lp_state;
  1212. }
  1213. static const uint16_t kRandomPressure = 0.22 * 4096;
  1214. static const uint16_t kDCBlockingPole = 0.99 * 4096;
  1215. void DigitalOscillator::RenderFluted(
  1216. const uint8_t* sync,
  1217. int16_t* buffer,
  1218. size_t size) {
  1219. uint16_t delay_ptr = state_.phy.delay_ptr;
  1220. uint16_t excitation_ptr = state_.phy.excitation_ptr;
  1221. int32_t lp_state = state_.phy.lp_state;
  1222. int32_t dc_blocking_x0 = state_.phy.filter_state[0];
  1223. int32_t dc_blocking_y0 = state_.phy.filter_state[1];
  1224. int8_t* dl_b = delay_lines_.fluted.bore;
  1225. int8_t* dl_j = delay_lines_.fluted.jet;
  1226. if (strike_) {
  1227. excitation_ptr = 0;
  1228. memset(dl_b, 0, sizeof(delay_lines_.fluted.bore));
  1229. memset(dl_j, 0, sizeof(delay_lines_.fluted.jet));
  1230. lp_state = 0;
  1231. strike_ = false;
  1232. }
  1233. // Setup delay times and interpolation coefficients.
  1234. uint32_t bore_delay = (delay_ << 1) - (2 << 16);
  1235. uint32_t jet_delay = (bore_delay >> 8) * (48 + (parameter_[1] >> 10));
  1236. bore_delay -= jet_delay;
  1237. while (bore_delay > ((kWGFBoreLength - 1) << 16)
  1238. || jet_delay > ((kWGJetLength - 1) << 16)) {
  1239. bore_delay >>= 1;
  1240. jet_delay >>= 1;
  1241. }
  1242. uint16_t bore_delay_integral = bore_delay >> 16;
  1243. uint16_t bore_delay_fractional = bore_delay & 0xffff;
  1244. uint32_t jet_delay_integral = jet_delay >> 16;
  1245. uint16_t jet_delay_fractional = jet_delay & 0xffff;
  1246. uint16_t breath_intensity = 2100 - (parameter_[0] >> 4);
  1247. uint16_t filter_coefficient = lut_flute_body_filter[pitch_ >> 7];
  1248. while (size--) {
  1249. phase_ += phase_increment_;
  1250. uint16_t bore_delay_ptr = delay_ptr + 2 * kWGFBoreLength \
  1251. - bore_delay_integral;
  1252. uint16_t jet_delay_ptr = delay_ptr + 2 * kWGJetLength \
  1253. - jet_delay_integral;
  1254. int16_t bore_dl_a = dl_b[bore_delay_ptr % kWGFBoreLength];
  1255. int16_t bore_dl_b = dl_b[(bore_delay_ptr - 1) % kWGFBoreLength];
  1256. int16_t jet_dl_a = dl_j[jet_delay_ptr % kWGJetLength];
  1257. int16_t jet_dl_b = dl_j[(jet_delay_ptr - 1) % kWGJetLength];
  1258. int32_t bore_value = Mix(bore_dl_a, bore_dl_b, bore_delay_fractional) << 9;
  1259. int32_t jet_value = Mix(jet_dl_a, jet_dl_b, jet_delay_fractional) << 9;
  1260. int32_t breath_pressure = lut_blowing_envelope[excitation_ptr];
  1261. breath_pressure <<= 1;
  1262. int32_t random_pressure = Random::GetSample() * breath_intensity >> 12;
  1263. random_pressure = random_pressure * breath_pressure >> 15;
  1264. breath_pressure += random_pressure;
  1265. lp_state = (-filter_coefficient * bore_value + \
  1266. (4096 - filter_coefficient) * lp_state) >> 12;
  1267. int32_t reflection = lp_state;
  1268. dc_blocking_y0 = (kDCBlockingPole * dc_blocking_y0 >> 12);
  1269. dc_blocking_y0 += reflection - dc_blocking_x0;
  1270. dc_blocking_x0 = reflection;
  1271. reflection = dc_blocking_y0;
  1272. int32_t pressure_delta = breath_pressure - (reflection >> 1);
  1273. dl_j[delay_ptr % kWGJetLength] = pressure_delta >> 9;
  1274. pressure_delta = jet_value;
  1275. int32_t jet_table_index = pressure_delta;
  1276. if (jet_table_index < 0) {
  1277. jet_table_index = 0;
  1278. }
  1279. if (jet_table_index > 65535) {
  1280. jet_table_index = 65535;
  1281. }
  1282. pressure_delta = static_cast<int16_t>(
  1283. lut_blowing_jet[jet_table_index >> 8]) + (reflection >> 1);
  1284. dl_b[delay_ptr % kWGFBoreLength] = pressure_delta >> 9;
  1285. ++delay_ptr;
  1286. int32_t out = bore_value >> 1;
  1287. CLIP(out)
  1288. *buffer++ = out;
  1289. if (size & 3) {
  1290. ++excitation_ptr;
  1291. }
  1292. }
  1293. if (excitation_ptr >= LUT_BLOWING_ENVELOPE_SIZE - 32) {
  1294. excitation_ptr = LUT_BLOWING_ENVELOPE_SIZE - 32;
  1295. }
  1296. state_.phy.delay_ptr = delay_ptr;
  1297. state_.phy.excitation_ptr = excitation_ptr;
  1298. state_.phy.lp_state = lp_state;
  1299. state_.phy.filter_state[0] = dc_blocking_x0;
  1300. state_.phy.filter_state[1] = dc_blocking_y0;
  1301. }
  1302. struct WavetableDefinition {
  1303. uint8_t num_steps;
  1304. uint8_t wave_index[17];
  1305. };
  1306. static const WavetableDefinition wavetable_definitions[] = {
  1307. // 01 male
  1308. { 16 , { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15 } },
  1309. // 02 female
  1310. { 16 , { 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 31 } },
  1311. // 03 choir
  1312. { 16 , { 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 47 } },
  1313. // 04 space_voice
  1314. { 16 , { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 63 } },
  1315. // 05 tampura
  1316. { 16 , { 64, 65, 66, 67, 68, 68, 69, 70, 71, 72, 73, 73, 74, 75, 75, 76, 76 } },
  1317. // 06 shamus
  1318. { 16 , { 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 92 } },
  1319. // 07 swept_string
  1320. { 16 , { 93, 94, 95, 96, 97, 98, 99, 100, 101,
  1321. 102, 103, 104, 105, 106, 107, 108, 108 } },
  1322. // 08 bowed
  1323. { 16 , { 109, 110, 111, 112, 113, 114, 115, 116,
  1324. 117, 118, 119, 120, 121, 122, 123, 124, 124 } },
  1325. // 09 cello
  1326. { 16 , { 125, 126, 127, 128, 129, 130, 131, 132,
  1327. 132, 132, 132, 132, 132, 132, 132, 132, 132 } },
  1328. // 10 vibes
  1329. { 16 , { 133, 134, 135, 136, 137, 138, 139, 140,
  1330. 141, 142, 143, 144, 144, 144, 145, 145, 145 } },
  1331. // 11 slap
  1332. { 16 , { 146, 147, 148, 149, 150, 151, 151, 151,
  1333. 152, 152, 152, 152, 153, 153, 153, 153, 153 } },
  1334. // 12 piano
  1335. { 8 , { 154, 154, 154, 154, 154, 154, 155, 156, 156 } },
  1336. // 13 organ!
  1337. { 16 , { 176, 157, 158, 159, 160, 161, 162, 163,
  1338. 164, 165, 166, 167, 168, 169, 170, 171, 171 } },
  1339. // 14 waves!
  1340. { 16 , { 172, 173, 174, 175, 176, 177, 178, 179,
  1341. 180, 181, 182, 183, 184, 185, 186, 187, 187 } },
  1342. // 15 digital
  1343. { 16 , { 176, 188, 189, 190, 191, 192, 193, 194,
  1344. 195, 196, 197, 198, 199, 200, 201, 202, 202 } },
  1345. // 16 drone 1
  1346. { 16 , { 203, 205, 204, 205, 212, 206, 207, 208,
  1347. 208, 209, 210, 210, 211, 211, 212, 212, 212 } },
  1348. // 17 drone 2
  1349. { 8 , { 213, 213, 213, 214, 215, 216, 217, 218, 219 } },
  1350. // 18 metallic
  1351. { 16 , { 220, 221, 222, 223, 224, 225, 226, 227,
  1352. 228, 229, 230, 231, 232, 233, 234, 235, 235 } },
  1353. // 19 fantasy
  1354. { 16 , { 236, 237, 238, 239, 240, 241, 242, 243,
  1355. 244, 245, 246, 247, 248, 249, 250, 251, 251 } },
  1356. // 20 bell
  1357. { 4 , { 252, 253, 254, 255, 254 } },
  1358. };
  1359. void DigitalOscillator::RenderWavetables(
  1360. const uint8_t* sync,
  1361. int16_t* buffer,
  1362. size_t size) {
  1363. // Add some hysteresis to the second parameter to prevent a single DAC bit
  1364. // error to cause a sharp and glitchy wavetable transition.
  1365. if ((parameter_[1] > previous_parameter_[1] + 64) ||
  1366. (parameter_[1] < previous_parameter_[1] - 64)) {
  1367. previous_parameter_[1] = parameter_[1];
  1368. }
  1369. uint32_t wavetable_index = static_cast<uint32_t>(previous_parameter_[1]) * 20;
  1370. wavetable_index >>= 15;
  1371. uint32_t wave_pointer;
  1372. const uint8_t* wave[2];
  1373. const WavetableDefinition& wt = wavetable_definitions[wavetable_index];
  1374. wave_pointer = (parameter_[0] << 1) * wt.num_steps;
  1375. for (size_t i = 0; i < 2; ++i) {
  1376. size_t wave_index = wt.wave_index[(wave_pointer >> 16) + i];
  1377. wave[i] = wt_waves + wave_index * 129;
  1378. }
  1379. uint32_t phase_increment = phase_increment_ >> 1;
  1380. while (size--) {
  1381. int16_t sample;
  1382. // 2x naive oversampling.
  1383. phase_ += phase_increment;
  1384. if (*sync++) {
  1385. phase_ = 0;
  1386. }
  1387. sample = Crossfade(wave[0], wave[1], phase_ >> 1, wave_pointer) >> 1;
  1388. phase_ += phase_increment;
  1389. sample += Crossfade(wave[0], wave[1], phase_ >> 1, wave_pointer) >> 1;
  1390. *buffer++ = sample;
  1391. }
  1392. }
  1393. void DigitalOscillator::RenderWaveMap(
  1394. const uint8_t* sync,
  1395. int16_t* buffer,
  1396. size_t size) {
  1397. // The grid is 16x16; so there are 15 interpolation squares.
  1398. uint16_t p[2];
  1399. uint16_t wave_xfade[2];
  1400. uint16_t wave_coordinate[2];
  1401. p[0] = parameter_[0] * 15 >> 4;
  1402. p[1] = parameter_[1] * 15 >> 4;
  1403. wave_xfade[0] = p[0] << 5;
  1404. wave_xfade[1] = p[1] << 5;
  1405. wave_coordinate[0] = p[0] >> 11;
  1406. wave_coordinate[1] = p[1] >> 11;
  1407. const uint8_t* wave[2][2];
  1408. for (size_t i = 0; i < 2; ++i) {
  1409. for (size_t j = 0; j < 2; ++j) {
  1410. uint16_t wave_index = \
  1411. (wave_coordinate[0] + i) * 16 + (wave_coordinate[1] + j);
  1412. wave[i][j] = wt_waves + wt_map[wave_index] * 129;
  1413. }
  1414. }
  1415. uint32_t phase_increment = phase_increment_ >> 1;
  1416. while (size--) {
  1417. int16_t sample;
  1418. // 2x naive oversampling.
  1419. phase_ += phase_increment;
  1420. if (*sync++) {
  1421. phase_ = 0;
  1422. }
  1423. sample = Mix(
  1424. Crossfade(wave[0][0], wave[0][1], phase_ >> 1, wave_xfade[1]),
  1425. Crossfade(wave[1][0], wave[1][1], phase_ >> 1, wave_xfade[1]),
  1426. wave_xfade[0]) >> 1;
  1427. phase_ += phase_increment;
  1428. sample += Mix(
  1429. Crossfade(wave[0][0], wave[0][1], phase_ >> 1, wave_xfade[1]),
  1430. Crossfade(wave[1][0], wave[1][1], phase_ >> 1, wave_xfade[1]),
  1431. wave_xfade[0]) >> 1;
  1432. *buffer++ = sample;
  1433. }
  1434. }
  1435. static const uint8_t wave_line[] = {
  1436. 187, 179, 154, 155, 135, 134, 137, 19, 24, 3, 8, 66, 79, 25, 180, 174, 64,
  1437. 127, 198, 15, 10, 7, 11, 0, 191, 192, 115, 238, 237, 236, 241, 47, 70, 76,
  1438. 235, 26, 133, 208, 34, 175, 183, 146, 147, 148, 150, 151, 152, 153, 117,
  1439. 138, 32, 33, 35, 125, 199, 201, 30, 31, 193, 27, 29, 21, 18, 182
  1440. };
  1441. static const uint8_t mini_wave_line[] = {
  1442. 157, 161, 171, 188, 189, 191, 192, 193, 196, 198, 201, 234, 232,
  1443. 229, 226, 224, 1, 2, 3, 4, 5, 8, 12, 32, 36, 42, 47, 252, 254, 141, 139,
  1444. 135, 174
  1445. };
  1446. void DigitalOscillator::RenderWaveLine(
  1447. const uint8_t* sync,
  1448. int16_t* buffer,
  1449. size_t size) {
  1450. smoothed_parameter_ = (3 * smoothed_parameter_ + (parameter_[0] << 1)) >> 2;
  1451. uint16_t scan = smoothed_parameter_;
  1452. const uint8_t* wave_0 = wt_waves + wave_line[previous_parameter_[0] >> 9] * 129;
  1453. const uint8_t* wave_1 = wt_waves + wave_line[scan >> 10] * 129;
  1454. const uint8_t* wave_2 = wt_waves + wave_line[(scan >> 10) + 1] * 129;
  1455. uint16_t smooth_xfade = scan << 6;
  1456. uint16_t rough_xfade = 0;
  1457. uint16_t rough_xfade_increment = 32768 / size;
  1458. uint32_t balance = parameter_[1] << 3;
  1459. uint32_t phase = phase_;
  1460. uint32_t phase_increment = phase_increment_ >> 1;
  1461. int16_t rough, smooth;
  1462. if (parameter_[1] < 8192) {
  1463. while (size--) {
  1464. if (*sync++) {
  1465. phase = 0;
  1466. }
  1467. int32_t sample = 0;
  1468. rough = Crossfade(wave_0, wave_1, (phase >> 1) & 0xfe000000, rough_xfade);
  1469. smooth = Crossfade(wave_0, wave_1, phase >> 1, rough_xfade);
  1470. sample += Mix(rough, smooth, balance);
  1471. phase += phase_increment;
  1472. rough_xfade += rough_xfade_increment;
  1473. rough = Crossfade(wave_0, wave_1, (phase >> 1) & 0xfe000000, rough_xfade);
  1474. smooth = Crossfade(wave_0, wave_1, phase >> 1, rough_xfade);
  1475. sample += Mix(rough, smooth, balance);
  1476. phase += phase_increment;
  1477. rough_xfade += rough_xfade_increment;
  1478. *buffer++ = sample >> 1;
  1479. }
  1480. } else if (parameter_[1] < 16384) {
  1481. while (size--) {
  1482. if (*sync++) {
  1483. phase = 0;
  1484. }
  1485. int32_t sample = 0;
  1486. rough = Crossfade(wave_0, wave_1, phase >> 1, rough_xfade);
  1487. smooth = Crossfade(wave_1, wave_2, phase >> 1, smooth_xfade);
  1488. sample += Mix(rough, smooth, balance);
  1489. phase += phase_increment;
  1490. rough_xfade += rough_xfade_increment;
  1491. rough = Crossfade(wave_0, wave_1, phase >> 1, rough_xfade);
  1492. smooth = Crossfade(wave_1, wave_2, phase >> 1, smooth_xfade);
  1493. sample += Mix(rough, smooth, balance);
  1494. phase += phase_increment;
  1495. rough_xfade += rough_xfade_increment;
  1496. *buffer++ = sample >> 1;
  1497. }
  1498. } else if (parameter_[1] < 24576) {
  1499. while (size--) {
  1500. if (*sync++) {
  1501. phase = 0;
  1502. }
  1503. int32_t sample = 0;
  1504. smooth = Crossfade(wave_1, wave_2, phase >> 1, smooth_xfade);
  1505. rough = Crossfade(wave_1, wave_2, (phase >> 1) & 0xfe000000, smooth_xfade);
  1506. sample += Mix(smooth, rough, balance);
  1507. phase += phase_increment;
  1508. smooth = Crossfade(wave_1, wave_2, phase >> 1, smooth_xfade);
  1509. rough = Crossfade(wave_1, wave_2, (phase >> 1) & 0xfe000000, smooth_xfade);
  1510. sample += Mix(smooth, rough, balance);
  1511. phase += phase_increment;
  1512. *buffer++ = sample >> 1;
  1513. }
  1514. } else {
  1515. while (size--) {
  1516. if (*sync++) {
  1517. phase = 0;
  1518. }
  1519. int32_t sample = 0;
  1520. smooth = Crossfade(wave_1, wave_2, (phase >> 1) & 0xfe000000, smooth_xfade);
  1521. rough = Crossfade(wave_1, wave_2, (phase >> 1) & 0xf8000000, smooth_xfade);
  1522. sample += Mix(smooth, rough, balance);
  1523. phase += phase_increment;
  1524. smooth = Crossfade(wave_1, wave_2, (phase >> 1) & 0xfe000000, smooth_xfade);
  1525. rough = Crossfade(wave_1, wave_2, (phase >> 1) & 0xf8000000, smooth_xfade);
  1526. sample += Mix(smooth, rough, balance);
  1527. phase += phase_increment;
  1528. *buffer++ = sample >> 1;
  1529. }
  1530. }
  1531. phase_ = phase;
  1532. previous_parameter_[0] = smoothed_parameter_ >> 1;
  1533. }
  1534. #define SEMI * 128
  1535. static const uint16_t chords[17][3] = {
  1536. { 2, 4, 6 },
  1537. { 16, 32, 48 },
  1538. { 2 SEMI, 7 SEMI, 12 SEMI },
  1539. { 3 SEMI, 7 SEMI, 10 SEMI },
  1540. { 3 SEMI, 7 SEMI, 12 SEMI },
  1541. { 3 SEMI, 7 SEMI, 14 SEMI },
  1542. { 3 SEMI, 7 SEMI, 17 SEMI },
  1543. { 7 SEMI, 12 SEMI, 19 SEMI },
  1544. { 7 SEMI, 3 + 12 SEMI, 5 + 19 SEMI },
  1545. { 4 SEMI, 7 SEMI, 17 SEMI },
  1546. { 4 SEMI, 7 SEMI, 14 SEMI },
  1547. { 4 SEMI, 7 SEMI, 12 SEMI },
  1548. { 4 SEMI, 7 SEMI, 11 SEMI },
  1549. { 5 SEMI, 7 SEMI, 12 SEMI },
  1550. { 4, 7 SEMI, 12 SEMI },
  1551. { 4, 4 + 12 SEMI, 12 SEMI },
  1552. { 4, 4 + 12 SEMI, 12 SEMI },
  1553. };
  1554. void DigitalOscillator::RenderWaveParaphonic(
  1555. const uint8_t* sync,
  1556. int16_t* buffer,
  1557. size_t size) {
  1558. if (strike_) {
  1559. for (size_t i = 0; i < 4; ++i) {
  1560. state_.saw.phase[i] = Random::GetWord();
  1561. }
  1562. strike_ = false;
  1563. }
  1564. // Do not use an array here to allow these to be kept in arbitrary registers.
  1565. uint32_t phase_0, phase_1, phase_2, phase_3;
  1566. uint32_t phase_increment[3];
  1567. uint32_t phase_increment_0;
  1568. phase_increment_0 = phase_increment_;
  1569. phase_0 = state_.saw.phase[0];
  1570. phase_1 = state_.saw.phase[1];
  1571. phase_2 = state_.saw.phase[2];
  1572. phase_3 = state_.saw.phase[3];
  1573. uint16_t chord_integral = parameter_[1] >> 11;
  1574. uint16_t chord_fractional = parameter_[1] << 5;
  1575. if (chord_fractional < 30720) {
  1576. chord_fractional = 0;
  1577. } else if (chord_fractional >= 34816) {
  1578. chord_fractional = 65535;
  1579. } else {
  1580. chord_fractional = (chord_fractional - 30720) * 16;
  1581. }
  1582. for (size_t i = 0; i < 3; ++i) {
  1583. uint16_t detune_1 = chords[chord_integral][i];
  1584. uint16_t detune_2 = chords[chord_integral + 1][i];
  1585. uint16_t detune = detune_1 + ((detune_2 - detune_1) * chord_fractional >> 16);
  1586. phase_increment[i] = ComputePhaseIncrement(pitch_ + detune);
  1587. }
  1588. const uint8_t* wave_1 = wt_waves + mini_wave_line[parameter_[0] >> 10] * 129;
  1589. const uint8_t* wave_2 = wt_waves + mini_wave_line[(parameter_[0] >> 10) + 1] * 129;
  1590. uint16_t wave_xfade = parameter_[0] << 6;
  1591. while (size) {
  1592. int32_t sample = 0;
  1593. phase_0 += phase_increment_0;
  1594. phase_1 += phase_increment[0];
  1595. phase_2 += phase_increment[1];
  1596. phase_3 += phase_increment[2];
  1597. sample += Crossfade(wave_1, wave_2, phase_0 >> 1, wave_xfade);
  1598. sample += Crossfade(wave_1, wave_2, phase_1 >> 1, wave_xfade);
  1599. sample += Crossfade(wave_1, wave_2, phase_2 >> 1, wave_xfade);
  1600. sample += Crossfade(wave_1, wave_2, phase_3 >> 1, wave_xfade);
  1601. *buffer++ = sample >> 2;
  1602. phase_0 += phase_increment_0;
  1603. phase_1 += phase_increment[0];
  1604. phase_2 += phase_increment[1];
  1605. phase_3 += phase_increment[2];
  1606. sample = 0;
  1607. sample += Crossfade(wave_1, wave_2, phase_0 >> 1, wave_xfade);
  1608. sample += Crossfade(wave_1, wave_2, phase_1 >> 1, wave_xfade);
  1609. sample += Crossfade(wave_1, wave_2, phase_2 >> 1, wave_xfade);
  1610. sample += Crossfade(wave_1, wave_2, phase_3 >> 1, wave_xfade);
  1611. *buffer++ = sample >> 2;
  1612. size -= 2;
  1613. }
  1614. state_.saw.phase[0] = phase_0;
  1615. state_.saw.phase[1] = phase_1;
  1616. state_.saw.phase[2] = phase_2;
  1617. state_.saw.phase[3] = phase_3;
  1618. }
  1619. void DigitalOscillator::RenderFilteredNoise(
  1620. const uint8_t* sync,
  1621. int16_t* buffer,
  1622. size_t size) {
  1623. int32_t f = Interpolate824(lut_svf_cutoff, pitch_ << 17);
  1624. int32_t damp = Interpolate824(lut_svf_damp, parameter_[0] << 17);
  1625. int32_t scale = Interpolate824(lut_svf_scale, parameter_[0] << 17);
  1626. int32_t bp = state_.svf.bp;
  1627. int32_t lp = state_.svf.lp;
  1628. int32_t bp_gain, lp_gain, hp_gain;
  1629. // Morph between LP, BP, HP.
  1630. if (parameter_[1] < 16384) {
  1631. bp_gain = parameter_[1];
  1632. lp_gain = 16384 - bp_gain;
  1633. hp_gain = 0;
  1634. } else {
  1635. bp_gain = 32767 - parameter_[1];
  1636. hp_gain = parameter_[1] - 16384;
  1637. lp_gain = 0;
  1638. }
  1639. int32_t gain_correction = f > scale ? scale * 32767 / f : 32767;
  1640. while (size--) {
  1641. int32_t notch, hp, in;
  1642. in = Random::GetSample() >> 1;
  1643. notch = in - (bp * damp >> 15);
  1644. lp += f * bp >> 15;
  1645. CLIP(lp)
  1646. hp = notch - lp;
  1647. bp += f * hp >> 15;
  1648. int32_t result = 0;
  1649. result += (lp_gain * lp) >> 14;
  1650. result += (bp_gain * bp) >> 14;
  1651. result += (hp_gain * hp) >> 14;
  1652. CLIP(result)
  1653. result = result * gain_correction >> 15;
  1654. *buffer++ = Interpolate88(ws_moderate_overdrive, result + 32768);
  1655. }
  1656. state_.svf.lp = lp;
  1657. state_.svf.bp = bp;
  1658. }
  1659. void DigitalOscillator::RenderTwinPeaksNoise(
  1660. const uint8_t* sync,
  1661. int16_t* buffer,
  1662. size_t size) {
  1663. int32_t sample;
  1664. int32_t y10, y20;
  1665. int32_t y11 = state_.pno.filter_state[0][0];
  1666. int32_t y12 = state_.pno.filter_state[0][1];
  1667. int32_t y21 = state_.pno.filter_state[1][0];
  1668. int32_t y22 = state_.pno.filter_state[1][1];
  1669. uint32_t q = 65240 + (parameter_[0] >> 7);
  1670. int32_t q_squared = q * q >> 17;
  1671. int16_t p1 = pitch_;
  1672. CONSTRAIN(p1, 0, 16383)
  1673. int32_t c1 = Interpolate824(lut_resonator_coefficient, p1 << 17);
  1674. int32_t s1 = Interpolate824(lut_resonator_scale, p1 << 17);
  1675. int16_t p2 = pitch_ + ((parameter_[1] - 16384) >> 1);
  1676. CONSTRAIN(p2, 0, 16383)
  1677. int32_t c2 = Interpolate824(lut_resonator_coefficient, p2 << 17);
  1678. int32_t s2 = Interpolate824(lut_resonator_scale, p2 << 17);
  1679. c1 = c1 * q >> 16;
  1680. c2 = c2 * q >> 16;
  1681. int32_t makeup_gain = 8191 - (parameter_[0] >> 2);
  1682. while (size) {
  1683. sample = Random::GetSample() >> 1;
  1684. if (sample > 0) {
  1685. y10 = sample * s1 >> 16;
  1686. y20 = sample * s2 >> 16;
  1687. } else {
  1688. y10 = -((-sample) * s1 >> 16);
  1689. y20 = -((-sample) * s2 >> 16);
  1690. }
  1691. y10 += y11 * c1 >> 15;
  1692. y10 -= y12 * q_squared >> 15;
  1693. CLIP(y10)
  1694. y12 = y11;
  1695. y11 = y10;
  1696. y20 += y21 * c2 >> 15;
  1697. y20 -= y22 * q_squared >> 15;
  1698. CLIP(y20)
  1699. y22 = y21;
  1700. y21 = y20;
  1701. y10 += y20;
  1702. y10 += (y10 * makeup_gain >> 13);
  1703. CLIP(y10)
  1704. sample = y10;
  1705. sample = Interpolate88(ws_moderate_overdrive, sample + 32768);
  1706. *buffer++ = sample;
  1707. *buffer++ = sample;
  1708. size -= 2;
  1709. }
  1710. state_.pno.filter_state[0][0] = y11;
  1711. state_.pno.filter_state[0][1] = y12;
  1712. state_.pno.filter_state[1][0] = y21;
  1713. state_.pno.filter_state[1][1] = y22;
  1714. }
  1715. void DigitalOscillator::RenderClockedNoise(
  1716. const uint8_t* sync,
  1717. int16_t* buffer,
  1718. size_t size) {
  1719. ClockedNoiseState* state = &state_.clk;
  1720. if ((parameter_[1] > previous_parameter_[1] + 64) ||
  1721. (parameter_[1] < previous_parameter_[1] - 64)) {
  1722. previous_parameter_[1] = parameter_[1];
  1723. }
  1724. if ((parameter_[0] > previous_parameter_[0] + 16) ||
  1725. (parameter_[0] < previous_parameter_[0] - 16)) {
  1726. previous_parameter_[0] = parameter_[0];
  1727. }
  1728. if (strike_) {
  1729. state->seed = Random::GetWord();
  1730. strike_ = false;
  1731. }
  1732. // Shift the range of the Coarse knob to reach higher clock rates, close
  1733. // to the sample rate.
  1734. uint32_t phase = phase_;
  1735. uint32_t phase_increment = phase_increment_;
  1736. for (size_t i = 0; i < 3; ++i) {
  1737. if (phase_increment < (1UL << 31)) {
  1738. phase_increment <<= 1;
  1739. }
  1740. }
  1741. // Compute the period of the random generator.
  1742. state->cycle_phase_increment = ComputePhaseIncrement(
  1743. previous_parameter_[0] - 16384) << 1;
  1744. // Compute the number of quantization steps
  1745. uint32_t num_steps = 1 + (previous_parameter_[1] >> 10);
  1746. if (num_steps == 1) {
  1747. num_steps = 2;
  1748. }
  1749. uint32_t quantizer_divider = 65536 / num_steps;
  1750. while (size--) {
  1751. phase += phase_increment;
  1752. if (*sync++) {
  1753. phase = 0;
  1754. }
  1755. // Clock.
  1756. if (phase < phase_increment) {
  1757. state->rng_state = state->rng_state * 1664525L + 1013904223L;
  1758. state->cycle_phase += state->cycle_phase_increment;
  1759. // Enforce period
  1760. if (state->cycle_phase < state->cycle_phase_increment) {
  1761. state->rng_state = state->seed;
  1762. // Make the period an integer.
  1763. state->cycle_phase = state->cycle_phase_increment;
  1764. }
  1765. uint16_t sample = state->rng_state;
  1766. sample -= sample % quantizer_divider;
  1767. sample += quantizer_divider >> 1;
  1768. state->sample = sample;
  1769. // Make the clock rate an exact divisor of the sample rate.
  1770. phase = phase_increment;
  1771. }
  1772. *buffer++ = state->sample;
  1773. }
  1774. phase_ = phase;
  1775. }
  1776. void DigitalOscillator::RenderGranularCloud(
  1777. const uint8_t* sync,
  1778. int16_t* buffer,
  1779. size_t size) {
  1780. for (size_t i = 0; i < 4; ++i) {
  1781. Grain* g = &state_.grain[i];
  1782. // If a grain has reached the end of its envelope, reset it.
  1783. if (g->envelope_phase > (1 << 24) ||
  1784. g->envelope_phase_increment == 0) {
  1785. g->envelope_phase_increment = 0;
  1786. if ((Random::GetWord() & 0xffff) < 0x4000) {
  1787. g->envelope_phase_increment = \
  1788. lut_granular_envelope_rate[parameter_[0] >> 7] << 3;
  1789. g->envelope_phase = 0;
  1790. g->phase_increment = phase_increment_;
  1791. int32_t pitch_mod = Random::GetSample() * parameter_[1] >> 16;
  1792. int32_t phi = phase_increment_ >> 8;
  1793. if (pitch_mod < 0) {
  1794. g->phase_increment += phi * (pitch_mod >> 8);
  1795. } else {
  1796. g->phase_increment += phi * (pitch_mod >> 7);
  1797. }
  1798. }
  1799. }
  1800. }
  1801. // TODO(pichenettes): Check if it's possible to interpolate envelope
  1802. // increment too!
  1803. while (size--) {
  1804. int32_t sample = 0;
  1805. state_.grain[0].phase += state_.grain[0].phase_increment;
  1806. state_.grain[0].envelope_phase += state_.grain[0].envelope_phase_increment;
  1807. sample += Interpolate824(wav_sine, state_.grain[0].phase) * \
  1808. lut_granular_envelope[state_.grain[0].envelope_phase >> 16] >> 17;
  1809. state_.grain[1].phase += state_.grain[1].phase_increment;
  1810. state_.grain[1].envelope_phase += state_.grain[1].envelope_phase_increment;
  1811. sample += Interpolate824(wav_sine, state_.grain[1].phase) * \
  1812. lut_granular_envelope[state_.grain[1].envelope_phase >> 16] >> 17;
  1813. state_.grain[2].phase += state_.grain[2].phase_increment;
  1814. state_.grain[2].envelope_phase += state_.grain[2].envelope_phase_increment;
  1815. sample += Interpolate824(wav_sine, state_.grain[2].phase) * \
  1816. lut_granular_envelope[state_.grain[2].envelope_phase >> 16] >> 17;
  1817. state_.grain[3].phase += state_.grain[3].phase_increment;
  1818. state_.grain[3].envelope_phase += state_.grain[3].envelope_phase_increment;
  1819. sample += Interpolate824(wav_sine, state_.grain[3].phase) * \
  1820. lut_granular_envelope[state_.grain[3].envelope_phase >> 16] >> 17;
  1821. if (sample < -32768) {
  1822. sample = -32768;
  1823. }
  1824. if (sample > 32767) {
  1825. sample = 32767;
  1826. }
  1827. *buffer++ = sample;
  1828. }
  1829. }
  1830. static const uint16_t kParticleNoiseDecay = 64763;
  1831. static const int32_t kResonanceSquared = 32768 * 0.996 * 0.996;
  1832. static const int32_t kResonanceFactor = 32768 * 0.996;
  1833. void DigitalOscillator::RenderParticleNoise(
  1834. const uint8_t* sync,
  1835. int16_t* buffer,
  1836. size_t size) {
  1837. uint16_t amplitude = state_.pno.amplitude;
  1838. uint32_t density = 1024 + parameter_[0];
  1839. int32_t sample;
  1840. int32_t y10, y20, y30;
  1841. int32_t y11 = state_.pno.filter_state[0][0];
  1842. int32_t y12 = state_.pno.filter_state[0][1];
  1843. int32_t s1 = state_.pno.filter_scale[0];
  1844. int32_t c1 = state_.pno.filter_coefficient[0];
  1845. int32_t y21 = state_.pno.filter_state[1][0];
  1846. int32_t y22 = state_.pno.filter_state[1][1];
  1847. int32_t s2 = state_.pno.filter_scale[1];
  1848. int32_t c2 = state_.pno.filter_coefficient[1];
  1849. int32_t y31 = state_.pno.filter_state[2][0];
  1850. int32_t y32 = state_.pno.filter_state[2][1];
  1851. int32_t s3 = state_.pno.filter_scale[2];
  1852. int32_t c3 = state_.pno.filter_coefficient[2];
  1853. while (size) {
  1854. uint32_t noise = Random::GetWord();
  1855. if ((noise & 0x7fffff) < density) {
  1856. amplitude = 65535;
  1857. int16_t noise_a = (noise & 0x0fff) - 0x800;
  1858. int16_t noise_b = ((noise >> 15) & 0x1fff) - 0x1000;
  1859. int16_t p1 = pitch_ + (3 * noise_a * parameter_[1] >> 17) + 0x600;
  1860. CONSTRAIN(p1, 0, 16383)
  1861. c1 = Interpolate824(lut_resonator_coefficient, p1 << 17);
  1862. s1 = Interpolate824(lut_resonator_scale, p1 << 17);
  1863. int16_t p2 = pitch_ + (noise_a * parameter_[1] >> 15) + 0x980;
  1864. CONSTRAIN(p2, 0, 16383)
  1865. c2 = Interpolate824(lut_resonator_coefficient, p2 << 17);
  1866. s2 = Interpolate824(lut_resonator_scale, p2 << 17);
  1867. int16_t p3 = pitch_ + (noise_b * parameter_[1] >> 16) + 0x790;
  1868. CONSTRAIN(p3, 0, 16383)
  1869. c3 = Interpolate824(lut_resonator_coefficient, p3 << 17);
  1870. s3 = Interpolate824(lut_resonator_scale, p3 << 17);
  1871. c1 = c1 * kResonanceFactor >> 15;
  1872. c2 = c2 * kResonanceFactor >> 15;
  1873. c3 = c3 * kResonanceFactor >> 15;
  1874. }
  1875. sample = (static_cast<int16_t>(noise) * amplitude) >> 16;
  1876. amplitude = (amplitude * kParticleNoiseDecay) >> 16;
  1877. if (sample > 0) {
  1878. y10 = sample * s1 >> 16;
  1879. y20 = sample * s2 >> 16;
  1880. y30 = sample * s3 >> 16;
  1881. } else {
  1882. y10 = -((-sample) * s1 >> 16);
  1883. y20 = -((-sample) * s2 >> 16);
  1884. y30 = -((-sample) * s3 >> 16);
  1885. }
  1886. y10 += y11 * c1 >> 15;
  1887. y10 -= y12 * kResonanceSquared >> 15;
  1888. CLIP(y10);
  1889. y12 = y11;
  1890. y11 = y10;
  1891. y20 += y21 * c2 >> 15;
  1892. y20 -= y22 * kResonanceSquared >> 15;
  1893. CLIP(y20);
  1894. y22 = y21;
  1895. y21 = y20;
  1896. y30 += y31 * c3 >> 15;
  1897. y30 -= y32 * kResonanceSquared >> 15;
  1898. CLIP(y30);
  1899. y32 = y31;
  1900. y31 = y30;
  1901. y10 += y20 + y30;
  1902. CLIP(y10)
  1903. *buffer++ = y10;
  1904. *buffer++ = y10;
  1905. size -= 2;
  1906. }
  1907. state_.pno.amplitude = amplitude;
  1908. state_.pno.filter_state[0][0] = y11;
  1909. state_.pno.filter_state[0][1] = y12;
  1910. state_.pno.filter_scale[0] = s1;
  1911. state_.pno.filter_coefficient[0] = c1;
  1912. state_.pno.filter_state[1][0] = y21;
  1913. state_.pno.filter_state[1][1] = y22;
  1914. state_.pno.filter_scale[1] = s2;
  1915. state_.pno.filter_coefficient[1] = c2;
  1916. state_.pno.filter_state[2][0] = y31;
  1917. state_.pno.filter_state[2][1] = y32;
  1918. state_.pno.filter_scale[2] = s3;
  1919. state_.pno.filter_coefficient[2] = c3;
  1920. }
  1921. static const int32_t kConstellationQ[] = { 23100, -23100, -23100, 23100 };
  1922. static const int32_t kConstellationI[] = { 23100, 23100, -23100, -23100 };
  1923. void DigitalOscillator::RenderDigitalModulation(
  1924. const uint8_t* sync,
  1925. int16_t* buffer,
  1926. size_t size) {
  1927. uint32_t phase = phase_;
  1928. uint32_t increment = phase_increment_;
  1929. uint32_t symbol_stream_phase = state_.dmd.symbol_phase;
  1930. uint32_t symbol_stream_phase_increment = ComputePhaseIncrement(
  1931. pitch_ - 1536 + ((parameter_[0] - 32767) >> 3));
  1932. uint8_t data_byte = state_.dmd.data_byte;
  1933. if (strike_) {
  1934. state_.dmd.symbol_count = 0;
  1935. strike_ = false;
  1936. }
  1937. while (size--) {
  1938. phase += increment;
  1939. symbol_stream_phase += symbol_stream_phase_increment;
  1940. if (symbol_stream_phase < symbol_stream_phase_increment) {
  1941. ++state_.dmd.symbol_count;
  1942. if (!(state_.dmd.symbol_count & 3)) {
  1943. if (state_.dmd.symbol_count >= (64 + 4 * 256)) {
  1944. state_.dmd.symbol_count = 0;
  1945. }
  1946. if (state_.dmd.symbol_count < 32) {
  1947. data_byte = 0x00;
  1948. } else if (state_.dmd.symbol_count < 48) {
  1949. data_byte = 0x99;
  1950. } else if (state_.dmd.symbol_count < 64) {
  1951. data_byte = 0xcc;
  1952. } else {
  1953. state_.dmd.filter_state = (state_.dmd.filter_state * 3 + \
  1954. static_cast<int32_t>(parameter_[1])) >> 2;
  1955. data_byte = state_.dmd.filter_state >> 7;
  1956. }
  1957. } else {
  1958. data_byte >>= 2;
  1959. }
  1960. }
  1961. int16_t i = Interpolate824(wav_sine, phase);
  1962. int16_t q = Interpolate824(wav_sine, phase + (1 << 30));
  1963. *buffer++ = (kConstellationQ[data_byte & 3] * q >> 15) + \
  1964. (kConstellationI[data_byte & 3] * i >> 15);
  1965. }
  1966. phase_ = phase;
  1967. state_.dmd.symbol_phase = symbol_stream_phase;
  1968. state_.dmd.data_byte = data_byte;
  1969. }
  1970. void DigitalOscillator::RenderQuestionMark(
  1971. const uint8_t* sync,
  1972. int16_t* buffer,
  1973. size_t size) {
  1974. ClockedNoiseState* state = &state_.clk;
  1975. if (strike_) {
  1976. state->rng_state = 0;
  1977. state->cycle_phase = 0;
  1978. state->sample = 10;
  1979. state->cycle_phase_increment = -1;
  1980. state->seed = 32767;
  1981. strike_ = false;
  1982. }
  1983. uint32_t phase = phase_;
  1984. uint32_t increment = phase_increment_;
  1985. uint32_t dit_duration = 3600 + ((32767 - parameter_[0]) >> 2);
  1986. int32_t noise_threshold = 1024 + (parameter_[1] >> 3);
  1987. while (size--) {
  1988. phase += increment;
  1989. int32_t sample;
  1990. if (state->rng_state) {
  1991. sample = (Interpolate824(wav_sine, phase) * 3) >> 2;
  1992. } else {
  1993. sample = 0;
  1994. }
  1995. if (++state->cycle_phase > dit_duration) {
  1996. --state->sample;
  1997. if (state->sample == 0) {
  1998. ++state->cycle_phase_increment;
  1999. state->rng_state = !state->rng_state;
  2000. size_t address = state->cycle_phase_increment >> 2;
  2001. size_t shift = (state->cycle_phase_increment & 0x3) << 1;
  2002. state->sample = (2 << ((wt_code[address] >> shift) & 3)) - 1;
  2003. if (state->sample == 15) {
  2004. state->sample = 100;
  2005. state->rng_state = 0;
  2006. state->cycle_phase_increment = - 1;
  2007. }
  2008. phase = 1L << 30;
  2009. }
  2010. state->cycle_phase = 0;
  2011. }
  2012. state->seed += Random::GetSample() >> 2;
  2013. int32_t noise_intensity = state->seed >> 8;
  2014. if (noise_intensity < 0) {
  2015. noise_intensity = -noise_intensity;
  2016. }
  2017. if (noise_intensity < noise_threshold) {
  2018. noise_intensity = noise_threshold;
  2019. }
  2020. if (noise_intensity > 16000) {
  2021. noise_intensity = 16000;
  2022. }
  2023. int32_t noise = (Random::GetSample() * noise_intensity >> 15);
  2024. noise = noise * wav_sine[(phase >> 22) & 0xff] >> 15;
  2025. sample += noise;
  2026. CLIP(sample);
  2027. int32_t distorted = sample * sample >> 14;
  2028. sample += distorted * parameter_[1] >> 15;
  2029. CLIP(sample);
  2030. *buffer++ = sample;
  2031. }
  2032. phase_ = phase;
  2033. }
  2034. void DigitalOscillator::RenderKick(
  2035. const uint8_t* sync,
  2036. int16_t* buffer,
  2037. size_t size) {
  2038. if (init_) {
  2039. pulse_[0].Init();
  2040. pulse_[0].set_delay(0);
  2041. pulse_[0].set_decay(3340);
  2042. pulse_[1].Init();
  2043. pulse_[1].set_delay(1.0e-3 * 48000);
  2044. pulse_[1].set_decay(3072);
  2045. pulse_[2].Init();
  2046. pulse_[2].set_delay(4.0e-3 * 48000);
  2047. pulse_[2].set_decay(4093);
  2048. svf_[0].Init();
  2049. svf_[0].set_punch(32768);
  2050. svf_[0].set_mode(SVF_MODE_BP);
  2051. init_ = false;
  2052. }
  2053. if (strike_) {
  2054. strike_ = false;
  2055. pulse_[0].Trigger(12 * 32768 * 0.7);
  2056. pulse_[1].Trigger(-19662 * 0.7);
  2057. pulse_[2].Trigger(18000);
  2058. svf_[0].set_punch(24000);
  2059. }
  2060. uint32_t decay = parameter_[0];
  2061. uint32_t scaled = 65535 - (decay << 1);
  2062. uint32_t squared = scaled * scaled >> 16;
  2063. scaled = squared * scaled >> 18;
  2064. svf_[0].set_resonance(32768 - 128 - scaled);
  2065. uint32_t coefficient = parameter_[1];
  2066. coefficient = coefficient * coefficient >> 15;
  2067. coefficient = coefficient * coefficient >> 15;
  2068. int32_t lp_coefficient = 128 + (coefficient >> 1) * 3;
  2069. int32_t lp_state = state_.svf.lp;
  2070. while (size) {
  2071. int32_t excitation = 0;
  2072. excitation += pulse_[0].Process();
  2073. excitation += !pulse_[1].done() ? 16384 : 0;
  2074. excitation += pulse_[1].Process();
  2075. pulse_[2].Process();
  2076. svf_[0].set_frequency(pitch_ + (pulse_[2].done() ? 0 : 17 << 7));
  2077. for (int32_t j = 0; j < 2; ++j) {
  2078. int32_t resonator_output, output;
  2079. resonator_output = (excitation >> 4) + svf_[0].Process(excitation);
  2080. lp_state += (resonator_output - lp_state) * lp_coefficient >> 15;
  2081. CLIP(lp_state);
  2082. output = lp_state;
  2083. *buffer++ = output;
  2084. }
  2085. size -= 2;
  2086. }
  2087. state_.svf.lp = lp_state;
  2088. }
  2089. void DigitalOscillator::RenderSnare(
  2090. const uint8_t* sync,
  2091. int16_t* buffer,
  2092. size_t size) {
  2093. if (init_) {
  2094. pulse_[0].Init();
  2095. pulse_[0].set_delay(0);
  2096. pulse_[0].set_decay(1536);
  2097. pulse_[1].Init();
  2098. pulse_[1].set_delay(1e-3 * 48000);
  2099. pulse_[1].set_decay(3072);
  2100. pulse_[2].Init();
  2101. pulse_[2].set_delay(1e-3 * 48000);
  2102. pulse_[2].set_decay(1200);
  2103. pulse_[3].Init();
  2104. pulse_[3].set_delay(0);
  2105. svf_[0].Init();
  2106. svf_[1].Init();
  2107. svf_[2].Init();
  2108. svf_[2].set_resonance(2000);
  2109. svf_[2].set_mode(SVF_MODE_BP);
  2110. init_ = false;
  2111. }
  2112. if (strike_) {
  2113. int32_t decay = 49152 - pitch_;
  2114. decay += parameter_[1] < 16384 ? 0 : parameter_[1] - 16384;
  2115. if (decay > 65535) {
  2116. decay = 65535;
  2117. }
  2118. svf_[0].set_resonance(29000 + (decay >> 5));
  2119. svf_[1].set_resonance(26500 + (decay >> 5));
  2120. pulse_[3].set_decay(4092 + (decay >> 14));
  2121. pulse_[0].Trigger(15 * 32768);
  2122. pulse_[1].Trigger(-1 * 32768);
  2123. pulse_[2].Trigger(13107);
  2124. int32_t snappy = parameter_[1];
  2125. if (snappy >= 14336) {
  2126. snappy = 14336;
  2127. }
  2128. pulse_[3].Trigger(512 + (snappy << 1));
  2129. strike_ = false;
  2130. }
  2131. svf_[0].set_frequency(pitch_ + (12 << 7));
  2132. svf_[1].set_frequency(pitch_ + (24 << 7));
  2133. svf_[2].set_frequency(pitch_ + (60 << 7));
  2134. int32_t g_1 = 22000 - (parameter_[0] >> 1);
  2135. int32_t g_2 = 22000 + (parameter_[0] >> 1);
  2136. while (size) {
  2137. int32_t excitation_1 = 0;
  2138. excitation_1 += pulse_[0].Process();
  2139. excitation_1 += pulse_[1].Process();
  2140. excitation_1 += !pulse_[1].done() ? 2621 : 0;
  2141. int32_t excitation_2 = 0;
  2142. excitation_2 += pulse_[2].Process();
  2143. excitation_2 += !pulse_[2].done() ? 13107 : 0;
  2144. int32_t noise_sample = Random::GetSample() * pulse_[3].Process() >> 15;
  2145. int32_t sd = 0;
  2146. sd += (svf_[0].Process(excitation_1) + (excitation_1 >> 4)) * g_1 >> 15;
  2147. sd += (svf_[1].Process(excitation_2) + (excitation_2 >> 4)) * g_2 >> 15;
  2148. sd += svf_[2].Process(noise_sample);
  2149. CLIP(sd);
  2150. *buffer++ = sd;
  2151. *buffer++ = sd;
  2152. size -= 2;
  2153. }
  2154. }
  2155. void DigitalOscillator::RenderCymbal(
  2156. const uint8_t* sync,
  2157. int16_t* buffer,
  2158. size_t size) {
  2159. if (init_) {
  2160. svf_[0].Init();
  2161. svf_[0].set_mode(SVF_MODE_BP);
  2162. svf_[0].set_resonance(12000);
  2163. svf_[1].Init();
  2164. svf_[1].set_mode(SVF_MODE_HP);
  2165. svf_[1].set_resonance(2000);
  2166. init_ = false;
  2167. }
  2168. HatState* hat = &state_.hat;
  2169. uint32_t increments[7];
  2170. int32_t note = (40 << 7) + (pitch_ >> 1);
  2171. increments[0] = ComputePhaseIncrement(note);
  2172. uint32_t root = increments[0] >> 10;
  2173. increments[1] = root * 24273 >> 4;
  2174. increments[2] = root * 12561 >> 4;
  2175. increments[3] = root * 18417 >> 4;
  2176. increments[4] = root * 22452 >> 4;
  2177. increments[5] = root * 31858 >> 4;
  2178. increments[6] = increments[0] * 24;
  2179. int32_t xfade = parameter_[1];
  2180. svf_[0].set_frequency(parameter_[0] >> 1);
  2181. svf_[1].set_frequency(parameter_[0] >> 1);
  2182. while (size--) {
  2183. phase_ += increments[6];
  2184. if (phase_ < increments[6]) {
  2185. hat->rng_state = hat->rng_state * 1664525L + 1013904223L;
  2186. }
  2187. hat->phase[0] += increments[0];
  2188. hat->phase[1] += increments[1];
  2189. hat->phase[2] += increments[2];
  2190. hat->phase[3] += increments[3];
  2191. hat->phase[4] += increments[4];
  2192. hat->phase[5] += increments[5];
  2193. int32_t hat_noise = 0;
  2194. hat_noise += hat->phase[0] >> 31;
  2195. hat_noise += hat->phase[1] >> 31;
  2196. hat_noise += hat->phase[2] >> 31;
  2197. hat_noise += hat->phase[3] >> 31;
  2198. hat_noise += hat->phase[4] >> 31;
  2199. hat_noise += hat->phase[5] >> 31;
  2200. hat_noise -= 3;
  2201. hat_noise *= 5461;
  2202. hat_noise = svf_[0].Process(hat_noise);
  2203. CLIP(hat_noise)
  2204. int32_t noise = (hat->rng_state >> 16) - 32768;
  2205. noise = svf_[1].Process(noise >> 1);
  2206. CLIP(noise)
  2207. *buffer++ = hat_noise + ((noise - hat_noise) * xfade >> 15);
  2208. }
  2209. }
  2210. /*
  2211. void DigitalOscillator::RenderYourAlgo(
  2212. const uint8_t* sync,
  2213. int16_t* buffer,
  2214. size_t size) {
  2215. while (size--) {
  2216. *buffer++ = 0;
  2217. }
  2218. }
  2219. */
  2220. /* static */
  2221. DigitalOscillator::RenderFn DigitalOscillator::fn_table_[] = {
  2222. &DigitalOscillator::RenderTripleRingMod,
  2223. &DigitalOscillator::RenderSawSwarm,
  2224. &DigitalOscillator::RenderComb,
  2225. &DigitalOscillator::RenderToy,
  2226. &DigitalOscillator::RenderDigitalFilter,
  2227. &DigitalOscillator::RenderDigitalFilter,
  2228. &DigitalOscillator::RenderDigitalFilter,
  2229. &DigitalOscillator::RenderDigitalFilter,
  2230. &DigitalOscillator::RenderVosim,
  2231. &DigitalOscillator::RenderVowel,
  2232. &DigitalOscillator::RenderVowelFof,
  2233. &DigitalOscillator::RenderHarmonics,
  2234. &DigitalOscillator::RenderFm,
  2235. &DigitalOscillator::RenderFeedbackFm,
  2236. &DigitalOscillator::RenderChaoticFeedbackFm,
  2237. &DigitalOscillator::RenderPlucked,
  2238. &DigitalOscillator::RenderBowed,
  2239. &DigitalOscillator::RenderBlown,
  2240. &DigitalOscillator::RenderFluted,
  2241. &DigitalOscillator::RenderStruckBell,
  2242. &DigitalOscillator::RenderStruckDrum,
  2243. &DigitalOscillator::RenderKick,
  2244. &DigitalOscillator::RenderCymbal,
  2245. &DigitalOscillator::RenderSnare,
  2246. &DigitalOscillator::RenderWavetables,
  2247. &DigitalOscillator::RenderWaveMap,
  2248. &DigitalOscillator::RenderWaveLine,
  2249. &DigitalOscillator::RenderWaveParaphonic,
  2250. &DigitalOscillator::RenderFilteredNoise,
  2251. &DigitalOscillator::RenderTwinPeaksNoise,
  2252. &DigitalOscillator::RenderClockedNoise,
  2253. &DigitalOscillator::RenderGranularCloud,
  2254. &DigitalOscillator::RenderParticleNoise,
  2255. &DigitalOscillator::RenderDigitalModulation,
  2256. // &DigitalOscillator::RenderYourAlgo,
  2257. &DigitalOscillator::RenderQuestionMark
  2258. };
  2259. } // namespace braids