1 /* WhySynth DSSI software synthesizer plugin
2  *
3  * Copyright (C) 2004-2008, 2010 Sean Bolton and others.
4  *
5  * Portions of this file may have come from Steve Brookes'
6  * Xsynth, copyright (C) 1999 S. J. Brookes.
7  * Portions of this file may have come from Peter Hanappe's
8  * Fluidsynth, copyright (C) 2003 Peter Hanappe and others.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be
16  * useful, but WITHOUT ANY WARRANTY; without even the implied
17  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18  * PURPOSE.  See the GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this program; if not, write to the Free
22  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  * Boston, MA 02110-1301 USA.
24  */
25 
26 #ifndef _WHYSYNTH_VOICE_H
27 #define _WHYSYNTH_VOICE_H
28 
29 #define _BSD_SOURCE    1
30 #define _SVID_SOURCE   1
31 #define _ISOC99_SOURCE 1
32 
33 #include <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36 
37 #include <ladspa.h>
38 #include <dssi.h>
39 
40 #include "whysynth_types.h"
41 #include "whysynth_ports.h"
42 
43 /* control-calculation period, in samples; also the maximum size of a rendering
44  * burst: */
45 #define Y_CONTROL_PERIOD        64
46 
47 /* minBLEP constants */
48 /* minBLEP table oversampling factor (must be a power of two): */
49 #define MINBLEP_PHASES          64
50 /* MINBLEP_PHASES minus one: */
51 #define MINBLEP_PHASE_MASK      63
52 /* length in samples of (truncated) discontinuity deltas: */
53 #define DD_PULSE_LENGTH         64
54 /* delay between start of DD pulse and the discontinuity, in samples: */
55 #define DD_SAMPLE_DELAY          4
56 /* OSC_BUS_LENGTH must be a power of two, equal to or greater than
57  * Y_CONTROL_PERIOD plus DD_PULSE_LENGTH: */
58 #define OSC_BUS_LENGTH         128
59 #define OSC_BUS_MASK          (OSC_BUS_LENGTH - 1)
60 
61 /* Length of sine wave table for FM and waveshaper oscillators (must be
62  * a power of two) */
63 #define SINETABLE_POINTS      1024
64 
65 #define WAVETABLE_SELECT_BIAS_RANGE  60.0f
66 
67 /* ==== y_patch_t ==== */
68 
69 /* -PORTS- */
70 struct posc
71 {
72     int    mode;
73     int    waveform;
74     int    pitch;
75     float  detune;
76     int    pitch_mod_src;
77     float  pitch_mod_amt;
78     float  mparam1;       /* sync / grain lz / mod freq ratio / ws phase offset / slave freq / noise cutoff freq */
79     float  mparam2;       /* pulsewidth / wave sel bias / grain spread / mod freq detune / mod amount bias */
80     int    mmod_src;      /* (pw | mod index | slave freq ) mod source / grain envelope */
81     float  mmod_amt;      /* (pw | mod index | slave freq ) mod amount / grain pitch distribution */
82     int    amp_mod_src;
83     float  amp_mod_amt;
84     float  level_a;
85     float  level_b;
86 };
87 
88 struct pvcf
89 {
90     int    mode;
91     int    source;
92     float  frequency;
93     int    freq_mod_src;
94     float  freq_mod_amt;
95     float  qres;
96     float  mparam;
97 };
98 
99 struct plfo
100 {
101     float  frequency;
102     int    waveform;
103     float  delay;
104     int    amp_mod_src;
105     float  amp_mod_amt;
106 };
107 
108 struct peg
109 {
110     int    mode;
111     int    shape1;
112     float  time1;
113     float  level1;
114     int    shape2;
115     float  time2;
116     float  level2;
117     int    shape3;
118     float  time3;
119     float  level3;
120     int    shape4;
121     float  time4;
122     float  vel_level_sens;
123     float  vel_time_scale;
124     float  kbd_time_scale;
125     int    amp_mod_src;
126     float  amp_mod_amt;
127 };
128 
129 struct _y_patch_t
130 {
131     char          name[31];
132     char          comment[61];
133 
134     struct posc   osc1,
135                   osc2,
136                   osc3,
137                   osc4;
138     struct pvcf   vcf1,
139                   vcf2;
140 
141     float         busa_level;
142     float         busa_pan;
143     float         busb_level;
144     float         busb_pan;
145     float         vcf1_level;
146     float         vcf1_pan;
147     float         vcf2_level;
148     float         vcf2_pan;
149     float         volume;
150 
151     int           effect_mode;
152     float         effect_param1;
153     float         effect_param2;
154     float         effect_param3;
155     float         effect_param4;
156     float         effect_param5;
157     float         effect_param6;
158     float         effect_mix;
159 
160     float         glide_time;
161     int           bend_range;
162 
163     struct plfo   glfo,
164                   vlfo,
165                   mlfo;
166     float         mlfo_phase_spread;
167     float         mlfo_random_freq;
168 
169     struct peg    ego,
170                   eg1,
171                   eg2,
172                   eg3,
173                   eg4;
174 
175     float         modmix_bias;
176     int           modmix_mod1_src;
177     float         modmix_mod1_amt;
178     int           modmix_mod2_src;
179     float         modmix_mod2_amt;
180 };
181 
182 /* ==== y_voice_t ==== */
183 
184 enum y_voice_status
185 {
186     Y_VOICE_OFF,       /* silent: is not processed by render loop */
187     Y_VOICE_ON,        /* has not received a note off event */
188     Y_VOICE_SUSTAINED, /* has received note off, but sustain controller is on */
189     Y_VOICE_RELEASED   /* had note off, not sustained, in final decay phase of envelopes */
190 };
191 
192 /* -PORTS- */
193 struct vosc
194 {
195     /* copies of LADSPA ports, copied in each render burst */
196     int           mode,        /* oscillator mode; conditioned to integer */
197                   waveform;    /* waveform; conditioned to integer */
198 
199     /* persistent voice state */
200     /* -- all oscillators */
201     int           last_mode,
202                   last_waveform;
203     double        pos0,        /* wavetable phase or sample position */
204                   pos1;
205     /* -- wavetable, async granular, FM, waveshaper, PADsynth */
206     int           wave_select_key;
207     signed short *wave0,       /* pointer to lower wavetable */
208                  *wave1;       /* pointer to upper wavetable */
209     float         wavemix0,    /* crossfade: lower table/sample amplitude */
210                   wavemix1;    /* crossfade: upper table/sample amplitude */
211     /* -- async granular */
212     grain_t      *grain_list;  /* active grain list */
213     /* -- shared storage for miscellaneous state */
214     int           i0,          /* agran next_onset; PADsynth lower sample index */
215                   i1;          /* minBLEP bp_high;  PADsynth upper sample index */
216     float         f0,          /* minBLEP out; noise filter state; wt chorus pos2 */
217                   f1,          /* noise filter state; wt chorus pos3 */
218                   f2;          /* noise filter state; wt chorus pos4 */
219 };
220 
221 struct vvcf
222 {
223     int   mode,
224           last_mode;
225     float delay1,
226           delay2,
227           delay3,
228           delay4,
229           delay5;
230 };
231 
232 struct vlfo
233 {
234     float pos;          /* LFO phase, 0 to 1 */
235     float freqmult;     /* random frequency multiplier, 0.5 to 1.5 */
236     float delay_length; /* onset delay length, in control ticks */
237     int   delay_count;  /* control ticks remaining in delay */
238 };
239 
240 enum dssp_eg_state {
241     DSSP_EG_FINISHED,
242     DSSP_EG_RUNNING,
243     DSSP_EG_SUSTAINING
244 };
245 
246 struct veg
247 {
248     int   shape[4];
249     int   sustain_segment;  /* 2 for ADSR or AAASR, 1 for AASRR, 0 for ASRRR, -1 for One-Shot */
250 
251     int   state;            /* enum dssp_eg_state (finished, running, sustaining) */
252     int   segment;          /* 0 to 3 */
253     int   count;            /* control ticks until end of this phase */
254     float time_scale;       /* amount to scale envelope times due to velocity time scaling and keyboard time scaling, multiplied by control rate */
255     float level_scale;      /* amount to scale envelope levels due to velocity level sensitivity */
256     float target;           /* segment target level */
257     float a, b, c, d;       /* segment shape function coefficients */
258 };
259 
260 struct vmod
261 {
262     float value;
263     float next_value;  /* value at next control tick */
264     float delta;
265 };
266 
267 /*
268  * y_voice_t
269  */
270 struct _y_voice_t
271 {
272     unsigned int  note_id;
273 
274     unsigned char status;
275     unsigned char key;
276     unsigned char velocity;
277     unsigned char rvelocity;   /* the note-off velocity */
278 
279     /* translated controller values */
280     float         pressure;
281 
282     /* persistent voice state */
283     float         prev_pitch,
284                   target_pitch,
285                   current_pitch;
286     struct vosc   osc1,
287                   osc2,
288                   osc3,
289                   osc4;
290     struct vvcf   vcf1,
291                   vcf2;
292     struct vlfo   /* glfo is in y_synth_t */
293                   vlfo,
294                   mlfo0,
295                   mlfo1,
296                   mlfo2,
297                   mlfo3;
298     struct veg    ego,
299                   eg1,
300                   eg2,
301                   eg3,
302                   eg4;
303     struct vmod   mod[Y_MODS_COUNT];
304 
305     /* buffers */
306     int           osc_index;                        /* shared index into osc_bus_{a,b} */
307     float         osc_sync[Y_CONTROL_PERIOD];       /* buffer for sync subsample offsets */
308     float         osc_bus_a[OSC_BUS_LENGTH],
309                   osc_bus_b[OSC_BUS_LENGTH];
310 };
311 
312 #define _PLAYING(voice)    ((voice)->status != Y_VOICE_OFF)
313 #define _ON(voice)         ((voice)->status == Y_VOICE_ON)
314 #define _SUSTAINED(voice)  ((voice)->status == Y_VOICE_SUSTAINED)
315 #define _RELEASED(voice)   ((voice)->status == Y_VOICE_RELEASED)
316 #define _AVAILABLE(voice)  ((voice)->status == Y_VOICE_OFF)
317 
318 extern float y_pitch[129];
319 
320 typedef struct { float value, delta; } float_value_delta;
321 extern float_value_delta y_step_dd_table[];
322 
323 extern float y_slope_dd_table[];
324 
325 /* in whysynth_voice.c */
326 extern float eg_shape_coeffs[][4];
327 y_voice_t *y_voice_new(y_synth_t *synth);
328 void       y_voice_note_on(y_synth_t *synth, y_voice_t *voice,
329                            unsigned char key, unsigned char velocity);
330 void       y_voice_note_off(y_synth_t *synth, y_voice_t *voice,
331                             unsigned char key, unsigned char rvelocity);
332 void       y_voice_release_note(y_synth_t *synth, y_voice_t *voice);
333 void       y_voice_set_ports(y_synth_t *synth, y_patch_t *patch);
334 void       y_voice_update_pressure_mod(y_synth_t *synth, y_voice_t *voice);
335 
336 /* in whysynth_voice_render.c */
337 extern float sine_wave[4 + SINETABLE_POINTS + 1];
338 extern float volume_cv_to_amplitude_table[257];
339 void y_init_tables(void);
340 void y_voice_setup_lfo(y_synth_t *synth, y_slfo_t *slfo, struct vlfo *vlfo,
341                        float phase, float randfreq,
342                        struct vmod *srcmods, struct vmod *destmod);
343 void y_voice_update_lfo(y_synth_t *synth, y_slfo_t *slfo, struct vlfo *vlfo,
344                         struct vmod *srcmods, struct vmod *destmod);
345 void y_voice_render(y_synth_t *synth, y_voice_t *voice,
346                     LADSPA_Data *out_left, LADSPA_Data *out_right,
347                     unsigned long sample_count, int do_control_update);
348 
349 /* in agran_oscillator.c */
350 void free_active_grains(y_synth_t *synth, y_voice_t *voice);
351 
352 /* ==== inline functions ==== */
353 
354 /*
355  * y_voice_off
356  *
357  * Purpose: Turns off a voice immediately, meaning that it is not processed
358  * anymore by the render loop.
359  */
360 static inline void
y_voice_off(y_synth_t * synth,y_voice_t * voice)361 y_voice_off(y_synth_t *synth, y_voice_t* voice)
362 {
363     voice->status = Y_VOICE_OFF;
364 
365     /* silence the oscillator buffers for the next use */
366     memset(voice->osc_bus_a, 0, OSC_BUS_LENGTH * sizeof(float));
367     memset(voice->osc_bus_b, 0, OSC_BUS_LENGTH * sizeof(float));
368 
369     /* free any still-active grains */
370     if (voice->osc1.grain_list || voice->osc2.grain_list ||
371         voice->osc3.grain_list || voice->osc4.grain_list)
372         free_active_grains(synth, voice);
373 
374     /* -FIX- decrement active voice count? */
375 }
376 
377 /*
378  * y_voice_start_voice
379  */
380 static inline void
y_voice_start_voice(y_voice_t * voice)381 y_voice_start_voice(y_voice_t *voice)
382 {
383     voice->status = Y_VOICE_ON;
384     /* -FIX- increment active voice count? */
385 }
386 
387 #endif /* _WHYSYNTH_VOICE_H */
388 
389