+ GUI: remove more unused control pointers
[calf.git] / src / organ.cpp
blob91f55c8166588070a5dd1368b7dc4ec3f9a10bb8
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., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, 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 blDest.compute_waveform(tmp.data());
178 normalize_waveform(tmp.data(), ORGAN_BIG_WAVE_SIZE);
179 blDest.compute_spectrum(tmp.data());
181 // limit is 1/2 of the number of harmonics of the original wave
182 result.make_from_spectrum(blDest, foldover, ORGAN_WAVE_SIZE >> (1 + ORGAN_BIG_WAVE_SHIFT));
183 memcpy(result.original, result.begin()->second, sizeof(result.original));
184 #if 0
185 blDest.compute_waveform(result);
186 normalize_waveform(result, ORGAN_BIG_WAVE_SIZE);
187 result[ORGAN_BIG_WAVE_SIZE] = result[0];
188 for (int i =0 ; i<ORGAN_BIG_WAVE_SIZE + 1; i++)
189 printf("%f\n", result[i]);
190 #endif
193 #define LARGE_WAVEFORM_PROGRESS() do { if (reporter) { progress += 100; reporter->report_progress(floor(progress / totalwaves), "Precalculating large waveforms"); } } while(0)
196 void organ_voice_base::precalculate_waves(progress_report_iface *reporter)
198 static bool inited = false;
199 if (!inited)
201 static organ_voice_base::small_wave_family waves[organ_voice_base::wave_count_small];
202 static organ_voice_base::big_wave_family big_waves[organ_voice_base::wave_count_big];
203 organ_voice_base::waves = &waves;
204 organ_voice_base::big_waves = &big_waves;
206 float progress = 0.0;
207 int totalwaves = 1 + wave_count_big;
208 if (reporter)
209 reporter->report_progress(0, "Precalculating small waveforms");
210 float tmp[ORGAN_WAVE_SIZE];
211 static bandlimiter<ORGAN_WAVE_BITS> bl;
212 static bandlimiter<ORGAN_BIG_WAVE_BITS> blBig;
213 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
214 tmp[i] = sin(i * 2 * M_PI / ORGAN_WAVE_SIZE);
215 waves[wave_sine].make(bl, tmp);
216 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
217 tmp[i] = (i < (ORGAN_WAVE_SIZE / 16)) ? 1 : 0;
218 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
219 waves[wave_pulse].make(bl, tmp);
220 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
221 tmp[i] = i < (ORGAN_WAVE_SIZE / 2) ? sin(i * 2 * 2 * M_PI / ORGAN_WAVE_SIZE) : 0;
222 waves[wave_sinepl1].make(bl, tmp);
223 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
224 tmp[i] = i < (ORGAN_WAVE_SIZE / 3) ? sin(i * 3 * 2 * M_PI / ORGAN_WAVE_SIZE) : 0;
225 waves[wave_sinepl2].make(bl, tmp);
226 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
227 tmp[i] = i < (ORGAN_WAVE_SIZE / 4) ? sin(i * 4 * 2 * M_PI / ORGAN_WAVE_SIZE) : 0;
228 waves[wave_sinepl3].make(bl, tmp);
229 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
230 tmp[i] = (i < (ORGAN_WAVE_SIZE / 2)) ? 1 : -1;
231 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
232 waves[wave_sqr].make(bl, tmp);
233 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
234 tmp[i] = -1 + (i * 2.0 / ORGAN_WAVE_SIZE);
235 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
236 waves[wave_saw].make(bl, tmp);
238 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
239 tmp[i] = (i < (ORGAN_WAVE_SIZE / 2)) ? 1 : -1;
240 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
241 smoothen(bl, tmp);
242 waves[wave_ssqr].make(bl, tmp);
244 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
245 tmp[i] = -1 + (i * 2.0 / ORGAN_WAVE_SIZE);
246 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
247 smoothen(bl, tmp);
248 waves[wave_ssaw].make(bl, tmp);
250 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
251 tmp[i] = (i < (ORGAN_WAVE_SIZE / 16)) ? 1 : 0;
252 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
253 smoothen(bl, tmp);
254 waves[wave_spls].make(bl, tmp);
255 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
256 tmp[i] = i < (ORGAN_WAVE_SIZE / 1.5) ? sin(i * 1.5 * 2 * M_PI / ORGAN_WAVE_SIZE) : 0;
257 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
258 waves[wave_sinepl05].make(bl, tmp);
259 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
260 tmp[i] = i < (ORGAN_WAVE_SIZE / 1.5) ? (i < ORGAN_WAVE_SIZE / 3 ? -1 : +1) : 0;
261 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
262 waves[wave_sqr05].make(bl, tmp);
263 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
264 tmp[i] = sin(i * M_PI / ORGAN_WAVE_SIZE);
265 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
266 waves[wave_halfsin].make(bl, tmp);
267 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
268 tmp[i] = sin(i * 3 * M_PI / ORGAN_WAVE_SIZE);
269 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
270 waves[wave_clvg].make(bl, tmp);
271 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
273 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
274 float fm = 0.3 * sin(6*ph) + 0.2 * sin(11*ph) + 0.2 * cos(17*ph) - 0.2 * cos(19*ph);
275 tmp[i] = sin(5*ph + fm) + 0.7 * cos(7*ph - fm);
277 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
278 waves[wave_bell].make(bl, tmp, true);
279 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
281 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
282 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);
283 tmp[i] = sin(3*ph + fm) + cos(7*ph - fm);
285 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
286 waves[wave_bell2].make(bl, tmp, true);
287 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
289 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
290 float fm = 0.5 * sin(3*ph) + 0.3 * sin(5*ph) + 0.3 * cos(6*ph) - 0.3 * cos(9*ph);
291 tmp[i] = sin(4*ph + fm) + cos(ph - fm);
293 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
294 waves[wave_w1].make(bl, tmp);
295 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
297 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
298 tmp[i] = sin(ph) * sin(2 * ph) * sin(4 * ph) * sin(8 * ph);
300 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
301 waves[wave_w2].make(bl, tmp);
302 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
304 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
305 tmp[i] = sin(ph) * sin(3 * ph) * sin(5 * ph) * sin(7 * ph) * sin(9 * ph);
307 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
308 waves[wave_w3].make(bl, tmp);
309 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
311 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
312 tmp[i] = sin(ph + 2 * sin(ph + 2 * sin(ph)));
314 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
315 waves[wave_w4].make(bl, tmp);
316 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
318 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
319 tmp[i] = ph * sin(ph);
321 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
322 waves[wave_w5].make(bl, tmp);
323 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
325 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
326 tmp[i] = ph * sin(ph) + (2 * M_PI - ph) * sin(2 * ph);
328 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
329 waves[wave_w6].make(bl, tmp);
330 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
332 float ph = i * 1.0 / ORGAN_WAVE_SIZE;
333 tmp[i] = exp(-ph * ph);
335 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
336 waves[wave_w7].make(bl, tmp);
337 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
339 float ph = i * 1.0 / ORGAN_WAVE_SIZE;
340 tmp[i] = exp(-ph * sin(2 * M_PI * ph));
342 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
343 waves[wave_w8].make(bl, tmp);
344 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
346 float ph = i * 1.0 / ORGAN_WAVE_SIZE;
347 tmp[i] = sin(2 * M_PI * ph * ph);
349 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
350 waves[wave_w9].make(bl, tmp);
352 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
353 tmp[i] = -1 + (i * 2.0 / ORGAN_WAVE_SIZE);
354 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
355 phaseshift(bl, tmp);
356 waves[wave_dsaw].make(bl, tmp);
358 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
359 tmp[i] = (i < (ORGAN_WAVE_SIZE / 2)) ? 1 : -1;
360 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
361 phaseshift(bl, tmp);
362 waves[wave_dsqr].make(bl, tmp);
364 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
365 tmp[i] = (i < (ORGAN_WAVE_SIZE / 16)) ? 1 : 0;
366 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
367 phaseshift(bl, tmp);
368 waves[wave_dpls].make(bl, tmp);
370 LARGE_WAVEFORM_PROGRESS();
371 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
372 tmp[i] = -1 + (i * 2.0 / ORGAN_WAVE_SIZE);
373 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
374 bl.compute_spectrum(tmp);
375 padsynth(bl, blBig, big_waves[wave_strings - wave_count_small], 15);
377 LARGE_WAVEFORM_PROGRESS();
378 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
379 tmp[i] = -1 + (i * 2.0 / ORGAN_WAVE_SIZE);
380 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
381 bl.compute_spectrum(tmp);
382 padsynth(bl, blBig, big_waves[wave_strings2 - wave_count_small], 40);
384 LARGE_WAVEFORM_PROGRESS();
385 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
386 tmp[i] = sin(i * 2 * M_PI / ORGAN_WAVE_SIZE);
387 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
388 bl.compute_spectrum(tmp);
389 padsynth(bl, blBig, big_waves[wave_sinepad - wave_count_small], 20);
391 LARGE_WAVEFORM_PROGRESS();
392 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
394 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
395 float fm = 0.3 * sin(6*ph) + 0.2 * sin(11*ph) + 0.2 * cos(17*ph) - 0.2 * cos(19*ph);
396 tmp[i] = sin(5*ph + fm) + 0.7 * cos(7*ph - fm);
398 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
399 bl.compute_spectrum(tmp);
400 padsynth(bl, blBig, big_waves[wave_bellpad - wave_count_small], 30, 30, true);
402 LARGE_WAVEFORM_PROGRESS();
403 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
405 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
406 float fm = 0.3 * sin(3*ph) + 0.2 * sin(4*ph) + 0.2 * cos(5*ph) - 0.2 * cos(6*ph);
407 tmp[i] = sin(2*ph + fm) + 0.7 * cos(3*ph - fm);
409 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
410 bl.compute_spectrum(tmp);
411 padsynth(bl, blBig, big_waves[wave_space - wave_count_small], 30, 30);
413 LARGE_WAVEFORM_PROGRESS();
414 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
416 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
417 float fm = 0.5 * sin(ph) + 0.5 * sin(2*ph) + 0.5 * sin(3*ph);
418 tmp[i] = sin(ph + fm) + 0.5 * cos(7*ph - 2 * fm) + 0.25 * cos(13*ph - 4 * fm);
420 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
421 bl.compute_spectrum(tmp);
422 padsynth(bl, blBig, big_waves[wave_choir - wave_count_small], 50, 10);
424 LARGE_WAVEFORM_PROGRESS();
425 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
427 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
428 float fm = sin(ph) ;
429 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);
431 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
432 bl.compute_spectrum(tmp);
433 padsynth(bl, blBig, big_waves[wave_choir2 - wave_count_small], 50, 10);
435 LARGE_WAVEFORM_PROGRESS();
436 for (int i = 0; i < ORGAN_WAVE_SIZE; i++)
438 float ph = i * 2 * M_PI / ORGAN_WAVE_SIZE;
439 float fm = sin(ph) ;
440 tmp[i] = sin(ph + 4 * fm) + 0.5 * sin(2 * ph + 4 * ph);
442 normalize_waveform(tmp, ORGAN_WAVE_SIZE);
443 bl.compute_spectrum(tmp);
444 padsynth(bl, blBig, big_waves[wave_choir3 - wave_count_small], 50, 10);
445 LARGE_WAVEFORM_PROGRESS();
447 inited = true;
451 organ_voice_base::organ_voice_base(organ_parameters *_parameters, int &_sample_rate_ref, bool &_released_ref)
452 : parameters(_parameters)
453 , sample_rate_ref(_sample_rate_ref)
454 , released_ref(_released_ref)
456 note = -1;
459 void organ_voice_base::render_percussion_to(float (*buf)[2], int nsamples)
461 if (note == -1)
462 return;
464 if (!pamp.get_active())
465 return;
466 if (parameters->percussion_level < small_value<float>())
467 return;
468 float level = parameters->percussion_level * 9;
469 static float zeros[ORGAN_WAVE_SIZE];
470 // XXXKF the decay needs work!
471 double age_const = parameters->perc_decay_const;
472 double fm_age_const = parameters->perc_fm_decay_const;
473 int timbre = parameters->get_percussion_wave();
474 if (timbre < 0 || timbre >= wave_count_small)
475 return;
476 int timbre2 = parameters->get_percussion_fm_wave();
477 if (timbre2 < 0 || timbre2 >= wave_count_small)
478 timbre2 = wave_sine;
479 float *fmdata = (*waves)[timbre2].get_level(moddphase.get());
480 if (!fmdata)
481 fmdata = zeros;
482 float *data = (*waves)[timbre].get_level(dpphase.get());
483 if (!data) {
484 pamp.deactivate();
485 return;
487 float s = parameters->percussion_stereo * ORGAN_WAVE_SIZE * (0.5 / 360.0);
488 for (int i = 0; i < nsamples; i++) {
489 float fm = wave(fmdata, modphase);
490 fm *= ORGAN_WAVE_SIZE * parameters->percussion_fm_depth * fm_amp.get();
491 modphase += moddphase;
492 fm_amp.age_exp(fm_age_const, 1.0 / 32768.0);
494 float lamp = level * pamp.get();
495 buf[i][0] += lamp * wave(data, pphase + dsp::fixed_point<int64_t, 52>(fm - s));
496 buf[i][1] += lamp * wave(data, pphase + dsp::fixed_point<int64_t, 52>(fm + s));
497 if (released_ref)
498 pamp.age_lin(rel_age_const,0.0);
499 else
500 pamp.age_exp(age_const, 1.0 / 32768.0);
501 pphase += dpphase;
505 void organ_vibrato::reset()
507 for (int i = 0; i < VibratoSize; i++)
508 vibrato_x1[i][0] = vibrato_y1[i][0] = vibrato_x1[i][1] = vibrato_y1[i][1] = 0.f;
509 vibrato[0].a0 = vibrato[1].a0 = 0;
510 lfo_phase = 0.f;
513 void organ_vibrato::process(organ_parameters *parameters, float (*data)[2], unsigned int len, float sample_rate)
515 float lfo1 = lfo_phase < 0.5 ? 2 * lfo_phase : 2 - 2 * lfo_phase;
516 float lfo_phase2 = lfo_phase + parameters->lfo_phase * (1.0 / 360.0);
517 if (lfo_phase2 >= 1.0)
518 lfo_phase2 -= 1.0;
519 float lfo2 = lfo_phase2 < 0.5 ? 2 * lfo_phase2 : 2 - 2 * lfo_phase2;
520 lfo_phase += parameters->lfo_rate * len / sample_rate;
521 if (lfo_phase >= 1.0)
522 lfo_phase -= 1.0;
523 if (!len)
524 return;
525 float olda0[2] = {vibrato[0].a0, vibrato[1].a0};
526 vibrato[0].set_ap(3000 + 7000 * parameters->lfo_amt * lfo1 * lfo1, sample_rate);
527 vibrato[1].set_ap(3000 + 7000 * parameters->lfo_amt * lfo2 * lfo2, sample_rate);
528 float ilen = 1.0 / len;
529 float deltaa0[2] = {(vibrato[0].a0 - olda0[0])*ilen, (vibrato[1].a0 - olda0[1])*ilen};
531 float vib_wet = parameters->lfo_wet;
532 for (int c = 0; c < 2; c++)
534 for (unsigned int i = 0; i < len; i++)
536 float v = data[i][c];
537 float v0 = v;
538 float coeff = olda0[c] + deltaa0[c] * i;
539 for (int t = 0; t < VibratoSize; t++)
540 v = vibrato[c].process_ap(v, vibrato_x1[t][c], vibrato_y1[t][c], coeff);
542 data[i][c] += (v - v0) * vib_wet;
544 for (int t = 0; t < VibratoSize; t++)
546 sanitize(vibrato_x1[t][c]);
547 sanitize(vibrato_y1[t][c]);
552 void organ_voice::update_pitch()
554 organ_voice_base::update_pitch();
555 dphase.set(dsp::midi_note_to_phase(note, 100 * parameters->global_transpose + parameters->global_detune, sample_rate) * inertia_pitchbend.get_last());
558 void organ_voice::render_block() {
559 if (note == -1)
560 return;
562 dsp::zero(&output_buffer[0][0], Channels * BlockSize);
563 dsp::zero(&aux_buffers[1][0][0], 2 * Channels * BlockSize);
564 if (!amp.get_active())
566 if (use_percussion())
567 render_percussion_to(output_buffer, BlockSize);
568 return;
571 inertia_pitchbend.set_inertia(parameters->pitch_bend);
572 inertia_pitchbend.step();
573 update_pitch();
574 dsp::fixed_point<int, 20> tphase, tdphase;
575 unsigned int foldvalue = parameters->foldvalue * inertia_pitchbend.get_last();
576 int vibrato_mode = fastf2i_drm(parameters->lfo_mode);
577 for (int h = 0; h < 9; h++)
579 float amp = parameters->drawbars[h];
580 if (amp < small_value<float>())
581 continue;
582 float *data;
583 dsp::fixed_point<int, 24> hm = dsp::fixed_point<int, 24>(parameters->multiplier[h]);
584 int waveid = (int)parameters->waveforms[h];
585 if (waveid < 0 || waveid >= wave_count)
586 waveid = 0;
588 uint32_t rate = (dphase * hm).get();
589 if (waveid >= wave_count_small)
591 float *data = (*big_waves)[waveid - wave_count_small].get_level(rate >> (ORGAN_BIG_WAVE_BITS - ORGAN_WAVE_BITS + ORGAN_BIG_WAVE_SHIFT));
592 if (!data)
593 continue;
594 hm.set(hm.get() >> ORGAN_BIG_WAVE_SHIFT);
595 dsp::fixed_point<int64_t, 20> tphase, tdphase;
596 tphase.set(((phase * hm).get()) + parameters->phaseshift[h]);
597 tdphase.set(rate >> ORGAN_BIG_WAVE_SHIFT);
598 float ampl = amp * 0.5f * (1 - parameters->pan[h]);
599 float ampr = amp * 0.5f * (1 + parameters->pan[h]);
600 float (*out)[Channels] = aux_buffers[dsp::fastf2i_drm(parameters->routing[h])];
602 for (int i=0; i < (int)BlockSize; i++) {
603 float wv = big_wave(data, tphase);
604 out[i][0] += wv * ampl;
605 out[i][1] += wv * ampr;
606 tphase += tdphase;
609 else
611 unsigned int foldback = 0;
612 while (rate > foldvalue)
614 rate >>= 1;
615 foldback++;
617 hm.set(hm.get() >> foldback);
618 data = (*waves)[waveid].get_level(rate);
619 if (!data)
620 continue;
621 tphase.set((uint32_t)((phase * hm).get()) + parameters->phaseshift[h]);
622 tdphase.set((uint32_t)rate);
623 float ampl = amp * 0.5f * (1 - parameters->pan[h]);
624 float ampr = amp * 0.5f * (1 + parameters->pan[h]);
625 float (*out)[Channels] = aux_buffers[dsp::fastf2i_drm(parameters->routing[h])];
627 for (int i=0; i < (int)BlockSize; i++) {
628 float wv = wave(data, tphase);
629 out[i][0] += wv * ampl;
630 out[i][1] += wv * ampr;
631 tphase += tdphase;
636 bool is_quad = parameters->quad_env >= 0.5f;
638 expression.set_inertia(parameters->cutoff);
639 phase += dphase * BlockSize;
640 float escl[EnvCount], eval[EnvCount];
641 for (int i = 0; i < EnvCount; i++)
642 escl[i] = (1.f + parameters->envs[i].velscale * (velocity - 1.f));
644 if (is_quad)
646 for (int i = 0; i < EnvCount; i++)
647 eval[i] = envs[i].value * envs[i].value * escl[i];
649 else
651 for (int i = 0; i < EnvCount; i++)
652 eval[i] = envs[i].value * escl[i];
654 for (int i = 0; i < FilterCount; i++)
656 float mod = parameters->filters[i].envmod[0] * eval[0] ;
657 mod += parameters->filters[i].keyf * 100 * (note - 60);
658 for (int j = 1; j < EnvCount; j++)
660 mod += parameters->filters[i].envmod[j] * eval[j];
662 if (i) mod += expression.get() * 1200 * 4;
663 float fc = parameters->filters[i].cutoff * pow(2.0f, mod * (1.f / 1200.f));
664 filterL[i].set_lp_rbj(dsp::clip<float>(fc, 10, 18000), parameters->filters[i].resonance, sample_rate);
665 filterR[i].copy_coeffs(filterL[i]);
667 float amp_pre[ampctl_count - 1], amp_post[ampctl_count - 1];
668 for (int i = 0; i < ampctl_count - 1; i++)
670 amp_pre[i] = 1.f;
671 amp_post[i] = 1.f;
673 bool any_running = false;
674 for (int i = 0; i < EnvCount; i++)
676 float pre = eval[i];
677 envs[i].advance();
678 int mode = fastf2i_drm(parameters->envs[i].ampctl);
679 if (!envs[i].stopped())
680 any_running = true;
681 if (mode == ampctl_none)
682 continue;
683 float post = (is_quad ? envs[i].value : 1) * envs[i].value * escl[i];
684 amp_pre[mode - 1] *= pre;
685 amp_post[mode - 1] *= post;
687 if (vibrato_mode >= lfomode_direct && vibrato_mode <= lfomode_filter2)
688 vibrato.process(parameters, aux_buffers[vibrato_mode - lfomode_direct], BlockSize, sample_rate);
689 if (!any_running)
690 finishing = true;
691 // calculate delta from pre and post
692 for (int i = 0; i < ampctl_count - 1; i++)
693 amp_post[i] = (amp_post[i] - amp_pre[i]) * (1.0 / BlockSize);
694 float a0 = amp_pre[0], a1 = amp_pre[1], a2 = amp_pre[2], a3 = amp_pre[3];
695 float d0 = amp_post[0], d1 = amp_post[1], d2 = amp_post[2], d3 = amp_post[3];
696 if (parameters->filter_chain >= 0.5f)
698 for (int i=0; i < (int) BlockSize; i++) {
699 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]));
700 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]));
701 a0 += d0, a1 += d1, a2 += d2, a3 += d3;
704 else
706 for (int i=0; i < (int) BlockSize; i++) {
707 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]));
708 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]));
709 a0 += d0, a1 += d1, a2 += d2, a3 += d3;
712 filterL[0].sanitize();
713 filterR[0].sanitize();
714 filterL[1].sanitize();
715 filterR[1].sanitize();
716 if (vibrato_mode == lfomode_voice)
717 vibrato.process(parameters, output_buffer, BlockSize, sample_rate);
719 if (finishing)
721 for (int i = 0; i < (int) BlockSize; i++) {
722 output_buffer[i][0] *= amp.get();
723 output_buffer[i][1] *= amp.get();
724 amp.age_lin((1.0/44100.0)/0.03,0.0);
728 if (use_percussion())
729 render_percussion_to(output_buffer, BlockSize);
732 void drawbar_organ::update_params()
734 parameters->perc_decay_const = dsp::decay::calc_exp_constant(1.0 / 1024.0, 0.001 * parameters->percussion_time * sample_rate);
735 parameters->perc_fm_decay_const = dsp::decay::calc_exp_constant(1.0 / 1024.0, 0.001 * parameters->percussion_fm_time * sample_rate);
736 for (int i = 0; i < 9; i++)
738 parameters->multiplier[i] = parameters->harmonics[i] * pow(2.0, parameters->detune[i] * (1.0 / 1200.0));
739 parameters->phaseshift[i] = int(parameters->phase[i] * 65536 / 360) << 16;
741 double dphase = dsp::midi_note_to_phase((int)parameters->foldover, 0, sample_rate);
742 parameters->foldvalue = (int)(dphase);
745 void drawbar_organ::pitch_bend(int amt)
747 parameters->pitch_bend = pow(2.0, (amt * parameters->pitch_bend_range) / (1200.0 * 8192.0));
748 for (list<voice *>::iterator i = active_voices.begin(); i != active_voices.end(); i++)
750 organ_voice *v = dynamic_cast<organ_voice *>(*i);
751 v->update_pitch();
753 percussion.update_pitch();
756 void organ_audio_module::execute(int cmd_no)
758 switch(cmd_no)
760 case 0:
761 panic_flag = true;
762 break;
766 void organ_voice_base::perc_note_on(int note, int vel)
768 perc_reset();
769 released_ref = false;
770 this->note = note;
771 if (parameters->percussion_level > 0)
772 pamp.set(1.0f + (vel - 127) * parameters->percussion_vel2amp / 127.0);
773 update_pitch();
774 float (*kt)[2] = parameters->percussion_keytrack;
775 // assume last point (will be put there by padding)
776 fm_keytrack = kt[ORGAN_KEYTRACK_POINTS - 1][1];
777 // yes binary search would be nice if we had more than those crappy 4 points
778 for (int i = 0; i < ORGAN_KEYTRACK_POINTS - 1; i++)
780 float &lower = kt[i][0], upper = kt[i + 1][0];
781 if (note >= lower && note < upper)
783 fm_keytrack = kt[i][1] + (note - lower) * (kt[i + 1][1] - kt[i][1]) / (upper - lower);
784 break;
787 fm_amp.set(fm_keytrack * (1.0f + (vel - 127) * parameters->percussion_vel2fm / 127.0));
790 char *organ_audio_module::configure(const char *key, const char *value)
792 if (!strcmp(key, "map_curve"))
794 var_map_curve = value;
795 stringstream ss(value);
796 int i = 0;
797 float x = 0, y = 1;
798 if (*value)
800 int points;
801 ss >> points;
802 for (i = 0; i < points; i++)
804 static const int whites[] = { 0, 2, 4, 5, 7, 9, 11 };
805 ss >> x >> y;
806 int wkey = (int)(x * 71);
807 x = whites[wkey % 7] + 12 * (wkey / 7);
808 parameters->percussion_keytrack[i][0] = x;
809 parameters->percussion_keytrack[i][1] = y;
810 // cout << "(" << x << ", " << y << ")" << endl;
813 // pad with constant Y
814 for (; i < ORGAN_KEYTRACK_POINTS; i++) {
815 parameters->percussion_keytrack[i][0] = x;
816 parameters->percussion_keytrack[i][1] = y;
818 return NULL;
820 cout << "Set unknown configure value " << key << " to " << value << endl;
821 return NULL;
824 void organ_audio_module::send_configures(send_configure_iface *sci)
826 sci->send_configure("map_curve", var_map_curve.c_str());
829 void organ_audio_module::deactivate()