+ Monosynth: Osc Mix modulation destination, use smoothing for xfade value, destinati...
[calf.git] / src / calf / modules_synths.h
blob0918614c214a3e4154b2ba7fbd96027c9a5aff8c
1 /* Calf DSP Library
2 * Audio modules - synthesizers
4 * Copyright (C) 2001-2007 Krzysztof Foltman
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02111-1307, USA.
21 #ifndef __CALF_MODULES_SYNTHS_H
22 #define __CALF_MODULES_SYNTHS_H
24 #include <assert.h>
25 #include "biquad.h"
26 #include "onepole.h"
27 #include "audio_fx.h"
28 #include "inertia.h"
29 #include "osc.h"
30 #include "synth.h"
31 #include "envelope.h"
32 #include "organ.h"
34 namespace calf_plugins {
36 #define MONOSYNTH_WAVE_BITS 12
38 struct modulation_entry
40 int src1, src2;
41 float amount;
42 int dest;
45 /// Monosynth-in-making. Parameters may change at any point, so don't make songs with it!
46 /// It lacks inertia for parameters, even for those that really need it.
47 class monosynth_audio_module: public audio_module<monosynth_metadata>, public line_graph_iface, public table_edit_iface
49 public:
50 enum { mod_matrix_slots = 10 };
51 float *ins[in_count];
52 float *outs[out_count];
53 float *params[param_count];
54 uint32_t srate, crate;
55 static dsp::waveform_family<MONOSYNTH_WAVE_BITS> *waves;
56 dsp::waveform_oscillator<MONOSYNTH_WAVE_BITS> osc1, osc2;
57 dsp::triangle_lfo lfo;
58 bool running, stopping, gate, force_fadeout;
59 int last_key;
61 float buffer[step_size], buffer2[step_size];
62 uint32_t output_pos;
63 dsp::onepole<float> phaseshifter;
64 dsp::biquad_d1_lerp<float> filter;
65 dsp::biquad_d1_lerp<float> filter2;
66 /// Waveform number - OSC1
67 int wave1;
68 /// Waveform number - OSC2
69 int wave2;
70 /// Last used waveform number - OSC1
71 int prev_wave1;
72 /// Last used waveform number - OSC2
73 int prev_wave2;
74 int filter_type, last_filter_type;
75 float freq, start_freq, target_freq, cutoff, decay_factor, fgain, fgain_delta, separation;
76 float detune, xpose, xfade, ampctl, fltctl, queue_vel;
77 float odcr, porta_time, lfo_bend, lfo_clock, last_lfov, modwheel_value;
78 /// Last value of phase shift for pulse width emulation for OSC1
79 int32_t last_pwshift1;
80 /// Last value of phase shift for pulse width emulation for OSC2
81 int32_t last_pwshift2;
82 int queue_note_on, stop_count, modwheel_value_int;
83 int legato;
84 dsp::adsr envelope;
85 dsp::keystack stack;
86 dsp::gain_smoothing master;
87 /// Smoothed cutoff value
88 dsp::inertia<dsp::exponential_ramp> inertia_cutoff;
89 /// Smoothed pitch bend value
90 dsp::inertia<dsp::exponential_ramp> inertia_pitchbend;
91 /// Smoothed channel pressure value
92 dsp::inertia<dsp::linear_ramp> inertia_pressure;
93 /// Rows of the modulation matrix
94 modulation_entry mod_matrix[mod_matrix_slots];
95 /// Currently used velocity
96 float velocity;
97 /// Last value of oscillator mix ratio
98 float last_xfade;
99 /// Current calculated mod matrix outputs
100 float moddest[moddest_count];
102 monosynth_audio_module();
103 static void precalculate_waves(progress_report_iface *reporter);
104 void set_sample_rate(uint32_t sr);
105 void delayed_note_on();
106 /// Handle MIDI Note On message (does not immediately trigger a note, as it must start on
107 /// boundary of step_size samples).
108 void note_on(int note, int vel);
109 /// Handle MIDI Note Off message
110 void note_off(int note, int vel);
111 /// Handle MIDI Channel Pressure
112 void channel_pressure(int value);
113 /// Handle pitch bend message.
114 inline void pitch_bend(int value)
116 inertia_pitchbend.set_inertia(pow(2.0, (value * *params[par_pwhlrange]) / (1200.0 * 8192.0)));
118 /// Update oscillator frequency based on base frequency, detune amount, pitch bend scaling factor and sample rate.
119 inline void set_frequency()
121 float detune_scaled = (detune - 1); // * log(freq / 440);
122 if (*params[par_scaledetune] > 0)
123 detune_scaled *= pow(20.0 / freq, *params[par_scaledetune]);
124 float p1 = 1, p2 = 1;
125 if (moddest[moddest_o1detune] != 0)
126 p1 = pow(2.0, moddest[moddest_o1detune] * (1.0 / 1200.0));
127 if (moddest[moddest_o2detune] != 0)
128 p2 = pow(2.0, moddest[moddest_o2detune] * (1.0 / 1200.0));
129 osc1.set_freq(freq * (1 - detune_scaled) * p1 * inertia_pitchbend.get_last() * lfo_bend, srate);
130 osc2.set_freq(freq * (1 + detune_scaled) * p2 * inertia_pitchbend.get_last() * lfo_bend * xpose, srate);
132 /// Handle control change messages.
133 void control_change(int controller, int value);
134 /// Update variables from control ports.
135 void params_changed() {
136 float sf = 0.001f;
137 envelope.set(*params[par_attack] * sf, *params[par_decay] * sf, std::min(0.999f, *params[par_sustain]), *params[par_release] * sf, srate / step_size, *params[par_fade] * sf);
138 filter_type = dsp::fastf2i_drm(*params[par_filtertype]);
139 decay_factor = odcr * 1000.0 / *params[par_decay];
140 separation = pow(2.0, *params[par_cutoffsep] / 1200.0);
141 wave1 = dsp::clip(dsp::fastf2i_drm(*params[par_wave1]), 0, (int)wave_count - 1);
142 wave2 = dsp::clip(dsp::fastf2i_drm(*params[par_wave2]), 0, (int)wave_count - 1);
143 detune = pow(2.0, *params[par_detune] / 1200.0);
144 xpose = pow(2.0, *params[par_osc2xpose] / 12.0);
145 xfade = *params[par_oscmix];
146 legato = dsp::fastf2i_drm(*params[par_legato]);
147 master.set_inertia(*params[par_master]);
148 set_frequency();
149 if (wave1 != prev_wave1 || wave2 != prev_wave2)
150 lookup_waveforms();
152 void activate();
153 void deactivate();
154 void post_instantiate()
156 precalculate_waves(progress_report);
158 /// Set waveform addresses for oscillators
159 void lookup_waveforms();
160 /// Run oscillators
161 void calculate_buffer_oscs(float lfo);
162 /// Run two filters in series to produce mono output samples.
163 void calculate_buffer_ser();
164 /// Run one filter to produce mono output samples.
165 void calculate_buffer_single();
166 /// Run two filters (one per channel) to produce stereo output samples.
167 void calculate_buffer_stereo();
168 /// Retrieve filter graph (which is 'live' so it cannot be generated by get_static_graph), or fall back to get_static_graph.
169 bool get_graph(int index, int subindex, float *data, int points, cairo_iface *context);
170 /// @retval true if the filter 1 is to be used for the left channel and filter 2 for the right channel
171 /// @retval false if filters are to be connected in series and sent (mono) to both channels
172 inline bool is_stereo_filter() const
174 return filter_type == flt_2lp12 || filter_type == flt_2bp6;
176 /// No CV inputs for now
177 bool is_cv(int param_no) { return false; }
178 /// Practically all the stuff here is noisy
179 bool is_noisy(int param_no) { return param_no != par_cutoff; }
180 /// Calculate control signals and produce step_size samples of output.
181 void calculate_step();
182 /// Process modulation matrix
183 void calculate_modmatrix(float *modsrc);
184 /// Main processing function
185 uint32_t process(uint32_t offset, uint32_t nsamples, uint32_t inputs_mask, uint32_t outputs_mask);
187 virtual const table_column_info *get_table_columns(int param);
188 virtual uint32_t get_table_rows(int param);
189 virtual std::string get_cell(int param, int row, int column);
190 virtual void set_cell(int param, int row, int column, const std::string &src, std::string &error);
193 struct organ_audio_module: public audio_module<organ_metadata>, public dsp::drawbar_organ, public line_graph_iface
195 public:
196 using drawbar_organ::note_on;
197 using drawbar_organ::note_off;
198 using drawbar_organ::control_change;
199 enum { param_count = drawbar_organ::param_count};
200 float *ins[in_count];
201 float *outs[out_count];
202 float *params[param_count];
203 dsp::organ_parameters par_values;
204 uint32_t srate;
205 bool panic_flag;
206 /// Value for configure variable map_curve
207 std::string var_map_curve;
209 organ_audio_module()
210 : drawbar_organ(&par_values)
212 var_map_curve = "2\n0 1\n1 1\n"; // XXXKF hacky bugfix
215 void post_instantiate()
217 dsp::organ_voice_base::precalculate_waves(progress_report);
220 void set_sample_rate(uint32_t sr) {
221 srate = sr;
223 void params_changed() {
224 for (int i = 0; i < param_count - var_count; i++)
225 ((float *)&par_values)[i] = *params[i];
227 unsigned int old_poly = polyphony_limit;
228 polyphony_limit = dsp::clip(dsp::fastf2i_drm(*params[par_polyphony]), 1, 32);
229 if (polyphony_limit < old_poly)
230 trim_voices();
232 update_params();
234 inline void pitch_bend(int amt)
236 drawbar_organ::pitch_bend(amt);
238 void activate() {
239 setup(srate);
240 panic_flag = false;
242 void deactivate();
243 uint32_t process(uint32_t offset, uint32_t nsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
244 float *o[2] = { outs[0] + offset, outs[1] + offset };
245 if (panic_flag)
247 control_change(120, 0); // stop all sounds
248 control_change(121, 0); // reset all controllers
249 panic_flag = false;
251 render_separate(o, nsamples);
252 return 3;
254 /// No CV inputs for now
255 bool is_cv(int param_no) { return false; }
256 /// Practically all the stuff here is noisy
257 bool is_noisy(int param_no) { return true; }
258 void execute(int cmd_no);
259 bool get_graph(int index, int subindex, float *data, int points, cairo_iface *context);
261 char *configure(const char *key, const char *value);
262 void send_configures(send_configure_iface *);
263 uint32_t message_run(const void *valid_inputs, void *output_ports) {
264 // silence a default printf (which is kind of a warning about unhandled message_run)
265 return 0;
271 #if ENABLE_EXPERIMENTAL
273 #include "wavetable.h"
275 #endif
277 #endif