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.
27 #include <jack/jack.h>
29 #include <calf/giface.h>
30 #include <calf/modules_synths.h>
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
);
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;
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
++)
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);
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));
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);
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
);
145 amp
*= (ORGAN_BIG_WAVE_SIZE
/ ORGAN_WAVE_SIZE
);
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)
158 int pos2
= orig
- dist
;
159 if (pos2
< 1 || pos2
>= ORGAN_BIG_WAVE_SIZE
/ 2)
161 blDest
.spectrum
[pos
] += val
;
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
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
));
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
]);
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;
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
;
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
);
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
);
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
);
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
);
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
);
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
);
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
;
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
;
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();
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
)
459 void organ_voice_base::render_percussion_to(float (*buf
)[2], int nsamples
)
464 if (!pamp
.get_active())
466 if (parameters
->percussion_level
< small_value
<float>())
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
)
476 int timbre2
= parameters
->get_percussion_fm_wave();
477 if (timbre2
< 0 || timbre2
>= wave_count_small
)
479 float *fmdata
= (*waves
)[timbre2
].get_level(moddphase
.get());
482 float *data
= (*waves
)[timbre
].get_level(dpphase
.get());
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
));
498 pamp
.age_lin(rel_age_const
,0.0);
500 pamp
.age_exp(age_const
, 1.0 / 32768.0);
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
;
512 void organ_vibrato::process(organ_parameters
*parameters
, float (*data
)[2], unsigned int len
, float sample_rate
)
514 float lfo1
= lfo_phase
< 0.5 ? 2 * lfo_phase
: 2 - 2 * lfo_phase
;
515 float lfo_phase2
= lfo_phase
+ parameters
->lfo_phase
* (1.0 / 360.0);
516 if (lfo_phase2
>= 1.0)
518 float lfo2
= lfo_phase2
< 0.5 ? 2 * lfo_phase2
: 2 - 2 * lfo_phase2
;
519 lfo_phase
+= parameters
->lfo_rate
* len
/ sample_rate
;
520 if (lfo_phase
>= 1.0)
524 float olda0
[2] = {vibrato
[0].a0
, vibrato
[1].a0
};
525 vibrato
[0].set_ap(3000 + 7000 * parameters
->lfo_amt
* lfo1
* lfo1
, sample_rate
);
526 vibrato
[1].set_ap(3000 + 7000 * parameters
->lfo_amt
* lfo2
* lfo2
, sample_rate
);
527 float ilen
= 1.0 / len
;
528 float deltaa0
[2] = {(vibrato
[0].a0
- olda0
[0])*ilen
, (vibrato
[1].a0
- olda0
[1])*ilen
};
530 float vib_wet
= parameters
->lfo_wet
;
531 for (int c
= 0; c
< 2; c
++)
533 for (unsigned int i
= 0; i
< len
; i
++)
535 float v
= data
[i
][c
];
537 float coeff
= olda0
[c
] + deltaa0
[c
] * i
;
538 for (int t
= 0; t
< VibratoSize
; t
++)
539 v
= vibrato
[c
].process_ap(v
, vibrato_x1
[t
][c
], vibrato_y1
[t
][c
], coeff
);
541 data
[i
][c
] += (v
- v0
) * vib_wet
;
543 for (int t
= 0; t
< VibratoSize
; t
++)
545 sanitize(vibrato_x1
[t
][c
]);
546 sanitize(vibrato_y1
[t
][c
]);
551 void organ_voice::update_pitch()
553 organ_voice_base::update_pitch();
554 dphase
.set(dsp::midi_note_to_phase(note
, 100 * parameters
->global_transpose
+ parameters
->global_detune
, sample_rate
) * parameters
->pitch_bend
);
557 void organ_voice::render_block() {
561 dsp::zero(&output_buffer
[0][0], Channels
* BlockSize
);
562 dsp::zero(&aux_buffers
[1][0][0], 2 * Channels
* BlockSize
);
563 if (!amp
.get_active())
565 if (use_percussion())
566 render_percussion_to(output_buffer
, BlockSize
);
571 dsp::fixed_point
<int, 20> tphase
, tdphase
;
572 unsigned int foldvalue
= parameters
->foldvalue
;
573 int vibrato_mode
= fastf2i_drm(parameters
->lfo_mode
);
574 for (int h
= 0; h
< 9; h
++)
576 float amp
= parameters
->drawbars
[h
];
577 if (amp
< small_value
<float>())
580 dsp::fixed_point
<int, 24> hm
= dsp::fixed_point
<int, 24>(parameters
->multiplier
[h
]);
581 int waveid
= (int)parameters
->waveforms
[h
];
582 if (waveid
< 0 || waveid
>= wave_count
)
585 uint32_t rate
= (dphase
* hm
).get();
586 if (waveid
>= wave_count_small
)
588 float *data
= (*big_waves
)[waveid
- wave_count_small
].get_level(rate
>> (ORGAN_BIG_WAVE_BITS
- ORGAN_WAVE_BITS
+ ORGAN_BIG_WAVE_SHIFT
));
591 hm
.set(hm
.get() >> ORGAN_BIG_WAVE_SHIFT
);
592 dsp::fixed_point
<int64_t, 20> tphase
, tdphase
;
593 tphase
.set(((phase
* hm
).get()) + parameters
->phaseshift
[h
]);
594 tdphase
.set(rate
>> ORGAN_BIG_WAVE_SHIFT
);
595 float ampl
= amp
* 0.5f
* (1 - parameters
->pan
[h
]);
596 float ampr
= amp
* 0.5f
* (1 + parameters
->pan
[h
]);
597 float (*out
)[Channels
] = aux_buffers
[dsp::fastf2i_drm(parameters
->routing
[h
])];
599 for (int i
=0; i
< (int)BlockSize
; i
++) {
600 float wv
= big_wave(data
, tphase
);
601 out
[i
][0] += wv
* ampl
;
602 out
[i
][1] += wv
* ampr
;
608 unsigned int foldback
= 0;
609 while (rate
> foldvalue
)
614 hm
.set(hm
.get() >> foldback
);
615 data
= (*waves
)[waveid
].get_level(rate
);
618 tphase
.set((uint32_t)((phase
* hm
).get()) + parameters
->phaseshift
[h
]);
619 tdphase
.set((uint32_t)rate
);
620 float ampl
= amp
* 0.5f
* (1 - parameters
->pan
[h
]);
621 float ampr
= amp
* 0.5f
* (1 + parameters
->pan
[h
]);
622 float (*out
)[Channels
] = aux_buffers
[dsp::fastf2i_drm(parameters
->routing
[h
])];
624 for (int i
=0; i
< (int)BlockSize
; i
++) {
625 float wv
= wave(data
, tphase
);
626 out
[i
][0] += wv
* ampl
;
627 out
[i
][1] += wv
* ampr
;
632 expression
.set_inertia(parameters
->cutoff
);
633 phase
+= dphase
* BlockSize
;
634 float escl
[EnvCount
], eval
[EnvCount
];
635 for (int i
= 0; i
< EnvCount
; i
++)
636 escl
[i
] = (1.f
+ parameters
->envs
[i
].velscale
* (velocity
- 1.f
));
638 for (int i
= 0; i
< EnvCount
; i
++)
639 eval
[i
] = envs
[i
].value
* escl
[i
];
640 for (int i
= 0; i
< FilterCount
; i
++)
642 float mod
= parameters
->filters
[i
].envmod
[0] * eval
[0] ;
643 mod
+= parameters
->filters
[i
].keyf
* 100 * (note
- 60);
644 for (int j
= 1; j
< EnvCount
; j
++)
646 mod
+= parameters
->filters
[i
].envmod
[j
] * eval
[j
];
648 if (i
) mod
+= expression
.get() * 1200 * 4;
649 float fc
= parameters
->filters
[i
].cutoff
* pow(2.0f
, mod
* (1.f
/ 1200.f
));
650 filterL
[i
].set_lp_rbj(dsp::clip
<float>(fc
, 10, 18000), parameters
->filters
[i
].resonance
, sample_rate
);
651 filterR
[i
].copy_coeffs(filterL
[i
]);
653 float amp_pre
[ampctl_count
- 1], amp_post
[ampctl_count
- 1];
654 for (int i
= 0; i
< ampctl_count
- 1; i
++)
659 bool any_running
= false;
660 for (int i
= 0; i
< EnvCount
; i
++)
664 int mode
= fastf2i_drm(parameters
->envs
[i
].ampctl
);
665 if (!envs
[i
].stopped())
667 if (mode
== ampctl_none
)
669 float post
= envs
[i
].value
* escl
[i
];
670 amp_pre
[mode
- 1] *= pre
;
671 amp_post
[mode
- 1] *= post
;
673 if (vibrato_mode
>= lfomode_direct
&& vibrato_mode
<= lfomode_filter2
)
674 vibrato
.process(parameters
, aux_buffers
[vibrato_mode
- lfomode_direct
], BlockSize
, sample_rate
);
677 // calculate delta from pre and post
678 for (int i
= 0; i
< ampctl_count
- 1; i
++)
679 amp_post
[i
] = (amp_post
[i
] - amp_pre
[i
]) * (1.0 / BlockSize
);
680 float a0
= amp_pre
[0], a1
= amp_pre
[1], a2
= amp_pre
[2], a3
= amp_pre
[3];
681 float d0
= amp_post
[0], d1
= amp_post
[1], d2
= amp_post
[2], d3
= amp_post
[3];
682 if (parameters
->filter_chain
>= 0.5f
)
684 for (int i
=0; i
< (int) BlockSize
; i
++) {
685 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]));
686 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]));
687 a0
+= d0
, a1
+= d1
, a2
+= d2
, a3
+= d3
;
692 for (int i
=0; i
< (int) BlockSize
; i
++) {
693 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]));
694 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]));
695 a0
+= d0
, a1
+= d1
, a2
+= d2
, a3
+= d3
;
698 filterL
[0].sanitize();
699 filterR
[0].sanitize();
700 filterL
[1].sanitize();
701 filterR
[1].sanitize();
702 if (vibrato_mode
== lfomode_voice
)
703 vibrato
.process(parameters
, output_buffer
, BlockSize
, sample_rate
);
707 for (int i
= 0; i
< (int) BlockSize
; i
++) {
708 output_buffer
[i
][0] *= amp
.get();
709 output_buffer
[i
][1] *= amp
.get();
710 amp
.age_lin((1.0/44100.0)/0.03,0.0);
714 if (use_percussion())
715 render_percussion_to(output_buffer
, BlockSize
);
718 void drawbar_organ::update_params()
720 parameters
->perc_decay_const
= dsp::decay::calc_exp_constant(1.0 / 1024.0, 0.001 * parameters
->percussion_time
* sample_rate
);
721 parameters
->perc_fm_decay_const
= dsp::decay::calc_exp_constant(1.0 / 1024.0, 0.001 * parameters
->percussion_fm_time
* sample_rate
);
722 for (int i
= 0; i
< 9; i
++)
724 parameters
->multiplier
[i
] = parameters
->harmonics
[i
] * pow(2.0, parameters
->detune
[i
] * (1.0 / 1200.0));
725 parameters
->phaseshift
[i
] = int(parameters
->phase
[i
] * 65536 / 360) << 16;
727 double dphase
= dsp::midi_note_to_phase((int)parameters
->foldover
, 0, sample_rate
);
728 parameters
->foldvalue
= (int)(dphase
);
731 void drawbar_organ::pitch_bend(int amt
)
733 parameters
->pitch_bend
= pow(2.0, amt
* 2 / (12.0 * 8192));
734 for (list
<voice
*>::iterator i
= active_voices
.begin(); i
!= active_voices
.end(); i
++)
736 organ_voice
*v
= dynamic_cast<organ_voice
*>(*i
);
739 percussion
.update_pitch();
742 void organ_audio_module::execute(int cmd_no
)
752 void organ_voice_base::perc_note_on(int note
, int vel
)
755 released_ref
= false;
757 pamp
.set(1.0f
+ (vel
- 127) * parameters
->percussion_vel2amp
/ 127.0);
759 float (*kt
)[2] = parameters
->percussion_keytrack
;
760 // assume last point (will be put there by padding)
761 fm_keytrack
= kt
[ORGAN_KEYTRACK_POINTS
- 1][1];
762 // yes binary search would be nice if we had more than those crappy 4 points
763 for (int i
= 0; i
< ORGAN_KEYTRACK_POINTS
- 1; i
++)
765 float &lower
= kt
[i
][0], upper
= kt
[i
+ 1][0];
766 if (note
>= lower
&& note
< upper
)
768 fm_keytrack
= kt
[i
][1] + (note
- lower
) * (kt
[i
+ 1][1] - kt
[i
][1]) / (upper
- lower
);
772 fm_amp
.set(fm_keytrack
* (1.0f
+ (vel
- 127) * parameters
->percussion_vel2fm
/ 127.0));
775 char *organ_audio_module::configure(const char *key
, const char *value
)
777 if (!strcmp(key
, "map_curve"))
779 var_map_curve
= value
;
780 stringstream
ss(value
);
787 for (i
= 0; i
< points
; i
++)
789 static const int whites
[] = { 0, 2, 4, 5, 7, 9, 11 };
791 int wkey
= (int)(x
* 71);
792 x
= whites
[wkey
% 7] + 12 * (wkey
/ 7);
793 parameters
->percussion_keytrack
[i
][0] = x
;
794 parameters
->percussion_keytrack
[i
][1] = y
;
795 // cout << "(" << x << ", " << y << ")" << endl;
798 // pad with constant Y
799 for (; i
< ORGAN_KEYTRACK_POINTS
; i
++) {
800 parameters
->percussion_keytrack
[i
][0] = x
;
801 parameters
->percussion_keytrack
[i
][1] = y
;
805 cout
<< "Set unknown configure value " << key
<< " to " << value
<< endl
;
809 void organ_audio_module::send_configures(send_configure_iface
*sci
)
811 sci
->send_configure("map_curve", var_map_curve
.c_str());
814 void organ_audio_module::deactivate()