Entry/spinbutton fixed
[calf.git] / src / organ.cpp
blob0d531c4a48ded120e564d44c67d55f9a6f198114
1 /* Calf DSP Library
2 * Example audio modules - organ
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 02110-1301 USA
21 #include <config.h>
23 #include <assert.h>
24 #include <memory.h>
25 #include <complex>
26 #if USE_JACK
27 #include <jack/jack.h>
28 #endif
29 #include <calf/giface.h>
30 #include <calf/modules_synths.h>
31 #include <iostream>
33 using namespace std;
34 using namespace dsp;
35 using namespace calf_plugins;
37 //////////////////////////////////////////////////////////////////////////////////////////////////////////
39 bool organ_audio_module::get_graph(int index, int subindex, float *data, int points, cairo_iface *context)
41 if (index == par_master) {
42 organ_voice_base::precalculate_waves(progress_report);
43 if (subindex)
44 return false;
45 float *waveforms[9];
46 int S[9], S2[9];
47 enum { small_waves = organ_voice_base::wave_count_small};
48 for (int i = 0; i < 9; i++)
50 int wave = dsp::clip((int)(parameters->waveforms[i]), 0, (int)organ_voice_base::wave_count - 1);
51 if (wave >= small_waves)
53 waveforms[i] = organ_voice_base::get_big_wave(wave - small_waves).original;
54 S[i] = ORGAN_BIG_WAVE_SIZE;
55 S2[i] = ORGAN_WAVE_SIZE / 64;
57 else
59 waveforms[i] = organ_voice_base::get_wave(wave).original;
60 S[i] = S2[i] = ORGAN_WAVE_SIZE;
63 for (int i = 0; i < points; i++)
65 float sum = 0.f;
66 for (int j = 0; j < 9; j++)
68 float shift = parameters->phase[j] * S[j] / 360.0;
69 sum += parameters->drawbars[j] * waveforms[j][int(parameters->harmonics[j] * i * S2[j] / points + shift) & (S[j] - 1)];
71 data[i] = sum * 2 / (9 * 8);
73 return true;
75 return false;
78 ////////////////////////////////////////////////////////////////////////////
80 organ_voice_base::small_wave_family (*organ_voice_base::waves)[organ_voice_base::wave_count_small];
81 organ_voice_base::big_wave_family (*organ_voice_base::big_waves)[organ_voice_base::wave_count_big];
83 static void smoothen(bandlimiter<ORGAN_WAVE_BITS> &bl, float tmp[ORGAN_WAVE_SIZE])
85 bl.compute_spectrum(tmp);
86 for (int i = 1; i <= ORGAN_WAVE_SIZE / 2; i++) {
87 bl.spectrum[i] *= 1.0 / sqrt(i);
88 bl.spectrum[ORGAN_WAVE_SIZE - i] *= 1.0 / sqrt(i);
90 bl.compute_waveform(tmp);
91 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
94 static void phaseshift(bandlimiter<ORGAN_WAVE_BITS> &bl, float tmp[ORGAN_WAVE_SIZE])
96 bl.compute_spectrum(tmp);
97 for (int i = 1; i <= ORGAN_WAVE_SIZE / 2; i++) {
98 float frac = i * 2.0 / ORGAN_WAVE_SIZE;
99 float phase = M_PI / sqrt(frac) ;
100 complex<float> shift = complex<float>(cos(phase), sin(phase));
101 bl.spectrum[i] *= shift;
102 bl.spectrum[ORGAN_WAVE_SIZE - i] *= conj(shift);
104 bl.compute_waveform(tmp);
105 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
108 static void padsynth(bandlimiter<ORGAN_WAVE_BITS> blSrc, bandlimiter<ORGAN_BIG_WAVE_BITS> &blDest, organ_voice_base::big_wave_family &result, int bwscale = 20, float bell_factor = 0, bool foldover = false)
110 // kept in a vector to avoid putting large arrays on stack
111 vector<complex<float> >orig_spectrum;
112 orig_spectrum.resize(ORGAN_WAVE_SIZE / 2);
113 for (int i = 0; i < ORGAN_WAVE_SIZE / 2; i++)
115 orig_spectrum[i] = blSrc.spectrum[i];
116 // printf("@%d = %f\n", i, abs(orig_spectrum[i]));
119 int periods = (1 << ORGAN_BIG_WAVE_SHIFT) * ORGAN_BIG_WAVE_SIZE / ORGAN_WAVE_SIZE;
120 for (int i = 0; i <= ORGAN_BIG_WAVE_SIZE / 2; i++) {
121 blDest.spectrum[i] = 0;
123 int MAXHARM = (ORGAN_WAVE_SIZE >> (1 + ORGAN_BIG_WAVE_SHIFT));
124 for (int i = 1; i <= MAXHARM; i++) {
125 //float esc = 0.25 * (1 + 0.5 * log(i));
126 float esc = 0.5;
127 float amp = abs(blSrc.spectrum[i]);
128 // fade out starting from half
129 if (i >= MAXHARM / 2) {
130 float pos = (i - MAXHARM/2) * 1.0 / (MAXHARM / 2);
131 amp *= 1.0 - pos;
132 amp *= 1.0 - pos;
134 int bw = 1 + 20 * i;
135 float sum = 1;
136 int delta = 1;
137 if (bw > 20) delta = bw / 20;
138 for (int j = delta; j <= bw; j+=delta)
140 float p = j * 1.0 / bw;
141 sum += 2 * exp(-p * p * esc);
143 if (sum < 0.0001)
144 continue;
145 amp *= (ORGAN_BIG_WAVE_SIZE / ORGAN_WAVE_SIZE);
146 amp /= sum;
147 int orig = i * periods + bell_factor * cos(i);
148 if (orig > 0 && orig < ORGAN_BIG_WAVE_SIZE / 2)
149 blDest.spectrum[orig] += amp;
150 for (int j = delta; j <= bw; j += delta)
152 float p = j * 1.0 / bw;
153 float val = amp * exp(-p * p * esc);
154 int dist = j * bwscale / 40;
155 int pos = orig + dist;
156 if (pos < 1 || pos >= ORGAN_BIG_WAVE_SIZE / 2)
157 continue;
158 int pos2 = orig - dist;
159 if (pos2 < 1 || pos2 >= ORGAN_BIG_WAVE_SIZE / 2)
160 continue;
161 blDest.spectrum[pos] += val;
162 if (j)
163 blDest.spectrum[pos2] += val;
166 for (int i = 1; i <= ORGAN_BIG_WAVE_SIZE / 2; i++) {
167 float phase = M_PI * 2 * (rand() & 255) / 256;
168 complex<float> shift = complex<float>(cos(phase), sin(phase));
169 blDest.spectrum[i] *= shift;
170 // printf("@%d = %f\n", i, abs(blDest.spectrum[i]));
172 blDest.spectrum[ORGAN_BIG_WAVE_SIZE - i] = conj(blDest.spectrum[i]);
174 // same as above - put large array on heap to avoid stack overflow in ingen
175 vector<float> tmp;
176 tmp.resize(ORGAN_BIG_WAVE_SIZE);
177 float *ptmp = &tmp.front();
178 blDest.compute_waveform(ptmp);
179 normalize_waveform(ptmp, ORGAN_BIG_WAVE_SIZE);
180 blDest.compute_spectrum(ptmp);
182 // limit is 1/2 of the number of harmonics of the original wave
183 result.make_from_spectrum(blDest, foldover, ORGAN_WAVE_SIZE >> (1 + ORGAN_BIG_WAVE_SHIFT));
184 memcpy(result.original, result.begin()->second, sizeof(result.original));
185 #if 0
186 blDest.compute_waveform(result);
187 normalize_waveform(result, ORGAN_BIG_WAVE_SIZE);
188 result[ORGAN_BIG_WAVE_SIZE] = result[0];
189 for (int i =0 ; i<ORGAN_BIG_WAVE_SIZE + 1; i++)
190 printf("%f\n", result[i]);
191 #endif
194 #define LARGE_WAVEFORM_PROGRESS() do { if (reporter) { progress += 100; reporter->report_progress(floor(progress / totalwaves), "Precalculating large waveforms"); } } while(0)
197 void organ_voice_base::precalculate_waves(progress_report_iface *reporter)
199 static bool inited = false;
200 if (!inited)
202 static organ_voice_base::small_wave_family waves[organ_voice_base::wave_count_small];
203 static organ_voice_base::big_wave_family big_waves[organ_voice_base::wave_count_big];
204 organ_voice_base::waves = &waves;
205 organ_voice_base::big_waves = &big_waves;
207 float progress = 0.0;
208 int totalwaves = 1 + wave_count_big;
209 if (reporter)
210 reporter->report_progress(0, "Precalculating small waveforms");
211 float tmp[ORGAN_WAVE_SIZE];
212 static bandlimiter<ORGAN_WAVE_BITS> bl;
213 static bandlimiter<ORGAN_BIG_WAVE_BITS> blBig;
214 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
215 tmp[i] = sin(i * 2 * M_PI / ORGAN_WAVE_SIZE);
216 waves[wave_sine].make(bl, tmp);
217 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
218 tmp[i] = (i < (ORGAN_WAVE_SIZE / 16)) ? 1 : 0;
219 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
220 waves[wave_pulse].make(bl, tmp);
221 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
222 tmp[i] = i < (ORGAN_WAVE_SIZE / 2) ? sin(i * 2 * 2 * M_PI / ORGAN_WAVE_SIZE) : 0;
223 waves[wave_sinepl1].make(bl, tmp);
224 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
225 tmp[i] = i < (ORGAN_WAVE_SIZE / 3) ? sin(i * 3 * 2 * M_PI / ORGAN_WAVE_SIZE) : 0;
226 waves[wave_sinepl2].make(bl, tmp);
227 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
228 tmp[i] = i < (ORGAN_WAVE_SIZE / 4) ? sin(i * 4 * 2 * M_PI / ORGAN_WAVE_SIZE) : 0;
229 waves[wave_sinepl3].make(bl, tmp);
230 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
231 tmp[i] = (i < (ORGAN_WAVE_SIZE / 2)) ? 1 : -1;
232 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
233 waves[wave_sqr].make(bl, tmp);
234 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
235 tmp[i] = -1 + (i * 2.0 / ORGAN_WAVE_SIZE);
236 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
237 waves[wave_saw].make(bl, tmp);
239 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
240 tmp[i] = (i < (ORGAN_WAVE_SIZE / 2)) ? 1 : -1;
241 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
242 smoothen(bl, tmp);
243 waves[wave_ssqr].make(bl, tmp);
245 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
246 tmp[i] = -1 + (i * 2.0 / ORGAN_WAVE_SIZE);
247 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
248 smoothen(bl, tmp);
249 waves[wave_ssaw].make(bl, tmp);
251 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
252 tmp[i] = (i < (ORGAN_WAVE_SIZE / 16)) ? 1 : 0;
253 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
254 smoothen(bl, tmp);
255 waves[wave_spls].make(bl, tmp);
256 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
257 tmp[i] = i < (ORGAN_WAVE_SIZE / 1.5) ? sin(i * 1.5 * 2 * M_PI / ORGAN_WAVE_SIZE) : 0;
258 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
259 waves[wave_sinepl05].make(bl, tmp);
260 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
261 tmp[i] = i < (ORGAN_WAVE_SIZE / 1.5) ? (i < ORGAN_WAVE_SIZE / 3 ? -1 : +1) : 0;
262 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
263 waves[wave_sqr05].make(bl, tmp);
264 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
265 tmp[i] = sin(i * M_PI / ORGAN_WAVE_SIZE);
266 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
267 waves[wave_halfsin].make(bl, tmp);
268 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
269 tmp[i] = sin(i * 3 * M_PI / ORGAN_WAVE_SIZE);
270 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
271 waves[wave_clvg].make(bl, tmp);
272 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
274 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
275 float fm = 0.3 * sin(6*ph) + 0.2 * sin(11*ph) + 0.2 * cos(17*ph) - 0.2 * cos(19*ph);
276 tmp[i] = sin(5*ph + fm) + 0.7 * cos(7*ph - fm);
278 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
279 waves[wave_bell].make(bl, tmp, true);
280 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
282 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
283 float fm = 0.3 * sin(3*ph) + 0.3 * sin(11*ph) + 0.3 * cos(17*ph) - 0.3 * cos(19*ph) + 0.3 * cos(25*ph) - 0.3 * cos(31*ph) + 0.3 * cos(37*ph);
284 tmp[i] = sin(3*ph + fm) + cos(7*ph - fm);
286 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
287 waves[wave_bell2].make(bl, tmp, true);
288 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
290 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
291 float fm = 0.5 * sin(3*ph) + 0.3 * sin(5*ph) + 0.3 * cos(6*ph) - 0.3 * cos(9*ph);
292 tmp[i] = sin(4*ph + fm) + cos(ph - fm);
294 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
295 waves[wave_w1].make(bl, tmp);
296 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
298 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
299 tmp[i] = sin(ph) * sin(2 * ph) * sin(4 * ph) * sin(8 * ph);
301 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
302 waves[wave_w2].make(bl, tmp);
303 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
305 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
306 tmp[i] = sin(ph) * sin(3 * ph) * sin(5 * ph) * sin(7 * ph) * sin(9 * ph);
308 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
309 waves[wave_w3].make(bl, tmp);
310 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
312 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
313 tmp[i] = sin(ph + 2 * sin(ph + 2 * sin(ph)));
315 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
316 waves[wave_w4].make(bl, tmp);
317 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
319 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
320 tmp[i] = ph * sin(ph);
322 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
323 waves[wave_w5].make(bl, tmp);
324 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
326 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
327 tmp[i] = ph * sin(ph) + (2 * M_PI - ph) * sin(2 * ph);
329 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
330 waves[wave_w6].make(bl, tmp);
331 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
333 float ph = i * 1.0 / ORGAN_WAVE_SIZE;
334 tmp[i] = exp(-ph * ph);
336 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
337 waves[wave_w7].make(bl, tmp);
338 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
340 float ph = i * 1.0 / ORGAN_WAVE_SIZE;
341 tmp[i] = exp(-ph * sin(2 * M_PI * ph));
343 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
344 waves[wave_w8].make(bl, tmp);
345 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
347 float ph = i * 1.0 / ORGAN_WAVE_SIZE;
348 tmp[i] = sin(2 * M_PI * ph * ph);
350 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
351 waves[wave_w9].make(bl, tmp);
353 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
354 tmp[i] = -1 + (i * 2.0 / ORGAN_WAVE_SIZE);
355 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
356 phaseshift(bl, tmp);
357 waves[wave_dsaw].make(bl, tmp);
359 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
360 tmp[i] = (i < (ORGAN_WAVE_SIZE / 2)) ? 1 : -1;
361 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
362 phaseshift(bl, tmp);
363 waves[wave_dsqr].make(bl, tmp);
365 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
366 tmp[i] = (i < (ORGAN_WAVE_SIZE / 16)) ? 1 : 0;
367 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
368 phaseshift(bl, tmp);
369 waves[wave_dpls].make(bl, tmp);
371 LARGE_WAVEFORM_PROGRESS();
372 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
373 tmp[i] = -1 + (i * 2.0 / ORGAN_WAVE_SIZE);
374 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
375 bl.compute_spectrum(tmp);
376 padsynth(bl, blBig, big_waves[wave_strings - wave_count_small], 15);
378 LARGE_WAVEFORM_PROGRESS();
379 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
380 tmp[i] = -1 + (i * 2.0 / ORGAN_WAVE_SIZE);
381 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
382 bl.compute_spectrum(tmp);
383 padsynth(bl, blBig, big_waves[wave_strings2 - wave_count_small], 40);
385 LARGE_WAVEFORM_PROGRESS();
386 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
387 tmp[i] = sin(i * 2 * M_PI / ORGAN_WAVE_SIZE);
388 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
389 bl.compute_spectrum(tmp);
390 padsynth(bl, blBig, big_waves[wave_sinepad - wave_count_small], 20);
392 LARGE_WAVEFORM_PROGRESS();
393 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
395 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
396 float fm = 0.3 * sin(6*ph) + 0.2 * sin(11*ph) + 0.2 * cos(17*ph) - 0.2 * cos(19*ph);
397 tmp[i] = sin(5*ph + fm) + 0.7 * cos(7*ph - fm);
399 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
400 bl.compute_spectrum(tmp);
401 padsynth(bl, blBig, big_waves[wave_bellpad - wave_count_small], 30, 30, true);
403 LARGE_WAVEFORM_PROGRESS();
404 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
406 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
407 float fm = 0.3 * sin(3*ph) + 0.2 * sin(4*ph) + 0.2 * cos(5*ph) - 0.2 * cos(6*ph);
408 tmp[i] = sin(2*ph + fm) + 0.7 * cos(3*ph - fm);
410 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
411 bl.compute_spectrum(tmp);
412 padsynth(bl, blBig, big_waves[wave_space - wave_count_small], 30, 30);
414 LARGE_WAVEFORM_PROGRESS();
415 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
417 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
418 float fm = 0.5 * sin(ph) + 0.5 * sin(2*ph) + 0.5 * sin(3*ph);
419 tmp[i] = sin(ph + fm) + 0.5 * cos(7*ph - 2 * fm) + 0.25 * cos(13*ph - 4 * fm);
421 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
422 bl.compute_spectrum(tmp);
423 padsynth(bl, blBig, big_waves[wave_choir - wave_count_small], 50, 10);
425 LARGE_WAVEFORM_PROGRESS();
426 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
428 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
429 float fm = sin(ph) ;
430 tmp[i] = sin(ph + fm) + 0.25 * cos(11*ph - 2 * fm) + 0.125 * cos(23*ph - 2 * fm) + 0.0625 * cos(49*ph - 2 * fm);
432 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
433 bl.compute_spectrum(tmp);
434 padsynth(bl, blBig, big_waves[wave_choir2 - wave_count_small], 50, 10);
436 LARGE_WAVEFORM_PROGRESS();
437 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
439 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
440 float fm = sin(ph) ;
441 tmp[i] = sin(ph + 4 * fm) + 0.5 * sin(2 * ph + 4 * ph);
443 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
444 bl.compute_spectrum(tmp);
445 padsynth(bl, blBig, big_waves[wave_choir3 - wave_count_small], 50, 10);
446 LARGE_WAVEFORM_PROGRESS();
448 inited = true;
452 organ_voice_base::organ_voice_base(organ_parameters *_parameters, int &_sample_rate_ref, bool &_released_ref)
453 : parameters(_parameters)
454 , sample_rate_ref(_sample_rate_ref)
455 , released_ref(_released_ref)
457 note = -1;
460 void organ_voice_base::render_percussion_to(float (*buf)[2], int nsamples)
462 if (note == -1)
463 return;
465 if (!pamp.get_active())
466 return;
467 if (parameters->percussion_level < small_value<float>())
468 return;
469 float level = parameters->percussion_level * 9;
470 static float zeros[ORGAN_WAVE_SIZE];
471 // XXXKF the decay needs work!
472 double age_const = parameters->perc_decay_const;
473 double fm_age_const = parameters->perc_fm_decay_const;
474 int timbre = parameters->get_percussion_wave();
475 if (timbre < 0 || timbre >= wave_count_small)
476 return;
477 int timbre2 = parameters->get_percussion_fm_wave();
478 if (timbre2 < 0 || timbre2 >= wave_count_small)
479 timbre2 = wave_sine;
480 float *fmdata = (*waves)[timbre2].get_level(moddphase.get());
481 if (!fmdata)
482 fmdata = zeros;
483 float *data = (*waves)[timbre].get_level(dpphase.get());
484 if (!data) {
485 pamp.deactivate();
486 return;
488 float s = parameters->percussion_stereo * ORGAN_WAVE_SIZE * (0.5 / 360.0);
489 for (int i = 0; i < nsamples; i++) {
490 float fm = wave(fmdata, modphase);
491 fm *= ORGAN_WAVE_SIZE * parameters->percussion_fm_depth * fm_amp.get();
492 modphase += moddphase;
493 fm_amp.age_exp(fm_age_const, 1.0 / 32768.0);
495 float lamp = level * pamp.get();
496 buf[i][0] += lamp * wave(data, pphase + dsp::fixed_point<int64_t, 52>(fm - s));
497 buf[i][1] += lamp * wave(data, pphase + dsp::fixed_point<int64_t, 52>(fm + s));
498 if (released_ref)
499 pamp.age_lin(rel_age_const,0.0);
500 else
501 pamp.age_exp(age_const, 1.0 / 32768.0);
502 pphase += dpphase;
506 void organ_vibrato::reset()
508 for (int i = 0; i < VibratoSize; i++)
509 vibrato_x1[i][0] = vibrato_y1[i][0] = vibrato_x1[i][1] = vibrato_y1[i][1] = 0.f;
510 vibrato[0].a0 = vibrato[1].a0 = 0;
511 lfo_phase = 0.f;
514 void organ_vibrato::process(organ_parameters *parameters, float (*data)[2], unsigned int len, float sample_rate)
516 float lfo1 = lfo_phase < 0.5 ? 2 * lfo_phase : 2 - 2 * lfo_phase;
517 float lfo_phase2 = lfo_phase + parameters->lfo_phase * (1.0 / 360.0);
518 if (lfo_phase2 >= 1.0)
519 lfo_phase2 -= 1.0;
520 float lfo2 = lfo_phase2 < 0.5 ? 2 * lfo_phase2 : 2 - 2 * lfo_phase2;
521 lfo_phase += parameters->lfo_rate * len / sample_rate;
522 if (lfo_phase >= 1.0)
523 lfo_phase -= 1.0;
524 if (!len)
525 return;
526 float olda0[2] = {vibrato[0].a0, vibrato[1].a0};
527 vibrato[0].set_ap(3000 + 7000 * parameters->lfo_amt * lfo1 * lfo1, sample_rate);
528 vibrato[1].set_ap(3000 + 7000 * parameters->lfo_amt * lfo2 * lfo2, sample_rate);
529 float ilen = 1.0 / len;
530 float deltaa0[2] = {(vibrato[0].a0 - olda0[0])*ilen, (vibrato[1].a0 - olda0[1])*ilen};
532 float vib_wet = parameters->lfo_wet;
533 for (int c = 0; c < 2; c++)
535 for (unsigned int i = 0; i < len; i++)
537 float v = data[i][c];
538 float v0 = v;
539 float coeff = olda0[c] + deltaa0[c] * i;
540 for (int t = 0; t < VibratoSize; t++)
541 v = vibrato[c].process_ap(v, vibrato_x1[t][c], vibrato_y1[t][c], coeff);
543 data[i][c] += (v - v0) * vib_wet;
545 for (int t = 0; t < VibratoSize; t++)
547 sanitize(vibrato_x1[t][c]);
548 sanitize(vibrato_y1[t][c]);
553 void organ_voice::update_pitch()
555 organ_voice_base::update_pitch();
556 dphase.set(dsp::midi_note_to_phase(note, 100 * parameters->global_transpose + parameters->global_detune, sample_rate) * inertia_pitchbend.get_last());
559 void organ_voice::render_block() {
560 if (note == -1)
561 return;
563 dsp::zero(&output_buffer[0][0], Channels * BlockSize);
564 dsp::zero(&aux_buffers[1][0][0], 2 * Channels * BlockSize);
565 if (!amp.get_active())
567 if (use_percussion())
568 render_percussion_to(output_buffer, BlockSize);
569 return;
572 inertia_pitchbend.set_inertia(parameters->pitch_bend);
573 inertia_pitchbend.step();
574 update_pitch();
575 dsp::fixed_point<int, 20> tphase, tdphase;
576 unsigned int foldvalue = parameters->foldvalue * inertia_pitchbend.get_last();
577 int vibrato_mode = fastf2i_drm(parameters->lfo_mode);
578 for (int h = 0; h < 9; h++)
580 float amp = parameters->drawbars[h];
581 if (amp < small_value<float>())
582 continue;
583 float *data;
584 dsp::fixed_point<int, 24> hm = dsp::fixed_point<int, 24>(parameters->multiplier[h]);
585 int waveid = (int)parameters->waveforms[h];
586 if (waveid < 0 || waveid >= wave_count)
587 waveid = 0;
589 uint32_t rate = (dphase * hm).get();
590 if (waveid >= wave_count_small)
592 float *data = (*big_waves)[waveid - wave_count_small].get_level(rate >> (ORGAN_BIG_WAVE_BITS - ORGAN_WAVE_BITS + ORGAN_BIG_WAVE_SHIFT));
593 if (!data)
594 continue;
595 hm.set(hm.get() >> ORGAN_BIG_WAVE_SHIFT);
596 dsp::fixed_point<int64_t, 20> tphase, tdphase;
597 tphase.set(((phase * hm).get()) + parameters->phaseshift[h]);
598 tdphase.set(rate >> ORGAN_BIG_WAVE_SHIFT);
599 float ampl = amp * 0.5f * (1 - parameters->pan[h]);
600 float ampr = amp * 0.5f * (1 + parameters->pan[h]);
601 float (*out)[Channels] = aux_buffers[dsp::fastf2i_drm(parameters->routing[h])];
603 for (int i=0; i < (int)BlockSize; i++) {
604 float wv = big_wave(data, tphase);
605 out[i][0] += wv * ampl;
606 out[i][1] += wv * ampr;
607 tphase += tdphase;
610 else
612 unsigned int foldback = 0;
613 while (rate > foldvalue)
615 rate >>= 1;
616 foldback++;
618 hm.set(hm.get() >> foldback);
619 data = (*waves)[waveid].get_level(rate);
620 if (!data)
621 continue;
622 tphase.set((uint32_t)((phase * hm).get()) + parameters->phaseshift[h]);
623 tdphase.set((uint32_t)rate);
624 float ampl = amp * 0.5f * (1 - parameters->pan[h]);
625 float ampr = amp * 0.5f * (1 + parameters->pan[h]);
626 float (*out)[Channels] = aux_buffers[dsp::fastf2i_drm(parameters->routing[h])];
628 for (int i=0; i < (int)BlockSize; i++) {
629 float wv = wave(data, tphase);
630 out[i][0] += wv * ampl;
631 out[i][1] += wv * ampr;
632 tphase += tdphase;
637 bool is_quad = parameters->quad_env >= 0.5f;
639 expression.set_inertia(parameters->cutoff);
640 phase += dphase * BlockSize;
641 float escl[EnvCount], eval[EnvCount];
642 for (int i = 0; i < EnvCount; i++)
643 escl[i] = (1.f + parameters->envs[i].velscale * (velocity - 1.f));
645 if (is_quad)
647 for (int i = 0; i < EnvCount; i++)
648 eval[i] = envs[i].value * envs[i].value * escl[i];
650 else
652 for (int i = 0; i < EnvCount; i++)
653 eval[i] = envs[i].value * escl[i];
655 for (int i = 0; i < FilterCount; i++)
657 float mod = parameters->filters[i].envmod[0] * eval[0] ;
658 mod += parameters->filters[i].keyf * 100 * (note - 60);
659 for (int j = 1; j < EnvCount; j++)
661 mod += parameters->filters[i].envmod[j] * eval[j];
663 if (i) mod += expression.get() * 1200 * 4;
664 float fc = parameters->filters[i].cutoff * pow(2.0f, mod * (1.f / 1200.f));
665 if (i == 0 && parameters->filter1_type >= 0.5f)
666 filterL[i].set_hp_rbj(dsp::clip<float>(fc, 10, 18000), parameters->filters[i].resonance, sample_rate);
667 else
668 filterL[i].set_lp_rbj(dsp::clip<float>(fc, 10, 18000), parameters->filters[i].resonance, sample_rate);
669 filterR[i].copy_coeffs(filterL[i]);
671 float amp_pre[ampctl_count - 1], amp_post[ampctl_count - 1];
672 for (int i = 0; i < ampctl_count - 1; i++)
674 amp_pre[i] = 1.f;
675 amp_post[i] = 1.f;
677 bool any_running = false;
678 for (int i = 0; i < EnvCount; i++)
680 float pre = eval[i];
681 envs[i].advance();
682 int mode = fastf2i_drm(parameters->envs[i].ampctl);
683 if (!envs[i].stopped())
684 any_running = true;
685 if (mode == ampctl_none)
686 continue;
687 float post = (is_quad ? envs[i].value : 1) * envs[i].value * escl[i];
688 amp_pre[mode - 1] *= pre;
689 amp_post[mode - 1] *= post;
691 if (vibrato_mode >= lfomode_direct && vibrato_mode <= lfomode_filter2)
692 vibrato.process(parameters, aux_buffers[vibrato_mode - lfomode_direct], BlockSize, sample_rate);
693 if (!any_running)
694 finishing = true;
695 // calculate delta from pre and post
696 for (int i = 0; i < ampctl_count - 1; i++)
697 amp_post[i] = (amp_post[i] - amp_pre[i]) * (1.0 / BlockSize);
698 float a0 = amp_pre[0], a1 = amp_pre[1], a2 = amp_pre[2], a3 = amp_pre[3];
699 float d0 = amp_post[0], d1 = amp_post[1], d2 = amp_post[2], d3 = amp_post[3];
700 if (parameters->filter_chain >= 0.5f)
702 for (int i=0; i < (int) BlockSize; i++) {
703 output_buffer[i][0] = a3 * (a0 * output_buffer[i][0] + a2 * filterL[1].process(a1 * filterL[0].process(aux_buffers[1][i][0]) + aux_buffers[2][i][0]));
704 output_buffer[i][1] = a3 * (a0 * output_buffer[i][1] + a2 * filterR[1].process(a1 * filterR[0].process(aux_buffers[1][i][1]) + aux_buffers[2][i][1]));
705 a0 += d0, a1 += d1, a2 += d2, a3 += d3;
708 else
710 for (int i=0; i < (int) BlockSize; i++) {
711 output_buffer[i][0] = a3 * (a0 * output_buffer[i][0] + a1 * filterL[0].process(aux_buffers[1][i][0]) + a2 * filterL[1].process(aux_buffers[2][i][0]));
712 output_buffer[i][1] = a3 * (a0 * output_buffer[i][1] + a1 * filterR[0].process(aux_buffers[1][i][1]) + a2 * filterR[1].process(aux_buffers[2][i][1]));
713 a0 += d0, a1 += d1, a2 += d2, a3 += d3;
716 filterL[0].sanitize();
717 filterR[0].sanitize();
718 filterL[1].sanitize();
719 filterR[1].sanitize();
720 if (vibrato_mode == lfomode_voice)
721 vibrato.process(parameters, output_buffer, BlockSize, sample_rate);
723 if (finishing)
725 for (int i = 0; i < (int) BlockSize; i++) {
726 output_buffer[i][0] *= amp.get();
727 output_buffer[i][1] *= amp.get();
728 amp.age_lin((1.0/44100.0)/0.03,0.0);
732 if (use_percussion())
733 render_percussion_to(output_buffer, BlockSize);
736 void drawbar_organ::update_params()
738 parameters->perc_decay_const = dsp::decay::calc_exp_constant(1.0 / 1024.0, 0.001 * parameters->percussion_time * sample_rate);
739 parameters->perc_fm_decay_const = dsp::decay::calc_exp_constant(1.0 / 1024.0, 0.001 * parameters->percussion_fm_time * sample_rate);
740 for (int i = 0; i < 9; i++)
742 parameters->multiplier[i] = parameters->harmonics[i] * pow(2.0, parameters->detune[i] * (1.0 / 1200.0));
743 parameters->phaseshift[i] = int(parameters->phase[i] * 65536 / 360) << 16;
745 double dphase = dsp::midi_note_to_phase((int)parameters->foldover, 0, sample_rate);
746 parameters->foldvalue = (int)(dphase);
749 void drawbar_organ::pitch_bend(int amt)
751 parameters->pitch_bend = pow(2.0, (amt * parameters->pitch_bend_range) / (1200.0 * 8192.0));
752 for (list<voice *>::iterator i = active_voices.begin(); i != active_voices.end(); i++)
754 organ_voice *v = dynamic_cast<organ_voice *>(*i);
755 v->update_pitch();
757 percussion.update_pitch();
760 void organ_audio_module::execute(int cmd_no)
762 switch(cmd_no)
764 case 0:
765 panic_flag = true;
766 break;
770 void organ_voice_base::perc_note_on(int note, int vel)
772 perc_reset();
773 released_ref = false;
774 this->note = note;
775 if (parameters->percussion_level > 0)
776 pamp.set(1.0f + (vel - 127) * parameters->percussion_vel2amp / 127.0);
777 update_pitch();
778 float (*kt)[2] = parameters->percussion_keytrack;
779 // assume last point (will be put there by padding)
780 fm_keytrack = kt[ORGAN_KEYTRACK_POINTS - 1][1];
781 // yes binary search would be nice if we had more than those crappy 4 points
782 for (int i = 0; i < ORGAN_KEYTRACK_POINTS - 1; i++)
784 float &lower = kt[i][0], upper = kt[i + 1][0];
785 if (note >= lower && note < upper)
787 fm_keytrack = kt[i][1] + (note - lower) * (kt[i + 1][1] - kt[i][1]) / (upper - lower);
788 break;
791 fm_amp.set(fm_keytrack * (1.0f + (vel - 127) * parameters->percussion_vel2fm / 127.0));
794 char *organ_audio_module::configure(const char *key, const char *value)
796 if (!strcmp(key, "map_curve"))
798 var_map_curve = value;
799 stringstream ss(value);
800 int i = 0;
801 float x = 0, y = 1;
802 if (*value)
804 int points;
805 ss >> points;
806 for (i = 0; i < points; i++)
808 static const int whites[] = { 0, 2, 4, 5, 7, 9, 11 };
809 ss >> x >> y;
810 int wkey = (int)(x * 71);
811 x = whites[wkey % 7] + 12 * (wkey / 7);
812 parameters->percussion_keytrack[i][0] = x;
813 parameters->percussion_keytrack[i][1] = y;
814 // cout << "(" << x << ", " << y << ")" << endl;
817 // pad with constant Y
818 for (; i < ORGAN_KEYTRACK_POINTS; i++) {
819 parameters->percussion_keytrack[i][0] = x;
820 parameters->percussion_keytrack[i][1] = y;
822 return NULL;
824 cout << "Set unknown configure value " << key << " to " << value << endl;
825 return NULL;
828 void organ_audio_module::send_configures(send_configure_iface *sci)
830 sci->send_configure("map_curve", var_map_curve.c_str());
833 void organ_audio_module::deactivate()
838 void drawbar_organ::render_separate(float *output[], int nsamples)
840 float buf[4096][2];
841 dsp::zero(&buf[0][0], 2 * nsamples);
842 basic_synth::render_to(buf, nsamples);
843 if (dsp::fastf2i_drm(parameters->lfo_mode) == organ_voice_base::lfomode_global)
845 for (int i = 0; i < nsamples; i += 64)
846 global_vibrato.process(parameters, buf + i, std::min(64, nsamples - i), sample_rate);
848 if (percussion.get_active())
849 percussion.render_percussion_to(buf, nsamples);
850 float gain = parameters->master * (1.0 / 8);
851 eq_l.set(parameters->bass_freq, parameters->bass_gain, parameters->treble_freq, parameters->treble_gain, sample_rate);
852 eq_r.copy_coeffs(eq_l);
853 for (int i=0; i<nsamples; i++) {
854 output[0][i] = gain*eq_l.process(buf[i][0]);
855 output[1][i] = gain*eq_r.process(buf[i][1]);
857 eq_l.sanitize();
858 eq_r.sanitize();