+ Wavetable: one more wavetable (experimental, will be changed)
[calf.git] / src / calf / organ.h
blob8a9b316666238c519999a408944384d232d484d0
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., 51 Franklin Street, Fifth Floor,
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 filter1_type;
74 float master;
76 organ_filter_parameters filters[organ_parameters::FilterCount];
77 organ_env_parameters envs[organ_parameters::EnvCount];
78 float lfo_rate;
79 float lfo_amt;
80 float lfo_wet;
81 float lfo_phase;
82 float lfo_mode;
84 float global_transpose;
85 float global_detune;
87 float polyphony;
89 float quad_env;
91 float pitch_bend_range;
93 float bass_freq;
94 float bass_gain;
95 float treble_freq;
96 float treble_gain;
98 float dummy_mapcurve;
100 //////////////////////////////////////////////////////////////////////////
101 // these parameters are calculated
103 double perc_decay_const, perc_fm_decay_const;
104 float multiplier[9];
105 int phaseshift[9];
106 float cutoff;
107 unsigned int foldvalue;
108 float pitch_bend;
110 float percussion_keytrack[ORGAN_KEYTRACK_POINTS][2];
112 organ_parameters() : pitch_bend(1.0f) {}
114 inline int get_percussion_wave() { return dsp::fastf2i_drm(percussion_wave); }
115 inline int get_percussion_fm_wave() { return dsp::fastf2i_drm(percussion_fm_wave); }
118 #define ORGAN_WAVE_BITS 12
119 #define ORGAN_WAVE_SIZE 4096
120 #define ORGAN_BIG_WAVE_BITS 17
121 #define ORGAN_BIG_WAVE_SIZE 131072
122 /// 2^ORGAN_BIG_WAVE_SHIFT = how many (quasi)periods per sample
123 #define ORGAN_BIG_WAVE_SHIFT 5
125 class organ_voice_base: public calf_plugins::organ_enums
127 public:
128 typedef waveform_family<ORGAN_WAVE_BITS> small_wave_family;
129 typedef waveform_family<ORGAN_BIG_WAVE_BITS> big_wave_family;
130 public:
131 organ_parameters *parameters;
132 protected:
133 static small_wave_family (*waves)[wave_count_small];
134 static big_wave_family (*big_waves)[wave_count_big];
136 // dsp::sine_table<float, ORGAN_WAVE_SIZE, 1> sine_wave;
137 int note;
138 dsp::decay amp;
139 /// percussion FM carrier amplitude envelope
140 dsp::decay pamp;
141 /// percussion FM modulator amplitude envelope
142 dsp::decay fm_amp;
143 dsp::fixed_point<int64_t, 20> pphase, dpphase;
144 dsp::fixed_point<int64_t, 20> modphase, moddphase;
145 float fm_keytrack;
146 int &sample_rate_ref;
147 bool &released_ref;
148 /// 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)
149 float rel_age_const;
151 organ_voice_base(organ_parameters *_parameters, int &_sample_rate_ref, bool &_released_ref);
153 inline float wave(float *data, dsp::fixed_point<int, 20> ph) {
154 return ph.lerp_table_lookup_float(data);
156 inline float big_wave(float *data, dsp::fixed_point<int64_t, 20> &ph) {
157 // wrap to fit within the wave
158 return ph.lerp_table_lookup_float_mask(data, ORGAN_BIG_WAVE_SIZE - 1);
160 public:
161 static inline small_wave_family &get_wave(int wave) {
162 return (*waves)[wave];
164 static inline big_wave_family &get_big_wave(int wave) {
165 return (*big_waves)[wave];
167 static void precalculate_waves(calf_plugins::progress_report_iface *reporter);
168 void update_pitch()
170 float phase = dsp::midi_note_to_phase(note, 100 * parameters->global_transpose + parameters->global_detune, sample_rate_ref);
171 dpphase.set((long int) (phase * parameters->percussion_harmonic * parameters->pitch_bend));
172 moddphase.set((long int) (phase * parameters->percussion_fm_harmonic * parameters->pitch_bend));
174 // this doesn't really have a voice interface
175 void render_percussion_to(float (*buf)[2], int nsamples);
176 void perc_note_on(int note, int vel);
177 void perc_note_off(int note, int vel);
178 void perc_reset()
180 pphase = 0;
181 modphase = 0;
182 dpphase = 0;
183 moddphase = 0;
184 note = -1;
188 class organ_vibrato
190 protected:
191 enum { VibratoSize = 6 };
192 float vibrato_x1[VibratoSize][2], vibrato_y1[VibratoSize][2];
193 float lfo_phase;
194 dsp::onepole<float> vibrato[2];
195 public:
196 void reset();
197 void process(organ_parameters *parameters, float (*data)[2], unsigned int len, float sample_rate);
200 class organ_voice: public dsp::voice, public organ_voice_base {
201 protected:
202 enum { Channels = 2, BlockSize = 64, EnvCount = organ_parameters::EnvCount, FilterCount = organ_parameters::FilterCount };
203 union {
204 float output_buffer[BlockSize][Channels];
205 float aux_buffers[3][BlockSize][Channels];
207 dsp::fixed_point<int64_t, 52> phase, dphase;
208 dsp::biquad_d1<float> filterL[2], filterR[2];
209 adsr envs[EnvCount];
210 dsp::inertia<dsp::linear_ramp> expression;
211 organ_vibrato vibrato;
212 float velocity;
213 bool perc_released;
214 /// The envelopes have ended and the voice is in final fadeout stage
215 bool finishing;
216 dsp::inertia<dsp::exponential_ramp> inertia_pitchbend;
218 public:
219 organ_voice()
220 : organ_voice_base(NULL, sample_rate, perc_released)
221 , expression(dsp::linear_ramp(16))
222 , inertia_pitchbend(dsp::exponential_ramp(1))
224 inertia_pitchbend.set_now(1);
227 void reset() {
228 inertia_pitchbend.ramp.set_length(sample_rate / (BlockSize * 30)); // 1/30s
229 vibrato.reset();
230 phase = 0;
231 for (int i = 0; i < FilterCount; i++)
233 filterL[i].reset();
234 filterR[i].reset();
238 void note_on(int note, int vel) {
239 stolen = false;
240 finishing = false;
241 perc_released = false;
242 released = false;
243 reset();
244 this->note = note;
245 const float sf = 0.001f;
246 for (int i = 0; i < EnvCount; i++)
248 organ_parameters::organ_env_parameters &p = parameters->envs[i];
249 envs[i].set(sf * p.attack, sf * p.decay, p.sustain, sf * p.release, sample_rate / BlockSize);
250 envs[i].note_on();
252 update_pitch();
253 velocity = vel * 1.0 / 127.0;
254 amp.set(1.0f);
255 perc_note_on(note, vel);
258 void note_off(int /* vel */) {
259 // reset age to 0 (because decay will turn from exponential to linear, necessary because of error cumulation prevention)
260 perc_released = true;
261 if (pamp.get_active())
263 pamp.reinit();
265 rel_age_const = pamp.get() * ((1.0/44100.0)/0.03);
266 for (int i = 0; i < EnvCount; i++)
267 envs[i].note_off();
270 virtual float get_priority() { return stolen ? 20000 : (perc_released ? 1 : (sostenuto ? 200 : 100)); }
272 virtual void steal() {
273 perc_released = true;
274 finishing = true;
275 stolen = true;
278 void render_block();
280 virtual int get_current_note() {
281 return note;
283 virtual bool get_active() {
284 // printf("note %d getactive %d use_percussion %d pamp active %d\n", note, amp.get_active(), use_percussion(), pamp.get_active());
285 return (note != -1) && (amp.get_active() || (use_percussion() && pamp.get_active()));
287 void update_pitch();
288 inline bool use_percussion()
290 return dsp::fastf2i_drm(parameters->percussion_trigger) == perctrig_polyphonic && parameters->percussion_level > 0;
294 /// Not a true voice, just something with similar-ish interface.
295 class percussion_voice: public organ_voice_base {
296 public:
297 int sample_rate;
298 bool released;
300 percussion_voice(organ_parameters *_parameters)
301 : organ_voice_base(_parameters, sample_rate, released)
302 , released(false)
306 bool get_active() {
307 return (note != -1) && pamp.get_active();
309 bool get_noticable() {
310 return (note != -1) && (pamp.get() > 0.2 * parameters->percussion_level);
312 void setup(int sr) {
313 sample_rate = sr;
317 struct drawbar_organ: public dsp::basic_synth, public calf_plugins::organ_enums {
318 organ_parameters *parameters;
319 percussion_voice percussion;
320 organ_vibrato global_vibrato;
321 two_band_eq eq_l, eq_r;
323 drawbar_organ(organ_parameters *_parameters)
324 : parameters(_parameters)
325 , percussion(_parameters) {
327 void render_separate(float *output[], int nsamples);
328 dsp::voice *alloc_voice() {
329 block_voice<organ_voice> *v = new block_voice<organ_voice>();
330 v->parameters = parameters;
331 return v;
333 virtual void percussion_note_on(int note, int vel) {
334 percussion.perc_note_on(note, vel);
336 virtual void params_changed() = 0;
337 virtual void setup(int sr) {
338 basic_synth::setup(sr);
339 percussion.setup(sr);
340 parameters->cutoff = 0;
341 params_changed();
342 global_vibrato.reset();
344 void update_params();
345 void control_change(int controller, int value)
347 #if 0
348 if (controller == 11)
350 parameters->cutoff = value / 64.0 - 1;
352 #endif
353 dsp::basic_synth::control_change(controller, value);
355 void pitch_bend(int amt);
356 virtual bool check_percussion() {
357 switch(dsp::fastf2i_drm(parameters->percussion_trigger))
359 case organ_voice_base::perctrig_first:
360 return active_voices.empty();
361 case organ_voice_base::perctrig_each:
362 default:
363 return true;
364 case organ_voice_base::perctrig_eachplus:
365 return !percussion.get_noticable();
366 case organ_voice_base::perctrig_polyphonic:
367 return false;
374 #endif