1 /* WhySynth DSSI software synthesizer plugin
2  *
3  * Copyright (C) 2004-2007, 2010, 2012 Sean Bolton and others.
4  *
5  * Portions of this file come from Steve Brookes' Xsynth,
6  * copyright (C) 1999 S. J. Brookes.
7  * Portions of this file come from Fons Adriaensen's VCO-plugins
8  * and MCP-plugins, copyright (C) 2003 Fons Adriaensen.
9  * Portions of this file may have come from Peter Hanappe's
10  * Fluidsynth, copyright (C) 2003 Peter Hanappe and others.
11  * Portions of this file may have come from Csound, copyright
12  * (C) 1999 Sean Costello, rasmus ekman, et. al.
13  * Portions of this file come from Nick Dowell's amSynth,
14  * copyright (c) 2001,2002 Nick Dowell.
15  * Chamberlin filter refactoring with high-pass and band-reject
16  * versions copyright (c) 2011 Luke Andrew.
17  *
18  * This program is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU General Public License as
20  * published by the Free Software Foundation; either version 2 of
21  * the License, or (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be
24  * useful, but WITHOUT ANY WARRANTY; without even the implied
25  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
26  * PURPOSE.  See the GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public
29  * License along with this program; if not, write to the Free
30  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
31  * Boston, MA 02110-1301 USA.
32  */
33 
34 #ifdef HAVE_CONFIG_H
35 #  include <config.h>
36 #endif
37 
38 #define _BSD_SOURCE    1
39 #define _SVID_SOURCE   1
40 #define _ISOC99_SOURCE 1
41 
42 #include <stdlib.h>
43 #include <math.h>
44 
45 #include <ladspa.h>
46 
47 #include "whysynth.h"
48 #include "dssp_event.h"
49 #include "whysynth_voice.h"
50 #include "wave_tables.h"
51 #include "agran_oscillator.h"
52 #include "padsynth.h"
53 
54 #include "whysynth_voice_inline.h"
55 
56 #define M_2PI_F (2.0f * (float)M_PI)
57 #define M_PI_F (float)M_PI
58 
59 static int   tables_initialized = 0;
60 
61 float        y_pitch[129];
62 
63 float        sine_wave[4 + SINETABLE_POINTS + 1];
64 
65 float        volume_cv_to_amplitude_table[257];
66 
67 #define VOLUME_TO_AMPLITUDE_SCALE 128
68 
69 static float volume_to_amplitude_table[4 + VOLUME_TO_AMPLITUDE_SCALE + 2]; /* -FIX- get rid of this */
70 
71 void
y_init_tables(void)72 y_init_tables(void)
73 {
74     int i;
75     float pexp;
76     float volume, volume_exponent;
77 
78     if (tables_initialized)
79         return;
80 
81     /* oscillator waveforms */
82     for (i = 0; i <= SINETABLE_POINTS; ++i) {
83         sine_wave[i + 4] = sinf(M_2PI_F * (float)i / (float)SINETABLE_POINTS) * 0.5f;
84     }
85     sine_wave[-1 + 4] = sine_wave[SINETABLE_POINTS - 1 + 4];  /* guard points both ends */
86 
87     /* MIDI note to pitch */
88     for (i = 0; i <= 128; ++i) {
89         pexp = (float)(i - 69) / 12.0f;
90         y_pitch[i] = powf(2.0f, pexp);
91     }
92 
93     /* volume to amplitude
94      *
95      * This generates a curve which is:
96      *  volume_to_amplitude_table[128 + 4] = 0.25 * 3.16...   ~=  -2dB
97      *  volume_to_amplitude_table[64 + 4]  = 0.25 * 1.0       ~= -12dB  (yields -18dB per voice nominal)
98      *  volume_to_amplitude_table[32 + 4]  = 0.25 * 0.316...  ~= -22dB
99      *  volume_to_amplitude_table[16 + 4]  = 0.25 * 0.1       ~= -32dB
100      *   etc.
101      */
102     volume_exponent = 1.0f / (2.0f * log10f(2.0f));
103     for (i = 0; i <= VOLUME_TO_AMPLITUDE_SCALE; i++) {
104         volume = (float)i / (float)VOLUME_TO_AMPLITUDE_SCALE;
105         volume_to_amplitude_table[i + 4] = powf(2.0f * volume, volume_exponent) / 4.0f;
106     }
107     volume_to_amplitude_table[ -1 + 4] = 0.0f;
108     volume_to_amplitude_table[129 + 4] = volume_to_amplitude_table[128 + 4];
109 
110 #if 0
111 /* -FIX- may still need this or something like it (vel scaling code in whysynth_voice.c is baroque) */
112 static float velocity_to_attenuation[128];
113     float ol, amp;
114 
115     /* velocity to attenuation
116      *
117      * Creates the velocity to attenuation lookup table, for converting
118      * velocities [1, 127] to full-velocity-sensitivity (VS=7) attenuation
119      * in quarter decibels.  Modeled after my TX-7's velocity response.*/
120     velocity_to_attenuation[0] = 253.9999f; /* -FIX- ? */
121     for (i = 1; i < 127; i++) {
122         if (i >= 10) {
123             ol = (powf(((float)i / 127.0f), 0.32f) - 1.0f) * 100.0f;
124             amp = powf(2.0f, ol / 8.0f);
125         } else {
126             ol = (powf(((float)10 / 127.0f), 0.32f) - 1.0f) * 100.0f;
127             amp = powf(2.0f, ol / 8.0f) * (float)i / 10.0f;
128         }
129         velocity_to_attenuation[i] = log10f(amp) * -80.0f;
130     }
131     velocity_to_attenuation[127] = 0.0f;
132 #endif
133 
134     /* CV to amplitude
135      *
136      * This creates the 'control voltage' to amplitude curve, for converting
137      * CVs of (-1.27, 1.27) to output amplitudes, as:
138      *    CV      amplitude
139      *    1.0        1.0     = 0dB
140      *    0.92       0.5     = -6dB
141      *    0.84       0.25    = -12dB
142      *    0.0        0.0     = -infinity dB
143      *   -0.92      -0.5     = -6dB
144      *   -1.0       -1.0     = 0dB
145      * Table indices are scaled by a factor of 100, and are bipolar, with the
146      * zero point at index 128.  The curve is modeled after the 'output level'
147      * to amplitude curve of the DX-7. */
148     volume_cv_to_amplitude_table[128] = 0.0f;
149     for (i = 1; i < 6; i++)
150         volume_cv_to_amplitude_table[128 + i] = powf(2.0f, -108.0f / 8.0f) * (float)i / 6.0f;
151     for ( ; i < 20; i++)
152         volume_cv_to_amplitude_table[128 + i] = powf(2.0f, (float)(i * 2 - 120) / 8.0f);
153     for ( ; i <= 128; i++)
154         volume_cv_to_amplitude_table[128 + i] = powf(2.0f, (float)(i - 100) / 8.0f);
155     for (i = 1; i <= 128; i++)
156         volume_cv_to_amplitude_table[128 - i] = -volume_cv_to_amplitude_table[128 + i];
157 
158     tables_initialized = 1;
159 }
160 
161 static inline float
volume(float level)162 volume(float level)
163 {
164     unsigned char segment;
165     float fract;
166 
167     level *= (float)VOLUME_TO_AMPLITUDE_SCALE;
168     segment = lrintf(level - 0.5f);
169     fract = level - (float)segment;
170 
171     return volume_to_amplitude_table[segment + 4] + fract *
172                (volume_to_amplitude_table[segment + 5] -
173                 volume_to_amplitude_table[segment + 4]);
174 }
175 
176 static inline float
pan_cv_to_amplitude(float cv)177 pan_cv_to_amplitude(float cv)
178 {
179     /* equal power panning code after Ardour, copyright (c) 2004 Paul Davis */
180 
181     const float scale = -0.831783137; /* 2 - 4 * 10 ^ (-3 / 20) */
182 
183     return cv * (scale * cv + 1.0f - scale);
184 }
185 
186 /*
187  * y_voice_waveform_index
188  */
189 static inline int
y_voice_waveform_index(LADSPA_Data * p)190 y_voice_waveform_index(LADSPA_Data *p)
191 {
192     int i = lrintf(*p);
193 
194     if (i < 0 || i >= wavetables_count)
195         return 0;
196 
197     return i;
198 }
199 
200 /* ==== Modulators ==== */
201 
202 static inline float
y_voice_lfo_get_value(float pos,int waveform)203 y_voice_lfo_get_value(float pos, int waveform)
204 {
205     float f;
206     int i;
207     signed short *wave = wavetable[waveform].wave[0].data;
208 
209     pos *= (float)WAVETABLE_POINTS;
210     i = lrintf(pos - 0.5f);
211     f = pos - (float)i;
212     return ((float)wave[i] + (float)(wave[i + 1] - wave[i]) * f) / 32767.0f;
213 }
214 
215 /*
216  * y_voice_setup_lfo
217  *
218  * need not be called only during control tick
219  */
220 void
y_voice_setup_lfo(y_synth_t * synth,y_slfo_t * slfo,struct vlfo * vlfo,float phase,float randfreq,struct vmod * srcmods,struct vmod * destmods)221 y_voice_setup_lfo(y_synth_t *synth, y_slfo_t *slfo, struct vlfo *vlfo,
222                   float phase, float randfreq,
223                   struct vmod *srcmods, struct vmod *destmods)
224 {
225     int mod = y_voice_mod_index(slfo->amp_mod_src),
226         waveform = y_voice_waveform_index(slfo->waveform);
227     float mult0, mult1;
228     struct vmod *bpmod = destmods,
229                 *upmod = destmods + 1;
230 
231     vlfo->freqmult = random_float(1.0f - randfreq * 0.5f, randfreq);
232     vlfo->pos = phase + *(slfo->frequency) * vlfo->freqmult / synth->control_rate;
233     vlfo->pos = fmodf(vlfo->pos, 1.0f);
234     vlfo->delay_count = lrintf(*(slfo->delay) * synth->control_rate);
235 
236     mult1 = *(slfo->amp_mod_amt);
237     if (mult1 > 0.0f) {
238         mult0 = 1.0f - mult1 + mult1 * srcmods[mod].value;
239         mult1 = 1.0f - mult1 + mult1 * srcmods[mod].next_value;
240     } else {
241         mult0 = 1.0f + mult1 * srcmods[mod].value;
242         mult1 = 1.0f + mult1 * srcmods[mod].next_value;
243     }
244 
245     if (vlfo->delay_count == 0) {
246 
247         bpmod->value = y_voice_lfo_get_value(phase, waveform) * mult0;
248         bpmod->next_value = y_voice_lfo_get_value(vlfo->pos, waveform) * mult1;
249         bpmod->delta = (bpmod->next_value - bpmod->value) / (float)synth->control_remains;
250         upmod->value = (bpmod->value + mult0) * 0.5f;
251         upmod->next_value = (bpmod->next_value + mult1) * 0.5f;
252         upmod->delta = (upmod->next_value - upmod->value) / (float)synth->control_remains;
253 
254     } else {
255 
256         if (synth->control_remains != Y_CONTROL_PERIOD) {
257             mult0 = (float)(Y_CONTROL_PERIOD - synth->control_remains) /
258                           (float)Y_CONTROL_PERIOD;
259             vlfo->delay_length = (float)vlfo->delay_count + mult0;
260             mult0 = mult0 / vlfo->delay_length;
261         } else {
262             vlfo->delay_length = (float)vlfo->delay_count;
263             vlfo->delay_count--;
264             mult0 = 1.0f / vlfo->delay_length;
265         }
266         mult1 *= mult0;
267 
268         bpmod->value = 0.0f;
269         bpmod->next_value = y_voice_lfo_get_value(vlfo->pos, waveform) * mult1;
270         bpmod->delta = bpmod->next_value / (float)synth->control_remains;
271         upmod->value = 0.0f;
272         upmod->next_value = (bpmod->next_value + mult1) * 0.5f;
273         upmod->delta = upmod->next_value / (float)synth->control_remains;
274     }
275 };
276 
277 /*
278  * y_voice_update_lfo
279  *
280  * may only be called during control tick
281  */
282 void
y_voice_update_lfo(y_synth_t * synth,y_slfo_t * slfo,struct vlfo * vlfo,struct vmod * srcmods,struct vmod * destmods)283 y_voice_update_lfo(y_synth_t *synth, y_slfo_t *slfo, struct vlfo *vlfo,
284                    struct vmod *srcmods, struct vmod *destmods)
285 {
286     int mod = y_voice_mod_index(slfo->amp_mod_src),
287         waveform = y_voice_waveform_index(slfo->waveform);
288     float mult;
289     struct vmod *bpmod = destmods,
290                 *upmod = destmods + 1;
291 
292     vlfo->pos += *(slfo->frequency) * vlfo->freqmult / synth->control_rate;
293     if (vlfo->pos >= 1.0f) vlfo->pos -= 1.0f;
294 
295     mult = *(slfo->amp_mod_amt);
296     if (mult > 0.0f) {
297         mult = 1.0f - mult + mult * srcmods[mod].next_value;
298     } else {
299         mult = 1.0f + mult * srcmods[mod].next_value;
300     }
301     if (vlfo->delay_count != 0) {
302         mult *= 1.0f - (float)vlfo->delay_count / vlfo->delay_length;
303         vlfo->delay_count--;
304     }
305 
306     bpmod->value = bpmod->next_value;
307     bpmod->next_value = y_voice_lfo_get_value(vlfo->pos, waveform) * mult;
308     bpmod->delta = (bpmod->next_value - bpmod->value) / (float)Y_CONTROL_PERIOD;
309     upmod->value = upmod->next_value;
310     upmod->next_value = (bpmod->next_value + mult) * 0.5f;
311     upmod->delta = (upmod->next_value - upmod->value) / (float)Y_CONTROL_PERIOD;
312 }
313 
314 /*
315  * y_voice_eg_set_next_segment
316  *
317  * assumes a DSSP_EG_RUNNING envelope
318  * may only be called during control tick
319  */
320 static void
y_voice_eg_set_next_segment(y_seg_t * seg,y_voice_t * voice,struct veg * veg,struct vmod * destmod)321 y_voice_eg_set_next_segment(y_seg_t *seg, y_voice_t *voice, struct veg *veg,
322                             struct vmod *destmod)
323 {
324     if (veg->segment >= 3) {
325 
326         veg->state = DSSP_EG_FINISHED;
327         destmod->value = destmod->next_value = destmod->delta = 0.0f;
328         // YDB_MESSAGE(YDB_NOTE, " next_segment: eg %p to finished\n", veg);
329 
330     } else if (veg->segment == veg->sustain_segment) {
331 
332         int i;
333         float mult;
334 
335         veg->state = DSSP_EG_SUSTAINING;
336 
337         i = y_voice_mod_index(seg->amp_mod_src);
338         mult = *(seg->amp_mod_amt);
339         if (mult > 0.0f) {
340             mult = 1.0f - mult + mult * voice->mod[i].next_value;
341         } else {
342             mult = 1.0f + mult * voice->mod[i].next_value;
343         }
344 
345         destmod->value = destmod->next_value;
346         destmod->next_value = veg->d * mult;
347         destmod->delta = (destmod->next_value - destmod->value) / (float)Y_CONTROL_PERIOD;
348         // YDB_MESSAGE(YDB_NOTE, " next_segment: eg %p to sustain\n", veg);
349 
350     } else {
351 
352         int mode = lrintf(*(seg->mode)),
353             time, i;
354         float f, inv_duration, level, mult;
355 
356         veg->segment++;
357         destmod->value = destmod->next_value;
358 
359         if (veg->segment == 1 && mode == 1) {  /* second segment of simple ADSR */
360             time = 1;
361             level = veg->level_scale;
362         } else {
363             time = lrintf(*(seg->time[veg->segment]) * veg->time_scale);
364             if (time < 1) time = 1;
365             level = *(seg->level[veg->segment]) * veg->level_scale;
366         }
367         veg->count = time - 1;
368 
369         f = veg->target - level;  /* segment delta * -1 */
370         i = veg->shape[veg->segment];
371         inv_duration = 1.0f / (float)time;
372 
373         veg->target = level;
374         veg->d = eg_shape_coeffs[i][3] * f + level;
375         f *= inv_duration;
376         veg->c = eg_shape_coeffs[i][2] * f;
377         f *= inv_duration;
378         veg->b = eg_shape_coeffs[i][1] * f;
379         f *= inv_duration;
380         veg->a = eg_shape_coeffs[i][0] * f;
381 
382         i = y_voice_mod_index(seg->amp_mod_src);
383         mult = *(seg->amp_mod_amt);
384         if (mult > 0.0f) {
385             mult = 1.0f - mult + mult * voice->mod[i].next_value;
386         } else {
387             mult = 1.0f + mult * voice->mod[i].next_value;
388         }
389 
390         f = (float)veg->count;
391         destmod->next_value = (((veg->a * f + veg->b) * f + veg->c) * f + veg->d) * mult;
392         destmod->delta = (destmod->next_value - destmod->value) / (float)Y_CONTROL_PERIOD;
393 
394         // YDB_MESSAGE(YDB_NOTE, " next_segment: eg %p to segment %d\n", veg, veg->segment);
395     }
396 }
397 
398 /*
399  * y_voice_update_eg
400  *
401  * may only be called during control tick
402  */
403 static void
y_voice_update_eg(y_seg_t * seg,y_voice_t * voice,struct veg * veg,struct vmod * destmod)404 y_voice_update_eg(y_seg_t *seg, y_voice_t *voice, struct veg *veg,
405                   struct vmod *destmod)
406 {
407     if (veg->state == DSSP_EG_FINISHED) {
408 
409         return;
410 
411     } else if (veg->state == DSSP_EG_SUSTAINING) {
412 
413         int i;
414         float mult;
415 
416         i = y_voice_mod_index(seg->amp_mod_src);
417         mult = *(seg->amp_mod_amt);
418         if (mult > 0.0f) {
419             mult = 1.0f - mult + mult * voice->mod[i].next_value;
420         } else {
421             mult = 1.0f + mult * voice->mod[i].next_value;
422         }
423 
424         destmod->value = destmod->next_value;
425         destmod->next_value = veg->d * mult;
426         destmod->delta = (destmod->next_value - destmod->value) / (float)Y_CONTROL_PERIOD;
427 
428     } else if (veg->count) {
429 
430         float f, mult;
431         int i;
432 
433         veg->count--;
434         destmod->value = destmod->next_value;
435 
436         i = y_voice_mod_index(seg->amp_mod_src);
437         mult = *(seg->amp_mod_amt);
438         if (mult > 0.0f) {
439             mult = 1.0f - mult + mult * voice->mod[i].next_value;
440         } else {
441             mult = 1.0f + mult * voice->mod[i].next_value;
442         }
443 
444         f = (float)veg->count;
445         destmod->next_value = (((veg->a * f + veg->b) * f + veg->c) * f + veg->d) * mult;
446         destmod->delta = (destmod->next_value - destmod->value) / (float)Y_CONTROL_PERIOD;
447         // YDB_MESSAGE(YDB_NOTE, " update_eg: %g -> %g by %g\n", destmod->value, destmod->next_value, destmod->delta);
448 
449     } else {
450 
451         y_voice_eg_set_next_segment(seg, voice, veg, destmod);
452     }
453 }
454 
455 /*
456  * y_voice_check_for_dead
457  */
458 static inline int
y_voice_check_for_dead(y_synth_t * synth,y_voice_t * voice)459 y_voice_check_for_dead(y_synth_t *synth, y_voice_t *voice)
460 {
461     if (voice->ego.state == DSSP_EG_FINISHED) {
462         /* -FIX- this could also check if eg->segment > eg->sustain_segment, level is already zero, and any subsequent segment levels are zero as well... */
463         // YDB_MESSAGE(YDB_NOTE, " eps_voice_check_for_dead: killing voice %p:%d\n", voice, voice->note_id);
464         y_voice_off(synth, voice);
465         return 1;
466     }
467     return 0;
468 }
469 
470 /*
471  * y_mod_update_pressure
472  */
473 static inline void
y_mod_update_pressure(y_synth_t * synth,y_voice_t * voice)474 y_mod_update_pressure(y_synth_t *synth, y_voice_t *voice)
475 {
476     if (fabsf(voice->mod[Y_MOD_PRESSURE].next_value - voice->mod[Y_MOD_PRESSURE].value) > 1e-10) {
477         voice->mod[Y_MOD_PRESSURE].delta =
478            (voice->mod[Y_MOD_PRESSURE].next_value - voice->mod[Y_MOD_PRESSURE].value) /
479                (float)Y_CONTROL_PERIOD;
480     }
481 }
482 
483 /*
484  * y_mod_update_modmix
485  */
486 static inline void
y_mod_update_modmix(y_synth_t * synth,y_voice_t * voice,unsigned long sample_count)487 y_mod_update_modmix(y_synth_t *synth, y_voice_t *voice, unsigned long sample_count)
488 {
489     int mod;
490     float n = (float)sample_count,
491           f = *(synth->modmix_bias);
492 
493     mod = y_voice_mod_index(synth->modmix_mod1_src);
494     f += *(synth->modmix_mod1_amt) * (voice->mod[mod].next_value + voice->mod[mod].delta * n);
495     mod = y_voice_mod_index(synth->modmix_mod2_src);
496     f += *(synth->modmix_mod2_amt) * (voice->mod[mod].next_value + voice->mod[mod].delta * n);
497 
498     if (f > 2.0f) f = 2.0f;
499     else if (f < -2.0f) f = -2.0f;
500     voice->mod[Y_MOD_MIX].next_value = f;
501     voice->mod[Y_MOD_MIX].delta = (f - voice->mod[Y_MOD_MIX].value) / n;
502 }
503 
504 /* ==== Oscillators ==== */
505 
506 /* Xsynth{,-DSSI} pitch modulation was:
507  *     osc2_omega *= (1.0f + eg1 * synth->eg1_amount_o) *
508  *                   (1.0f + eg2 * synth->eg2_amount_o) *
509  *                   (1.0f + lfo * *(synth->osc2.pitch_mod_amt));
510  */
511 
512 static inline void
blosc_place_step_dd(int index,float phase,float w,float * buffer_a,float scale_a,float * buffer_b,float scale_b)513 blosc_place_step_dd(int index, float phase, float w, float *buffer_a, float scale_a,
514                                                      float *buffer_b, float scale_b)
515 {
516     float r, dd;
517     int i;
518 
519     r = MINBLEP_PHASES * phase / w;
520     i = lrintf(r - 0.5f);
521     r -= (float)i;
522     i &= MINBLEP_PHASE_MASK;  /* port changes can cause i to be out-of-range */
523     /* This would be better than the above, but more expensive:
524      *  while (i < 0) {
525      *    i += MINBLEP_PHASES;
526      *    index++;
527      *  }
528      */
529 
530     while (i < MINBLEP_PHASES * DD_PULSE_LENGTH) {
531         dd = y_step_dd_table[i].value + r * y_step_dd_table[i].delta;
532         buffer_a[index] += scale_a * dd;
533         buffer_b[index] += scale_b * dd;
534         i += MINBLEP_PHASES;
535         index = (index + 1) & OSC_BUS_MASK;
536     }
537 }
538 
539 static inline void
blosc_place_slope_dd(int index,float phase,float w,float * buffer_a,float slope_delta_a,float * buffer_b,float slope_delta_b)540 blosc_place_slope_dd(int index, float phase, float w, float *buffer_a, float slope_delta_a,
541                                                       float *buffer_b, float slope_delta_b)
542 {
543     float r, dd;
544     int i;
545 
546     r = MINBLEP_PHASES * phase / w;
547     i = lrintf(r - 0.5f);
548     r -= (float)i;
549     i &= MINBLEP_PHASE_MASK;  /* port changes can cause i to be out-of-range */
550 
551     slope_delta_a *= w;
552     slope_delta_b *= w;
553 
554     while (i < MINBLEP_PHASES * DD_PULSE_LENGTH) {
555         dd = y_slope_dd_table[i] + r * (y_slope_dd_table[i + 1] - y_slope_dd_table[i]);
556         buffer_a[index] += slope_delta_a * dd;
557         buffer_b[index] += slope_delta_b * dd;
558         i += MINBLEP_PHASES;
559         index = (index + 1) & OSC_BUS_MASK;
560     }
561 }
562 
563 /* declare the master and slave versions of the minBLEP and wavetable
564  * oscillator functions */
565 #define BLOSC_MASTER  1
566 #include "minblep_oscillator.h"
567 #undef BLOSC_MASTER
568 #define BLOSC_MASTER  0
569 #include "minblep_oscillator.h"
570 #undef BLOSC_MASTER
571 
572 static int fm_mod_ratio_to_keys[17] = {
573     -12, 0, 12, 19, 24, 28, 31, 34, 36, 38, 40, 42, 43, 44, 46, 47, 48
574 };
575 
576 static void
fm_wave2sine(unsigned long sample_count,y_sosc_t * sosc,y_voice_t * voice,struct vosc * vosc,int index,float w0)577 fm_wave2sine(unsigned long sample_count, y_sosc_t *sosc, y_voice_t *voice,
578              struct vosc *vosc, int index, float w0)
579 {
580     signed short *wave0, *wave1;
581     unsigned long sample;
582     float cpos = (float)vosc->pos0,
583           mpos = (float)vosc->pos1,
584           freq_ratio,
585           wavemix0, wavemix1,
586           w, w_delta,
587           mod, mod_delta,
588           level_a, level_a_delta,
589           level_b, level_b_delta;
590     float f;
591     int   i;
592 
593     i = lrintf(*(sosc->mparam1) * 16.0f);
594     freq_ratio = (float)i;
595     if (freq_ratio < 1.0f) freq_ratio = 0.5f;
596     freq_ratio *= 1.0f + 0.012 * (*(sosc->mparam2) - 0.5f);
597 
598     if (vosc->mode != vosc->last_mode)
599         cpos = mpos = 0.0f;
600     i = voice->key + lrintf(*(sosc->pitch)) + fm_mod_ratio_to_keys[i];
601     if (vosc->mode     != vosc->last_mode ||
602         vosc->waveform != vosc->last_waveform ||
603         i              != vosc->wave_select_key) {
604 
605         /* select wave(s) and crossfade from wavetable */
606         wavetable_select(vosc, i);
607         vosc->last_mode     = vosc->mode;
608         vosc->last_waveform = vosc->waveform;
609     }
610 
611     i = y_voice_mod_index(sosc->pitch_mod_src);
612     f = *(sosc->pitch_mod_amt);
613     w = 1.0f + f * voice->mod[i].value;
614     w_delta = w + f * voice->mod[i].delta * (float)sample_count;
615     w_delta *= w0;
616     w       *= w0;
617     w_delta = (w_delta - w) / (float)sample_count;
618     /* -FIX- condition to [0, 0.5)? */
619 
620     i = y_voice_mod_index(sosc->mmod_src);
621     f = *(sosc->mmod_amt);
622     mod = f * voice->mod[i].value;
623     mod_delta = volume_cv_to_amplitude(mod + f * voice->mod[i].delta * (float)sample_count);
624     mod       = volume_cv_to_amplitude(mod);
625     mod       *= 2.089f / 32767.0f;
626     mod_delta *= 2.089f / 32767.0f;
627     mod_delta = (mod_delta - mod) / (float)sample_count;
628 
629     i = y_voice_mod_index(sosc->amp_mod_src);
630     f = *(sosc->amp_mod_amt);
631     if (f > 0.0f)
632         level_a = 1.0f - f + f * voice->mod[i].value;
633     else
634         level_a = 1.0f + f * voice->mod[i].value;
635     level_a_delta = volume_cv_to_amplitude(level_a + f * voice->mod[i].delta * (float)sample_count);
636     level_a       = volume_cv_to_amplitude(level_a);
637     level_b       = level_a       * *(sosc->level_b);
638     level_b_delta = level_a_delta * *(sosc->level_b);
639     level_a       *= *(sosc->level_a);
640     level_a_delta *= *(sosc->level_a);
641     level_a_delta = (level_a_delta - level_a) / (float)sample_count;
642     level_b_delta = (level_b_delta - level_b) / (float)sample_count;
643     /* -FIX- condition to [0, 1]? */
644 
645     wave0 = vosc->wave0;
646     wave1 = vosc->wave1;
647     wavemix0 = vosc->wavemix0;
648     wavemix1 = vosc->wavemix1;
649 
650     /* -FIX- optimize for the case of no crossfade */
651 
652     for (sample = 0; sample < sample_count; sample++) {
653 
654         cpos += w;
655 
656         if (cpos >= 1.0f) {
657             cpos -= 1.0f;
658             voice->osc_sync[sample] = cpos / w;
659         } else {
660             voice->osc_sync[sample] = -1.0f;
661         }
662 
663         mpos += w * freq_ratio;
664 
665         while (mpos >= 1.0f) mpos -= 1.0f;
666 
667         f = mpos * (float)WAVETABLE_POINTS;
668         i = lrintf(f - 0.5f);
669         f -= (float)i;
670 
671         f = ((float)wave0[i] + (float)(wave0[i + 1] - wave0[i]) * f) * wavemix0 +
672             ((float)wave1[i] + (float)(wave1[i + 1] - wave1[i]) * f) * wavemix1;
673         f *= mod;  /* f is now modulation index, in periods */
674 
675         f = (cpos + f) * (float)SINETABLE_POINTS;
676         i = lrintf(f - 0.5f);
677         f -= (float)i;
678         i &= (SINETABLE_POINTS - 1);
679         f = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * f;
680         voice->osc_bus_a[index]   += level_a * f;
681         voice->osc_bus_b[index++] += level_b * f;
682 
683         w       += w_delta;
684         mod     += mod_delta;
685         level_a += level_a_delta;
686         level_b += level_b_delta;
687     }
688 
689     vosc->pos0 = (double)cpos;
690     vosc->pos1 = (double)mpos;
691 }
692 
693 static void
fm_sine2wave(unsigned long sample_count,y_sosc_t * sosc,y_voice_t * voice,struct vosc * vosc,int index,float w0)694 fm_sine2wave(unsigned long sample_count, y_sosc_t *sosc, y_voice_t *voice,
695              struct vosc *vosc, int index, float w0)
696 {
697     signed short *wave0, *wave1;
698     unsigned long sample;
699     float cpos = (float)vosc->pos0,
700           mpos = (float)vosc->pos1,
701           freq_ratio,
702           wavemix0, wavemix1,
703           w, w_delta,
704           mod, mod_delta,
705           level_a, level_a_delta,
706           level_b, level_b_delta;
707     float f;
708     int   i;
709 
710     if (vosc->mode != vosc->last_mode)
711         cpos = mpos = 0.0f;
712     i = voice->key + lrintf(*(sosc->pitch));
713     if (vosc->mode     != vosc->last_mode ||
714         vosc->waveform != vosc->last_waveform ||
715         i              != vosc->wave_select_key) {
716 
717         /* select wave(s) and crossfade from wavetable */
718         wavetable_select(vosc, i);
719         vosc->last_mode     = vosc->mode;
720         vosc->last_waveform = vosc->waveform;
721     }
722 
723     i = y_voice_mod_index(sosc->pitch_mod_src);
724     f = *(sosc->pitch_mod_amt);
725     w = 1.0f + f * voice->mod[i].value;
726     w_delta = w + f * voice->mod[i].delta * (float)sample_count;
727     w_delta *= w0;
728     w       *= w0;
729     w_delta = (w_delta - w) / (float)sample_count;
730     /* -FIX- condition to [0, 0.5)? */
731 
732     freq_ratio = lrintf(*(sosc->mparam1) * 16.0f);
733     if (freq_ratio < 1.0f) freq_ratio = 0.5f;
734     freq_ratio *= 1.0f + 0.012 * (*(sosc->mparam2) - 0.5f);
735 
736     i = y_voice_mod_index(sosc->mmod_src);
737     f = *(sosc->mmod_amt);
738     mod = f * voice->mod[i].value;
739     mod_delta = volume_cv_to_amplitude(mod + f * voice->mod[i].delta * (float)sample_count);
740     mod       = volume_cv_to_amplitude(mod);
741     mod       *= 2.089f * 2.0f;
742     mod_delta *= 2.089f * 2.0f;
743     mod_delta = (mod_delta - mod) / (float)sample_count;
744 
745     i = y_voice_mod_index(sosc->amp_mod_src);
746     f = *(sosc->amp_mod_amt);
747     if (f > 0.0f)
748         level_a = 1.0f - f + f * voice->mod[i].value;
749     else
750         level_a = 1.0f + f * voice->mod[i].value;
751     level_a_delta = volume_cv_to_amplitude(level_a + f * voice->mod[i].delta * (float)sample_count);
752     level_a       = volume_cv_to_amplitude(level_a);
753     level_b       = level_a       * *(sosc->level_b);
754     level_b_delta = level_a_delta * *(sosc->level_b);
755     level_a       *= *(sosc->level_a);
756     level_a_delta *= *(sosc->level_a);
757     level_a_delta = (level_a_delta - level_a) / (float)sample_count;
758     level_b_delta = (level_b_delta - level_b) / (float)sample_count;
759     /* -FIX- condition to [0, 1]? */
760 
761     wave0 = vosc->wave0;
762     wave1 = vosc->wave1;
763     wavemix0 = vosc->wavemix0;
764     wavemix1 = vosc->wavemix1;
765 
766     /* -FIX- optimize for the case of no crossfade */
767 
768     for (sample = 0; sample < sample_count; sample++) {
769 
770         cpos += w;
771 
772         if (cpos >= 1.0f) {
773             cpos -= 1.0f;
774             voice->osc_sync[sample] = cpos / w;
775         } else {
776             voice->osc_sync[sample] = -1.0f;
777         }
778 
779         mpos += w * freq_ratio;
780 
781         while (mpos >= 1.0f) mpos -= 1.0f;
782 
783         f = mpos * (float)SINETABLE_POINTS;
784         i = lrintf(f - 0.5f);
785         f -= (float)i;
786 
787         f = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * f;
788         f *= mod;  /* f is now modulation index, in periods */
789 
790         f = (cpos + f) * (float)WAVETABLE_POINTS;
791         i = lrintf(f - 0.5f);
792         f -= (float)i;
793         i &= (WAVETABLE_POINTS - 1);
794         f = ((float)wave0[i] + (float)(wave0[i + 1] - wave0[i]) * f) * wavemix0 +
795             ((float)wave1[i] + (float)(wave1[i + 1] - wave1[i]) * f) * wavemix1;
796         f /= 65534.0f;
797         voice->osc_bus_a[index]   += level_a * f;
798         voice->osc_bus_b[index++] += level_b * f;
799 
800         w       += w_delta;
801         mod     += mod_delta;
802         level_a += level_a_delta;
803         level_b += level_b_delta;
804     }
805 
806     vosc->pos0 = (double)cpos;
807     vosc->pos1 = (double)mpos;
808 }
809 
810 static void
fm_wave2lf(unsigned long sample_count,y_synth_t * synth,y_sosc_t * sosc,y_voice_t * voice,struct vosc * vosc,int index,float w0)811 fm_wave2lf(unsigned long sample_count, y_synth_t *synth, y_sosc_t *sosc,
812            y_voice_t *voice, struct vosc *vosc, int index, float w0)
813 {
814     signed short *wave0, *wave1;
815     unsigned long sample;
816     float cpos = (float)vosc->pos0,
817           mpos = (float)vosc->pos1,
818           lfw, w, w_delta,
819           wavemix0, wavemix1,
820           mod, mod_delta,
821           level_a, level_a_delta,
822           level_b, level_b_delta;
823     float f;
824     int   i;
825 
826     lfw = y_pitch[lrintf(*(sosc->mparam1) * 48.0f) + 33]; /* MParam1 = carrier freq, 0.125 to 2 Hz */
827     lfw *= synth->deltat;
828 
829     if (vosc->mode != vosc->last_mode)
830         cpos = mpos = 0.0f;
831     i = voice->key + lrintf(*(sosc->pitch));
832     if (vosc->mode     != vosc->last_mode ||
833         vosc->waveform != vosc->last_waveform ||
834         i              != vosc->wave_select_key) {
835 
836         /* select wave(s) and crossfade from wavetable */
837         wavetable_select(vosc, i);
838         vosc->last_mode     = vosc->mode;
839         vosc->last_waveform = vosc->waveform;
840     }
841 
842     i = y_voice_mod_index(sosc->pitch_mod_src);
843     f = *(sosc->pitch_mod_amt);
844     w = 1.0f + f * voice->mod[i].value;
845     w_delta = w + f * voice->mod[i].delta * (float)sample_count;
846     w_delta *= w0;
847     w       *= w0;
848     w_delta = (w_delta - w) / (float)sample_count;
849     /* -FIX- condition to [0, 0.5)? */
850 
851     i = y_voice_mod_index(sosc->mmod_src);
852     f = *(sosc->mmod_amt);
853     mod = *(sosc->mparam2) + f * voice->mod[i].value;
854     mod_delta = volume_cv_to_amplitude(mod + f * voice->mod[i].delta * (float)sample_count);
855     mod       = volume_cv_to_amplitude(mod);
856     mod       *= 2.089f / 32767.0f;
857     mod_delta *= 2.089f / 32767.0f;
858     mod_delta = (mod_delta - mod) / (float)sample_count;
859 
860     i = y_voice_mod_index(sosc->amp_mod_src);
861     f = *(sosc->amp_mod_amt);
862     if (f > 0.0f)
863         level_a = 1.0f - f + f * voice->mod[i].value;
864     else
865         level_a = 1.0f + f * voice->mod[i].value;
866     level_a_delta = volume_cv_to_amplitude(level_a + f * voice->mod[i].delta * (float)sample_count);
867     level_a       = volume_cv_to_amplitude(level_a);
868     level_b       = level_a       * *(sosc->level_b);
869     level_b_delta = level_a_delta * *(sosc->level_b);
870     level_a       *= *(sosc->level_a);
871     level_a_delta *= *(sosc->level_a);
872     level_a_delta = (level_a_delta - level_a) / (float)sample_count;
873     level_b_delta = (level_b_delta - level_b) / (float)sample_count;
874     /* -FIX- condition to [0, 1]? */
875 
876     wave0 = vosc->wave0;
877     wave1 = vosc->wave1;
878     wavemix0 = vosc->wavemix0;
879     wavemix1 = vosc->wavemix1;
880 
881     /* -FIX- optimize for the case of no crossfade */
882 
883     for (sample = 0; sample < sample_count; sample++) {
884 
885         cpos += lfw;
886         if (cpos >= 1.0f) {
887             cpos -= 1.0f;
888         }
889 
890         mpos += w;
891         if (mpos >= 1.0f) {
892             mpos -= 1.0f;
893             voice->osc_sync[sample] = mpos / w;
894         } else {
895             voice->osc_sync[sample] = -1.0f;
896         }
897 
898         f = mpos * (float)WAVETABLE_POINTS;
899         i = lrintf(f - 0.5f);
900         f -= (float)i;
901         f = ((float)wave0[i] + (float)(wave0[i + 1] - wave0[i]) * f) * wavemix0 +
902             ((float)wave1[i] + (float)(wave1[i + 1] - wave1[i]) * f) * wavemix1;
903         f *= mod;  /* f is now modulation index, in periods */
904 
905         f = (cpos + f) * (float)SINETABLE_POINTS;
906         i = lrintf(f - 0.5f);
907         f -= (float)i;
908         i &= (SINETABLE_POINTS - 1);
909         f = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * f;
910         voice->osc_bus_a[index]   += level_a * f;
911         voice->osc_bus_b[index++] += level_b * f;
912 
913         w       += w_delta;
914         mod     += mod_delta;
915         level_a += level_a_delta;
916         level_b += level_b_delta;
917     }
918 
919     vosc->pos0 = (double)cpos;
920     vosc->pos1 = (double)mpos;
921 }
922 
923 static void
waveshaper(unsigned long sample_count,y_sosc_t * sosc,y_voice_t * voice,struct vosc * vosc,int index,float w0)924 waveshaper(unsigned long sample_count, y_sosc_t *sosc, y_voice_t *voice,
925            struct vosc *vosc, int index, float w0)
926 {
927     signed short *wave;
928     unsigned long sample;
929     float pos = (float)vosc->pos0,
930           w, w_delta,
931           mod, mod_delta,
932           bias,
933           level_a, level_a_delta,
934           level_b, level_b_delta;
935     float f;
936     int   i;
937 
938     if (vosc->mode     != vosc->last_mode ||
939         vosc->waveform != vosc->last_waveform) {
940 
941         /* select wave from wavetable: so that waveshaping is predictable,
942          * always use the wave for key 60 (which means that some waveforms will
943          * alias badly at higher notes -- -FIX- is there a better way?
944          * => probably a 'wave select bias' control would be more useful than
945          * the 'phase bias' is). */
946         wavetable_select(vosc, 60);
947         vosc->last_mode     = vosc->mode;
948         vosc->last_waveform = vosc->waveform;
949         pos = 0.0f;
950     }
951 
952     i = y_voice_mod_index(sosc->pitch_mod_src);
953     f = *(sosc->pitch_mod_amt);
954     w = 1.0f + f * voice->mod[i].value;
955     w_delta = w + f * voice->mod[i].delta * (float)sample_count;
956     w_delta *= w0;
957     w       *= w0;
958     w_delta = (w_delta - w) / (float)sample_count;
959     /* -FIX- condition to [0, 0.5)? */
960 
961     i = y_voice_mod_index(sosc->mmod_src);
962     f = *(sosc->mmod_amt);
963     mod = *(sosc->mparam2) * 1.4f + f * voice->mod[i].value;
964     mod_delta = mod + f * voice->mod[i].delta * (float)sample_count;
965     /* mod_delta = volume_cv_to_amplitude(mod + f * voice->mod[i].delta * (float)sample_count);
966      * mod       = volume_cv_to_amplitude(mod);
967      * linearly scaled modulation seems to work better than the logarithmic above: */
968     mod       *= (float)WAVETABLE_POINTS;
969     mod_delta *= (float)WAVETABLE_POINTS;
970     mod_delta = (mod_delta - mod) / (float)sample_count;
971 
972     bias = *(sosc->mparam1) * (float)WAVETABLE_POINTS;
973 
974     i = y_voice_mod_index(sosc->amp_mod_src);
975     f = *(sosc->amp_mod_amt);
976     if (f > 0.0f)
977         level_a = 1.0f - f + f * voice->mod[i].value;
978     else
979         level_a = 1.0f + f * voice->mod[i].value;
980     level_a_delta = volume_cv_to_amplitude(level_a + f * voice->mod[i].delta * (float)sample_count);
981     level_a       = volume_cv_to_amplitude(level_a);
982     level_b       = level_a       * *(sosc->level_b);
983     level_b_delta = level_a_delta * *(sosc->level_b);
984     level_a       *= *(sosc->level_a);
985     level_a_delta *= *(sosc->level_a);
986     level_a_delta = (level_a_delta - level_a) / (float)sample_count;
987     level_b_delta = (level_b_delta - level_b) / (float)sample_count;
988     /* -FIX- condition to [0, 1]? */
989 
990     wave = vosc->wave0;
991 
992     for (sample = 0; sample < sample_count; sample++) {
993 
994         pos += w;
995 
996         if (pos >= 1.0f) {
997             pos -= 1.0f;
998             voice->osc_sync[sample] = pos / w;
999         } else {
1000             voice->osc_sync[sample] = -1.0f;
1001         }
1002 
1003         f = pos * SINETABLE_POINTS;
1004         i = lrintf(f - 0.5f);
1005         f -= (float)i;
1006 
1007         f = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * f;
1008         f *= mod;
1009         f += bias;
1010         i = lrintf(f - 0.5f);
1011         f -= (float)i;
1012         i &= (WAVETABLE_POINTS - 1);
1013         f = ((float)wave[i] + (float)(wave[i + 1] - wave[i]) * f) / 65534.0f;
1014         voice->osc_bus_a[index]   += level_a * f;
1015         voice->osc_bus_b[index++] += level_b * f;
1016 
1017         w       += w_delta;
1018         mod     += mod_delta;
1019         level_a += level_a_delta;
1020         level_b += level_b_delta;
1021     }
1022 
1023     vosc->pos0 = (double)pos;
1024 }
1025 
1026 static void
noise(unsigned long sample_count,y_sosc_t * sosc,y_voice_t * voice,struct vosc * vosc,int index,float w)1027 noise(unsigned long sample_count, y_sosc_t *sosc, y_voice_t *voice,
1028       struct vosc *vosc, int index, float w)
1029 {
1030     int mod, sample;
1031     float f,
1032           level_a, level_a_delta,
1033           level_b, level_b_delta,
1034           c0, c1, c2, q;
1035 
1036     if (vosc->mode != vosc->last_mode) {
1037         vosc->f0 = 0.0f;
1038         vosc->f1 = 0.0f;
1039         vosc->f2 = 0.0f;
1040         vosc->last_mode = vosc->mode;
1041     }
1042 
1043     mod = y_voice_mod_index(sosc->amp_mod_src);
1044     f = *(sosc->amp_mod_amt);
1045     if (f > 0.0f)
1046         level_a = 1.0f - f + f * voice->mod[mod].value;
1047     else
1048         level_a = 1.0f + f * voice->mod[mod].value;
1049     level_a_delta = volume_cv_to_amplitude(level_a + f * voice->mod[mod].delta * (float)sample_count);
1050     level_a       = volume_cv_to_amplitude(level_a);
1051     level_b       = level_a       * *(sosc->level_b);
1052     level_b_delta = level_a_delta * *(sosc->level_b);
1053     level_a       *= *(sosc->level_a);
1054     level_a_delta *= *(sosc->level_a);
1055     level_a_delta = (level_a_delta - level_a) / (float)sample_count;
1056     level_b_delta = (level_b_delta - level_b) / (float)sample_count;
1057     /* -FIX- condition to [0, 1]? */
1058 
1059     switch (vosc->waveform) {
1060       default:
1061       case 0:   /* White */
1062         for (sample = 0; sample < sample_count; sample++) {
1063             f = ((float)random() / (float)RAND_MAX) - 0.5f;
1064             voice->osc_bus_a[index]   += level_a * f;
1065             voice->osc_bus_b[index++] += level_b * f;
1066             /* noise oscillators do not export sync */
1067 
1068             level_a += level_a_delta;
1069             level_b += level_b_delta;
1070         }
1071         break;
1072 
1073       case 1:  /* Paul Kellet's "economy" pink filter from Csound */
1074         c0 = vosc->f0;
1075         c1 = vosc->f1;
1076         c2 = vosc->f2;
1077         for (sample = 0; sample < sample_count; sample++) {
1078             f = ((float)random() / (float)RAND_MAX) - 0.5f;
1079             c0 = c0 * 0.99765 + f * 0.0990460;
1080             c1 = c1 * 0.96300 + f * 0.2965164;
1081             c2 = c2 * 0.57000 + f * 1.0526913;
1082             f = c0 + c1 + c2 + f * 0.1848;
1083             f *= 0.11;  /* (roughly) compensate for gain */
1084 
1085             voice->osc_bus_a[index]   += level_a * f;
1086             voice->osc_bus_b[index++] += level_b * f;
1087             /* noise oscillators do not export sync */
1088 
1089             level_a += level_a_delta;
1090             level_b += level_b_delta;
1091         }
1092         vosc->f0 = c0;
1093         vosc->f1 = c1;
1094         vosc->f2 = c2;
1095         break;
1096 
1097       case 2:   /* Low-pass */
1098         /* Chamberlin state-variable with cutoff limiting after Brandt; see comments in vcf_2pole */
1099         q = 2.0f - *(sosc->mparam2) * 1.995f;
1100         c2 = 0.115375f * q * q - 0.673851f * q + 1.67588f;
1101         f = *(sosc->mparam1);
1102         f = w * f * f * 128.0f;
1103         if (f > 0.48f) f = 0.48f;
1104         f = (-5.98261f * f + 7.11034f) * f; /* quick approximation of 2.0f * sinf(M_PI_F * f) */
1105         if (f > c2) f = c2;                 /* limit f to keep it stable */
1106         c0 = vosc->f0;
1107         c1 = vosc->f1;
1108         for (sample = 0; sample < sample_count; sample++) {
1109 
1110             c1 = c1 + f * c0;
1111             c2 = (((float)random() / (float)RAND_MAX) - 0.5f) - c1 - q * c0;
1112             c0 = f * c2 + c0;
1113 
1114             voice->osc_bus_a[index]   += level_a * c1;
1115             voice->osc_bus_b[index++] += level_b * c1;
1116             /* noise oscillators do not export sync */
1117 
1118             level_a += level_a_delta;
1119             level_b += level_b_delta;
1120         }
1121         vosc->f0 = c0;
1122         vosc->f1 = c1;
1123         break;
1124 
1125       case 3:   /* Band-pass */
1126         /* Chamberlin state-variable with cutoff limiting after Brandt; see comments in vcf_2pole */
1127         q = 2.0f - *(sosc->mparam2) * 1.995f;
1128         c2 = 0.115375f * q * q - 0.673851f * q + 1.67588f;
1129         f = *(sosc->mparam1);
1130         f = w * f * f * 128.0f;
1131         if (f > 0.48f) f = 0.48f;
1132         f = (-5.98261f * f + 7.11034f) * f; /* quick approximation of 2.0f * sinf(M_PI_F * f) */
1133         if (f > c2) f = c2;                 /* limit f to keep it stable */
1134         c0 = vosc->f0;
1135         c1 = vosc->f1;
1136         for (sample = 0; sample < sample_count; sample++) {
1137 
1138             c1 = c1 + f * c0;
1139             c2 = (((float)random() / (float)RAND_MAX) - 0.5f) - c1 - q * c0;
1140             c0 = f * c2 + c0;
1141 
1142             voice->osc_bus_a[index]   += level_a * c0;
1143             voice->osc_bus_b[index++] += level_b * c0;
1144             /* noise oscillators do not export sync */
1145 
1146             level_a += level_a_delta;
1147             level_b += level_b_delta;
1148         }
1149         vosc->f0 = c0;
1150         vosc->f1 = c1;
1151         break;
1152     }
1153 }
1154 
1155 static void
phase_distortion(unsigned long sample_count,y_sosc_t * sosc,y_voice_t * voice,struct vosc * vosc,int index,float w0)1156 phase_distortion(unsigned long sample_count, y_sosc_t *sosc, y_voice_t *voice,
1157                  struct vosc *vosc, int index, float w0)
1158 {
1159     unsigned long sample;
1160     float pos = (float)vosc->pos0,
1161           dpos, window,
1162           w, w_delta,
1163           mod, mod_delta,
1164           level_a, level_a_delta,
1165           level_b, level_b_delta;
1166     float f;
1167     int   cycle = vosc->i0,
1168           i;
1169 
1170     if (vosc->mode     != vosc->last_mode
1171         /* || vosc->waveform != vosc->last_waveform */) {
1172 
1173         vosc->last_mode     = vosc->mode;
1174         vosc->last_waveform = vosc->waveform;
1175         pos = 0.0f;
1176         cycle = 0;
1177     }
1178 
1179     i = y_voice_mod_index(sosc->pitch_mod_src);
1180     f = *(sosc->pitch_mod_amt);
1181     w = 1.0f + f * voice->mod[i].value;
1182     w_delta = w + f * voice->mod[i].delta * (float)sample_count;
1183     w_delta *= w0;
1184     w       *= w0;
1185     w_delta = (w_delta - w) / (float)sample_count;
1186     /* -FIX- condition to [0, 0.5)? */
1187 
1188     i = y_voice_mod_index(sosc->mmod_src);
1189     f = *(sosc->mmod_amt);
1190     mod = *(sosc->mparam2) + f * voice->mod[i].value;
1191     mod_delta = mod + f * voice->mod[i].delta * (float)sample_count;
1192     /* at this point, mod_delta is actually the target value for mod */
1193 
1194     i = y_voice_mod_index(sosc->amp_mod_src);
1195     f = *(sosc->amp_mod_amt);
1196     if (f > 0.0f)
1197         level_a = 1.0f - f + f * voice->mod[i].value;
1198     else
1199         level_a = 1.0f + f * voice->mod[i].value;
1200     level_a_delta = volume_cv_to_amplitude(level_a + f * voice->mod[i].delta * (float)sample_count);
1201     level_a       = volume_cv_to_amplitude(level_a);
1202     level_b       = level_a       * *(sosc->level_b);
1203     level_b_delta = level_a_delta * *(sosc->level_b);
1204     level_a       *= *(sosc->level_a);
1205     level_a_delta *= *(sosc->level_a);
1206     level_a_delta = (level_a_delta - level_a) / (float)sample_count;
1207     level_b_delta = (level_b_delta - level_b) / (float)sample_count;
1208     /* -FIX- condition to [0, 1]? */
1209 
1210     if (vosc->waveform < 12) {  /* single waveform */
1211 
1212         switch (vosc->waveform) {
1213 
1214           default:
1215           case 0:  /* cosine<->saw */
1216             mod = 0.5f - mod * 0.5f;
1217             mod_delta = 0.5f - mod_delta * 0.5f;
1218             if (mod < w) mod = w;
1219             else if (mod > 1.0f - w) mod = 1.0f - w;
1220             if (mod_delta < w) mod_delta = w;
1221             else if (mod_delta > 1.0f - w) mod_delta = 1.0f - w;
1222             mod_delta = (mod_delta - mod) / (float)sample_count;
1223 
1224             for (sample = 0; sample < sample_count; sample++) {
1225 
1226                 pos += w;
1227 
1228                 if (pos >= 1.0f) {
1229                     pos -= 1.0f;
1230                     voice->osc_sync[sample] = pos / w;
1231                 } else {
1232                     voice->osc_sync[sample] = -1.0f;
1233                 }
1234 
1235                 if (pos < mod) {
1236                     dpos = pos * 0.5f / mod;
1237                 } else {
1238                     dpos = 0.5f + (pos - mod) * 0.5f / (1.0f - mod);
1239                 }
1240 
1241                 f = dpos * (float)SINETABLE_POINTS;
1242                 i = lrintf(f - 0.5f);
1243                 f -= (float)i;
1244                 i += SINETABLE_POINTS / 4; /* shift to get cosine from sine table */
1245                 i &= (SINETABLE_POINTS - 1);
1246                 f = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * f;
1247                 voice->osc_bus_a[index]   += level_a * f;
1248                 voice->osc_bus_b[index++] += level_b * f;
1249 
1250                 w       += w_delta;
1251                 mod     += mod_delta;
1252                 level_a += level_a_delta;
1253                 level_b += level_b_delta;
1254             }
1255             break;
1256 
1257           case 1:  /* cosine<->square */
1258             /* Hmm, according to Tom�s Mulcahy, http://www.madtheory.com/CZ%20article/CZ%20article.htm,
1259              * the CZ series doesn't output a square wave, but rather a sort of cosine with a spike in
1260              * it.  The scope trace he shows couldn't sound like a square wave, so I question that, but
1261              * hey, he's got a CZ and I don't.... */
1262             mod = 0.5f - mod * 0.5f;
1263             mod_delta = 0.5f - mod_delta * 0.5f;
1264             if (mod < w) mod = w;
1265             else if (mod > 0.5f) mod = 0.5f;
1266             if (mod_delta < w) mod_delta = w;
1267             else if (mod_delta > 0.5f) mod_delta = 0.5f;
1268             mod_delta = (mod_delta - mod) / (float)sample_count;
1269 
1270             for (sample = 0; sample < sample_count; sample++) {
1271 
1272                 pos += w;
1273 
1274                 if (pos >= 1.0f) {
1275                     pos -= 1.0f;
1276                     voice->osc_sync[sample] = pos / w;
1277                 } else {
1278                     voice->osc_sync[sample] = -1.0f;
1279                 }
1280 
1281                 if (pos < mod) {
1282                     dpos = pos * 0.5f / mod;
1283                 } else if (pos < 0.5f) {
1284                     dpos = 0.5f;
1285                 } else if (pos < 0.5f + mod) {
1286                     dpos = (pos - 0.5f) * 0.5f / mod + 0.5f;
1287                 } else {
1288                     dpos = 1.0f;
1289                 }
1290 
1291                 f = dpos * (float)SINETABLE_POINTS;
1292                 i = lrintf(f - 0.5f);
1293                 f -= (float)i;
1294                 i += SINETABLE_POINTS / 4; /* shift to get cosine from sine table */
1295                 i &= (SINETABLE_POINTS - 1);
1296                 f = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * f;
1297                 voice->osc_bus_a[index]   += level_a * f;
1298                 voice->osc_bus_b[index++] += level_b * f;
1299 
1300                 w       += w_delta;
1301                 mod     += mod_delta;
1302                 level_a += level_a_delta;
1303                 level_b += level_b_delta;
1304             }
1305             break;
1306 
1307           case 2:  /* cosine<->pulse */
1308             mod = 1.0f - mod;
1309             mod_delta = 1.0f - mod_delta;
1310             if (mod < 4.0f * w) mod = 4.0f * w;
1311             else if (mod > 1.0f - 4.0f * w) mod = 1.0f - 4.0f * w;
1312             if (mod_delta < 4.0f * w) mod_delta = 4.0f * w;
1313             else if (mod_delta > 1.0f - 4.0f * w) mod_delta = 1.0f - 4.0f * w;
1314             mod_delta = (mod_delta - mod) / (float)sample_count;
1315 
1316             for (sample = 0; sample < sample_count; sample++) {
1317 
1318                 pos += w;
1319 
1320                 if (pos >= 1.0f) {
1321                     pos -= 1.0f;
1322                     voice->osc_sync[sample] = pos / w;
1323                 } else {
1324                     voice->osc_sync[sample] = -1.0f;
1325                 }
1326 
1327                 if (pos < mod) {
1328                     dpos = pos / mod;
1329                 } else {
1330                     dpos = 1.0f;
1331                 }
1332 
1333                 f = dpos * (float)SINETABLE_POINTS;
1334                 i = lrintf(f - 0.5f);
1335                 f -= (float)i;
1336                 i += SINETABLE_POINTS / 4; /* shift to get cosine from sine table */
1337                 i &= (SINETABLE_POINTS - 1);
1338                 f = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * f;
1339                 voice->osc_bus_a[index]   += level_a * f;
1340                 voice->osc_bus_b[index++] += level_b * f;
1341 
1342                 w       += w_delta;
1343                 mod     += mod_delta;
1344                 level_a += level_a_delta;
1345                 level_b += level_b_delta;
1346             }
1347             break;
1348 
1349           case 5:  /* 'Resonant I' (sawtooth window) */
1350             mod = expf(mod * 6.0f * (float)M_LN2);
1351             mod_delta = expf(mod_delta * 6.0f * (float)M_LN2);
1352             if (mod * w > 0.5f) mod = 0.5f / w;
1353             if (mod_delta * w > 0.5f) mod_delta = 0.5f / w;
1354             mod_delta = (mod_delta - mod) / (float)sample_count;
1355 
1356             for (sample = 0; sample < sample_count; sample++) {
1357 
1358                 pos += w;
1359 
1360                 if (pos >= 1.0f) {
1361                     pos -= 1.0f;
1362                     voice->osc_sync[sample] = pos / w;
1363                 } else {
1364                     voice->osc_sync[sample] = -1.0f;
1365                 }
1366 
1367                 dpos = pos * mod;
1368                 window = 1.0f - pos;
1369 
1370                 f = dpos * (float)SINETABLE_POINTS;
1371                 i = lrintf(f - 0.5f);
1372                 f -= (float)i;
1373                 i += SINETABLE_POINTS / 4; /* shift to get cosine from sine table */
1374                 i &= (SINETABLE_POINTS - 1);
1375                 f = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * f;
1376                 f = 0.5f - f;
1377                 f *= window;
1378                 f = 0.5f - f;
1379                 voice->osc_bus_a[index]   += level_a * f;
1380                 voice->osc_bus_b[index++] += level_b * f;
1381 
1382                 w       += w_delta;
1383                 mod     += mod_delta;
1384                 level_a += level_a_delta;
1385                 level_b += level_b_delta;
1386             }
1387             break;
1388 
1389           case 6:  /* 'Resonant II' (triangle window) */
1390             mod = expf(mod * 6.0f * (float)M_LN2);
1391             mod_delta = expf(mod_delta * 6.0f * (float)M_LN2);
1392             if (mod * w > 0.5f) mod = 0.5f / w;
1393             if (mod_delta * w > 0.5f) mod_delta = 0.5f / w;
1394             mod_delta = (mod_delta - mod) / (float)sample_count;
1395 
1396             for (sample = 0; sample < sample_count; sample++) {
1397 
1398                 pos += w;
1399 
1400                 if (pos >= 1.0f) {
1401                     pos -= 1.0f;
1402                     voice->osc_sync[sample] = pos / w;
1403                 } else {
1404                     voice->osc_sync[sample] = -1.0f;
1405                 }
1406 
1407                 dpos = pos * mod;
1408                 if (pos < 0.5)
1409                     window = 2.0f * pos;
1410                 else
1411                     window = 2.0f * (1.0f - pos);
1412 
1413                 f = dpos * (float)SINETABLE_POINTS;
1414                 i = lrintf(f - 0.5f);
1415                 f -= (float)i;
1416                 i += SINETABLE_POINTS / 4; /* shift to get cosine from sine table */
1417                 i &= (SINETABLE_POINTS - 1);
1418                 f = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * f;
1419                 f = 0.5f - f;
1420                 f *= window;
1421                 f = 0.5f - f;
1422                 voice->osc_bus_a[index]   += level_a * f;
1423                 voice->osc_bus_b[index++] += level_b * f;
1424 
1425                 w       += w_delta;
1426                 mod     += mod_delta;
1427                 level_a += level_a_delta;
1428                 level_b += level_b_delta;
1429             }
1430             break;
1431 
1432           case 7:  /* 'Resonant III' (trapezoidal window) */
1433             mod = expf(mod * 6.0f * (float)M_LN2);
1434             mod_delta = expf(mod_delta * 6.0f * (float)M_LN2);
1435             if (mod * w > 0.5f) mod = 0.5f / w;
1436             if (mod_delta * w > 0.5f) mod_delta = 0.5f / w;
1437             mod_delta = (mod_delta - mod) / (float)sample_count;
1438 
1439             for (sample = 0; sample < sample_count; sample++) {
1440 
1441                 pos += w;
1442 
1443                 if (pos >= 1.0f) {
1444                     pos -= 1.0f;
1445                     voice->osc_sync[sample] = pos / w;
1446                 } else {
1447                     voice->osc_sync[sample] = -1.0f;
1448                 }
1449 
1450                 dpos = pos * mod;
1451                 if (pos < 0.5)  /* -FIX- where should this breakpoint be? */
1452                     window = 1.0f;
1453                 else
1454                     window = 2.0f * (1.0f - pos);
1455 
1456                 f = dpos * (float)SINETABLE_POINTS;
1457                 i = lrintf(f - 0.5f);
1458                 f -= (float)i;
1459                 i += SINETABLE_POINTS / 4; /* shift to get cosine from sine table */
1460                 i &= (SINETABLE_POINTS - 1);
1461                 f = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * f;
1462                 f = 0.5f - f;
1463                 f *= window;
1464                 f = 0.5f - f;
1465                 voice->osc_bus_a[index]   += level_a * f;
1466                 voice->osc_bus_b[index++] += level_b * f;
1467 
1468                 w       += w_delta;
1469                 mod     += mod_delta;
1470                 level_a += level_a_delta;
1471                 level_b += level_b_delta;
1472             }
1473             break;
1474         }
1475 
1476     } else {  /* alternating waveform ('octave modulation') */
1477 
1478         float mod0, mod0_delta,
1479               mod1, mod1_delta;
1480         int   wave0 = (vosc->waveform - 12) % 12,
1481               wave1 = (vosc->waveform - 12) / 12;
1482 
1483         if (*(sosc->mparam1) < 0.5f)
1484             f = 1.0f;
1485         else
1486             f = 2.0f * (1.0f - *(sosc->mparam1));
1487         switch (wave0) {
1488           default:
1489           case 0:  /* cosine<->saw */
1490             mod0 = 0.5f - f * mod * 0.5f;
1491             mod0_delta = 0.5f - f * mod_delta * 0.5f;
1492             if (mod0 < w) mod0 = w;
1493             else if (mod0 > 1.0f - w) mod0 = 1.0f - w;
1494             if (mod0_delta < w) mod0_delta = w;
1495             else if (mod0_delta > 1.0f - w) mod0_delta = 1.0f - w;
1496             break;
1497           case 1:  /* cosine<->square */
1498             mod0 = 0.5f - f * mod * 0.5f;
1499             mod0_delta = 0.5f - f * mod_delta * 0.5f;
1500             if (mod0 < w) mod0 = w;
1501             else if (mod0 > 0.5f) mod0 = 0.5f;
1502             if (mod0_delta < w) mod0_delta = w;
1503             else if (mod0_delta > 0.5f) mod0_delta = 0.5f;
1504             break;
1505           case 2:  /* cosine<->pulse */
1506             mod0 = 1.0f - f * mod;
1507             mod0_delta = 1.0f - f * mod_delta;
1508             if (mod0 < 4.0f * w) mod0 = 4.0f * w;
1509             else if (mod0 > 1.0f - 4.0f * w) mod0 = 1.0f - 4.0f * w;
1510             if (mod0_delta < 4.0f * w) mod0_delta = 4.0f * w;
1511             else if (mod0_delta > 1.0f - 4.0f * w) mod0_delta = 1.0f - 4.0f * w;
1512             break;
1513           case 5:  /* 'Resonant I' (sawtooth window) */
1514           case 6:  /* 'Resonant II' (triangle window) */
1515           case 7:  /* 'Resonant III' (trapezoidal window) */
1516             mod0 = expf(f * mod * 6.0f * (float)M_LN2);
1517             mod0_delta = expf(f * mod_delta * 6.0f * (float)M_LN2);
1518             if (mod0 * w > 0.5f) mod0 = 0.5f / w;
1519             if (mod0_delta * w > 0.5f) mod0_delta = 0.5f / w;
1520             break;
1521         }
1522         mod0_delta = (mod0_delta - mod0) / (float)sample_count;
1523 
1524         if (*(sosc->mparam1) < 0.5f)
1525             f = 2.0f * *(sosc->mparam1);
1526         else
1527             f = 1.0f;
1528         switch (wave1) {
1529           default:
1530           case 0:  /* cosine<->saw */
1531             mod1 = 0.5f - f * mod * 0.5f;
1532             mod1_delta = 0.5f - f * mod_delta * 0.5f;
1533             if (mod1 < w) mod1 = w;
1534             else if (mod1 > 1.0f - w) mod1 = 1.0f - w;
1535             if (mod1_delta < w) mod1_delta = w;
1536             else if (mod1_delta > 1.0f - w) mod1_delta = 1.0f - w;
1537             break;
1538           case 1:  /* cosine<->square */
1539             mod1 = 0.5f - f * mod * 0.5f;
1540             mod1_delta = 0.5f - f * mod_delta * 0.5f;
1541             if (mod1 < w) mod1 = w;
1542             else if (mod1 > 0.5f) mod1 = 0.5f;
1543             if (mod1_delta < w) mod1_delta = w;
1544             else if (mod1_delta > 0.5f) mod1_delta = 0.5f;
1545             break;
1546           case 2:  /* cosine<->pulse */
1547             mod1 = 1.0f - f * mod;
1548             mod1_delta = 1.0f - f * mod_delta;
1549             if (mod1 < 4.0f * w) mod1 = 4.0f * w;
1550             else if (mod1 > 1.0f - 4.0f * w) mod1 = 1.0f - 4.0f * w;
1551             if (mod1_delta < 4.0f * w) mod1_delta = 4.0f * w;
1552             else if (mod1_delta > 1.0f - 4.0f * w) mod1_delta = 1.0f - 4.0f * w;
1553             break;
1554           case 5:  /* 'Resonant I' (sawtooth window) */
1555           case 6:  /* 'Resonant II' (triangle window) */
1556           case 7:  /* 'Resonant III' (trapezoidal window) */
1557             mod1 = expf(f * mod * 6.0f * (float)M_LN2);
1558             mod1_delta = expf(f * mod_delta * 6.0f * (float)M_LN2);
1559             if (mod1 * w > 0.5f) mod1 = 0.5f / w;
1560             if (mod1_delta * w > 0.5f) mod1_delta = 0.5f / w;
1561             break;
1562         }
1563         mod1_delta = (mod1_delta - mod1) / (float)sample_count;
1564 
1565         sample = 0;
1566         while (sample < sample_count) {
1567 
1568             if (cycle == 0) {
1569                 mod = mod0;
1570                 mod_delta = mod0_delta;
1571                 i = wave0;
1572             } else {
1573                 mod = mod1;
1574                 mod_delta = mod1_delta;
1575                 i = wave1;
1576             }
1577             switch (i) {
1578 
1579               default:
1580               case 0:  /* cosine<->saw */
1581                 for (; sample < sample_count; sample++) {
1582 
1583                     if (pos < mod) {
1584                         dpos = pos * 0.5f / mod;
1585                     } else {
1586                         dpos = 0.5f + (pos - mod) * 0.5f / (1.0f - mod);
1587                     }
1588 
1589                     f = dpos * (float)SINETABLE_POINTS;
1590                     i = lrintf(f - 0.5f);
1591                     f -= (float)i;
1592                     i += SINETABLE_POINTS / 4;  /* shift to get cosine from sine table */
1593                     i &= (SINETABLE_POINTS - 1);
1594                     f = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * f;
1595                     voice->osc_bus_a[index]   += level_a * f;
1596                     voice->osc_bus_b[index++] += level_b * f;
1597 
1598                     pos     += w;
1599                     w       += w_delta;
1600                     mod     += mod_delta;
1601                     mod0    += mod0_delta;
1602                     mod1    += mod1_delta;
1603                     level_a += level_a_delta;
1604                     level_b += level_b_delta;
1605 
1606                     if (pos >= 1.0f) {
1607                         pos -= 1.0f;
1608                         voice->osc_sync[sample] = pos / w;
1609                         cycle ^= 1;
1610                         sample++;
1611                         break;
1612                     } else {
1613                         voice->osc_sync[sample] = -1.0f;
1614                     }
1615                 }
1616                 break;
1617 
1618               case 1:  /* cosine<->square */
1619                 for (; sample < sample_count; sample++) {
1620 
1621                     if (pos < mod) {
1622                         dpos = pos * 0.5f / mod;
1623                     } else if (pos < 0.5f) {
1624                         dpos = 0.5f;
1625                     } else if (pos < 0.5f + mod) {
1626                         dpos = (pos - 0.5f) * 0.5f / mod + 0.5f;
1627                     } else {
1628                         dpos = 1.0f;
1629                     }
1630 
1631                     f = dpos * (float)SINETABLE_POINTS;
1632                     i = lrintf(f - 0.5f);
1633                     f -= (float)i;
1634                     i += SINETABLE_POINTS / 4;  /* shift to get cosine from sine table */
1635                     i &= (SINETABLE_POINTS - 1);
1636                     f = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * f;
1637                     voice->osc_bus_a[index]   += level_a * f;
1638                     voice->osc_bus_b[index++] += level_b * f;
1639 
1640                     pos     += w;
1641                     w       += w_delta;
1642                     mod     += mod_delta;
1643                     mod0    += mod0_delta;
1644                     mod1    += mod1_delta;
1645                     level_a += level_a_delta;
1646                     level_b += level_b_delta;
1647 
1648                     if (pos >= 1.0f) {
1649                         pos -= 1.0f;
1650                         voice->osc_sync[sample] = pos / w;
1651                         cycle ^= 1;
1652                         sample++;
1653                         break;
1654                     } else {
1655                         voice->osc_sync[sample] = -1.0f;
1656                     }
1657                 }
1658                 break;
1659 
1660               case 2:  /* cosine<->pulse */
1661                 for (; sample < sample_count; sample++) {
1662 
1663                     if (pos < mod) {
1664                         dpos = pos / mod;
1665                     } else {
1666                         dpos = 1.0f;
1667                     }
1668 
1669                     f = dpos * (float)SINETABLE_POINTS;
1670                     i = lrintf(f - 0.5f);
1671                     f -= (float)i;
1672                     i += SINETABLE_POINTS / 4;  /* shift to get cosine from sine table */
1673                     i &= (SINETABLE_POINTS - 1);
1674                     f = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * f;
1675                     voice->osc_bus_a[index]   += level_a * f;
1676                     voice->osc_bus_b[index++] += level_b * f;
1677 
1678                     pos     += w;
1679                     w       += w_delta;
1680                     mod     += mod_delta;
1681                     mod0    += mod0_delta;
1682                     mod1    += mod1_delta;
1683                     level_a += level_a_delta;
1684                     level_b += level_b_delta;
1685 
1686                     if (pos >= 1.0f) {
1687                         pos -= 1.0f;
1688                         voice->osc_sync[sample] = pos / w;
1689                         cycle ^= 1;
1690                         sample++;
1691                         break;
1692                     } else {
1693                         voice->osc_sync[sample] = -1.0f;
1694                     }
1695                 }
1696                 break;
1697 
1698               case 5:  /* 'Resonant I' (sawtooth window) */
1699                 for (; sample < sample_count; sample++) {
1700 
1701                     dpos = pos * mod;
1702                     window = 1.0f - pos;
1703 
1704                     f = dpos * (float)SINETABLE_POINTS;
1705                     i = lrintf(f - 0.5f);
1706                     f -= (float)i;
1707                     i += SINETABLE_POINTS / 4;  /* shift to get cosine from sine table */
1708                     i &= (SINETABLE_POINTS - 1);
1709                     f = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * f;
1710                     f = 0.5f - f;
1711                     f *= window;
1712                     f = 0.5f - f;
1713                     voice->osc_bus_a[index]   += level_a * f;
1714                     voice->osc_bus_b[index++] += level_b * f;
1715 
1716                     pos     += w;
1717                     w       += w_delta;
1718                     mod     += mod_delta;
1719                     mod0    += mod0_delta;
1720                     mod1    += mod1_delta;
1721                     level_a += level_a_delta;
1722                     level_b += level_b_delta;
1723 
1724                     if (pos >= 1.0f) {
1725                         pos -= 1.0f;
1726                         voice->osc_sync[sample] = pos / w;
1727                         cycle ^= 1;
1728                         sample++;
1729                         break;
1730                     } else {
1731                         voice->osc_sync[sample] = -1.0f;
1732                     }
1733                 }
1734                 break;
1735 
1736               case 6:  /* 'Resonant II' (triangle window) */
1737                 for (; sample < sample_count; sample++) {
1738 
1739                     dpos = pos * mod;
1740                     if (pos < 0.5)
1741                         window = 2.0f * pos;
1742                     else
1743                         window = 2.0f * (1.0f - pos);
1744 
1745                     f = dpos * (float)SINETABLE_POINTS;
1746                     i = lrintf(f - 0.5f);
1747                     f -= (float)i;
1748                     i += SINETABLE_POINTS / 4;  /* shift to get cosine from sine table */
1749                     i &= (SINETABLE_POINTS - 1);
1750                     f = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * f;
1751                     f = 0.5f - f;
1752                     f *= window;
1753                     f = 0.5f - f;
1754                     voice->osc_bus_a[index]   += level_a * f;
1755                     voice->osc_bus_b[index++] += level_b * f;
1756 
1757                     pos     += w;
1758                     w       += w_delta;
1759                     mod     += mod_delta;
1760                     mod0    += mod0_delta;
1761                     mod1    += mod1_delta;
1762                     level_a += level_a_delta;
1763                     level_b += level_b_delta;
1764 
1765                     if (pos >= 1.0f) {
1766                         pos -= 1.0f;
1767                         voice->osc_sync[sample] = pos / w;
1768                         cycle ^= 1;
1769                         sample++;
1770                         break;
1771                     } else {
1772                         voice->osc_sync[sample] = -1.0f;
1773                     }
1774                 }
1775                 break;
1776 
1777               case 7:  /* 'Resonant III' (trapezoidal window) */
1778                 for (; sample < sample_count; sample++) {
1779 
1780                     dpos = pos * mod;
1781                     if (pos < 0.5)  /* -FIX- where should this breakpoint be? */
1782                         window = 1.0f;
1783                     else
1784                         window = 2.0f * (1.0f - pos);
1785 
1786                     f = dpos * (float)SINETABLE_POINTS;
1787                     i = lrintf(f - 0.5f);
1788                     f -= (float)i;
1789                     i += SINETABLE_POINTS / 4;  /* shift to get cosine from sine table */
1790                     i &= (SINETABLE_POINTS - 1);
1791                     f = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * f;
1792                     f = 0.5f - f;
1793                     f *= window;
1794                     f = 0.5f - f;
1795                     voice->osc_bus_a[index]   += level_a * f;
1796                     voice->osc_bus_b[index++] += level_b * f;
1797 
1798                     pos     += w;
1799                     w       += w_delta;
1800                     mod     += mod_delta;
1801                     mod0    += mod0_delta;
1802                     mod1    += mod1_delta;
1803                     level_a += level_a_delta;
1804                     level_b += level_b_delta;
1805 
1806                     if (pos >= 1.0f) {
1807                         pos -= 1.0f;
1808                         voice->osc_sync[sample] = pos / w;
1809                         cycle ^= 1;
1810                         sample++;
1811                         break;
1812                     } else {
1813                         voice->osc_sync[sample] = -1.0f;
1814                     }
1815                 }
1816                 break;
1817             }
1818         }
1819     }
1820 
1821     vosc->pos0 = (double)pos;
1822     vosc->i0 = cycle;
1823 }
1824 
1825 static void
wt_chorus(unsigned long sample_count,y_synth_t * synth,y_sosc_t * sosc,y_voice_t * voice,struct vosc * vosc,int index,float w0)1826 wt_chorus(unsigned long sample_count, y_synth_t *synth, y_sosc_t *sosc,
1827           y_voice_t *voice, struct vosc *vosc, int index, float w0)
1828 {
1829     signed short *wave0, *wave1;
1830     unsigned long sample;
1831     float pos0 = (float)vosc->pos0,
1832           pos1 = (float)vosc->pos1,
1833           pos2 = vosc->f0,
1834           pos3 = vosc->f1,
1835           pos4 = vosc->f2,
1836           wavemix0, wavemix1,
1837           w, w_delta, wm0, wm1, wm3, wm4,
1838           am04, am13,
1839           level_a, level_a_delta,
1840           level_b, level_b_delta;
1841     float f, a;
1842     int   i;
1843 
1844     wm1 = 0.999f - *(sosc->mparam1) * 0.008f;  /* MParam1 = tuning spread */
1845     wm3 = 1.0f / wm1;
1846     wm0 = 0.998f - *(sosc->mparam1) * 0.016f;
1847     wm4 = 1.0f / wm0;
1848 
1849     f = 1.0f - *(sosc->mmod_amt);              /* MMod AMt = mix profile */
1850     f *= f;
1851     am13 = expf(-f * 2.4f);
1852     am04 = am13 * am13;
1853 
1854     if (vosc->mode != vosc->last_mode) {
1855         vosc->last_mode = vosc->mode;
1856         vosc->last_waveform = -1;
1857         pos2 = random_float(0.0f, 1.0f);
1858         pos0 = pos2 + 0.8f; if (pos0 >= 1.0f) pos0 -= 1.0f;
1859         pos1 = pos2 + 0.1f; if (pos1 >= 1.0f) pos1 -= 1.0f;
1860         pos3 = pos2 + 0.5f; if (pos3 >= 1.0f) pos3 -= 1.0f;
1861         pos4 = pos2 + 0.2f; if (pos4 >= 1.0f) pos4 -= 1.0f;
1862     }
1863     i = voice->key + lrintf(*(sosc->pitch) + *(sosc->mparam2) * WAVETABLE_SELECT_BIAS_RANGE);
1864     if (vosc->waveform != vosc->last_waveform ||
1865         i              != vosc->wave_select_key) {
1866 
1867         /* select wave(s) and crossfade from wavetable */
1868         wavetable_select(vosc, i);
1869         vosc->last_waveform = vosc->waveform;
1870     }
1871 
1872     i = y_voice_mod_index(sosc->pitch_mod_src);
1873     f = *(sosc->pitch_mod_amt);
1874     w = 1.0f + f * voice->mod[i].value;
1875     w_delta = w + f * voice->mod[i].delta * (float)sample_count;
1876     w_delta *= w0;
1877     w       *= w0;
1878     w_delta = (w_delta - w) / (float)sample_count;
1879     /* -FIX- condition to [0, 0.5)? */
1880 
1881     i = y_voice_mod_index(sosc->amp_mod_src);
1882     f = *(sosc->amp_mod_amt);
1883     if (f > 0.0f)
1884         level_a = 1.0f - f + f * voice->mod[i].value;
1885     else
1886         level_a = 1.0f + f * voice->mod[i].value;
1887     level_a_delta = volume_cv_to_amplitude(level_a + f * voice->mod[i].delta * (float)sample_count);
1888     level_a       = volume_cv_to_amplitude(level_a);
1889     level_a       /= (65534.0f * 2.0f);
1890     level_a_delta /= (65534.0f * 2.0f);
1891     level_b       = level_a       * *(sosc->level_b);
1892     level_b_delta = level_a_delta * *(sosc->level_b);
1893     level_a       *= *(sosc->level_a);
1894     level_a_delta *= *(sosc->level_a);
1895     level_a_delta = (level_a_delta - level_a) / (float)sample_count;
1896     level_b_delta = (level_b_delta - level_b) / (float)sample_count;
1897     /* -FIX- condition to [0, 1]? */
1898 
1899     wave0 = vosc->wave0;
1900     wave1 = vosc->wave1;
1901     wavemix0 = vosc->wavemix0;
1902     wavemix1 = vosc->wavemix1;
1903 
1904     /* -FIX- optimize for the case of no crossfade */
1905 
1906     for (sample = 0; sample < sample_count; sample++) {
1907 
1908         pos0 += w * wm0;
1909         pos1 += w * wm1;
1910         pos2 += w;
1911         pos3 += w * wm3;
1912         pos4 += w * wm3;
1913         if (pos0 >= 1.0f) pos0 -= 1.0f;
1914         if (pos1 >= 1.0f) pos1 -= 1.0f;
1915         if (pos2 >= 1.0f) {
1916             pos2 -= 1.0f;
1917             voice->osc_sync[sample] = pos2 / w;
1918         } else {
1919             voice->osc_sync[sample] = -1.0f;
1920         }
1921         if (pos3 >= 1.0f) pos3 -= 1.0f;
1922         if (pos4 >= 1.0f) pos4 -= 1.0f;
1923 
1924         f = pos0 * (float)WAVETABLE_POINTS;
1925         i = lrintf(f - 0.5f);
1926         f -= (float)i;
1927         f = ((float)wave0[i] + (float)(wave0[i + 1] - wave0[i]) * f) * wavemix0 +
1928             ((float)wave1[i] + (float)(wave1[i + 1] - wave1[i]) * f) * wavemix1;
1929         a = f * am04;
1930 
1931         f = pos1 * (float)WAVETABLE_POINTS;
1932         i = lrintf(f - 0.5f);
1933         f -= (float)i;
1934         f = ((float)wave0[i] + (float)(wave0[i + 1] - wave0[i]) * f) * wavemix0 +
1935             ((float)wave1[i] + (float)(wave1[i + 1] - wave1[i]) * f) * wavemix1;
1936         a += f * am13;
1937 
1938         f = pos2 * (float)WAVETABLE_POINTS;
1939         i = lrintf(f - 0.5f);
1940         f -= (float)i;
1941         f = ((float)wave0[i] + (float)(wave0[i + 1] - wave0[i]) * f) * wavemix0 +
1942             ((float)wave1[i] + (float)(wave1[i + 1] - wave1[i]) * f) * wavemix1;
1943         a += f;
1944 
1945         f = pos3 * (float)WAVETABLE_POINTS;
1946         i = lrintf(f - 0.5f);
1947         f -= (float)i;
1948         f = ((float)wave0[i] + (float)(wave0[i + 1] - wave0[i]) * f) * wavemix0 +
1949             ((float)wave1[i] + (float)(wave1[i + 1] - wave1[i]) * f) * wavemix1;
1950         a += f * am13;
1951 
1952         f = pos4 * (float)WAVETABLE_POINTS;
1953         i = lrintf(f - 0.5f);
1954         f -= (float)i;
1955         f = ((float)wave0[i] + (float)(wave0[i + 1] - wave0[i]) * f) * wavemix0 +
1956             ((float)wave1[i] + (float)(wave1[i + 1] - wave1[i]) * f) * wavemix1;
1957         a += f * am04;
1958 
1959         voice->osc_bus_a[index]   += level_a * a;
1960         voice->osc_bus_b[index++] += level_b * a;
1961 
1962         w       += w_delta;
1963         level_a += level_a_delta;
1964         level_b += level_b_delta;
1965     }
1966 
1967     vosc->pos0 = (double)pos0;
1968     vosc->pos1 = (double)pos1;
1969     vosc->f0   = pos2;
1970     vosc->f1   = pos3;
1971     vosc->f2   = pos4;
1972 }
1973 
1974 static void
oscillator(unsigned long sample_count,y_synth_t * synth,y_sosc_t * sosc,y_voice_t * voice,struct vosc * vosc,int index,float w)1975 oscillator(unsigned long sample_count, y_synth_t *synth, y_sosc_t *sosc,
1976            y_voice_t *voice, struct vosc *vosc, int index, float w)
1977 
1978 {
1979     switch (vosc->mode) {
1980       default:
1981       case 0: /* disabled */
1982         break;
1983 
1984       case 1: /* minBLEP */
1985         if (*(sosc->mparam1) > 0.9f) /* sync on */
1986            blosc_slave(sample_count, sosc, voice, vosc, index, w);
1987         else
1988            blosc_master(sample_count, sosc, voice, vosc, index, w);
1989         break;
1990 
1991       case 2: /* wavetable */
1992         if (*(sosc->mparam1) > 0.9f) /* sync on */
1993             wt_osc_slave(sample_count, sosc, voice, vosc, index, w);
1994         else
1995             wt_osc_master(sample_count, sosc, voice, vosc, index, w);
1996         break;
1997 
1998       case 3: /* async granular */
1999         agran_oscillator(sample_count, synth, sosc, voice, vosc, index, w);
2000         break;
2001 
2002       case 4: /* FM Wave->Sine: sine phase modulated by wave */
2003         fm_wave2sine(sample_count, sosc, voice, vosc, index, w);
2004         break;
2005 
2006       case 5: /* FM Sine->Wave: wave phase modulated by sine */
2007         fm_sine2wave(sample_count, sosc, voice, vosc, index, w);
2008         break;
2009 
2010       case 6: /* waveshaper */
2011         waveshaper(sample_count, sosc, voice, vosc, index, w);
2012         break;
2013 
2014       case 7: /* noise */
2015         noise(sample_count, sosc, voice, vosc, index, w);
2016         break;
2017 
2018       case Y_OSCILLATOR_MODE_PADSYNTH: /* PADsynth */
2019         padsynth_oscillator(sample_count, sosc, voice, vosc, index, w);
2020         break;
2021 
2022       case Y_OSCILLATOR_MODE_PD: /* phase distortion */
2023         phase_distortion(sample_count, sosc, voice, vosc, index, w);
2024         break;
2025 
2026       case 10: /* FM Wave->LF Sine: low-frequency sine modulated by wave */
2027         fm_wave2lf(sample_count, synth, sosc, voice, vosc, index, w);
2028         break;
2029 
2030       case 11: /* wavetable chorus */
2031         wt_chorus(sample_count, synth, sosc, voice, vosc, index, w);
2032         break;
2033     }
2034 }
2035 
2036 /* ==== Filters ==== */
2037 
2038 /*
2039  * parameter 'freq' is normalized, i.e. a fraction of the sample rate, with 0.5 being Nyquist.
2040  *
2041  * Xsynth{,-DSSI} used a cutoff frequency calculation of:
2042  *    freq = M_PI_F * deltat * voice->current_pitch * synth->mod_wheel;  /-* now (0 to 1) * pi *-/
2043  *    freqkey_b = freq * *(synth->vcf2.frequency);
2044  *    freqeg1 = freq * *(synth->eg1_amount_f);
2045  *    freqeg2 = freq * *(synth->eg2_amount_f);
2046  *    cutoff[sample] = (freqkey_b + freqeg1 * eg1 + freqeg2 * eg2) *
2047  *                         (1.0f + lfo * *(synth->vcf2.freq_mod_amt));
2048  *    freqcut = cutoff[sample] * 2.0f ...
2049  */
2050 
2051 /* vcf_off
2052  */
2053 static inline void
vcf_off(unsigned long sample_count,struct vvcf * vvcf,float * out)2054 vcf_off(unsigned long sample_count, struct vvcf *vvcf, float *out)
2055 {
2056     if (vvcf->mode != vvcf->last_mode) {
2057         vvcf->last_mode = vvcf->mode;
2058         /* silence the filter output buffer */
2059         memset(out, 0, Y_CONTROL_PERIOD * sizeof(float));
2060     }
2061 }
2062 
2063 static inline float
stabilize(float freqcut,float freq,float qres)2064 stabilize(float freqcut, float freq, float qres)
2065 {
2066     /* SVF stabilization based on Eli Brandt's work
2067      * Eli's original 'f3' function for limiting cutoff frequency based on Q:
2068      *     freqmax = (-qres + sqrt(4.f + qres * qres));
2069      * My slightly more stable version:
2070      *     freqmax = (-qres + sqrt(4.f * sqrt(2.f) + qres * qres)) / sqrt(2.f);
2071      * A quick approximation thereof: */
2072     float freqmax = (0.115375f * qres - 0.673851f) * qres + 1.67588f;
2073 
2074     freqcut *= freq;
2075 
2076     if (freqcut > 0.48f) freqcut = 0.48f;
2077     else if (freqcut < 1e-5f) freqcut = 1e-5f;
2078 
2079     freqcut = (-5.98261f * freqcut + 7.11034f) * freqcut; /* quick approximation of 2.0f * sinf(M_PI_F * freqcut) */
2080     if (freqcut > freqmax) freqcut = freqmax;
2081 
2082     return freqcut;
2083 }
2084 
2085 enum _filter_type_t {
2086     FT_LOWPASS_2POLE,
2087     FT_LOWPASS_4POLE,
2088     FT_LOWPASS_4POLE_CLIP,
2089     FT_HIGHPASS_2POLE,
2090     FT_HIGHPASS_4POLE,
2091     FT_BANDPASS,
2092     FT_BANDREJECT
2093 };
2094 
2095 typedef enum _filter_type_t filter_type_t;
2096 
2097 // We define all the inner-loops for the Chamberlin filters using #defines
2098 // so that we can keep most of the conditional statements out of the loops.
2099 #define FILTER_LOOP_BANDPASS       FILTER_LOOP_PRELUDE { BANDPASS          UPDATE_FREQCUT }
2100 #define FILTER_LOOP_BANDREJECT     FILTER_LOOP_PRELUDE { BANDREJECT        UPDATE_FREQCUT }
2101 #define FILTER_LOOP_2POLE_HP       FILTER_LOOP_PRELUDE { TWO_POLE_HP       UPDATE_FREQCUT }
2102 #define FILTER_LOOP_4POLE_HP       FILTER_LOOP_PRELUDE { FOUR_POLE_HP      UPDATE_FREQCUT }
2103 #define FILTER_LOOP_2POLE_LP       FILTER_LOOP_PRELUDE { TWO_POLE_LP       UPDATE_FREQCUT }
2104 #define FILTER_LOOP_4POLE_LP       FILTER_LOOP_PRELUDE { FOUR_POLE_LP      UPDATE_FREQCUT }
2105 #define FILTER_LOOP_4POLE_LP_CLIP  FILTER_LOOP_PRELUDE { FOUR_POLE_LP_CLIP UPDATE_FREQCUT }
2106 
2107 #define FILTER_LOOP_PRELUDE  for (sample = 0; sample < sample_count; sample++)
2108 #define UPDATE_FREQCUT       freqcut += freqcut_delta;
2109 
2110 
2111 #define TWO_POLE_LP                   \
2112     input = in[sample];               \
2113     FIRST_FILTER_STAGE                \
2114     out[sample] = delay2;             \
2115 
2116 #define FOUR_POLE_LP                  \
2117     input = in[sample];               \
2118     FIRST_FILTER_STAGE                \
2119                                       \
2120     stage2_input = delay2;            \
2121     SECOND_FILTER_STAGE               \
2122                                       \
2123     out[sample] = delay4;             \
2124 
2125 #define BANDPASS                      \
2126     input = in[sample];               \
2127     FIRST_FILTER_STAGE                \
2128                                       \
2129     stage2_input = delay1;            \
2130     SECOND_FILTER_STAGE               \
2131                                       \
2132     out[sample] = delay3;             \
2133 
2134 #define BANDREJECT                    \
2135     input = in[sample];               \
2136     FIRST_FILTER_STAGE                \
2137                                       \
2138     stage2_input = highpass + delay2; \
2139     SECOND_FILTER_STAGE               \
2140                                       \
2141     out[sample] = highpass + delay4;  \
2142 
2143 #define TWO_POLE_HP                   \
2144     input = in[sample];               \
2145     FIRST_FILTER_STAGE                \
2146                                       \
2147     out[sample] = highpass;           \
2148 
2149 #define FOUR_POLE_HP                  \
2150     input = in[sample];               \
2151     FIRST_FILTER_STAGE                \
2152                                       \
2153     stage2_input = highpass;          \
2154     SECOND_FILTER_STAGE               \
2155                                       \
2156     out[sample] = highpass;           \
2157 
2158 #define FOUR_POLE_LP_CLIP             \
2159     input = in[sample] * gain;        \
2160     if (input > 0.7f)                 \
2161         input = 0.7f;                 \
2162     else if (input < -0.7f)           \
2163         input = -0.7f;                \
2164                                       \
2165     FIRST_FILTER_STAGE                \
2166                                       \
2167     stage2_input = delay2 * gain;     \
2168     if (stage2_input > 0.7f)          \
2169         stage2_input = 0.7f;          \
2170     else if (stage2_input < -0.7f)    \
2171         stage2_input = -0.7f;         \
2172                                       \
2173     SECOND_FILTER_STAGE               \
2174                                       \
2175     out[sample] = delay4;             \
2176 
2177 
2178 #define FIRST_FILTER_STAGE                                                        \
2179     delay2 = delay2 + freqcut * delay1;         /* delay2/4 = lowpass output */   \
2180     highpass = input - delay2 - qres * delay1;                                    \
2181     delay1 = freqcut * highpass + delay1;       /* delay1/3 = bandpass output */  \
2182 
2183 #define SECOND_FILTER_STAGE                                                       \
2184     delay4 = delay4 + freqcut * delay3;                                           \
2185     highpass = stage2_input - delay4 - qres * delay3;                             \
2186     delay3 = freqcut * highpass + delay3;                                         \
2187 
2188 
2189 /* vcf_2_4pole
2190  *
2191  * 2/4-pole Chamberlin state-variable low-pass filter
2192  */
2193 static void
vcf_2_4pole(unsigned long sample_count,y_svcf_t * svcf,y_voice_t * voice,struct vvcf * vvcf,float freq,filter_type_t type,float * in,float * out)2194 vcf_2_4pole(unsigned long sample_count, y_svcf_t *svcf, y_voice_t *voice,
2195           struct vvcf *vvcf, float freq, filter_type_t type, float *in, float *out)
2196 {
2197     unsigned long sample;
2198     int mod;
2199     float freqcut, freqtmp, freqcut_delta,
2200           qres, highpass, gain,
2201           delay1, delay2, delay3, delay4,
2202           input, stage2_input;
2203 
2204     if (vvcf->last_mode != vvcf->mode) {
2205         vvcf->delay1 = 0.0f;
2206         vvcf->delay2 = 0.0f;
2207         vvcf->delay3 = 0.0f;
2208         vvcf->delay4 = 0.0f;
2209         vvcf->last_mode = vvcf->mode;
2210     }
2211 
2212     if (type == FT_LOWPASS_2POLE)
2213         qres = 2.0f - *(svcf->qres) * 1.995f;
2214     else
2215         qres = 2.0f - *(svcf->qres) * 1.96f;
2216 
2217     mod = y_voice_mod_index(svcf->freq_mod_src);
2218 
2219     freqcut = (*(svcf->frequency) +
2220                 *(svcf->freq_mod_amt) * 50.0f * voice->mod[mod].value);
2221     freqtmp = freqcut +
2222                 *(svcf->freq_mod_amt) * 50.0f * (float)sample_count * voice->mod[mod].delta;
2223 
2224     freqcut = stabilize(freqcut, freq, qres);
2225     freqtmp = stabilize(freqtmp, freq, qres);
2226 
2227     freqcut_delta = (freqtmp - freqcut) / (float)sample_count;
2228 
2229     /* gain range: -24dB to +24dB */
2230     gain = volume_cv_to_amplitude(0.36f + *(svcf->mparam) * 0.64f) * 16.0f;
2231 
2232     delay1 = vvcf->delay1;
2233     delay2 = vvcf->delay2;
2234     delay3 = vvcf->delay3;
2235     delay4 = vvcf->delay4;
2236 
2237     switch (type)
2238     {
2239         case FT_BANDPASS:
2240             FILTER_LOOP_BANDPASS
2241             break;
2242 
2243         case FT_BANDREJECT:
2244             FILTER_LOOP_BANDREJECT
2245             break;
2246 
2247         case FT_HIGHPASS_2POLE:
2248             FILTER_LOOP_2POLE_HP
2249             break;
2250 
2251         case FT_HIGHPASS_4POLE:
2252             FILTER_LOOP_4POLE_HP
2253             break;
2254 
2255         case FT_LOWPASS_2POLE:
2256             FILTER_LOOP_2POLE_LP
2257             break;
2258 
2259         case FT_LOWPASS_4POLE:
2260             FILTER_LOOP_4POLE_LP
2261             break;
2262 
2263         case FT_LOWPASS_4POLE_CLIP:
2264             FILTER_LOOP_4POLE_LP_CLIP
2265             break;
2266     }
2267 
2268     vvcf->delay1 = delay1;
2269     vvcf->delay2 = delay2;
2270     vvcf->delay3 = delay3;
2271     vvcf->delay4 = delay4;
2272 }
2273 
2274 static inline void
vcf_2pole(unsigned long sample_count,y_svcf_t * svcf,y_voice_t * voice,struct vvcf * vvcf,float freq,float * in,float * out)2275 vcf_2pole(unsigned long sample_count, y_svcf_t *svcf, y_voice_t *voice,
2276           struct vvcf *vvcf, float freq, float *in, float *out)
2277 {
2278     vcf_2_4pole(sample_count, svcf, voice, vvcf, freq, FT_LOWPASS_2POLE, in, out);
2279 }
2280 
2281 static inline void
vcf_4pole(unsigned long sample_count,y_svcf_t * svcf,y_voice_t * voice,struct vvcf * vvcf,float freq,float * in,float * out)2282 vcf_4pole(unsigned long sample_count, y_svcf_t *svcf, y_voice_t *voice,
2283           struct vvcf *vvcf, float freq, float *in, float *out)
2284 {
2285     vcf_2_4pole(sample_count, svcf, voice, vvcf, freq, FT_LOWPASS_4POLE, in, out);
2286 }
2287 
2288 static inline void
vcf_clip4pole(unsigned long sample_count,y_svcf_t * svcf,y_voice_t * voice,struct vvcf * vvcf,float freq,float * in,float * out)2289 vcf_clip4pole(unsigned long sample_count, y_svcf_t *svcf, y_voice_t *voice,
2290              struct vvcf *vvcf, float freq, float *in, float *out)
2291 {
2292     vcf_2_4pole(sample_count, svcf, voice, vvcf, freq, FT_LOWPASS_4POLE_CLIP, in, out);
2293 }
2294 
2295 static inline void
vcf_bandpass(unsigned long sample_count,y_svcf_t * svcf,y_voice_t * voice,struct vvcf * vvcf,float freq,float * in,float * out)2296 vcf_bandpass(unsigned long sample_count, y_svcf_t *svcf, y_voice_t *voice,
2297              struct vvcf *vvcf, float freq, float *in, float *out)
2298 {
2299     vcf_2_4pole(sample_count, svcf, voice, vvcf, freq, FT_BANDPASS, in, out);
2300 }
2301 
2302 static inline void
vcf_bandreject(unsigned long sample_count,y_svcf_t * svcf,y_voice_t * voice,struct vvcf * vvcf,float freq,float * in,float * out)2303 vcf_bandreject(unsigned long sample_count, y_svcf_t *svcf, y_voice_t *voice,
2304              struct vvcf *vvcf, float freq, float *in, float *out)
2305 {
2306     vcf_2_4pole(sample_count, svcf, voice, vvcf, freq, FT_BANDREJECT, in, out);
2307 }
2308 
2309 static inline void
vcf_highpass_2pole(unsigned long sample_count,y_svcf_t * svcf,y_voice_t * voice,struct vvcf * vvcf,float freq,float * in,float * out)2310 vcf_highpass_2pole(unsigned long sample_count, y_svcf_t *svcf, y_voice_t *voice,
2311              struct vvcf *vvcf, float freq, float *in, float *out)
2312 {
2313     vcf_2_4pole(sample_count, svcf, voice, vvcf, freq, FT_HIGHPASS_2POLE, in, out);
2314 }
2315 
2316 static inline void
vcf_highpass_4pole(unsigned long sample_count,y_svcf_t * svcf,y_voice_t * voice,struct vvcf * vvcf,float freq,float * in,float * out)2317 vcf_highpass_4pole(unsigned long sample_count, y_svcf_t *svcf, y_voice_t *voice,
2318              struct vvcf *vvcf, float freq, float *in, float *out)
2319 {
2320     vcf_2_4pole(sample_count, svcf, voice, vvcf, freq, FT_HIGHPASS_4POLE, in, out);
2321 }
2322 
2323 /* vcf_mvclpf
2324  *
2325  * Fons Adriaensen's MVCLPF-3
2326  */
2327 void
vcf_mvclpf(unsigned long sample_count,y_svcf_t * svcf,y_voice_t * voice,struct vvcf * vvcf,float freq,float * in,float * out)2328 vcf_mvclpf(unsigned long sample_count, y_svcf_t *svcf, y_voice_t *voice,
2329            struct vvcf *vvcf, float freq, float *in, float *out)
2330 {
2331     unsigned long s;
2332     int mod;
2333     float w0, w0d, g0, g1, res, w, x, d,
2334           delay1, delay2, delay3, delay4, delay5;
2335 
2336     if (vvcf->last_mode != vvcf->mode) {
2337         vvcf->delay1 = 0.0f;
2338         vvcf->delay2 = 0.0f;
2339         vvcf->delay3 = 0.0f;
2340         vvcf->delay4 = 0.0f;
2341         vvcf->delay5 = 0.0f;
2342         vvcf->last_mode = vvcf->mode;
2343     }
2344 
2345     mod = y_voice_mod_index(svcf->freq_mod_src);
2346     w0 = (*(svcf->frequency) +
2347              *(svcf->freq_mod_amt) * 50.0f * voice->mod[mod].value);
2348     w0d = w0 + *(svcf->freq_mod_amt) * 50.0f *
2349                    (float)sample_count * voice->mod[mod].delta;
2350     w0  *= M_PI_F * freq;
2351     w0d *= M_PI_F * freq;
2352     if (w0 < 0.0f)
2353         w0 = 0.0f;
2354     if (w0d < 0.0f)
2355         w0d = 0.0f;
2356     w0d = (w0d - w0) / (float)sample_count;
2357 
2358     /* g0 = dB_to_amplitude(input_gain_in_db) / 2
2359      * Fons used a port range of -60dB to 10 dB; here we use -18dB to +18dB
2360      * 0.0 port value -> -18dB / 2 -> g0 = 0.0625
2361      * 0.5 port value ->   0dB / 2 -> g0 = 0.5
2362      * 1.0 port value -> +18dB / 2 -> g0 = 4.0 */
2363     g0 = volume_cv_to_amplitude(0.52f + *(svcf->mparam) * 0.48f) * (8.0f / 2.0f);
2364     /* g1 = dB_to_amplitude(output_gain_in_db) * 2
2365      * Fons used a port range of -15dB to 15dB; here it is the inverse of the input gain */
2366     g1 = 1.0f / g0;
2367 
2368     /* res should be 0 to 1 already */
2369     res = *(svcf->qres);
2370 
2371     delay1 = vvcf->delay1;
2372     delay2 = vvcf->delay2;
2373     delay3 = vvcf->delay3;
2374     delay4 = vvcf->delay4;
2375     delay5 = vvcf->delay5;
2376 
2377     for (s = 0; s < sample_count; s++) {
2378 
2379         w = w0;
2380         w0 += w0d;
2381 
2382         if (w < 0.75f) w *= 1.005f - w * (0.624f - w * (0.65f - w * 0.54f));
2383         else
2384 	{
2385 	    w *= 0.6748f;
2386             if (w > 0.82f) w = 0.82f;
2387 	}
2388 
2389         x = in[s] * g0 - (4.3f - 0.2f * w) * res * delay5 + 1e-10f;
2390         x /= sqrtf(1.0f + x * x);  /* x = tanh(x) */
2391         d = w * (x  - delay1) / (1.0f + delay1 * delay1);
2392         x = delay1 + 0.77f * d;
2393         delay1 = x + 0.23f * d;
2394         d = w * (x  - delay2) / (1.0f + delay2 * delay2);
2395         x = delay2 + 0.77f * d;
2396         delay2 = x + 0.23f * d;
2397         d = w * (x  - delay3) / (1.0f + delay3 * delay3);
2398         x = delay3 + 0.77f * d;
2399         delay3 = x + 0.23f * d;
2400         d = w * (x  - delay4);
2401         x = delay4 + 0.77f * d;
2402         delay4 = x + 0.23f * d;
2403         delay5 += 0.85f * (delay4 - delay5);
2404 
2405         x = in[s] * g0 - (4.3f - 0.2f * w) * res * delay5;
2406         x /= sqrtf(1.0f + x * x);  /* x = tanh(x) */
2407         d = w * (x  - delay1) / (1.0f + delay1 * delay1);
2408         x = delay1 + 0.77f * d;
2409         delay1 = x + 0.23f * d;
2410         d = w * (x  - delay2) / (1.0f + delay2 * delay2);
2411         x = delay2 + 0.77f * d;
2412         delay2 = x + 0.23f * d;
2413         d = w * (x  - delay3) / (1.0f + delay3 * delay3);
2414         x = delay3 + 0.77f * d;
2415         delay3 = x + 0.23f * d;
2416         d = w * (x  - delay4);
2417         x = delay4 + 0.77f * d;
2418         delay4 = x + 0.23f * d;
2419         delay5 += 0.85f * (delay4 - delay5);
2420 
2421         out[s] = g1 * delay4;
2422     }
2423 
2424     vvcf->delay1 = delay1;
2425     vvcf->delay2 = delay2;
2426     vvcf->delay3 = delay3;
2427     vvcf->delay4 = delay4;
2428     vvcf->delay5 = delay5;
2429 }
2430 
2431 /* vcf_amsynth
2432  *
2433  * Nick Dowell's amSynth 24 dB/octave resonant low-pass filter.
2434  */
2435 static void
vcf_amsynth(unsigned long sample_count,y_svcf_t * svcf,y_voice_t * voice,struct vvcf * vvcf,float freq,float * in,float * out)2436 vcf_amsynth(unsigned long sample_count, y_svcf_t *svcf, y_voice_t *voice,
2437             struct vvcf *vvcf, float freq, float *in, float *out)
2438 {
2439     unsigned long sample;
2440     int mod;
2441     float freqtmp;
2442 
2443     float r, k, k_delta, k2, bh;  /* These were all doubles in the original */
2444     float a0, a1, b1, b2;         /* amSynth filter, but I don't hear a     */
2445     float x, y;                   /* noticeable difference with floats....  */
2446     float d1, d2, d3, d4;
2447 
2448     if (vvcf->last_mode != vvcf->mode) {
2449         vvcf->last_mode = vvcf->mode;
2450         d1 = d2 = d3 = d4 = 0.0f;
2451     } else {
2452         d1 = vvcf->delay1;
2453         d2 = vvcf->delay2;
2454         d3 = vvcf->delay3;
2455         d4 = vvcf->delay4;
2456     }
2457 
2458     /* find coeff values for start and end of this buffer */
2459     mod = y_voice_mod_index(svcf->freq_mod_src);
2460     freqtmp = (*(svcf->frequency) +
2461                   *(svcf->freq_mod_amt) * 50.0f * voice->mod[mod].value);
2462     freqtmp *= freq;
2463     if (freqtmp > 0.495f) freqtmp = 0.495f;  /* filter is unstable _AT_ PI */
2464     else if (freqtmp < 1e-4f) freqtmp = 1e-4f;
2465     k = tanf(freqtmp * M_PI_F);  /* -FIX- optimizable? */
2466     freqtmp += freq * *(svcf->freq_mod_amt) * 50.0f *
2467                    (float)sample_count * voice->mod[mod].delta;
2468     if (freqtmp > 0.495f) freqtmp = 0.495f;  /* filter is unstable _AT_ PI */
2469     else if (freqtmp < 1e-4f) freqtmp = 1e-4f;
2470     k_delta = tanf(freqtmp * M_PI_F);
2471     k_delta = (k_delta - k) / (float)sample_count;
2472 
2473     r = 2.0f * (1.0f - *(svcf->qres) * 0.97f);
2474     if (r == 0.0f) r = 0.001f;
2475 
2476     for (sample = 0; sample < sample_count; sample++) {
2477 
2478         k2 = k * k;
2479         bh = 1.0f + (r * k) + k2;
2480         a0 = k2 / bh;
2481         /* a2 = a0; */
2482         a1 = a0 * 2.0f;
2483         b1 = 2.0f * (k2 - 1.0f) / -bh;
2484         b2 = (1.0f - (r * k) + k2) / -bh;
2485 
2486         /* filter (2 cascaded second order filters) */
2487         x = in[sample];
2488 
2489         /* first 2nd-order unit */
2490         y = ( a0*x ) + d1;
2491         d1 = d2 + ( (a1)*x ) + ( (b1)*y );
2492         d2 = ( /* a2 */ a0*x ) + ( (b2)*y );
2493         x = y;
2494 
2495         /* and the second */
2496         y = ( a0*x ) + d3;
2497         d3 = d4 + ( a1*x ) + ( b1*y );
2498         d4 = ( /* a2 */ a0*x ) + ( b2*y );
2499 
2500         out[sample] = y;
2501 
2502         k += k_delta;
2503     }
2504 
2505     vvcf->delay1 = d1;
2506     vvcf->delay2 = d2;
2507     vvcf->delay3 = d3;
2508     vvcf->delay4 = d4;
2509 }
2510 
2511 /* vcf_resonz
2512  *
2513  * resonz from Csound ugsc.c
2514  *
2515  * An implementation of the 2-pole, 2-zero reson filter
2516  * described by Julius O. Smith and James B. Angell in
2517  * "A Constant Gain Digital Resonator Tuned by a Single
2518  * Coefficient," Computer Music Journal, Vol. 6, No. 4,
2519  * Winter 1982, p.36-39. resonz implements the version
2520  * where the zeros are located at z = 1 and z = -1.
2521  *
2522  * -FIX- 'resonance' knob should be labeled 'bandwidth' for this and resonz....
2523  */
2524 static void
vcf_resonz(unsigned long sample_count,y_svcf_t * svcf,y_voice_t * voice,struct vvcf * vvcf,float freq,float * in,float * out)2525 vcf_resonz(unsigned long sample_count, y_svcf_t *svcf, y_voice_t *voice,
2526            struct vvcf *vvcf, float freq, float *in, float *out)
2527 {
2528     unsigned long sample;
2529     int mod;
2530     float freqtmp, kbw;
2531 
2532     float r, scale; /* radius & scaling factor */
2533     float c1, c2;   /* filter coefficients */
2534     float xn, yn, xnm1, xnm2, ynm1, ynm2;
2535 
2536     if (vvcf->last_mode != vvcf->mode) {
2537         vvcf->delay1 = 0.0f;
2538         vvcf->delay2 = 0.0f;
2539         vvcf->delay3 = 0.0f;
2540         vvcf->delay4 = 0.0f;
2541         vvcf->last_mode = vvcf->mode;
2542     }
2543 
2544     mod = y_voice_mod_index(svcf->freq_mod_src);
2545     freqtmp = (*(svcf->frequency) +
2546                   *(svcf->freq_mod_amt) * 50.0f * voice->mod[mod].value);
2547     freq *= freqtmp;
2548     if (freq > 0.48f) freq = 0.48f;
2549     else if (freq < 2e-4f) freq = 2e-4f;
2550 
2551     kbw = 1.0f - *(svcf->qres);
2552     kbw = 0.5f * kbw * kbw * kbw * kbw;
2553     if (kbw < 6.25e-5f) kbw = 6.25e-5f;
2554 
2555     r = expf(-M_PI_F * kbw);
2556     c1 = 2.0f * r * cosf(M_2PI_F * freq);
2557     c2 = r * r;
2558 
2559     /* Normalizing factors derived from equations in Ken Steiglitz,
2560      * "A Note on Constant-Gain Digital Resonators," Computer
2561      * Music Journal, vol. 18, no. 4, pp. 8-10, Winter 1982.
2562      */
2563     /* -FIX- */
2564     // if (p->scaletype == 1)
2565     //   scale = (1.0f - c2) * 0.5f;
2566     // else if (p->scaletype == 2)
2567       scale = sqrtf((1.0f - c2) * 0.5f);
2568     // else scale = 1.0f;
2569 
2570     xnm1 = vvcf->delay1;
2571     xnm2 = vvcf->delay2;
2572     ynm1 = vvcf->delay3;
2573     ynm2 = vvcf->delay4;
2574 
2575     for (sample = 0; sample < sample_count; sample++) {
2576 
2577         xn = in[sample];
2578         out[sample] = yn = scale * (xn - xnm2) + c1 * ynm1 - c2 * ynm2;
2579         xnm2 = xnm1;
2580         xnm1 = xn;
2581         ynm2 = ynm1;
2582         ynm1 = yn;
2583     }
2584 
2585     vvcf->delay1 = xnm1;
2586     vvcf->delay2 = xnm2;
2587     vvcf->delay3 = ynm1;
2588     vvcf->delay4 = ynm2;
2589 }
2590 
2591 /*
2592  * y_voice_render
2593  *
2594  * generate the actual sound data for this voice
2595  */
2596 void
y_voice_render(y_synth_t * synth,y_voice_t * voice,LADSPA_Data * out_left,LADSPA_Data * out_right,unsigned long sample_count,int do_control_update)2597 y_voice_render(y_synth_t *synth, y_voice_t *voice,
2598                     LADSPA_Data *out_left, LADSPA_Data *out_right,
2599                     unsigned long sample_count, int do_control_update)
2600 {
2601     unsigned long sample;
2602     float         deltat = synth->deltat;
2603     int           osc_index = voice->osc_index;
2604     float         osc1_omega, osc2_omega, osc3_omega, osc4_omega,
2605                  *vcf_source;
2606 
2607     /* calculate fundamental pitch of voice */
2608     voice->current_pitch = *(synth->glide_time) * voice->target_pitch +
2609                             (1.0f - *(synth->glide_time)) * voice->prev_pitch;    /* portamento */
2610     if (do_control_update) {
2611         voice->prev_pitch = voice->current_pitch; /* save pitch for next time */
2612     }
2613     voice->current_pitch *= synth->pitch_bend * *(synth->tuning);
2614 
2615     /* condition some frequently-used integer ports */
2616     voice->osc1.mode        = lrintf(*(synth->osc1.mode));
2617     voice->osc1.waveform    = y_voice_waveform_index(synth->osc1.waveform);
2618     voice->osc2.mode        = lrintf(*(synth->osc2.mode));
2619     voice->osc2.waveform    = y_voice_waveform_index(synth->osc2.waveform);
2620     voice->osc3.mode        = lrintf(*(synth->osc3.mode));
2621     voice->osc3.waveform    = y_voice_waveform_index(synth->osc3.waveform);
2622     voice->osc4.mode        = lrintf(*(synth->osc4.mode));
2623     voice->osc4.waveform    = y_voice_waveform_index(synth->osc4.waveform);
2624     voice->vcf1.mode        = lrintf(*(synth->vcf1.mode));
2625     voice->vcf2.mode        = lrintf(*(synth->vcf2.mode));
2626 
2627     /* update modulators */
2628     voice->mod[Y_MOD_MODWHEEL] = synth->mod[Y_MOD_MODWHEEL];
2629     y_mod_update_pressure(synth, voice);
2630     voice->mod[Y_MOD_GLFO]     = synth->mod[Y_GLOBAL_MOD_GLFO];
2631     voice->mod[Y_MOD_GLFO_UP]  = synth->mod[Y_GLOBAL_MOD_GLFO_UP];
2632     y_mod_update_modmix(synth, voice, sample_count);
2633 
2634     /* --- VCO section */
2635 
2636     /* -FIX- this should move into oscillators, so they can do mode-specific things with it (like ignore it?...) */
2637     osc1_omega = voice->current_pitch * pitch_to_frequency(69.0f + *(synth->osc1.pitch) + *(synth->osc1.detune));
2638     osc2_omega = voice->current_pitch * pitch_to_frequency(69.0f + *(synth->osc2.pitch) + *(synth->osc2.detune));
2639     osc3_omega = voice->current_pitch * pitch_to_frequency(69.0f + *(synth->osc3.pitch) + *(synth->osc3.detune));
2640     osc4_omega = voice->current_pitch * pitch_to_frequency(69.0f + *(synth->osc4.pitch) + *(synth->osc4.detune));
2641 
2642     oscillator(sample_count, synth, &synth->osc1, voice, &voice->osc1, osc_index, deltat * osc1_omega);
2643     oscillator(sample_count, synth, &synth->osc2, voice, &voice->osc2, osc_index, deltat * osc2_omega);
2644     oscillator(sample_count, synth, &synth->osc3, voice, &voice->osc3, osc_index, deltat * osc3_omega);
2645     oscillator(sample_count, synth, &synth->osc4, voice, &voice->osc4, osc_index, deltat * osc4_omega);
2646 
2647     /* --- VCF section */
2648 
2649     voice->osc_bus_a[osc_index] += 1e-20f; /* make sure things don't get too quiet... */
2650     voice->osc_bus_b[osc_index] += 1e-20f;
2651     voice->osc_bus_a[osc_index + (sample_count >> 1)] -= 1e-20f;
2652     voice->osc_bus_b[osc_index + (sample_count >> 1)] -= 1e-20f;
2653 
2654     vcf_source = (*(synth->vcf1.source) < 0.001f) ? voice->osc_bus_a :
2655                                                     voice->osc_bus_b;
2656     vcf_source += osc_index;
2657     switch (lrintf(*(synth->vcf1.mode))) {
2658       default:
2659       case 0:
2660         vcf_off(sample_count, &voice->vcf1, synth->vcf1_out);
2661         break;
2662       case 1:
2663         vcf_2pole(sample_count, &synth->vcf1, voice, &voice->vcf1,
2664                   deltat * voice->current_pitch,
2665                   vcf_source, synth->vcf1_out);
2666         break;
2667       case 2:
2668         vcf_4pole(sample_count, &synth->vcf1, voice, &voice->vcf1,
2669                   deltat * voice->current_pitch,
2670                   vcf_source, synth->vcf1_out);
2671         break;
2672       case 3:
2673         vcf_mvclpf(sample_count, &synth->vcf1, voice, &voice->vcf1,
2674                    deltat * voice->current_pitch,
2675                    vcf_source, synth->vcf1_out);
2676         break;
2677       case 4:
2678         vcf_clip4pole(sample_count, &synth->vcf1, voice, &voice->vcf1,
2679                       deltat * voice->current_pitch,
2680                       vcf_source, synth->vcf1_out);
2681         break;
2682       case 5:
2683         vcf_bandpass(sample_count, &synth->vcf1, voice, &voice->vcf1,
2684                      deltat * voice->current_pitch,
2685                      vcf_source, synth->vcf1_out);
2686         break;
2687       case 6:
2688         vcf_amsynth(sample_count, &synth->vcf1, voice, &voice->vcf1,
2689                     deltat * voice->current_pitch,
2690                     vcf_source, synth->vcf1_out);
2691         break;
2692       case 7:
2693         vcf_resonz(sample_count, &synth->vcf1, voice, &voice->vcf1,
2694                   deltat * voice->current_pitch,
2695                   vcf_source, synth->vcf1_out);
2696         break;
2697       case 8:
2698         vcf_highpass_2pole(sample_count, &synth->vcf1, voice, &voice->vcf1,
2699                     deltat * voice->current_pitch,
2700                     vcf_source, synth->vcf1_out);
2701         break;
2702       case 9:
2703         vcf_highpass_4pole(sample_count, &synth->vcf1, voice, &voice->vcf1,
2704                     deltat * voice->current_pitch,
2705                     vcf_source, synth->vcf1_out);
2706         break;
2707       case 10:
2708         vcf_bandreject(sample_count, &synth->vcf1, voice, &voice->vcf1,
2709                     deltat * voice->current_pitch,
2710                     vcf_source, synth->vcf1_out);
2711         break;
2712     }
2713 
2714     switch (lrintf(*(synth->vcf2.source))) {
2715       default:
2716       case 0:
2717         vcf_source = voice->osc_bus_a + osc_index;
2718         break;
2719       case 1:
2720         vcf_source = voice->osc_bus_b + osc_index;
2721         break;
2722       case 2:
2723         vcf_source = synth->vcf1_out;
2724         break;
2725     }
2726     switch (lrintf(*(synth->vcf2.mode))) {
2727       default:
2728       case 0:
2729         vcf_off(sample_count, &voice->vcf2, synth->vcf2_out);
2730         break;
2731       case 1:
2732         vcf_2pole(sample_count, &synth->vcf2, voice, &voice->vcf2,
2733                   deltat * voice->current_pitch,
2734                   vcf_source, synth->vcf2_out);
2735         break;
2736       case 2:
2737         vcf_4pole(sample_count, &synth->vcf2, voice, &voice->vcf2,
2738                   deltat * voice->current_pitch,
2739                   vcf_source, synth->vcf2_out);
2740         break;
2741       case 3:
2742         vcf_mvclpf(sample_count, &synth->vcf2, voice, &voice->vcf2,
2743                    deltat * voice->current_pitch,
2744                    vcf_source, synth->vcf2_out);
2745         break;
2746       case 4:
2747         vcf_clip4pole(sample_count, &synth->vcf2, voice, &voice->vcf2,
2748                       deltat * voice->current_pitch,
2749                       vcf_source, synth->vcf2_out);
2750         break;
2751       case 5:
2752         vcf_bandpass(sample_count, &synth->vcf2, voice, &voice->vcf2,
2753                      deltat * voice->current_pitch,
2754                      vcf_source, synth->vcf2_out);
2755         break;
2756       case 6:
2757         vcf_amsynth(sample_count, &synth->vcf2, voice, &voice->vcf2,
2758                     deltat * voice->current_pitch,
2759                     vcf_source, synth->vcf2_out);
2760         break;
2761       case 7:
2762         vcf_resonz(sample_count, &synth->vcf2, voice, &voice->vcf2,
2763                   deltat * voice->current_pitch,
2764                   vcf_source, synth->vcf2_out);
2765         break;
2766       case 8:
2767         vcf_highpass_2pole(sample_count, &synth->vcf2, voice, &voice->vcf2,
2768                     deltat * voice->current_pitch,
2769                     vcf_source, synth->vcf2_out);
2770         break;
2771       case 9:
2772         vcf_highpass_4pole(sample_count, &synth->vcf2, voice, &voice->vcf2,
2773                     deltat * voice->current_pitch,
2774                     vcf_source, synth->vcf2_out);
2775         break;
2776       case 10:
2777         vcf_bandreject(sample_count, &synth->vcf2, voice, &voice->vcf2,
2778                     deltat * voice->current_pitch,
2779                     vcf_source, synth->vcf2_out);
2780         break;
2781     }
2782 
2783     /* --- VCA section */
2784 
2785     {   float amp_busa_l = *(synth->busa_level) * pan_cv_to_amplitude(1.0f - *(synth->busa_pan)),
2786               amp_busa_r = *(synth->busa_level) * pan_cv_to_amplitude(       *(synth->busa_pan)),
2787               amp_busb_l = *(synth->busb_level) * pan_cv_to_amplitude(1.0f - *(synth->busb_pan)),
2788               amp_busb_r = *(synth->busb_level) * pan_cv_to_amplitude(       *(synth->busb_pan)),
2789               amp_vcf1_l = *(synth->vcf1_level) * pan_cv_to_amplitude(1.0f - *(synth->vcf1_pan)),
2790               amp_vcf1_r = *(synth->vcf1_level) * pan_cv_to_amplitude(       *(synth->vcf1_pan)),
2791               amp_vcf2_l = *(synth->vcf2_level) * pan_cv_to_amplitude(1.0f - *(synth->vcf2_pan)),
2792               amp_vcf2_r = *(synth->vcf2_level) * pan_cv_to_amplitude(       *(synth->vcf2_pan)),
2793               vol_out   = volume(*(synth->volume) * synth->cc_volume),
2794               vca       = vol_out * volume_cv_to_amplitude(voice->mod[Y_MOD_EGO].value),
2795               vca_delta = vol_out * volume_cv_to_amplitude(voice->mod[Y_MOD_EGO].value +
2796                                                            voice->mod[Y_MOD_EGO].delta *
2797                                                                (float)sample_count);
2798 
2799         vca_delta = (vca_delta - vca) / (float)sample_count;
2800 
2801         for (sample = 0; sample < sample_count; sample++) {
2802             out_left[sample]  += vca *
2803                                  (amp_busa_l * voice->osc_bus_a[sample + osc_index] +
2804                                   amp_busb_l * voice->osc_bus_b[sample + osc_index] +
2805                                   amp_vcf1_l * synth->vcf1_out[sample] +
2806                                   amp_vcf2_l * synth->vcf2_out[sample]);
2807             out_right[sample] += vca *
2808                                  (amp_busa_r * voice->osc_bus_a[sample + osc_index] +
2809                                   amp_busb_r * voice->osc_bus_b[sample + osc_index] +
2810                                   amp_vcf1_r * synth->vcf1_out[sample] +
2811                                   amp_vcf2_r * synth->vcf2_out[sample]);
2812             vca += vca_delta;
2813 
2814             /* zero oscillator buses for next time around */
2815             voice->osc_bus_a[sample + osc_index] = 0.0f;
2816             voice->osc_bus_b[sample + osc_index] = 0.0f;
2817         }
2818     }
2819 
2820     osc_index += sample_count;
2821 
2822     if (do_control_update) {
2823         /* do those things that should be done only once per control-calculation
2824          * interval (render burst), such as voice check-for-dead, pitch
2825          * envelope calculations, volume envelope phase transition checks, etc.
2826          */
2827 
2828         /* -FIX- updating modulators _after_ rendering means a delay of up to 64 samples in
2829          * the response to those we can't predict (modwheel, pressure, etc.) -- as part of the
2830          * redesign, they need to be updated on the top side of the control period. */
2831         voice->mod[Y_MOD_MODWHEEL].value += (float)sample_count * voice->mod[Y_MOD_MODWHEEL].delta;
2832         voice->mod[Y_MOD_PRESSURE].value += (float)sample_count * voice->mod[Y_MOD_PRESSURE].delta;
2833         y_voice_update_lfo(synth, &synth->vlfo, &voice->vlfo,  voice->mod, &voice->mod[Y_MOD_VLFO]);
2834         y_voice_update_lfo(synth, &synth->mlfo, &voice->mlfo0, voice->mod, &voice->mod[Y_MOD_MLFO0]);
2835         y_voice_update_lfo(synth, &synth->mlfo, &voice->mlfo1, voice->mod, &voice->mod[Y_MOD_MLFO1]);
2836         y_voice_update_lfo(synth, &synth->mlfo, &voice->mlfo2, voice->mod, &voice->mod[Y_MOD_MLFO2]);
2837         y_voice_update_lfo(synth, &synth->mlfo, &voice->mlfo3, voice->mod, &voice->mod[Y_MOD_MLFO3]);
2838 
2839         y_voice_update_eg(&synth->ego, voice, &voice->ego, &voice->mod[Y_MOD_EGO]);
2840         /* check if we've decayed to nothing, turn off voice if so */
2841         if (y_voice_check_for_dead(synth, voice))
2842             return; /* we're dead now, so return */
2843 
2844         y_voice_update_eg(&synth->eg1, voice, &voice->eg1, &voice->mod[Y_MOD_EG1]);
2845         y_voice_update_eg(&synth->eg2, voice, &voice->eg2, &voice->mod[Y_MOD_EG2]);
2846         y_voice_update_eg(&synth->eg3, voice, &voice->eg3, &voice->mod[Y_MOD_EG3]);
2847         y_voice_update_eg(&synth->eg4, voice, &voice->eg4, &voice->mod[Y_MOD_EG4]);
2848         voice->mod[Y_MOD_MIX].value += (float)sample_count * voice->mod[Y_MOD_MIX].delta;
2849 
2850         osc_index &= OSC_BUS_MASK;
2851 
2852     } else {
2853         /* update modulator values for the incomplete control cycle */
2854         int i;
2855 
2856         for (i = 1; i < Y_MODS_COUNT; i++)
2857             voice->mod[i].value += (float)sample_count * voice->mod[i].delta;
2858     }
2859 
2860     /* save things for next time around */
2861 
2862     /* already saved prev_pitch above */
2863     voice->osc_index  = osc_index;
2864 }
2865 
2866