1 /* WhySynth DSSI software synthesizer plugin
2  *
3  * Copyright (C) 2004-2007 Sean Bolton and others.
4  *
5  * Portions of this file may have come from Peter Hanappe's
6  * Fluidsynth, copyright (C) 2003 Peter Hanappe and others.
7  * Portions of this file may have come from alsa-lib, copyright
8  * and licensed under the LGPL v2.1.
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 _DSSP_EVENT_H
27 #define _DSSP_EVENT_H
28 
29 #include <stdlib.h>
30 #include <pthread.h>
31 
32 #include <ladspa.h>
33 #include <dssi.h>
34 
35 #include "whysynth_types.h"
36 #include "whysynth.h"
37 #include "whysynth_voice.h"
38 
39 #define Y_MONO_MODE_OFF  0
40 #define Y_MONO_MODE_ON   1
41 #define Y_MONO_MODE_ONCE 2
42 #define Y_MONO_MODE_BOTH 3
43 
44 #define Y_GLIDE_MODE_LEGATO   0
45 #define Y_GLIDE_MODE_INITIAL  1
46 #define Y_GLIDE_MODE_ALWAYS   2
47 #define Y_GLIDE_MODE_LEFTOVER 3
48 #define Y_GLIDE_MODE_OFF      4
49 
50 /* -PORTS- */
51 struct _y_sosc_t
52 {
53     LADSPA_Data    *mode;
54     LADSPA_Data    *waveform;
55     LADSPA_Data    *pitch;
56     LADSPA_Data    *detune;
57     LADSPA_Data    *pitch_mod_src;
58     LADSPA_Data    *pitch_mod_amt;
59     LADSPA_Data    *mparam1;        /* sync / grain lz / mod freq ratio / ws phase offset / slave freq / noise cutoff freq */
60     LADSPA_Data    *mparam2;        /* pulsewidth / wave sel bias / grain spread / mod freq detune / mod amount bias */
61     LADSPA_Data    *mmod_src;       /* (pw | mod index | slave freq ) mod source / grain envelope */
62     LADSPA_Data    *mmod_amt;       /* (pw | mod index | slave freq ) mod amount / grain pitch distribution */
63     LADSPA_Data    *amp_mod_src;
64     LADSPA_Data    *amp_mod_amt;
65     LADSPA_Data    *level_a;
66     LADSPA_Data    *level_b;
67     y_sampleset_t  *sampleset;
68 };
69 
70 struct _y_svcf_t
71 {
72     LADSPA_Data    *mode;
73     LADSPA_Data    *source;
74     LADSPA_Data    *frequency;
75     LADSPA_Data    *freq_mod_src;
76     LADSPA_Data    *freq_mod_amt;
77     LADSPA_Data    *qres;
78     LADSPA_Data    *mparam;
79 };
80 
81 struct _y_slfo_t
82 {
83     LADSPA_Data    *frequency;
84     LADSPA_Data    *waveform;
85     LADSPA_Data    *delay;
86     LADSPA_Data    *amp_mod_src;
87     LADSPA_Data    *amp_mod_amt;
88 };
89 
90 struct _y_seg_t
91 {
92     LADSPA_Data    *mode;
93     LADSPA_Data    *shape[4];
94     LADSPA_Data    *time[4];
95     LADSPA_Data    *level[4];  /* level[3] always points to a 0.0f */
96     LADSPA_Data    *vel_level_sens;
97     LADSPA_Data    *vel_time_scale;
98     LADSPA_Data    *kbd_time_scale;
99     LADSPA_Data    *amp_mod_src;
100     LADSPA_Data    *amp_mod_amt;
101 };
102 
103 /*
104  * y_synth_t
105  */
106 struct _y_synth_t {
107     /* output */
108     LADSPA_Data    *output_left;
109     LADSPA_Data    *output_right;
110 
111     float           sample_rate;
112     float           deltat;            /* 1 / sample_rate */
113     float           control_rate;
114     unsigned long   control_remains;
115 
116     /* voice tracking and data */
117     unsigned int    note_id;           /* incremented for every new note, used for voice-stealing prioritization */
118     int             polyphony;         /* requested polyphony, must be <= Y_MAX_POLYPHONY */
119     int             voices;            /* current polyphony, either requested polyphony above or 1 while in monophonic mode */
120     int             monophonic;        /* true if operating in monophonic mode */
121     int             glide;             /* current glide mode */
122     float           last_noteon_pitch; /* glide start pitch for non-legato modes */
123     signed char     held_keys[8];      /* for monophonic key tracking, an array of note-ons, most recently received first */
124 
125     pthread_mutex_t voicelist_mutex;
126     int             voicelist_mutex_grab_failed;
127 
128     y_voice_t      *voice[Y_MAX_POLYPHONY];
129 
130     pthread_mutex_t patches_mutex;
131     unsigned int    patch_count;
132     unsigned int    patches_allocated;
133     y_patch_t      *patches;
134     int             pending_patch_change;
135     int             program_cancel;    /* if true, cancel any playing notes on recept of program change */
136     char           *project_dir;
137 
138     grain_t        *grains;                   /* array of all grains */
139     grain_t        *free_grain_list;          /* list of available grains */
140 
141     /* current non-LADSPA-port-mapped controller values */
142     unsigned char   key_pressure[128];
143     unsigned char   cc[128];                  /* controller values */
144     unsigned char   channel_pressure;
145     int             pitch_wheel;              /* range is -8192 - 8191 */
146 
147     /* translated controller values */
148     float           mod_wheel;                /* 0.0 to 1.0 -FIX- superfluous? */
149     float           pressure;                 /* 0.0 to 1.0 */
150     float           pitch_bend;               /* frequency multiplier, product of wheel setting and bend range, center = 1.0 */
151     float           cc_volume;                /* volume multiplier, 0.0 to 1.0 */
152 
153     /* global modulators */
154     struct vmod     mod[Y_GLOBAL_MODS_COUNT];
155     struct vlfo     glfo_vlfo;
156 
157     /* LADSPA ports / WhySynth patch parameters */
158     y_sosc_t        osc1,
159                     osc2,
160                     osc3,
161                     osc4;
162     y_svcf_t        vcf1,
163                     vcf2;
164     LADSPA_Data    *busa_level;
165     LADSPA_Data    *busa_pan;
166     LADSPA_Data    *busb_level;
167     LADSPA_Data    *busb_pan;
168     LADSPA_Data    *vcf1_level;
169     LADSPA_Data    *vcf1_pan;
170     LADSPA_Data    *vcf2_level;
171     LADSPA_Data    *vcf2_pan;
172     LADSPA_Data    *volume;
173     LADSPA_Data    *effect_mode;
174     LADSPA_Data    *effect_param1;
175     LADSPA_Data    *effect_param2;
176     LADSPA_Data    *effect_param3;
177     LADSPA_Data    *effect_param4;
178     LADSPA_Data    *effect_param5;
179     LADSPA_Data    *effect_param6;
180     LADSPA_Data    *effect_mix;
181     LADSPA_Data    *glide_time;
182     LADSPA_Data    *bend_range;
183     y_slfo_t        glfo,
184                     vlfo,
185                     mlfo;
186     LADSPA_Data    *mlfo_phase_spread;
187     LADSPA_Data    *mlfo_random_freq;
188     y_seg_t         ego,
189                     eg1,
190                     eg2,
191                     eg3,
192                     eg4;
193     LADSPA_Data    *modmix_bias;
194     LADSPA_Data    *modmix_mod1_src;
195     LADSPA_Data    *modmix_mod1_amt;
196     LADSPA_Data    *modmix_mod2_src;
197     LADSPA_Data    *modmix_mod2_amt;
198     LADSPA_Data    *tuning;
199 
200     /* reusable pre-mixdown voice buffers */
201     float           vcf1_out[Y_CONTROL_PERIOD],
202                     vcf2_out[Y_CONTROL_PERIOD];
203 
204     /* effects */
205     LADSPA_Data     voice_bus_l[Y_CONTROL_PERIOD],  /* pre-effect voice bus */
206                     voice_bus_r[Y_CONTROL_PERIOD];
207     int             last_effect_mode;
208     float           dc_block_r,
209                     dc_block_l_xnm1,
210                     dc_block_l_ynm1,
211                     dc_block_r_xnm1,
212                     dc_block_r_ynm1;
213     char           *effect_buffer;
214     size_t          effect_buffer_allocation;
215     size_t          effect_buffer_highwater;
216     size_t          effect_buffer_silence_count;
217 };
218 
219 /*
220  * y_global_t
221  */
222 struct _y_global_t {
223     int                    initialized;
224     int                    instance_count;
225     unsigned long          sample_rate;
226 
227     grain_envelope_data_t *grain_envelope;    /* array of grain envelopes */
228 
229     pthread_mutex_t        sampleset_mutex;
230     int                    sampleset_pipe_fd[2];
231     int                    worker_thread_started;
232     volatile int           worker_thread_done;
233     pthread_t              worker_thread;
234     int                    samplesets_allocated;
235     y_sampleset_t         *active_sampleset_list;
236     y_sampleset_t         *free_sampleset_list;
237     int                    samples_allocated;
238     y_sample_t            *active_sample_list;
239     y_sample_t            *free_sample_list;
240     int                    padsynth_table_size;
241     float                 *padsynth_inbuf;
242     float                 *padsynth_outfreqs;
243     float                 *padsynth_outsamples;
244     void                  *padsynth_fft_plan;
245     void                  *padsynth_ifft_plan;
246 };
247 
248 extern y_global_t global;
249 
250 void  y_synth_all_voices_off(y_synth_t *synth);
251 void  y_synth_note_off(y_synth_t *synth, unsigned char key,
252                             unsigned char rvelocity);
253 void  y_synth_all_notes_off(y_synth_t *synth);
254 void  y_synth_note_on(y_synth_t *synth, unsigned char key,
255                            unsigned char velocity);
256 void  y_synth_key_pressure(y_synth_t *synth, unsigned char key,
257                                 unsigned char pressure);
258 void  y_synth_damp_voices(y_synth_t *synth);
259 void  y_synth_update_wheel_mod(y_synth_t *synth);
260 void  y_synth_control_change(y_synth_t *synth, unsigned int param,
261                                   signed int value);
262 void  y_synth_channel_pressure(y_synth_t *synth, signed int pressure);
263 void  y_synth_pitch_bend(y_synth_t *synth, signed int value);
264 void  y_synth_init_controls(y_synth_t *synth);
265 void  y_synth_select_patch(y_synth_t *synth, unsigned long patch);
266 int   y_synth_set_program_descriptor(y_synth_t *synth,
267                                      DSSI_Program_Descriptor *pd,
268                                      unsigned long patch);
269 char *y_synth_handle_load(y_synth_t *synth, const char *value);
270 char *y_synth_handle_polyphony(y_synth_t *synth, const char *value);
271 char *y_synth_handle_monophonic(y_synth_t *synth, const char *value);
272 char *y_synth_handle_glide(y_synth_t *synth, const char *value);
273 char *y_synth_handle_program_cancel(y_synth_t *synth, const char *value);
274 char *y_synth_handle_project_dir(y_synth_t *synth, const char *value);
275 void  y_synth_render_voices(y_synth_t *synth, LADSPA_Data *out_left,
276                                  LADSPA_Data *out_right, unsigned long sample_count,
277                                  int do_control_update);
278 
279 /* these come right out of alsa/asoundef.h */
280 #define MIDI_CTL_MSB_MODWHEEL           0x01    /**< Modulation */
281 #define MIDI_CTL_MSB_PORTAMENTO_TIME    0x05    /**< Portamento time */
282 #define MIDI_CTL_MSB_MAIN_VOLUME        0x07    /**< Main volume */
283 #define MIDI_CTL_MSB_BALANCE            0x08    /**< Balance */
284 #define MIDI_CTL_LSB_MODWHEEL           0x21    /**< Modulation */
285 #define MIDI_CTL_LSB_PORTAMENTO_TIME    0x25    /**< Portamento time */
286 #define MIDI_CTL_LSB_MAIN_VOLUME        0x27    /**< Main volume */
287 #define MIDI_CTL_LSB_BALANCE            0x28    /**< Balance */
288 #define MIDI_CTL_SUSTAIN                0x40    /**< Sustain pedal */
289 #define MIDI_CTL_ALL_SOUNDS_OFF         0x78    /**< All sounds off */
290 #define MIDI_CTL_RESET_CONTROLLERS      0x79    /**< Reset Controllers */
291 #define MIDI_CTL_ALL_NOTES_OFF          0x7b    /**< All notes off */
292 
293 #define Y_SYNTH_SUSTAINED(_s)  ((_s)->cc[MIDI_CTL_SUSTAIN] >= 64)
294 
295 #endif /* _DSSP_EVENT_H */
296 
297