LineGraph: z axis for lo and hi pass handles
[calf.git] / src / calf / modules_synths.h
blob2cb1b05a6d930ec9513e7b7ecd949059356e9b6e
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 "biquad.h"
25 #include "onepole.h"
26 #include "audio_fx.h"
27 #include "inertia.h"
28 #include "osc.h"
29 #include "synth.h"
30 #include "envelope.h"
31 #include "modmatrix.h"
32 #include "metadata.h"
33 #include "giface.h"
35 namespace calf_plugins {
37 #define MONOSYNTH_WAVE_BITS 12
39 /// Monosynth-in-making. Parameters may change at any point, so don't make songs with it!
40 /// It lacks inertia for parameters, even for those that really need it.
41 class monosynth_audio_module: public audio_module<monosynth_metadata>, public line_graph_iface, public mod_matrix_impl
43 public:
44 uint32_t srate, crate;
45 static dsp::waveform_family<MONOSYNTH_WAVE_BITS> *waves;
46 dsp::waveform_oscillator<MONOSYNTH_WAVE_BITS> osc1, osc2, detosc;
47 dsp::triangle_lfo lfo1, lfo2;
48 dsp::simple_oscillator unison_osc;
49 dsp::biquad_d1_lerp filter, filter2;
50 /// The step code is producing non-zero values
51 bool running;
52 /// This is the last non-zero buffer (set on calculate_step after fadeout is complete, the next calculate_step will zero running)
53 bool stopping;
54 /// A key is kept pressed
55 bool gate;
56 /// All notes off fadeout
57 bool force_fadeout;
58 /// Last triggered note
59 int last_key;
61 /// Output buffers, used to ensure updates are done every step_size regardless of process buffer size
62 float buffer[step_size], buffer2[step_size];
63 /// Read position within the buffers, on each '0' the buffers are being filled with new data by calculate_step
64 uint32_t output_pos;
65 /// Waveform number - OSC1
66 int wave1;
67 /// Waveform number - OSC2
68 int wave2;
69 /// Last used waveform number - OSC1
70 int prev_wave1;
71 /// Last used waveform number - OSC2
72 int prev_wave2;
73 /// Filter type
74 int filter_type;
75 /// Filter type on the last calculate_step
76 int last_filter_type;
77 float freq, start_freq, target_freq, cutoff, fgain, fgain_delta, separation;
78 float detune, xpose1, xpose2, xfade, ampctl, fltctl;
79 float odcr, porta_time, lfo_bend;
80 /// Modulation wheel position (0.f-1.f)
81 float modwheel_value;
82 /// Delay counter for LFOs
83 float lfo_clock;
84 /// Last value of phase shift for pulse width emulation for OSC1
85 int32_t last_pwshift1;
86 /// Last value of phase shift for pulse width emulation for OSC2
87 int32_t last_pwshift2;
88 /// Last value of stretch for osc sync emulation for OSC1
89 int32_t last_stretch1;
90 /// Next note to play on the next calculate_step
91 int queue_note_on;
92 /// Whether the queued note has been already released
93 bool queue_note_on_and_off;
94 /// Velocity of the next note to play
95 float queue_vel;
96 /// Integer value for modwheel (0-16383, read from CC1 - MSBs and CC33 - LSBs)
97 int modwheel_value_int;
98 /// Legato mode (bitmask)
99 int legato;
100 /// Envelope Generators
101 dsp::adsr envelope1, envelope2;
102 dsp::keystack stack;
103 /// Smoothing for master volume
104 dsp::gain_smoothing master;
105 /// Fadeout for buffer 1
106 dsp::fadeout fadeout;
107 /// Fadeout for buffer 2
108 dsp::fadeout fadeout2;
109 /// Smoothed cutoff value
110 dsp::inertia<dsp::exponential_ramp> inertia_cutoff;
111 /// Smoothed pitch bend value
112 dsp::inertia<dsp::exponential_ramp> inertia_pitchbend;
113 /// Smoothed channel pressure value
114 dsp::inertia<dsp::linear_ramp> inertia_pressure;
115 /// Rows of the modulation matrix
116 dsp::modulation_entry mod_matrix_data[mod_matrix_slots];
117 /// Currently used velocity
118 float velocity;
119 /// Last value of oscillator mix ratio
120 float last_xfade;
121 /// Last value of unison amount
122 float last_unison;
123 /// Current calculated mod matrix outputs
124 float moddest[moddest_count];
126 monosynth_audio_module();
127 void set_sample_rate(uint32_t sr);
128 void delayed_note_on();
129 /// Release a note (physically), called from note-off handler or when note-off has been scheduled after note-on (very short queued note)
130 void end_note();
131 /// Handle MIDI Note On message (does not immediately trigger a note, as it must start on
132 /// boundary of step_size samples).
133 void note_on(int channel, int note, int vel);
134 /// Handle MIDI Note Off message
135 void note_off(int channel, int note, int vel);
136 /// Handle MIDI Channel Pressure
137 void channel_pressure(int channel, int value);
138 /// Handle pitch bend message.
139 inline void pitch_bend(int /*channel*/, int value)
141 inertia_pitchbend.set_inertia(pow(2.0, (value * *params[par_pwhlrange]) / (1200.0 * 8192.0)));
143 /// Update oscillator frequency based on base frequency, detune amount, pitch bend scaling factor and sample rate.
144 void set_frequency();
145 /// Handle control change messages.
146 void control_change(int channel, int controller, int value);
147 /// Update variables from control ports.
148 void params_changed();
149 void activate();
150 void deactivate();
151 void post_instantiate(uint32_t)
153 precalculate_waves(progress_report);
155 /// Set waveform addresses for oscillators
156 void lookup_waveforms();
157 /// Run oscillators
158 void calculate_buffer_oscs(float lfo);
159 /// Run two filters in series to produce mono output samples.
160 void calculate_buffer_ser();
161 /// Run one filter to produce mono output samples.
162 void calculate_buffer_single();
163 /// Run two filters (one per channel) to produce stereo output samples.
164 void calculate_buffer_stereo();
165 /// Retrieve filter graph (which is 'live' so it cannot be generated by get_static_graph), or fall back to get_static_graph.
166 bool get_graph(int index, int subindex, int phase, float *data, int points, cairo_iface *context, int *mode) const;
167 bool get_layers(int index, int generation, unsigned int &layers) const { layers = LG_REALTIME_GRAPH; return true; }
168 /// @retval true if the filter 1 is to be used for the left channel and filter 2 for the right channel
169 /// No CV inputs for now
170 bool is_cv(int param_no) const { return false; }
171 /// Practically all the stuff here is noisy
172 bool is_noisy(int param_no) const { return param_no != par_cutoff; }
173 /// Main processing function
174 uint32_t process(uint32_t offset, uint32_t nsamples, uint32_t inputs_mask, uint32_t outputs_mask);
175 /// Send all configure variables set within a plugin to given destination (which may be limited to only those that plugin understands)
176 virtual void send_configures(send_configure_iface *sci) { return mod_matrix_impl::send_configures(sci); }
177 virtual char *configure(const char *key, const char *value) { return mod_matrix_impl::configure(key, value); }
178 private:
179 void reset();
180 float get_lfo(dsp::triangle_lfo &lfo, int param);
181 /// Apply anti-click'n'pop fadeout (used at the end of the sound)
182 void apply_fadeout();
183 /// Calculate control signals and produce step_size samples of output.
184 void calculate_step();
185 /// @retval false if filters are to be connected in series and sent (mono) to both channels
186 inline bool is_stereo_filter() const
188 return filter_type == flt_2lp12 || filter_type == flt_2bp6;
190 static void precalculate_waves(progress_report_iface *reporter);
195 #if ENABLE_EXPERIMENTAL
197 #include "wavetable.h"
199 #endif
201 #endif