+ AutoHell: print the dependency detection result for the GUI code shared between...
[calf.git] / src / wavetable.cpp
blob2da79933210e84b53728393021dd155b80553122
1 /* Calf DSP Library
2 * Example audio modules - wavetable synthesizer
4 * Copyright (C) 2009 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 #if ENABLE_EXPERIMENTAL
25 #include <assert.h>
26 #include <memory.h>
27 #include <complex>
28 #if USE_JACK
29 #include <jack/jack.h>
30 #endif
31 #include <calf/giface.h>
32 #include <calf/modules_synths.h>
33 #include <iostream>
35 using namespace dsp;
36 using namespace calf_plugins;
38 static const char *mod_src_names[] = {
39 "None",
40 "Velocity",
41 "Pressure",
42 "ModWheel",
43 "Env 1",
44 "Env 2",
45 "Env 3",
46 NULL
49 static const char *mod_dest_names[] = {
50 "None",
51 "Attenuation",
52 "Osc Mix Ratio (%)",
53 "Cutoff [ct]",
54 "Resonance",
55 "O1: Shift (%)",
56 "O2: Shift (%)",
57 "O1: Detune [ct]",
58 "O2: Detune [ct]",
59 NULL
62 wavetable_voice::wavetable_voice()
64 sample_rate = -1;
67 void wavetable_voice::set_params_ptr(wavetable_audio_module *_parent, int _srate)
69 parent = _parent;
70 params = parent->params;
71 sample_rate = _srate;
74 void wavetable_voice::reset()
76 note = -1;
79 void wavetable_voice::note_on(int note, int vel)
81 typedef wavetable_metadata md;
82 this->note = note;
83 velocity = vel / 127.0;
84 amp.set(1.0);
85 for (int i = 0; i < OscCount; i++) {
86 oscs[i].reset();
87 oscs[i].set_freq(note_to_hz(note, 0), sample_rate);
88 last_oscshift[i] = 0;
90 int cr = sample_rate / BlockSize;
91 for (int i = 0; i < EnvCount; i++) {
92 envs[i].set(0.01, 0.1, 0.5, 1, cr);
93 envs[i].note_on();
95 float modsrc[wavetable_metadata::modsrc_count] = { 1, velocity, parent->inertia_pressure.get_last(), parent->modwheel_value, envs[0].value, envs[1].value, envs[2].value};
96 parent->calculate_modmatrix(moddest, md::moddest_count, modsrc);
97 calc_derived_dests();
99 float oscshift[2] = { moddest[md::moddest_o1shift], moddest[md::moddest_o2shift] };
100 memcpy(last_oscshift, oscshift, sizeof(oscshift));
101 memcpy(last_oscamp, cur_oscamp, sizeof(cur_oscamp));
104 void wavetable_voice::note_off(int vel)
106 for (int i = 0; i < EnvCount; i++)
107 envs[i].note_off();
110 void wavetable_voice::steal()
114 void wavetable_voice::render_block()
116 typedef wavetable_metadata md;
118 const float step = 1.f / BlockSize;
120 float s = 0.001;
121 float scl[EnvCount];
122 int espc = md::par_eg2attack - md::par_eg1attack;
123 for (int j = 0; j < EnvCount; j++) {
124 int o = j*espc;
125 envs[j].set(*params[md::par_eg1attack + o] * s, *params[md::par_eg1decay + o] * s, *params[md::par_eg1sustain + o], *params[md::par_eg1release + o] * s, sample_rate / BlockSize, *params[md::par_eg1fade + o] * s);
126 scl[j] = dsp::lerp(1.f, velocity, *params[md::par_eg1velscl + o]);;
129 for (int i = 0; i < EnvCount; i++)
130 envs[i].advance();
132 float modsrc[wavetable_metadata::modsrc_count] = { 1, velocity, parent->inertia_pressure.get_last(), parent->modwheel_value, envs[0].value, envs[1].value, envs[2].value};
133 parent->calculate_modmatrix(moddest, md::moddest_count, modsrc);
134 calc_derived_dests();
136 int ospc = md::par_o2level - md::par_o1level;
137 for (int j = 0; j < OscCount; j++) {
138 oscs[j].tables = parent->tables[(int)*params[md::par_o1wave + j * ospc]];
139 oscs[j].set_freq(note_to_hz(note, *params[md::par_o1transpose + j * ospc] * 100+ *params[md::par_o1detune + j * ospc] + moddest[md::moddest_o1detune]), sample_rate);
142 float oscshift[2] = { moddest[md::moddest_o1shift], moddest[md::moddest_o2shift] };
143 float osstep[2] = { (oscshift[0] - last_oscshift[0]) * step, (oscshift[1] - last_oscshift[1]) * step };
144 float oastep[2] = { (cur_oscamp[0] - last_oscamp[0]) * step, (cur_oscamp[1] - last_oscamp[1]) * step };
145 for (int i = 0; i < BlockSize; i++) {
146 float value = 0.f;
148 for (int j = 0; j < OscCount; j++) {
149 float o = last_oscshift[j] * 0.01;
150 value += last_oscamp[j] * oscs[j].get(dsp::clip(fastf2i_drm((o + *params[md::par_o1offset + j * ospc]) * 127.0 * 256), 0, 127 * 256));
151 last_oscshift[j] += osstep[j];
152 last_oscamp[j] += oastep[j];
155 output_buffer[i][0] = output_buffer[i][1] = value;
157 if (envs[0].stopped())
158 released = true;
159 memcpy(last_oscshift, oscshift, sizeof(oscshift));
160 memcpy(last_oscamp, cur_oscamp, sizeof(cur_oscamp));
163 /////////////////////////////////////////////////////////////////////////////////////////////////////
165 static inline float sincl(float x, float clip)
167 if (fabs(x) > clip)
168 return 0;
169 return sin(M_PI * x);
172 static inline float blip(float x, float center, float range)
174 if (x < center - range || x > center + range)
175 return 0;
176 return 1 - fabs(x - center)/range;
179 static void interpolate_wt(int16_t table[129][256], int step)
181 for (int i = 0; i < 128; i++)
183 if (!(i % step))
184 continue;
185 int prev = i - i % step;
186 int next = prev + step;
187 for (int j = 0; j < 256; j++)
189 table[i][j] = table[prev][j] + (i - prev) * (table[next][j] - table[prev][j]) / step;
194 wavetable_audio_module::wavetable_audio_module()
195 : mod_matrix(mod_matrix_data, mod_matrix_slots, ::mod_src_names, ::mod_dest_names)
196 , inertia_cutoff(1)
197 , inertia_pitchbend(1)
198 , inertia_pressure(64)
200 panic_flag = false;
201 modwheel_value = 0.;
202 for (int i = 0; i < 129; i += 8)
204 for (int j = 0; j < 256; j++)
206 float ph = j * 2 * M_PI / 256;
207 int harm = 1 + 2 * (i / 8);
208 float ii = i / 128.0;
209 float rezo1 = sin(harm * ph) * sin(ph);
210 float rezo2 = sin((harm+1) * ph) * sin(ph * 2);
211 float rezo3 = sin((harm+3) * ph) * sin(ph * 4);
212 float rezo = (rezo1 + rezo2 + rezo3) / 3;
213 float v = (sin (ph) + ii * ii * rezo) / 2;
214 tables[0][i][j] = 32767 * v;
217 interpolate_wt(tables[0], 8);
218 for (int i = 0; i < 129; i += 4)
220 for (int j = 0; j < 256; j++)
222 float ph = j * 2 * M_PI / 256;
223 int harm = 1 + (i / 4);
224 float ii = i / 128.0;
225 float h = sin(harm * ph);
226 float rezo1 = h * sin(ph);
227 float rezo2 = h * sin(ph * 2)/2;
228 float rezo3 = h * sin(ph * 3)/3;
229 float rezo4 = h * sin(ph * 4)/4;
230 float rezo5 = h * sin(ph * 5)/5;
231 float rezo = (rezo1 + rezo2 + rezo3 + rezo4 + rezo5) / 3;
232 float v = sin (ph + ii * rezo);
233 tables[1][i][j] = 32767 * v;
236 interpolate_wt(tables[1], 4);
237 for (int i = 0; i < 129; i++)
239 for (int j = 0; j < 256; j++)
241 float ph = j * 2 * M_PI / 256;
242 float ii = (i & ~3) / 128.0;
243 float ii2 = ((i & ~3) + 4) / 128.0;
244 float peak = (32 * ii);
245 float rezo1 = sin(floor(peak) * ph);
246 float rezo2 = sin(floor(peak + 1) * ph);
247 float widener = (0.5 + 0.3 * sin(ph) + 0.2 * sin (3 * ph));
248 float v1 = 0.5 * sin (ph) + 0.5 * ii * ii * rezo1 * widener;
249 float v2 = 0.5 * sin (ph) + 0.5 * ii2 * ii2 * rezo2 * widener;
250 tables[wavetable_metadata::wt_rezo][i][j] = 32767 * lerp(v1, v2, (i & 3) / 4.0);
253 for (int i = 0; i < 129; i++)
255 for (int j = 0; j < 256; j++)
257 float ph = j * 2 * M_PI / 256;
258 float ii = i / 128.0;
259 float v = (sin(ph) + ii * sin(ph + 2 * ii * sin(ph)) + ii * ii * sin(ph + 6 * ii * ii * sin(6 * ph)) + ii * ii * ii * ii * sin(ph + 11 * ii * ii * ii * ii * sin(11 * ph))) / 4;
260 tables[wavetable_metadata::wt_metal][i][j] = 32767 * v;
263 for (int i = 0; i < 129; i++)
265 for (int j = 0; j < 256; j++)
267 float ph = j * 2 * M_PI / 256;
268 float ii = i / 128.0;
269 float v = (sin(ph) + ii * sin(ph - 3 * ii * sin(ph)) + ii * ii * sin(5 * ph - 5 * ii * ii * ii * ii * sin(11 * ph))) / 3;
270 tables[wavetable_metadata::wt_bell][i][j] = 32767 * v;
273 for (int i = 0; i < 129; i++)
275 for (int j = 0; j < 256; j++)
277 float ph = j * 2 * M_PI / 256;
278 float ii = i / 128.0;
279 //float v = (sin(ph) + ii * sin(ph + 2 * ii * sin(ph)) + ii * ii * sin(ph + 3 * ii * ii * sin(3 * ph)) + ii * ii * ii * sin(ph + 5 * ii * ii * ii * sin(5 * ph))) / 4;
280 float v = (sin(ph) + sin(ph - 3 * sin(ii * 5 - 2) * sin(ph)) + sin(ii * 4 - 1.3) * sin(5 * ph + 3 * ii * ii * sin(6 * ph))) / 3;
281 tables[wavetable_metadata::wt_blah][i][j] = 32767 * v;
284 for (int i = 0; i < 256; i++)
286 tables[wavetable_metadata::wt_pluck][128][i] = (i < 128) ? 32000 * fabs(sin(i / 32.0 * M_PI) * sin(i / 13.0 * M_PI) * sin(i / 19.0 * M_PI)) : 0;
288 for (int i = 127; i >= 0; i--)
290 int16_t *parent = tables[wavetable_metadata::wt_pluck][i + 1];
291 float damp = 0.05;
292 for (int j = 0; j < 256; j++)
294 tables[wavetable_metadata::wt_pluck][i][j] = (1 - 2*damp) * parent[j] + damp * parent[(j+1)&255] + damp * parent[(j+2)&255];// + 0.1 * parent[(j-1)&255]+ 0.1 * parent[(j-2)&255];
297 for (int i = 0; i < 129; i++)
299 for (int j = 0; j < 256; j++)
301 float ph = j / 128.0 - 1.0;
302 float ii = i / 128.0;
303 float v = sincl(ph * (1 + 15 * ii), 1);
304 tables[wavetable_metadata::wt_stretch][i][j] = 32767 * v;
307 for (int i = 0; i < 129; i++)
309 for (int j = 0; j < 256; j++)
311 float ph = j / 128.0 - 1.0;
312 float ii = i / 128.0;
313 float v = sincl(ph * (1 + 15 * ii), 4) * sincl(j / 256.0, 1);
314 tables[wavetable_metadata::wt_stretch2][i][j] = 32000 * v;
317 for (int i = 0; i < 129; i++)
319 for (int j = 0; j < 256; j++)
321 float ph = j / 128.0 - 1.0;
322 float ii = i / 128.0;
323 float w = sincl(ph * (1 + 15 * ii), 4);
324 float v = pow(w, 9) * sincl(j / 256.0, 1);
325 tables[wavetable_metadata::wt_hardsync][i][j] = 32000 * v;
328 for (int i = 0; i < 129; i++)
330 for (int j = 0; j < 256; j++)
332 float ph = j / 128.0 - 1.0;
333 float ii = i / 128.0;
334 float w = sincl(ph * (1 + 31 * ii), 3);
335 float v = pow(w, 5) * sincl(j / 256.0, 1);
336 tables[wavetable_metadata::wt_hardsync2][i][j] = 32000 * v;
339 for (int i = 0; i < 129; i++)
341 for (int j = 0; j < 256; j++)
343 float ph = j / 128.0 - 1.0;
344 float ii = i / 128.0;
345 float w = sincl(ph * ph * (1 + 15 * ii), 2);
346 float v = pow(w, 4) * sincl(j / 256.0, 1);
347 tables[wavetable_metadata::wt_softsync][i][j] = 32000 * v;
350 for (int i = 0; i < 129; i++)
352 for (int j = 0; j < 256; j++)
354 float ph = j * 2 * M_PI / 256;
355 float ii = i / 128.0;
356 float v = (sin(ph) + ii * sin(ph - 3 * ii * sin(ph)) + ii * ii * ii * sin(7 * ph - 2 * ii * ii * ii * ii * sin(13 * ph))) / 3;
357 tables[wavetable_metadata::wt_bell2][i][j] = 32767 * v;
360 for (int i = 0; i < 129; i++)
362 for (int j = 0; j < 256; j++)
364 float ph = j * 2 * M_PI / 256;
365 float ii = i / 128.0;
366 float v = (sin(ph) + ii * sin(ph - 3 * ii * sin(ph)) + ii * ii * ii * sin(9 * ph - ii * ii * sin(11 * ph))) / 3;
367 tables[wavetable_metadata::wt_bell3][i][j] = 32767 * v;
370 for (int i = 0; i < 129; i++)
372 for (int j = 0; j < 256; j++)
374 float ph = j * 2 * M_PI / 256;
375 float ii = i / 128.0;
376 float v = (sin(ph + ii * sin(ph - 3 * ii * sin(ph) + ii * ii * ii * sin(5 * ph - ii * ii * sin(7 * ph)))));
377 tables[wavetable_metadata::wt_tine][i][j] = 32767 * v;
380 for (int i = 0; i < 129; i++)
382 for (int j = 0; j < 256; j++)
384 float ph = j * 2 * M_PI / 256;
385 float ii = i / 128.0;
386 float v = (sin(ph + ii * sin(ph - 2 * ii * sin(ph) + ii * ii * ii * sin(3 * ph - ii * ii * sin(4 * ph)))));
387 tables[wavetable_metadata::wt_tine2][i][j] = 32767 * v;
390 for (int i = 0; i < 129; i++)
392 for (int j = 0; j < 256; j++)
394 float ph = j * 2 * M_PI / 256;
395 float ph2 = j / 128.0 - 1;
396 float ii = i / 128.0;
397 float w = sincl(ph2 * (1 + 7 * ii * ii), 4) * pow(sincl(j / 256.0, 1), 2);
398 float v = sin(ph + ii * sin(ph - 2 * ii * w));
399 tables[wavetable_metadata::wt_clav][i][j] = 32767 * v;
402 for (int i = 0; i < 129; i++)
404 for (int j = 0; j < 256; j++)
406 float ph = j * 2 * M_PI / 256;
407 float ph2 = j / 128.0 - 1;
408 float ii = i / 128.0;
409 float w = sincl(ph2 * (1 + 7 * ii * ii), 6) * sincl(j / 256.0, 1);
410 float v = sin(ph + ii * sin(3 * ph - 2 * ii * w));
411 tables[wavetable_metadata::wt_clav2][i][j] = 32767 * v;
415 for (int i = 0; i < 129; i++)
417 for (int j = 0; j < 256; j++)
419 float ph = j * 2 * M_PI / 256;
420 float ph2 = j / 128.0 - 1;
421 float ii = i / 128.0;
422 float w = sincl(ph2 * (1 + 7 * ii * ii), 6) * pow(sincl(j / 256.0, 1), 1);
423 float v = sin(ph + ii * ii * ii * sin(3 * ph - ii * ii * ii * w));
424 tables[wavetable_metadata::wt_gtr][i][j] = 32767 * v;
428 for (int i = 0; i < 129; i++)
430 for (int j = 0; j < 256; j++)
432 float ph = j * 2 * M_PI / 256;
433 float ii = i / 128.0;
434 float ii2 = ii;
435 float w = pow(sincl(j / 256.0, 1), 1);
436 float v = sin(ph + ii2 * ii2 * ii2 * sin(3 * ph - ii2 * ii2 * ii2 * w * sin(ph + sin(3 * ph) + ii * sin(11 * ph) + ii * ii * sin(25 * ph))));
437 tables[wavetable_metadata::wt_gtr][i][j] = 32767 * v;
440 for (int i = 0; i < 129; i++)
442 for (int j = 0; j < 256; j++)
444 float ph = j * 2 * M_PI / 256;
445 float ii = i / 128.0;
446 float ii2 = dsp::clip(ii - 0.5, 0.0, 1.0);
447 float w = pow(sincl(j / 256.0, 1), 1);
448 float v = sin(ph + ii * ii * ii * sin(3 * ph - ii * ii * ii * w * sin(ph + sin(3 * ph + ii2 * sin(13 * ph)))));
449 tables[wavetable_metadata::wt_gtr2][i][j] = 32767 * v;
452 for (int i = 0; i < 129; i++)
454 for (int j = 0; j < 256; j++)
456 float ph = j * 2 * M_PI / 256;
457 float ii = i / 128.0;
458 float ii2 = dsp::clip(2 * (ii - 0.5), 0.0, 1.0);
459 //float w = sincl(ph2 * (1 + 15 * ii2 * ii2), 4) * pow(sincl(j / 256.0, 1), 1);
460 float w = pow(sincl(j / 256.0, 1), 1);
461 float v = sin(ph + ii * sin(3 * ph - ii * w * sin(ph + sin(3 * ph + 0.5 * ii2 * sin(13 * ph + 0.5 * sin(4 * ph))))));
462 tables[wavetable_metadata::wt_gtr3][i][j] = 32767 * v;
465 for (int i = 0; i < 129; i++)
467 for (int j = 0; j < 256; j++)
469 float ph = j * 2 * M_PI / 256;
470 float ii = i / 128.0;
471 float ii2 = dsp::clip(2 * (ii - 0.5), 0.0, 1.0);
472 //float w = sincl(ph2 * (1 + 15 * ii2 * ii2), 4) * pow(sincl(j / 256.0, 1), 1);
473 float w = pow(sincl(j / 256.0, 1), 1);
474 float v = sin(ph + ii * sin(3 * ph - ii * w * sin(2 * ph + sin(5 * ph + 0.5 * ii2 * sin(13 * ph + 0.5 * sin(4 * ph))))));
475 tables[wavetable_metadata::wt_gtr4][i][j] = 32767 * v;
478 for (int i = 0; i < 129; i++)
480 for (int j = 0; j < 256; j++)
482 float ph = j * 2 * M_PI / 256;
483 float ii = i / 128.0;
484 float ii2 = dsp::clip((ii - 0.25)/0.75, 0.0, 1.0);
485 //float w = sincl(ph2 * (1 + 15 * ii2 * ii2), 4) * pow(sincl(j / 256.0, 1), 1);
486 float w = pow(sincl(j / 256.0, 1), 3);
487 float v = sin(ph + (ii + 0.05) * sin(3 * ph - 2 * ii * w * sin(5 * ph + sin(7 * ph + 0.5 * ii2 * sin(13 * ph + 0.5 * sin(11 * ph))))));
488 tables[wavetable_metadata::wt_gtr5][i][j] = 32767 * v;
491 for (int i = 0; i < 129; i++)
493 for (int j = 0; j < 256; j++)
495 float ph = j * 2 * M_PI / 256;
496 float ii = i / 128.0;
497 float w = pow(sincl(2 * (j / 256.0), 2), 3);
498 float v = sin(ph + (ii + 0.05) * sin(7 * ph - 2 * ii * w * sin(11 * ph)));
499 tables[wavetable_metadata::wt_reed][i][j] = 32767 * v;
502 for (int i = 0; i < 129; i++)
504 for (int j = 0; j < 256; j++)
506 float ph = j * 2 * M_PI / 256;
507 float ii = i / 128.0;
508 float ii2 = dsp::clip((ii - 0.25)/0.75, 0.0, 1.0);
509 float ii3 = dsp::clip((ii - 0.5)/0.5, 0.0, 1.0);
510 float v = sin(ph + (ii + 0.05) * sin(ii * sin(2 * ph) - 2 * ii2 * sin(2 * ph + ii2 * sin(3 * ph)) + 3 * ii3 * sin(3 * ph)));
511 tables[wavetable_metadata::wt_reed2][i][j] = 32767 * v;
514 for (int i = 0; i < 129; i++)
516 for (int j = 0; j < 256; j++)
518 float ph = j * 2 * M_PI / 256;
519 float ii = i / 128.0;
520 float mod = 0;
521 for (int k = 0; k < 13; k++)
523 mod += blip(i, k * 10, 30) * sin (ph * (5 + 3 * k) + ii * cos(ph * (2 + 2 * k)));
525 float v = sin(ph + ii * mod);
526 tables[wavetable_metadata::wt_silver][i][j] = 32767 * v;
529 for (int i = 0; i < 129; i++)
531 for (int j = 0; j < 256; j++)
533 float ph = j * 2 * M_PI / 256;
534 float ii = i / 128.0;
535 float mod = 0;
536 for (int k = 0; k < 16; k++)
538 mod += 2 * blip(i, k * 8, k * 4 + 10) * cos (ph * (k + 1));
540 float v = sin(ph + ii * mod);
541 tables[wavetable_metadata::wt_brass][i][j] = 32767 * v;
544 for (int i = 0; i < 129; i++)
546 for (int j = 0; j < 256; j++)
548 float ph = j * 2 * M_PI / 256;
549 float ii = i / 128.0;
550 float mod = 0;
551 for (int k = 0; k < 16; k++)
553 mod += 2 * blip(i, k * 8, 16) * cos (ph * (2 * k + 1));
555 float v = (sin(ph + ii * mod) + ii * sin(2 * ph + ii * mod)) / 2;
556 tables[wavetable_metadata::wt_multi][i][j] = 32767 * v;
559 for (int i = 0; i < 129; i ++)
561 float h = 1 + i / 16.0;
562 for (int j = 0; j < 256; j++)
564 float ph = j * 2 * M_PI / 256;
565 float v = sin(ph), tv = 1;
566 for (int k = 1; k < 24; k++) {
567 float amp = blip(i, k * 6, 20) / k;
568 v += amp * sin((k + 1) * ph + h * sin(ph));
569 tv += amp;
571 tables[wavetable_metadata::wt_multi2][i][j] = 32767 * v / tv;
576 void wavetable_audio_module::channel_pressure(int value)
578 inertia_pressure.set_inertia(value * (1.0 / 127.0));
581 #endif