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