+ Organ: add global two-band EQ
[calf.git] / src / calf / organ.h
blob69066a8c658ed5fcb4e0839ac889a333c0fe5b90
1 /* Calf DSP Library
2 * Drawbar organ emulator.
4 * Copyright (C) 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.
22 #ifndef __CALF_ORGAN_H
23 #define __CALF_ORGAN_H
25 #include "synth.h"
26 #include "envelope.h"
27 #include "metadata.h"
29 #define ORGAN_KEYTRACK_POINTS 4
31 namespace dsp
34 struct organ_parameters {
35 enum { FilterCount = 2, EnvCount = 3 };
36 struct organ_filter_parameters
38 float cutoff;
39 float resonance;
40 float envmod[organ_parameters::EnvCount];
41 float keyf;
44 struct organ_env_parameters
46 float attack, decay, sustain, release, velscale, ampctl;
49 //////////////////////////////////////////////////////////////////////////
50 // these parameters are binary-copied from control ports (order is important!)
52 float drawbars[9];
53 float harmonics[9];
54 float waveforms[9];
55 float detune[9];
56 float phase[9];
57 float pan[9];
58 float routing[9];
59 float foldover;
60 float percussion_time;
61 float percussion_level;
62 float percussion_wave;
63 float percussion_harmonic;
64 float percussion_vel2amp;
65 float percussion_fm_time;
66 float percussion_fm_depth;
67 float percussion_fm_wave;
68 float percussion_fm_harmonic;
69 float percussion_vel2fm;
70 float percussion_trigger;
71 float percussion_stereo;
72 float filter_chain;
73 float master;
75 organ_filter_parameters filters[organ_parameters::FilterCount];
76 organ_env_parameters envs[organ_parameters::EnvCount];
77 float lfo_rate;
78 float lfo_amt;
79 float lfo_wet;
80 float lfo_phase;
81 float lfo_mode;
83 float global_transpose;
84 float global_detune;
86 float polyphony;
88 float quad_env;
90 float pitch_bend_range;
92 float bass_freq;
93 float bass_gain;
94 float treble_freq;
95 float treble_gain;
97 float dummy_mapcurve;
99 //////////////////////////////////////////////////////////////////////////
100 // these parameters are calculated
102 double perc_decay_const, perc_fm_decay_const;
103 float multiplier[9];
104 int phaseshift[9];
105 float cutoff;
106 unsigned int foldvalue;
107 float pitch_bend;
109 float percussion_keytrack[ORGAN_KEYTRACK_POINTS][2];
111 organ_parameters() : pitch_bend(1.0f) {}
113 inline int get_percussion_wave() { return dsp::fastf2i_drm(percussion_wave); }
114 inline int get_percussion_fm_wave() { return dsp::fastf2i_drm(percussion_fm_wave); }
117 #define ORGAN_WAVE_BITS 12
118 #define ORGAN_WAVE_SIZE 4096
119 #define ORGAN_BIG_WAVE_BITS 17
120 #define ORGAN_BIG_WAVE_SIZE 131072
121 /// 2^ORGAN_BIG_WAVE_SHIFT = how many (quasi)periods per sample
122 #define ORGAN_BIG_WAVE_SHIFT 5
124 class organ_voice_base: public calf_plugins::organ_enums
126 public:
127 typedef waveform_family<ORGAN_WAVE_BITS> small_wave_family;
128 typedef waveform_family<ORGAN_BIG_WAVE_BITS> big_wave_family;
129 public:
130 organ_parameters *parameters;
131 protected:
132 static small_wave_family (*waves)[wave_count_small];
133 static big_wave_family (*big_waves)[wave_count_big];
135 // dsp::sine_table<float, ORGAN_WAVE_SIZE, 1> sine_wave;
136 int note;
137 dsp::decay amp;
138 /// percussion FM carrier amplitude envelope
139 dsp::decay pamp;
140 /// percussion FM modulator amplitude envelope
141 dsp::decay fm_amp;
142 dsp::fixed_point<int64_t, 20> pphase, dpphase;
143 dsp::fixed_point<int64_t, 20> modphase, moddphase;
144 float fm_keytrack;
145 int &sample_rate_ref;
146 bool &released_ref;
147 /// pamp per-sample (linear) step during release stage (calculated on release so that it will take 30ms for it to go from "current value at release point" to 0)
148 float rel_age_const;
150 organ_voice_base(organ_parameters *_parameters, int &_sample_rate_ref, bool &_released_ref);
152 inline float wave(float *data, dsp::fixed_point<int, 20> ph) {
153 return ph.lerp_table_lookup_float(data);
155 inline float big_wave(float *data, dsp::fixed_point<int64_t, 20> &ph) {
156 // wrap to fit within the wave
157 return ph.lerp_table_lookup_float_mask(data, ORGAN_BIG_WAVE_SIZE - 1);
159 public:
160 static inline small_wave_family &get_wave(int wave) {
161 return (*waves)[wave];
163 static inline big_wave_family &get_big_wave(int wave) {
164 return (*big_waves)[wave];
166 static void precalculate_waves(calf_plugins::progress_report_iface *reporter);
167 void update_pitch()
169 float phase = dsp::midi_note_to_phase(note, 100 * parameters->global_transpose + parameters->global_detune, sample_rate_ref);
170 dpphase.set((long int) (phase * parameters->percussion_harmonic * parameters->pitch_bend));
171 moddphase.set((long int) (phase * parameters->percussion_fm_harmonic * parameters->pitch_bend));
173 // this doesn't really have a voice interface
174 void render_percussion_to(float (*buf)[2], int nsamples);
175 void perc_note_on(int note, int vel);
176 void perc_note_off(int note, int vel);
177 void perc_reset()
179 pphase = 0;
180 modphase = 0;
181 dpphase = 0;
182 moddphase = 0;
183 note = -1;
187 class organ_vibrato
189 protected:
190 enum { VibratoSize = 6 };
191 float vibrato_x1[VibratoSize][2], vibrato_y1[VibratoSize][2];
192 float lfo_phase;
193 dsp::onepole<float> vibrato[2];
194 public:
195 void reset();
196 void process(organ_parameters *parameters, float (*data)[2], unsigned int len, float sample_rate);
199 class organ_voice: public dsp::voice, public organ_voice_base {
200 protected:
201 enum { Channels = 2, BlockSize = 64, EnvCount = organ_parameters::EnvCount, FilterCount = organ_parameters::FilterCount };
202 union {
203 float output_buffer[BlockSize][Channels];
204 float aux_buffers[3][BlockSize][Channels];
206 dsp::fixed_point<int64_t, 52> phase, dphase;
207 dsp::biquad_d1<float> filterL[2], filterR[2];
208 adsr envs[EnvCount];
209 dsp::inertia<dsp::linear_ramp> expression;
210 organ_vibrato vibrato;
211 float velocity;
212 bool perc_released;
213 /// The envelopes have ended and the voice is in final fadeout stage
214 bool finishing;
215 dsp::inertia<dsp::exponential_ramp> inertia_pitchbend;
217 public:
218 organ_voice()
219 : organ_voice_base(NULL, sample_rate, perc_released)
220 , expression(dsp::linear_ramp(16))
221 , inertia_pitchbend(dsp::exponential_ramp(1))
223 inertia_pitchbend.set_now(1);
226 void reset() {
227 inertia_pitchbend.ramp.set_length(sample_rate / (BlockSize * 30)); // 1/30s
228 vibrato.reset();
229 phase = 0;
230 for (int i = 0; i < FilterCount; i++)
232 filterL[i].reset();
233 filterR[i].reset();
237 void note_on(int note, int vel) {
238 stolen = false;
239 finishing = false;
240 perc_released = false;
241 released = false;
242 reset();
243 this->note = note;
244 const float sf = 0.001f;
245 for (int i = 0; i < EnvCount; i++)
247 organ_parameters::organ_env_parameters &p = parameters->envs[i];
248 envs[i].set(sf * p.attack, sf * p.decay, p.sustain, sf * p.release, sample_rate / BlockSize);
249 envs[i].note_on();
251 update_pitch();
252 velocity = vel * 1.0 / 127.0;
253 amp.set(1.0f);
254 perc_note_on(note, vel);
257 void note_off(int /* vel */) {
258 // reset age to 0 (because decay will turn from exponential to linear, necessary because of error cumulation prevention)
259 perc_released = true;
260 if (pamp.get_active())
262 pamp.reinit();
264 rel_age_const = pamp.get() * ((1.0/44100.0)/0.03);
265 for (int i = 0; i < EnvCount; i++)
266 envs[i].note_off();
269 virtual float get_priority() { return stolen ? 20000 : (perc_released ? 1 : (sostenuto ? 200 : 100)); }
271 virtual void steal() {
272 perc_released = true;
273 finishing = true;
274 stolen = true;
277 void render_block();
279 virtual int get_current_note() {
280 return note;
282 virtual bool get_active() {
283 // printf("note %d getactive %d use_percussion %d pamp active %d\n", note, amp.get_active(), use_percussion(), pamp.get_active());
284 return (note != -1) && (amp.get_active() || (use_percussion() && pamp.get_active()));
286 void update_pitch();
287 inline bool use_percussion()
289 return dsp::fastf2i_drm(parameters->percussion_trigger) == perctrig_polyphonic && parameters->percussion_level > 0;
293 /// Not a true voice, just something with similar-ish interface.
294 class percussion_voice: public organ_voice_base {
295 public:
296 int sample_rate;
297 bool released;
299 percussion_voice(organ_parameters *_parameters)
300 : organ_voice_base(_parameters, sample_rate, released)
301 , released(false)
305 bool get_active() {
306 return (note != -1) && pamp.get_active();
308 bool get_noticable() {
309 return (note != -1) && (pamp.get() > 0.2 * parameters->percussion_level);
311 void setup(int sr) {
312 sample_rate = sr;
316 struct drawbar_organ: public dsp::basic_synth, public calf_plugins::organ_enums {
317 organ_parameters *parameters;
318 percussion_voice percussion;
319 organ_vibrato global_vibrato;
320 two_band_eq eq_l, eq_r;
322 drawbar_organ(organ_parameters *_parameters)
323 : parameters(_parameters)
324 , percussion(_parameters) {
326 void render_separate(float *output[], int nsamples);
327 dsp::voice *alloc_voice() {
328 block_voice<organ_voice> *v = new block_voice<organ_voice>();
329 v->parameters = parameters;
330 return v;
332 virtual void percussion_note_on(int note, int vel) {
333 percussion.perc_note_on(note, vel);
335 virtual void params_changed() = 0;
336 virtual void setup(int sr) {
337 basic_synth::setup(sr);
338 percussion.setup(sr);
339 parameters->cutoff = 0;
340 params_changed();
341 global_vibrato.reset();
343 void update_params();
344 void control_change(int controller, int value)
346 #if 0
347 if (controller == 11)
349 parameters->cutoff = value / 64.0 - 1;
351 #endif
352 dsp::basic_synth::control_change(controller, value);
354 void pitch_bend(int amt);
355 virtual bool check_percussion() {
356 switch(dsp::fastf2i_drm(parameters->percussion_trigger))
358 case organ_voice_base::perctrig_first:
359 return active_voices.empty();
360 case organ_voice_base::perctrig_each:
361 default:
362 return true;
363 case organ_voice_base::perctrig_eachplus:
364 return !percussion.get_noticable();
365 case organ_voice_base::perctrig_polyphonic:
366 return false;
373 #endif