+ DSSI: marshal cairo_iface as well
[calf.git] / src / calf / modules_synths.h
blob76fdd3b6bb1bf7b9808407772189badbb2087d6f
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., 59 Temple Place, Suite 330,
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 /// Monosynth-in-making. Parameters may change at any point, so don't make songs with it!
39 /// It lacks inertia for parameters, even for those that really need it.
40 class monosynth_audio_module: public audio_module<monosynth_metadata>, public line_graph_iface
42 public:
43 float *ins[in_count];
44 float *outs[out_count];
45 float *params[param_count];
46 uint32_t srate, crate;
47 static dsp::waveform_family<MONOSYNTH_WAVE_BITS> *waves;
48 dsp::waveform_oscillator<MONOSYNTH_WAVE_BITS> osc1, osc2;
49 bool running, stopping, gate;
50 int last_key;
52 float buffer[step_size], buffer2[step_size];
53 uint32_t output_pos;
54 dsp::onepole<float> phaseshifter;
55 dsp::biquad_d1<float> filter;
56 dsp::biquad_d1<float> filter2;
57 int wave1, wave2, filter_type, last_filter_type;
58 float freq, start_freq, target_freq, cutoff, decay_factor, fgain, fgain_delta, separation;
59 float detune, xpose, xfade, pitchbend, ampctl, fltctl, queue_vel;
60 float odcr, porta_time;
61 int queue_note_on, stop_count;
62 int legato;
63 dsp::adsr envelope;
64 dsp::keystack stack;
65 dsp::gain_smoothing master;
67 static void generate_waves();
68 void set_sample_rate(uint32_t sr);
69 void delayed_note_on();
70 /// Handle MIDI Note On message (does not immediately trigger a note, as it must start on
71 /// boundary of step_size samples).
72 void note_on(int note, int vel)
74 queue_note_on = note;
75 last_key = note;
76 queue_vel = vel / 127.f;
77 stack.push(note);
79 /// Handle MIDI Note Off message
80 void note_off(int note, int vel)
82 stack.pop(note);
83 // If releasing the currently played note, try to get another one from note stack.
84 if (note == last_key) {
85 if (stack.count())
87 last_key = note = stack.nth(stack.count() - 1);
88 start_freq = freq;
89 target_freq = freq = dsp::note_to_hz(note);
90 set_frequency();
91 if (!(legato & 1)) {
92 envelope.note_on();
93 stopping = false;
94 running = true;
96 return;
98 gate = false;
99 envelope.note_off();
102 /// Handle pitch bend message.
103 inline void pitch_bend(int value)
105 pitchbend = pow(2.0, value / 8192.0);
107 /// Update oscillator frequency based on base frequency, detune amount, pitch bend scaling factor and sample rate.
108 inline void set_frequency()
110 osc1.set_freq(freq * (2 - detune) * pitchbend, srate);
111 osc2.set_freq(freq * (detune) * pitchbend * xpose, srate);
113 /// Handle control change messages.
114 void control_change(int controller, int value);
115 /// Update variables from control ports.
116 void params_changed() {
117 float sf = 0.001f;
118 envelope.set(*params[par_attack] * sf, *params[par_decay] * sf, std::min(0.999f, *params[par_sustain]), *params[par_release] * sf, srate / step_size);
119 filter_type = dsp::fastf2i_drm(*params[par_filtertype]);
120 decay_factor = odcr * 1000.0 / *params[par_decay];
121 separation = pow(2.0, *params[par_cutoffsep] / 1200.0);
122 wave1 = dsp::clip(dsp::fastf2i_drm(*params[par_wave1]), 0, (int)wave_count - 1);
123 wave2 = dsp::clip(dsp::fastf2i_drm(*params[par_wave2]), 0, (int)wave_count - 1);
124 detune = pow(2.0, *params[par_detune] / 1200.0);
125 xpose = pow(2.0, *params[par_osc2xpose] / 12.0);
126 xfade = *params[par_oscmix];
127 legato = dsp::fastf2i_drm(*params[par_legato]);
128 master.set_inertia(*params[par_master]);
129 set_frequency();
131 void activate();
132 void deactivate() {}
133 /// Run oscillators and two filters in series to produce mono output samples.
134 void calculate_buffer_ser();
135 /// Run oscillators and just one filter to produce mono output samples.
136 void calculate_buffer_single();
137 /// Run oscillators and two filters (one per channel) to produce stereo output samples.
138 void calculate_buffer_stereo();
139 /// Retrieve filter graph (which is 'live' so it cannot be generated by get_static_graph), or fall back to get_static_graph.
140 bool get_graph(int index, int subindex, float *data, int points, cairo_iface *context);
141 /// Retrieve waveform graph (which does not need information about synth state)
142 bool get_static_graph(int index, int subindex, float value, float *data, int points, cairo_iface *context);
143 /// @retval true if the filter 1 is to be used for the left channel and filter 2 for the right channel
144 /// @retval false if filters are to be connected in series and sent (mono) to both channels
145 inline bool is_stereo_filter() const
147 return filter_type == flt_2lp12 || filter_type == flt_2bp6;
149 /// No CV inputs for now
150 bool is_cv(int param_no) { return false; }
151 /// Practically all the stuff here is noisy
152 bool is_noisy(int param_no) { return true; }
153 /// Calculate control signals and produce step_size samples of output.
154 void calculate_step();
155 /// Main processing function
156 uint32_t process(uint32_t offset, uint32_t nsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
157 if (!running && queue_note_on == -1)
158 return 0;
159 uint32_t op = offset;
160 uint32_t op_end = offset + nsamples;
161 while(op < op_end) {
162 if (output_pos == 0) {
163 if (running || queue_note_on != -1)
164 calculate_step();
165 else
166 dsp::zero(buffer, step_size);
168 if(op < op_end) {
169 uint32_t ip = output_pos;
170 uint32_t len = std::min(step_size - output_pos, op_end - op);
171 if (is_stereo_filter())
172 for(uint32_t i = 0 ; i < len; i++) {
173 float vol = master.get();
174 outs[0][op + i] = buffer[ip + i] * vol,
175 outs[1][op + i] = buffer2[ip + i] * vol;
177 else
178 for(uint32_t i = 0 ; i < len; i++)
179 outs[0][op + i] = outs[1][op + i] = buffer[ip + i] * master.get();
180 op += len;
181 output_pos += len;
182 if (output_pos == step_size)
183 output_pos = 0;
187 return 3;
191 struct organ_audio_module: public audio_module<organ_metadata>, public dsp::drawbar_organ, public line_graph_iface
193 public:
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;
202 uint32_t srate;
203 bool panic_flag;
204 /// Value for configure variable map_curve
205 std::string var_map_curve;
207 organ_audio_module()
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) {
219 srate = 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)
228 trim_voices();
230 update_params();
232 inline void pitch_bend(int amt)
234 drawbar_organ::pitch_bend(amt);
236 void activate() {
237 setup(srate);
238 panic_flag = false;
240 void deactivate() {
242 uint32_t process(uint32_t offset, uint32_t nsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
243 float *o[2] = { outs[0] + offset, outs[1] + offset };
244 if (panic_flag)
246 control_change(120, 0); // stop all sounds
247 control_change(121, 0); // reset all controllers
248 panic_flag = false;
250 render_separate(o, nsamples);
251 return 3;
253 /// No CV inputs for now
254 bool is_cv(int param_no) { return false; }
255 /// Practically all the stuff here is noisy
256 bool is_noisy(int param_no) { return true; }
257 void execute(int cmd_no);
258 bool get_graph(int index, int subindex, float *data, int points, cairo_iface *context);
260 char *configure(const char *key, const char *value);
261 void send_configures(send_configure_iface *);
262 uint32_t message_run(const void *valid_inputs, void *output_ports) {
263 // silence a default printf (which is kind of a warning about unhandled message_run)
264 return 0;
270 #endif