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.

779 lines
24KB

  1. // Copyright 2013 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. // Tidal generator.
  28. #include "tides/generator.h"
  29. #include <algorithm>
  30. #include <cmath>
  31. #include "stmlib/utils/dsp.h"
  32. #include "tides/resources.h"
  33. namespace tides {
  34. using namespace std;
  35. using namespace stmlib;
  36. const int16_t kOctave = 12 * 128;
  37. const uint16_t kSlopeBits = 12;
  38. const uint32_t kSyncCounterMaxTime = 8 * 48000;
  39. const int32_t kDownsampleCoefficient[4] = { 17162, 19069, 17162, 12140 };
  40. /* static */
  41. const FrequencyRatio Generator::frequency_ratios_[] = {
  42. { 1, 1 },
  43. { 5, 4 },
  44. { 4, 3 },
  45. { 3, 2 },
  46. { 5, 3 },
  47. { 2, 1 },
  48. { 3, 1 },
  49. { 4, 1 },
  50. { 6, 1 },
  51. { 8, 1 },
  52. { 12, 1 },
  53. { 16, 1 },
  54. };
  55. /* static */
  56. const int16_t Generator::num_frequency_ratios_ = \
  57. sizeof(Generator::frequency_ratios_) / sizeof(FrequencyRatio);
  58. void Generator::Init() {
  59. mode_ = GENERATOR_MODE_LOOPING;
  60. range_ = GENERATOR_RANGE_HIGH;
  61. clock_divider_ = 1;
  62. phase_ = 0;
  63. set_pitch(60 << 7);
  64. pattern_predictor_.Init();
  65. GeneratorSample s;
  66. s.flags = 0;
  67. s.unipolar = 0;
  68. s.bipolar = 0;
  69. for (size_t i = 0; i < kNumBlocks; ++i) {
  70. fill(&output_samples_[i][0], &output_samples_[i][kBlockSize], s);
  71. fill(&input_samples_[i][0], &input_samples_[i][kBlockSize], 0);
  72. }
  73. playback_block_ = kNumBlocks / 2;
  74. render_block_ = 0;
  75. current_sample_ = 0;
  76. shape_ = 0;
  77. slope_ = 0;
  78. smoothed_slope_ = 0;
  79. smoothness_ = 0;
  80. previous_sample_.unipolar = previous_sample_.bipolar = 0;
  81. running_ = false;
  82. ClearFilterState();
  83. sync_counter_ = kSyncCounterMaxTime;
  84. frequency_ratio_.p = 1;
  85. frequency_ratio_.q = 1;
  86. sync_ = false;
  87. phase_increment_ = 9448928;
  88. local_osc_phase_increment_ = phase_increment_;
  89. target_phase_increment_ = phase_increment_;
  90. }
  91. void Generator::ComputeFrequencyRatio(int16_t pitch) {
  92. int16_t delta = previous_pitch_ - pitch;
  93. // Hysteresis for preventing glitchy transitions.
  94. if (delta < 96 && delta > -96) {
  95. return;
  96. }
  97. previous_pitch_ = pitch;
  98. // Corresponds to a 0V CV after calibration
  99. pitch -= (36 << 7);
  100. // The range of the control panel knob is 4 octaves.
  101. pitch = pitch * 12 / (48 << 7);
  102. bool swap = false;
  103. if (pitch < 0) {
  104. pitch = -pitch;
  105. swap = true;
  106. }
  107. if (pitch >= num_frequency_ratios_) {
  108. pitch = num_frequency_ratios_ - 1;
  109. }
  110. frequency_ratio_ = frequency_ratios_[pitch];
  111. if (swap) {
  112. frequency_ratio_.q = frequency_ratio_.p;
  113. frequency_ratio_.p = frequency_ratios_[pitch].q;
  114. }
  115. }
  116. uint32_t Generator::ComputePhaseIncrement(int16_t pitch) {
  117. int16_t num_shifts = 0;
  118. while (pitch < 0) {
  119. pitch += kOctave;
  120. --num_shifts;
  121. }
  122. while (pitch >= kOctave) {
  123. pitch -= kOctave;
  124. ++num_shifts;
  125. }
  126. // Lookup phase increment
  127. uint32_t a = lut_increments[pitch >> 4];
  128. uint32_t b = lut_increments[(pitch >> 4) + 1];
  129. uint32_t phase_increment = a + ((b - a) * (pitch & 0xf) >> 4);
  130. // Compensate for downsampling
  131. phase_increment *= clock_divider_;
  132. return num_shifts >= 0
  133. ? phase_increment << num_shifts
  134. : phase_increment >> -num_shifts;
  135. }
  136. int16_t Generator::ComputePitch(uint32_t phase_increment) {
  137. uint32_t first = lut_increments[0];
  138. uint32_t last = lut_increments[LUT_INCREMENTS_SIZE - 2];
  139. int16_t pitch = 0;
  140. if (phase_increment == 0) {
  141. phase_increment = 1;
  142. }
  143. phase_increment /= clock_divider_;
  144. while (phase_increment > last) {
  145. phase_increment >>= 1;
  146. pitch += kOctave;
  147. }
  148. while (phase_increment < first) {
  149. phase_increment <<= 1;
  150. pitch -= kOctave;
  151. }
  152. pitch += (std::lower_bound(
  153. lut_increments,
  154. lut_increments + LUT_INCREMENTS_SIZE,
  155. phase_increment) - lut_increments) << 4;
  156. return pitch;
  157. }
  158. int32_t Generator::ComputeCutoffFrequency(int16_t pitch, int16_t smoothness) {
  159. size_t shifts = clock_divider_;
  160. while (shifts > 1) {
  161. shifts >>= 1;
  162. pitch += kOctave;
  163. }
  164. int32_t frequency;
  165. if (smoothness > 0) {
  166. frequency = 256 << 7;
  167. } else if (smoothness > -16384) {
  168. int32_t start = pitch + (36 << 7);
  169. int32_t end = 256 << 7;
  170. frequency = start + ((end - start) * (smoothness + 16384) >> 14);
  171. } else {
  172. int32_t start = pitch - (36 << 7);
  173. int32_t end = pitch + (36 << 7);
  174. frequency = start + ((end - start) * (smoothness + 32768) >> 14);
  175. }
  176. frequency += 32768;
  177. if (frequency < 0) {
  178. frequency = 0;
  179. }
  180. return frequency;
  181. }
  182. int32_t Generator::ComputeAntialiasAttenuation(
  183. int16_t pitch,
  184. int16_t slope,
  185. int16_t shape,
  186. int16_t smoothness) const {
  187. pitch += 12 * 128;
  188. if (pitch < 0) pitch = 0;
  189. if (slope < 0) slope = ~slope;
  190. if (shape < 0) shape = ~shape;
  191. if (smoothness < 0) smoothness = 0;
  192. int32_t p = 252059;
  193. p += -76 * smoothness >> 5;
  194. p += -30 * shape >> 5;
  195. p += -102 * slope >> 5;
  196. p += -664 * pitch >> 5;
  197. p += 31 * (smoothness * shape >> 16) >> 5;
  198. p += 12 * (smoothness * slope >> 16) >> 5;
  199. p += 14 * (shape * slope >> 16) >> 5;
  200. p += 219 * (pitch * smoothness >> 16) >> 5;
  201. p += 50 * (pitch * shape >> 16) >> 5;
  202. p += 425 * (pitch * slope >> 16) >> 5;
  203. p += 13 * (smoothness * smoothness >> 16) >> 5;
  204. p += 1 * (shape * shape >> 16) >> 5;
  205. p += -11 * (slope * slope >> 16) >> 5;
  206. p += 776 * (pitch * pitch >> 16) >> 5;
  207. if (p < 0) p = 0;
  208. if (p > 32767) p = 32767;
  209. return p;
  210. }
  211. void Generator::ProcessFilterWavefolder(
  212. GeneratorSample* in_out, size_t size) {
  213. int32_t frequency = ComputeCutoffFrequency(pitch_, smoothness_);
  214. int32_t f_a = lut_cutoff[frequency >> 7] >> 16;
  215. int32_t f_b = lut_cutoff[(frequency >> 7) + 1] >> 16;
  216. int32_t f = f_a + ((f_b - f_a) * (frequency & 0x7f) >> 7);
  217. int32_t wf_gain = 2048;
  218. int32_t wf_balance = 0;
  219. if (smoothness_ > 0) {
  220. int16_t attenuated_smoothness = smoothness_ * attenuation_ >> 15;
  221. wf_gain += attenuated_smoothness * (32767 - 1024) >> 14;
  222. wf_balance = attenuated_smoothness;
  223. }
  224. int32_t uni_lp_state_0 = uni_lp_state_[0];
  225. int32_t uni_lp_state_1 = uni_lp_state_[1];
  226. int32_t bi_lp_state_0 = bi_lp_state_[0];
  227. int32_t bi_lp_state_1 = bi_lp_state_[1];
  228. while (size--) {
  229. int32_t original, folded;
  230. // Run through LPF.
  231. bi_lp_state_0 += f * (in_out->bipolar - bi_lp_state_0) >> 15;
  232. bi_lp_state_1 += f * (bi_lp_state_0 - bi_lp_state_1) >> 15;
  233. // Fold.
  234. original = bi_lp_state_1;
  235. folded = Interpolate1022(wav_bipolar_fold, original * wf_gain + (1UL << 31));
  236. in_out->bipolar = original + ((folded - original) * wf_balance >> 15);
  237. // Run through LPF.
  238. uni_lp_state_0 += f * (in_out->unipolar - uni_lp_state_0) >> 15;
  239. uni_lp_state_1 += f * (uni_lp_state_0 - uni_lp_state_1) >> 15;
  240. // Fold.
  241. original = uni_lp_state_1 << 1;
  242. folded = Interpolate1022(wav_unipolar_fold, original * wf_gain) << 1;
  243. in_out->unipolar = original + ((folded - original) * wf_balance >> 15);
  244. uni_lp_state_[0] = uni_lp_state_0;
  245. uni_lp_state_[1] = uni_lp_state_1;
  246. bi_lp_state_[0] = bi_lp_state_0;
  247. bi_lp_state_[1] = bi_lp_state_1;
  248. in_out++;
  249. }
  250. uni_lp_state_[0] = uni_lp_state_0;
  251. uni_lp_state_[1] = uni_lp_state_1;
  252. bi_lp_state_[0] = bi_lp_state_0;
  253. bi_lp_state_[1] = bi_lp_state_1;
  254. }
  255. void Generator::ProcessAudioRate(
  256. const uint8_t* in, GeneratorSample* out, size_t size) {
  257. GeneratorSample sample = previous_sample_;
  258. if (sync_) {
  259. pitch_ = ComputePitch(phase_increment_);
  260. CONSTRAIN(pitch_, 0, 120 << 7);
  261. } else {
  262. CONSTRAIN(pitch_, 0, 120 << 7);
  263. phase_increment_ = ComputePhaseIncrement(pitch_);
  264. local_osc_phase_increment_ = phase_increment_;
  265. target_phase_increment_ = phase_increment_;
  266. }
  267. attenuation_ = ComputeAntialiasAttenuation(
  268. pitch_,
  269. slope_,
  270. shape_,
  271. smoothness_);
  272. uint16_t shape = static_cast<uint16_t>((shape_ * attenuation_ >> 15) + 32768);
  273. uint16_t wave_index = WAV_INVERSE_TAN_AUDIO + (shape >> 14);
  274. const int16_t* shape_1 = waveform_table[wave_index];
  275. const int16_t* shape_2 = waveform_table[wave_index + 1];
  276. uint16_t shape_xfade = shape << 2;
  277. uint32_t end_of_attack = (static_cast<uint32_t>(slope_ + 32768) << 16);
  278. // Load state into registers - saves some memory load/store inside the
  279. // rendering loop.
  280. uint32_t phase = phase_;
  281. uint32_t phase_increment = phase_increment_;
  282. bool wrap = wrap_;
  283. // Enforce that the EOA pulse is at least 1 sample wide.
  284. if (end_of_attack >= phase_increment) {
  285. end_of_attack -= phase_increment;
  286. }
  287. if (end_of_attack < phase_increment) {
  288. end_of_attack = phase_increment;
  289. }
  290. uint32_t mid_point = mid_point_;
  291. int32_t next_sample = next_sample_;
  292. while (size--) {
  293. ++sync_counter_;
  294. uint8_t control = *in++;
  295. // When freeze is high, discard any start/reset command.
  296. if (!(control & CONTROL_FREEZE)) {
  297. if (control & CONTROL_GATE_RISING) {
  298. phase = 0;
  299. running_ = true;
  300. } else if (mode_ != GENERATOR_MODE_LOOPING && wrap) {
  301. phase = 0;
  302. running_ = false;
  303. }
  304. }
  305. if (sync_) {
  306. if (control & CONTROL_CLOCK_RISING) {
  307. ++sync_edges_counter_;
  308. if (sync_edges_counter_ >= frequency_ratio_.q) {
  309. sync_edges_counter_ = 0;
  310. if (sync_counter_ < kSyncCounterMaxTime && sync_counter_) {
  311. uint64_t increment = frequency_ratio_.p * static_cast<uint64_t>(
  312. 0xffffffff / sync_counter_);
  313. if (increment > 0x20000000) {
  314. increment = 0x20000000;
  315. }
  316. target_phase_increment_ = static_cast<uint32_t>(increment);
  317. local_osc_phase_ = 0;
  318. }
  319. sync_counter_ = 0;
  320. }
  321. }
  322. // Fast tracking of the local oscillator to the external oscillator.
  323. local_osc_phase_increment_ += static_cast<int32_t>(
  324. target_phase_increment_ - local_osc_phase_increment_) >> 8;
  325. local_osc_phase_ += local_osc_phase_increment_;
  326. // Slow phase realignment between the master oscillator and the local
  327. // oscillator.
  328. int32_t phase_error = local_osc_phase_ - phase;
  329. phase_increment = local_osc_phase_increment_ + (phase_error >> 13);
  330. }
  331. if (control & CONTROL_FREEZE) {
  332. *out++ = sample;
  333. continue;
  334. }
  335. bool sustained = mode_ == GENERATOR_MODE_AR
  336. && phase >= (1UL << 31)
  337. && control & CONTROL_GATE;
  338. if (sustained) {
  339. phase = 1L << 31;
  340. }
  341. mid_point = (mid_point >> 5) * 31;
  342. mid_point += (end_of_attack >> 5);
  343. uint32_t min_mid_point = 2 * phase_increment;
  344. uint32_t max_mid_point = 0xffffffff - min_mid_point;
  345. CONSTRAIN(mid_point, min_mid_point, max_mid_point);
  346. CONSTRAIN(mid_point, 0x10000, 0xffff0000);
  347. int32_t slope_up = static_cast<int32_t>(0xffffffff / (mid_point >> 16));
  348. int32_t slope_down = static_cast<int32_t>(0xffffffff / (~mid_point >> 16));
  349. int32_t this_sample = next_sample;
  350. next_sample = 0;
  351. // Process reset discontinuity.
  352. if (phase < phase_increment) {
  353. slope_up_ = true;
  354. uint32_t t = phase / (phase_increment >> 16);
  355. int32_t discontinuity = slope_up + slope_down;
  356. discontinuity = (discontinuity * (phase_increment >> 18)) >> 14;
  357. this_sample += ThisIntegratedBlepSample(t) * discontinuity >> 16;
  358. next_sample += NextIntegratedBlepSample(t) * discontinuity >> 16;
  359. } else {
  360. // Process transition discontinuity.
  361. if (slope_up_ ^ (phase < mid_point)) {
  362. slope_up_ = phase < mid_point;
  363. uint32_t t = (phase - mid_point) / (phase_increment >> 16);
  364. int32_t discontinuity = slope_up + slope_down;
  365. discontinuity = (discontinuity * (phase_increment >> 18)) >> 14;
  366. this_sample -= ThisIntegratedBlepSample(t) * discontinuity >> 16;
  367. next_sample -= NextIntegratedBlepSample(t) * discontinuity >> 16;
  368. }
  369. }
  370. next_sample += slope_up_
  371. ? ((phase >> 16) * slope_up) >> 16
  372. : 65535 - (((phase - mid_point) >> 16) * slope_down >> 16);
  373. CONSTRAIN(this_sample, 0, 65535);
  374. sample.bipolar = Crossfade115(shape_1, shape_2, this_sample, shape_xfade);
  375. sample.unipolar = Crossfade115(shape_1, shape_2, (this_sample >> 1) + 32768,
  376. shape_xfade);
  377. sample.flags = 0;
  378. bool looped = mode_ == GENERATOR_MODE_LOOPING && wrap;
  379. if (phase >= end_of_attack || !running_) {
  380. sample.flags |= FLAG_END_OF_ATTACK;
  381. }
  382. if (!running_ || looped) {
  383. eor_counter_ = phase_increment < 44739242 ? 48 : 1;
  384. }
  385. if (eor_counter_) {
  386. sample.flags |= FLAG_END_OF_RELEASE;
  387. --eor_counter_;
  388. }
  389. *out++ = sample;
  390. if (running_ && !sustained) {
  391. phase += phase_increment;
  392. wrap = phase < phase_increment;
  393. }
  394. if (!running_ && !sustained) {
  395. sample.bipolar = 0;
  396. sample.unipolar = 0;
  397. }
  398. }
  399. previous_sample_ = sample;
  400. phase_ = phase;
  401. phase_increment_ = phase_increment;
  402. wrap_ = wrap;
  403. next_sample_ = next_sample;
  404. mid_point_ = mid_point;
  405. }
  406. void Generator::ProcessControlRate(
  407. const uint8_t* in, GeneratorSample* out, size_t size) {
  408. if (sync_) {
  409. pitch_ = ComputePitch(phase_increment_);
  410. } else {
  411. phase_increment_ = ComputePhaseIncrement(pitch_);
  412. local_osc_phase_increment_ = phase_increment_;
  413. target_phase_increment_ = phase_increment_;
  414. }
  415. attenuation_ = 32767;
  416. GeneratorSample sample = previous_sample_;
  417. uint16_t shape = static_cast<uint16_t>(shape_ + 32768);
  418. shape = (shape >> 2) * 3;
  419. uint16_t wave_index = WAV_REVERSED_CONTROL + (shape >> 13);
  420. const int16_t* shape_1 = waveform_table[wave_index];
  421. const int16_t* shape_2 = waveform_table[wave_index + 1];
  422. uint16_t shape_xfade = shape << 3;
  423. // Load state into registers - saves some memory load/store inside the
  424. // rendering loop.
  425. uint32_t phase = phase_;
  426. uint32_t phase_increment = phase_increment_;
  427. bool wrap = wrap_;
  428. int32_t smoothed_slope = smoothed_slope_;
  429. int32_t previous_smoothed_slope = 0x7fffffff;
  430. uint32_t end_of_attack = 1UL << 31;
  431. uint32_t attack_factor = 1 << kSlopeBits;
  432. uint32_t decay_factor = 1 << kSlopeBits;
  433. while (size--) {
  434. sync_counter_++;
  435. // Low-pass filter the slope parameter.
  436. smoothed_slope += (slope_ - smoothed_slope) >> 4;
  437. uint8_t control = *in++;
  438. // When freeze is high, discard any start/reset command.
  439. if (!(control & CONTROL_FREEZE)) {
  440. if (control & CONTROL_GATE_RISING) {
  441. phase = 0;
  442. running_ = true;
  443. } else if (mode_ != GENERATOR_MODE_LOOPING && wrap) {
  444. running_ = false;
  445. phase = 0;
  446. }
  447. }
  448. if ((control & CONTROL_CLOCK_RISING) && sync_ && sync_counter_) {
  449. if (sync_counter_ >= kSyncCounterMaxTime) {
  450. phase = 0;
  451. } else {
  452. uint32_t predicted_period = sync_counter_ < 480
  453. ? sync_counter_
  454. : pattern_predictor_.Predict(sync_counter_);
  455. uint64_t increment = frequency_ratio_.p * static_cast<uint64_t>(
  456. 0xffffffff / (predicted_period * frequency_ratio_.q));
  457. if (increment > 0x20000000) {
  458. increment = 0x20000000;
  459. }
  460. phase_increment = static_cast<uint32_t>(increment);
  461. }
  462. sync_counter_ = 0;
  463. }
  464. if (control & CONTROL_FREEZE) {
  465. *out++ = sample;
  466. continue;
  467. }
  468. // Recompute the waveshaping parameters only when the slope has changed.
  469. if (smoothed_slope != previous_smoothed_slope) {
  470. uint32_t slope_offset = Interpolate88(
  471. lut_slope_compression, smoothed_slope + 32768);
  472. if (slope_offset <= 1) {
  473. decay_factor = 32768 << kSlopeBits;
  474. attack_factor = 1 << (kSlopeBits - 1);
  475. } else {
  476. decay_factor = (32768 << kSlopeBits) / slope_offset;
  477. attack_factor = (32768 << kSlopeBits) / (65536 - slope_offset);
  478. }
  479. previous_smoothed_slope = smoothed_slope;
  480. end_of_attack = slope_offset << 16;
  481. }
  482. uint32_t skewed_phase = phase;
  483. if (phase <= end_of_attack) {
  484. skewed_phase = (phase >> kSlopeBits) * decay_factor;
  485. } else {
  486. skewed_phase = ((phase - end_of_attack) >> kSlopeBits) * attack_factor;
  487. skewed_phase += 1L << 31;
  488. }
  489. bool sustained = mode_ == GENERATOR_MODE_AR
  490. && phase >= end_of_attack
  491. && control & CONTROL_GATE;
  492. if (sustained) {
  493. skewed_phase = 1L << 31;
  494. phase = end_of_attack + 1;
  495. }
  496. sample.unipolar = Crossfade115(
  497. shape_1,
  498. shape_2,
  499. skewed_phase >> 16, shape_xfade);
  500. sample.bipolar = Crossfade115(
  501. shape_1,
  502. shape_2,
  503. skewed_phase >> 15, shape_xfade);
  504. if (skewed_phase >= (1UL << 31)) {
  505. sample.bipolar = -sample.bipolar;
  506. }
  507. uint32_t adjusted_end_of_attack = end_of_attack;
  508. if (adjusted_end_of_attack >= phase_increment) {
  509. adjusted_end_of_attack -= phase_increment;
  510. }
  511. if (adjusted_end_of_attack < phase_increment) {
  512. adjusted_end_of_attack = phase_increment;
  513. }
  514. sample.flags = 0;
  515. bool looped = mode_ == GENERATOR_MODE_LOOPING && wrap;
  516. if (phase >= adjusted_end_of_attack || !running_ || sustained) {
  517. sample.flags |= FLAG_END_OF_ATTACK;
  518. }
  519. if (!running_ || looped) {
  520. eor_counter_ = phase_increment < 44739242 ? 48 : 1;
  521. }
  522. if (eor_counter_) {
  523. sample.flags |= FLAG_END_OF_RELEASE;
  524. --eor_counter_;
  525. }
  526. // Two special cases for the "pure decay" scenario:
  527. // END_OF_ATTACK is always true except at the initial trigger.
  528. if (end_of_attack == 0) {
  529. sample.flags |= FLAG_END_OF_ATTACK;
  530. }
  531. bool triggered = control & CONTROL_GATE_RISING;
  532. if ((sustained || end_of_attack == 0) && (triggered || looped)) {
  533. sample.flags &= ~FLAG_END_OF_ATTACK;
  534. }
  535. *out++ = sample;
  536. if (running_ && !sustained) {
  537. phase += phase_increment;
  538. wrap = phase < phase_increment;
  539. } else {
  540. wrap = false;
  541. }
  542. }
  543. previous_sample_ = sample;
  544. phase_ = phase;
  545. phase_increment_ = phase_increment;
  546. wrap_ = wrap;
  547. smoothed_slope_ = smoothed_slope;
  548. }
  549. void Generator::ProcessWavetable(
  550. const uint8_t* in, GeneratorSample* out, size_t size) {
  551. GeneratorSample sample = previous_sample_;
  552. if (sync_) {
  553. pitch_ = ComputePitch(phase_increment_);
  554. } else {
  555. phase_increment_ = ComputePhaseIncrement(pitch_);
  556. }
  557. uint32_t phase = phase_;
  558. uint32_t phase_increment = phase_increment_;
  559. // The grid is only 8x8 rather than 9x9 so we need to scale by 7/8.0
  560. uint16_t target_x = static_cast<uint16_t>(slope_ + 32768);
  561. target_x = target_x * 57344 >> 16;
  562. uint16_t x = x_;
  563. uint16_t x_increment = (target_x - x) / size;
  564. uint16_t target_y = static_cast<uint16_t>(shape_ + 32768);
  565. target_y = target_y * 57344 >> 16;
  566. uint16_t y = y_;
  567. uint16_t y_increment = (target_y - y) / size;
  568. int32_t wf_gain = smoothness_ > 0 ? smoothness_ : 0;
  569. wf_gain = wf_gain * wf_gain >> 15;
  570. int32_t frequency = ComputeCutoffFrequency(pitch_, smoothness_);
  571. int32_t f_a = lut_cutoff[frequency >> 7] >> 16;
  572. int32_t f_b = lut_cutoff[(frequency >> 7) + 1] >> 16;
  573. int32_t f = f_a + ((f_b - f_a) * (frequency & 0x7f) >> 7);
  574. int32_t lp_state_0 = bi_lp_state_[0];
  575. int32_t lp_state_1 = bi_lp_state_[1];
  576. const int16_t* bank = wt_waves + mode_ * 64 * 257 - (mode_ & 2) * 4 * 257;
  577. while (size--) {
  578. ++sync_counter_;
  579. uint8_t control = *in++;
  580. // When freeze is high, discard any start/reset command.
  581. if (!(control & CONTROL_FREEZE)) {
  582. if (control & CONTROL_GATE_RISING) {
  583. phase = 0;
  584. }
  585. }
  586. if (control & CONTROL_CLOCK_RISING) {
  587. if (sync_) {
  588. if (range_ == GENERATOR_RANGE_HIGH) {
  589. ++sync_edges_counter_;
  590. if (sync_edges_counter_ >= frequency_ratio_.q) {
  591. sync_edges_counter_ = 0;
  592. if (sync_counter_ < kSyncCounterMaxTime && sync_counter_) {
  593. uint64_t increment = frequency_ratio_.p * static_cast<uint64_t>(
  594. 0xffffffff / sync_counter_);
  595. if (increment > 0x20000000) {
  596. increment = 0x20000000;
  597. }
  598. target_phase_increment_ = static_cast<uint32_t>(increment);
  599. local_osc_phase_ = 0;
  600. }
  601. sync_counter_ = 0;
  602. }
  603. } else {
  604. if (sync_counter_ >= kSyncCounterMaxTime) {
  605. phase = 0;
  606. } else if (sync_counter_) {
  607. uint32_t predicted_period = sync_counter_ < 480
  608. ? sync_counter_
  609. : pattern_predictor_.Predict(sync_counter_);
  610. uint64_t increment = frequency_ratio_.p * static_cast<uint64_t>(
  611. 0xffffffff / (predicted_period * frequency_ratio_.q));
  612. if (increment > 0x20000000) {
  613. increment = 0x20000000;
  614. }
  615. phase_increment = static_cast<uint32_t>(increment);
  616. }
  617. sync_counter_ = 0;
  618. }
  619. } else {
  620. // Normal behaviour: switch banks.
  621. uint8_t bank_index = mode_ + 1;
  622. if (bank_index > 2) {
  623. bank_index = 0;
  624. }
  625. mode_ = static_cast<GeneratorMode>(bank_index);
  626. bank = wt_waves + mode_ * 64 * 257 - (mode_ & 2) * 4 * 257;
  627. }
  628. }
  629. // PLL stuff
  630. if (sync_ && range_ == GENERATOR_RANGE_HIGH) {
  631. // Fast tracking of the local oscillator to the external oscillator.
  632. local_osc_phase_increment_ += static_cast<int32_t>(
  633. target_phase_increment_ - local_osc_phase_increment_) >> 8;
  634. local_osc_phase_ += local_osc_phase_increment_;
  635. // Slow phase realignment between the master oscillator and the local
  636. // oscillator.
  637. int32_t phase_error = local_osc_phase_ - phase;
  638. phase_increment = local_osc_phase_increment_ + (phase_error >> 13);
  639. }
  640. x += x_increment;
  641. y += y_increment;
  642. if (control & CONTROL_FREEZE) {
  643. *out++ = sample;
  644. continue;
  645. }
  646. uint16_t x_integral = x >> 13;
  647. uint16_t y_integral = y >> 13;
  648. const int16_t* wave_1 = &bank[(x_integral + y_integral * 8) * 257];
  649. const int16_t* wave_2 = wave_1 + 257 * 8;
  650. uint16_t x_fractional = x << 3;
  651. int32_t y_fractional = (y << 2) & 0x7fff;
  652. int32_t s = 0;
  653. for (int32_t subsample = 0; subsample < 4; ++subsample) {
  654. int32_t y_1 = Crossfade(wave_1, wave_1 + 257, phase << 1, x_fractional);
  655. int32_t y_2 = Crossfade(wave_2, wave_2 + 257, phase << 1, x_fractional);
  656. int32_t y_mix = y_1 + ((y_2 - y_1) * y_fractional >> 15);
  657. int32_t folded = Interpolate1022(
  658. ws_smooth_bipolar_fold, (y_mix + 32768) << 16);
  659. y_mix = y_mix + ((folded - y_mix) * wf_gain >> 15);
  660. s += y_mix * kDownsampleCoefficient[subsample];
  661. phase += (phase_increment >> 3);
  662. }
  663. lp_state_0 += f * ((s >> 16) - lp_state_0) >> 15;
  664. lp_state_1 += f * (lp_state_0 - lp_state_1) >> 15;
  665. uint8_t flags = 0;
  666. sample.bipolar = lp_state_1;
  667. sample.unipolar = sample.bipolar + 32768;
  668. if (sample.unipolar & 0x8000) {
  669. flags |= FLAG_END_OF_ATTACK;
  670. }
  671. if (phase & 0x80000000) {
  672. flags |= FLAG_END_OF_RELEASE;
  673. }
  674. sample.flags = flags;
  675. *out++ = sample;
  676. }
  677. previous_sample_ = sample;
  678. phase_ = phase;
  679. phase_increment_ = phase_increment;
  680. x_ = x;
  681. y_ = y;
  682. bi_lp_state_[0] = lp_state_0;
  683. bi_lp_state_[1] = lp_state_1;
  684. }
  685. } // namespace tides