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
34 namespace calf_plugins
{
36 #define MONOSYNTH_WAVE_BITS 12
38 struct modulation_entry
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
50 enum { mod_matrix_slots
= 10 };
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
;
61 float buffer
[step_size
], buffer2
[step_size
];
63 dsp::onepole
<float> phaseshifter
;
64 dsp::biquad_d1_lerp
<float> filter
;
65 dsp::biquad_d1_lerp
<float> filter2
;
66 /// Waveform number - OSC1
68 /// Waveform number - OSC2
70 /// Last used waveform number - OSC1
72 /// Last used waveform number - OSC2
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
;
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
97 /// Current calculated mod matrix outputs
98 float moddest
[moddest_count
];
100 monosynth_audio_module();
101 static void precalculate_waves(progress_report_iface
*reporter
);
102 void set_sample_rate(uint32_t sr
);
103 void delayed_note_on();
104 /// Handle MIDI Note On message (does not immediately trigger a note, as it must start on
105 /// boundary of step_size samples).
106 void note_on(int note
, int vel
);
107 /// Handle MIDI Note Off message
108 void note_off(int note
, int vel
);
109 /// Handle MIDI Channel Pressure
110 void channel_pressure(int value
);
111 /// Handle pitch bend message.
112 inline void pitch_bend(int value
)
114 inertia_pitchbend
.set_inertia(pow(2.0, (value
* *params
[par_pwhlrange
]) / (1200.0 * 8192.0)));
116 /// Update oscillator frequency based on base frequency, detune amount, pitch bend scaling factor and sample rate.
117 inline void set_frequency()
119 float detune_scaled
= (detune
- 1); // * log(freq / 440);
120 if (*params
[par_scaledetune
] > 0)
121 detune_scaled
*= pow(20.0 / freq
, *params
[par_scaledetune
]);
122 float p1
= 1, p2
= 1;
123 if (moddest
[moddest_o1detune
] != 0)
124 p1
= pow(2.0, moddest
[moddest_o1detune
] * (1.0 / 1200.0));
125 if (moddest
[moddest_o2detune
] != 0)
126 p2
= pow(2.0, moddest
[moddest_o2detune
] * (1.0 / 1200.0));
127 osc1
.set_freq(freq
* (1 - detune_scaled
) * p1
* inertia_pitchbend
.get_last() * lfo_bend
, srate
);
128 osc2
.set_freq(freq
* (1 + detune_scaled
) * p2
* inertia_pitchbend
.get_last() * lfo_bend
* xpose
, srate
);
130 /// Handle control change messages.
131 void control_change(int controller
, int value
);
132 /// Update variables from control ports.
133 void params_changed() {
135 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
);
136 filter_type
= dsp::fastf2i_drm(*params
[par_filtertype
]);
137 decay_factor
= odcr
* 1000.0 / *params
[par_decay
];
138 separation
= pow(2.0, *params
[par_cutoffsep
] / 1200.0);
139 wave1
= dsp::clip(dsp::fastf2i_drm(*params
[par_wave1
]), 0, (int)wave_count
- 1);
140 wave2
= dsp::clip(dsp::fastf2i_drm(*params
[par_wave2
]), 0, (int)wave_count
- 1);
141 detune
= pow(2.0, *params
[par_detune
] / 1200.0);
142 xpose
= pow(2.0, *params
[par_osc2xpose
] / 12.0);
143 xfade
= *params
[par_oscmix
];
144 legato
= dsp::fastf2i_drm(*params
[par_legato
]);
145 master
.set_inertia(*params
[par_master
]);
147 if (wave1
!= prev_wave1
|| wave2
!= prev_wave2
)
152 void post_instantiate()
154 precalculate_waves(progress_report
);
156 /// Set waveform addresses for oscillators
157 void lookup_waveforms();
159 void calculate_buffer_oscs(float lfo
);
160 /// Run two filters in series to produce mono output samples.
161 void calculate_buffer_ser();
162 /// Run one filter to produce mono output samples.
163 void calculate_buffer_single();
164 /// Run two filters (one per channel) to produce stereo output samples.
165 void calculate_buffer_stereo();
166 /// Retrieve filter graph (which is 'live' so it cannot be generated by get_static_graph), or fall back to get_static_graph.
167 bool get_graph(int index
, int subindex
, float *data
, int points
, cairo_iface
*context
);
168 /// @retval true if the filter 1 is to be used for the left channel and filter 2 for the right channel
169 /// @retval false if filters are to be connected in series and sent (mono) to both channels
170 inline bool is_stereo_filter() const
172 return filter_type
== flt_2lp12
|| filter_type
== flt_2bp6
;
174 /// No CV inputs for now
175 bool is_cv(int param_no
) { return false; }
176 /// Practically all the stuff here is noisy
177 bool is_noisy(int param_no
) { return param_no
!= par_cutoff
; }
178 /// Calculate control signals and produce step_size samples of output.
179 void calculate_step();
180 /// Process modulation matrix
181 void calculate_modmatrix(float *modsrc
);
182 /// Main processing function
183 uint32_t process(uint32_t offset
, uint32_t nsamples
, uint32_t inputs_mask
, uint32_t outputs_mask
);
185 virtual const table_column_info
*get_table_columns(int param
);
186 virtual uint32_t get_table_rows(int param
);
187 virtual std::string
get_cell(int param
, int row
, int column
);
188 virtual void set_cell(int param
, int row
, int column
, const std::string
&src
, std::string
&error
);
191 struct organ_audio_module
: public audio_module
<organ_metadata
>, public dsp::drawbar_organ
, public line_graph_iface
194 using drawbar_organ::note_on
;
195 using drawbar_organ::note_off
;
196 using drawbar_organ::control_change
;
197 enum { param_count
= drawbar_organ::param_count
};
198 float *ins
[in_count
];
199 float *outs
[out_count
];
200 float *params
[param_count
];
201 dsp::organ_parameters par_values
;
204 /// Value for configure variable map_curve
205 std::string var_map_curve
;
208 : drawbar_organ(&par_values
)
210 var_map_curve
= "2\n0 1\n1 1\n"; // XXXKF hacky bugfix
213 void post_instantiate()
215 dsp::organ_voice_base::precalculate_waves(progress_report
);
218 void set_sample_rate(uint32_t sr
) {
221 void params_changed() {
222 for (int i
= 0; i
< param_count
- var_count
; i
++)
223 ((float *)&par_values
)[i
] = *params
[i
];
225 unsigned int old_poly
= polyphony_limit
;
226 polyphony_limit
= dsp::clip(dsp::fastf2i_drm(*params
[par_polyphony
]), 1, 32);
227 if (polyphony_limit
< old_poly
)
232 inline void pitch_bend(int amt
)
234 drawbar_organ::pitch_bend(amt
);
241 uint32_t process(uint32_t offset
, uint32_t nsamples
, uint32_t inputs_mask
, uint32_t outputs_mask
) {
242 float *o
[2] = { outs
[0] + offset
, outs
[1] + offset
};
245 control_change(120, 0); // stop all sounds
246 control_change(121, 0); // reset all controllers
249 render_separate(o
, nsamples
);
252 /// No CV inputs for now
253 bool is_cv(int param_no
) { return false; }
254 /// Practically all the stuff here is noisy
255 bool is_noisy(int param_no
) { return true; }
256 void execute(int cmd_no
);
257 bool get_graph(int index
, int subindex
, float *data
, int points
, cairo_iface
*context
);
259 char *configure(const char *key
, const char *value
);
260 void send_configures(send_configure_iface
*);
261 uint32_t message_run(const void *valid_inputs
, void *output_ports
) {
262 // silence a default printf (which is kind of a warning about unhandled message_run)
269 #if ENABLE_EXPERIMENTAL
271 #include "wavetable.h"