Make Analyzer UI require instance-access
[calf.git] / src / monosynth.cpp
blob9d8730fa09e2db35d3a92fa8b184a55757c5e1a1
1 /* Calf DSP Library
2 * Example audio modules - monosynth
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 <calf/giface.h>
22 #include <calf/modules_synths.h>
24 using namespace dsp;
25 using namespace calf_plugins;
26 using namespace std;
28 float silence[4097];
30 monosynth_audio_module::monosynth_audio_module()
31 : mod_matrix_impl(mod_matrix_data, &mm_metadata)
32 , inertia_cutoff(1)
33 , inertia_pitchbend(1)
34 , inertia_pressure(64)
38 void monosynth_audio_module::reset()
40 last_stretch1 = 0;
41 stopping = false;
42 running = false;
43 output_pos = 0;
44 queue_note_on = -1;
45 inertia_pitchbend.set_now(1.f);
46 lfo_bend = 1.0;
47 modwheel_value = 0.f;
48 modwheel_value_int = 0;
49 inertia_cutoff.set_now(*params[par_cutoff]);
50 inertia_pressure.set_now(0);
51 osc1.phasedelta = 0;
52 osc2.phasedelta = 0;
53 filter.reset();
54 filter2.reset();
55 stack.clear();
56 last_pwshift1 = last_pwshift2 = 0;
57 last_stretch1 = 65536;
58 last_xfade = 0;
59 last_unison = 0.0;
60 queue_note_on_and_off = false;
61 prev_wave1 = -1;
62 prev_wave2 = -1;
63 wave1 = -1;
64 wave2 = -1;
65 queue_note_on = -1;
66 last_filter_type = -1;
67 lfo_clock = 0.f;
70 void monosynth_audio_module::activate()
72 reset();
75 waveform_family<MONOSYNTH_WAVE_BITS> *monosynth_audio_module::waves;
77 void monosynth_audio_module::precalculate_waves(progress_report_iface *reporter)
79 float data[1 << MONOSYNTH_WAVE_BITS];
80 bandlimiter<MONOSYNTH_WAVE_BITS> bl;
82 if (waves)
83 return;
85 static waveform_family<MONOSYNTH_WAVE_BITS> waves_data[wave_count];
86 waves = waves_data;
88 enum { S = 1 << MONOSYNTH_WAVE_BITS, HS = S / 2, QS = S / 4, QS3 = 3 * QS };
89 float iQS = 1.0 / QS;
91 if (reporter)
92 reporter->report_progress(0, "Precalculating waveforms");
94 // yes these waves don't have really perfect 1/x spectrum because of aliasing
95 // (so what?)
96 for (int i = 0 ; i < HS; i++)
97 data[i] = (float)(i * 1.0 / HS),
98 data[i + HS] = (float)(i * 1.0 / HS - 1.0f);
99 waves[wave_saw].make(bl, data);
101 // this one is dummy, fake and sham, we're using a difference of two sawtooths for square wave due to PWM
102 for (int i = 0 ; i < S; i++)
103 data[i] = (float)(i < HS ? -1.f : 1.f);
104 waves[wave_sqr].make(bl, data, 4);
106 for (int i = 0 ; i < S; i++)
107 data[i] = (float)(i < (64 * S / 2048)? -1.f : 1.f);
108 waves[wave_pulse].make(bl, data);
110 for (int i = 0 ; i < S; i++)
111 data[i] = (float)sin(i * M_PI / HS);
112 waves[wave_sine].make(bl, data);
114 for (int i = 0 ; i < QS; i++) {
115 data[i] = i * iQS,
116 data[i + QS] = 1 - i * iQS,
117 data[i + HS] = - i * iQS,
118 data[i + QS3] = -1 + i * iQS;
120 waves[wave_triangle].make(bl, data);
122 for (int i = 0, j = 1; i < S; i++) {
123 data[i] = -1 + j * 1.0 / HS;
124 if (i == j)
125 j *= 2;
127 waves[wave_varistep].make(bl, data);
129 for (int i = 0; i < S; i++) {
130 data[i] = (min(1.f, (float)(i / 64.f))) * (1.0 - i * 1.0 / S) * (-1 + fmod (i * i * 8/ (S * S * 1.0), 2.0));
132 normalize_waveform(data, S);
133 waves[wave_skewsaw].make(bl, data);
134 for (int i = 0; i < S; i++) {
135 data[i] = (min(1.f, (float)(i / 64.f))) * (1.0 - i * 1.0 / S) * (fmod (i * i * 8/ (S * S * 1.0), 2.0) < 1.0 ? -1.0 : +1.0);
137 normalize_waveform(data, S);
138 waves[wave_skewsqr].make(bl, data);
140 if (reporter)
141 reporter->report_progress(50, "Precalculating waveforms");
143 for (int i = 0; i < S; i++) {
144 if (i < QS3) {
145 float p = i * 1.0 / QS3;
146 data[i] = sin(M_PI * p * p * p);
147 } else {
148 float p = (i - QS3 * 1.0) / QS;
149 data[i] = -0.5 * sin(3 * M_PI * p * p);
152 normalize_waveform(data, S);
153 waves[wave_test1].make(bl, data);
154 for (int i = 0; i < S; i++) {
155 data[i] = exp(-i * 1.0 / HS) * sin(i * M_PI / HS) * cos(2 * M_PI * i / HS);
157 normalize_waveform(data, S);
158 waves[wave_test2].make(bl, data);
159 for (int i = 0; i < S; i++) {
160 //int ii = (i < HS) ? i : S - i;
161 int ii = HS;
162 data[i] = (ii * 1.0 / HS) * sin(i * 3 * M_PI / HS + 2 * M_PI * sin(M_PI / 4 + i * 4 * M_PI / HS)) * sin(i * 5 * M_PI / HS + 2 * M_PI * sin(M_PI / 8 + i * 6 * M_PI / HS));
164 normalize_waveform(data, S);
165 waves[wave_test3].make(bl, data);
166 for (int i = 0; i < S; i++) {
167 data[i] = sin(i * 2 * M_PI / HS + sin(i * 2 * M_PI / HS + 0.5 * M_PI * sin(i * 18 * M_PI / HS)) * sin(i * 1 * M_PI / HS + 0.5 * M_PI * sin(i * 11 * M_PI / HS)));
169 normalize_waveform(data, S);
170 waves[wave_test4].make(bl, data);
171 for (int i = 0; i < S; i++) {
172 data[i] = sin(i * 2 * M_PI / HS + 0.2 * M_PI * sin(i * 13 * M_PI / HS) + 0.1 * M_PI * sin(i * 37 * M_PI / HS)) * sin(i * M_PI / HS + 0.2 * M_PI * sin(i * 15 * M_PI / HS));
174 normalize_waveform(data, S);
175 waves[wave_test5].make(bl, data);
176 for (int i = 0; i < S; i++) {
177 if (i < HS)
178 data[i] = sin(i * 2 * M_PI / HS);
179 else
180 if (i < 3 * S / 4)
181 data[i] = sin(i * 4 * M_PI / HS);
182 else
183 if (i < 7 * S / 8)
184 data[i] = sin(i * 8 * M_PI / HS);
185 else
186 data[i] = sin(i * 8 * M_PI / HS) * (S - i) / (S / 8);
188 normalize_waveform(data, S);
189 waves[wave_test6].make(bl, data);
190 for (int i = 0; i < S; i++) {
191 int j = i >> (MONOSYNTH_WAVE_BITS - 11);
192 data[i] = (j ^ 0x1D0) * 1.0 / HS - 1;
194 normalize_waveform(data, S);
195 waves[wave_test7].make(bl, data);
196 for (int i = 0; i < S; i++) {
197 int j = i >> (MONOSYNTH_WAVE_BITS - 11);
198 data[i] = -1 + 0.66 * (3 & ((j >> 8) ^ (j >> 10) ^ (j >> 6)));
200 normalize_waveform(data, S);
201 waves[wave_test8].make(bl, data);
202 if (reporter)
203 reporter->report_progress(100, "");
207 bool monosynth_audio_module::get_graph(int index, int subindex, int phase, float *data, int points, cairo_iface *context, int *mode) const
209 if (!phase)
210 return false;
211 monosynth_audio_module::precalculate_waves(NULL);
212 // printf("get_graph %d %p %d wave1=%d wave2=%d\n", index, data, points, wave1, wave2);
213 if (index == par_wave1 || index == par_wave2) {
214 if (subindex)
215 return false;
216 enum { S = 1 << MONOSYNTH_WAVE_BITS };
217 float value = *params[index];
218 int wave = dsp::clip(dsp::fastf2i_drm(value), 0, (int)wave_count - 1);
220 uint32_t shift = index == par_wave1 ? last_pwshift1 : last_pwshift2;
221 if (!running)
222 shift = (int32_t)(0x78000000 * (*params[index == par_wave1 ? par_pw1 : par_pw2]));
223 int flag = (wave == wave_sqr);
225 shift = (flag ? S/2 : 0) + (shift >> (32 - MONOSYNTH_WAVE_BITS));
226 int sign = flag ? -1 : 1;
227 if (wave == wave_sqr)
228 wave = wave_saw;
229 float *waveform = waves[wave].original;
230 float rnd_start = 1 - *params[par_window1] * 0.5f;
231 float scl = rnd_start < 1.0 ? 1.f / (1 - rnd_start) : 0.f;
232 for (int i = 0; i < points; i++)
234 int pos = i * S / points;
235 float r = 1;
236 if (index == par_wave1)
238 float ph = i * 1.0 / points;
239 if (ph < 0.5f)
240 ph = 1.f - ph;
241 ph = (ph - rnd_start) * scl;
242 if (ph < 0)
243 ph = 0;
244 r = 1.0 - ph * ph;
245 pos = int(pos * 1.0 * last_stretch1 / 65536.0 ) % S;
247 data[i] = r * (sign * waveform[pos] + waveform[(pos + shift) & (S - 1)]) / (sign == -1 ? 1 : 2);
249 return true;
251 if (index == par_filtertype) {
252 if (!running)
253 return false;
254 if (subindex > (is_stereo_filter() ? 1 : 0))
255 return false;
256 for (int i = 0; i < points; i++)
258 double freq = 20.0 * pow (20000.0 / 20.0, i * 1.0 / points);
260 const dsp::biquad_d1_lerp &f = subindex ? filter2 : filter;
261 float level = f.freq_gain(freq, srate);
262 if (!is_stereo_filter())
263 level *= filter2.freq_gain(freq, srate);
264 else
265 set_channel_color(context, subindex);
266 level *= fgain;
268 data[i] = log(level) / log(1024.0) + 0.5;
270 return true;
272 return false;
275 void monosynth_audio_module::calculate_buffer_oscs(float lfo1)
277 int flag1 = (wave1 == wave_sqr);
278 int flag2 = (wave2 == wave_sqr);
280 int32_t shift1 = last_pwshift1;
281 int32_t shift2 = last_pwshift2;
282 int32_t stretch1 = last_stretch1;
283 int32_t shift_target1 = (int32_t)(0x78000000 * dsp::clip11(*params[par_pw1] + lfo1 * *params[par_lfopw] + 0.01f * moddest[moddest_o1pw]));
284 int32_t shift_target2 = (int32_t)(0x78000000 * dsp::clip11(*params[par_pw2] + lfo1 * *params[par_lfopw] + 0.01f * moddest[moddest_o2pw]));
285 int32_t stretch_target1 = (int32_t)(65536 * dsp::clip(*params[par_stretch1] + 0.01f * moddest[moddest_o1stretch], 1.f, 16.f));
286 int32_t shift_delta1 = ((shift_target1 >> 1) - (last_pwshift1 >> 1)) >> (step_shift - 1);
287 int32_t shift_delta2 = ((shift_target2 >> 1) - (last_pwshift2 >> 1)) >> (step_shift - 1);
288 int32_t stretch_delta1 = ((stretch_target1 >> 1) - (last_stretch1 >> 1)) >> (step_shift - 1);
289 last_pwshift1 = shift_target1;
290 last_pwshift2 = shift_target2;
291 last_stretch1 = stretch_target1;
292 lookup_waveforms();
294 shift1 += (flag1 << 31);
295 shift2 += (flag2 << 31);
296 float mix1 = 1 - 2 * flag1, mix2 = 1 - 2 * flag2;
298 float new_xfade = dsp::clip<float>(xfade + 0.01f * moddest[moddest_oscmix], 0.f, 1.f);
299 float cur_xfade = last_xfade;
300 float xfade_step = (new_xfade - cur_xfade) * (1.0 / step_size);
302 float rnd_start = 1 - *params[par_window1] * 0.5f;
303 float scl = rnd_start < 1.0 ? 1.f / (1 - rnd_start) : 0.f;
305 static const int muls[8] = { 33, -47, 53, -67, 87, -101, 121, -139 };
306 float unison = *params[par_o2unison] + moddest[moddest_o2unisonamp] * 0.01;
307 float unison_scale = 1.0, unison_delta = 0.0, last_unison_scale = 1.0, unison_scale_delta = 0.0;
308 if (unison > 0)
310 float freq = fabs(*params[par_o2unisonfrq] / muls[7]);
311 if (moddest[moddest_o2unisondetune] != 0)
312 freq *= pow(2.0, moddest[moddest_o2unisondetune]);
313 unison_osc.set_freq(freq, srate);
314 last_unison_scale = 1.0 / (1.0 + 2 * last_unison);
315 unison_scale = 1.0 / (1.0 + 2 * unison);
316 unison_delta = (unison - last_unison) * (1.0 / step_size);
317 unison_scale_delta = (unison_scale - last_unison_scale) * (1.0 / step_size);
319 for (uint32_t i = 0; i < step_size; i++)
321 //buffer[i] = lerp(osc1.get_phaseshifted(shift1, mix1), osc2.get_phaseshifted(shift2, mix2), cur_xfade);
322 float o1phase = osc1.phase / (65536.0 * 65536.0);
323 if (o1phase < 0.5)
324 o1phase = 1 - o1phase;
325 o1phase = (o1phase - rnd_start) * scl;
326 if (o1phase < 0)
327 o1phase = 0;
328 float r = 1.0 - o1phase * o1phase;
329 float osc1val = osc1.get_phasedist(stretch1, shift1, mix1);
330 float osc2val = osc2.get_phaseshifted(shift2, mix2);
331 if (unison > 0 || last_unison > 0)
333 for (int j = 0; j < 8; j++)
334 osc2val += last_unison * osc2.get_phaseshifted2(shift2, unison_osc.phase * muls[j], mix2);
335 osc2val *= last_unison_scale;
337 unison_osc.step();
338 last_unison += unison_delta;
339 last_unison_scale += unison_scale_delta;
341 buffer[i] = lerp(r * osc1val, osc2val, cur_xfade);
342 osc1.advance();
343 osc2.advance();
344 shift1 += shift_delta1;
345 shift2 += shift_delta2;
346 stretch1 += stretch_delta1;
347 cur_xfade += xfade_step;
349 last_xfade = new_xfade;
350 last_unison = unison;
353 void monosynth_audio_module::calculate_buffer_ser()
355 filter.big_step(1.0 / step_size);
356 filter2.big_step(1.0 / step_size);
357 for (uint32_t i = 0; i < step_size; i++)
359 float wave = buffer[i] * fgain;
360 wave = filter.process(wave);
361 wave = filter2.process(wave);
362 buffer[i] = wave;
363 fgain += fgain_delta;
367 void monosynth_audio_module::calculate_buffer_single()
369 filter.big_step(1.0 / step_size);
370 for (uint32_t i = 0; i < step_size; i++)
372 float wave = buffer[i] * fgain;
373 wave = filter.process(wave);
374 buffer[i] = wave;
375 fgain += fgain_delta;
379 void monosynth_audio_module::calculate_buffer_stereo()
381 filter.big_step(1.0 / step_size);
382 filter2.big_step(1.0 / step_size);
383 for (uint32_t i = 0; i < step_size; i++)
385 float wave1 = buffer[i] * fgain;
386 buffer[i] = fgain * filter.process(wave1);
387 buffer2[i] = fgain * filter2.process(wave1);
388 fgain += fgain_delta;
392 void monosynth_audio_module::lookup_waveforms()
394 osc1.waveform = waves[wave1 == wave_sqr ? wave_saw : wave1].get_level((uint32_t)(((uint64_t)osc1.phasedelta) * last_stretch1 >> 16));
395 osc2.waveform = waves[wave2 == wave_sqr ? wave_saw : wave2].get_level(osc2.phasedelta);
396 if (!osc1.waveform) osc1.waveform = silence;
397 if (!osc2.waveform) osc2.waveform = silence;
398 prev_wave1 = wave1;
399 prev_wave2 = wave2;
402 void monosynth_audio_module::delayed_note_on()
404 force_fadeout = false;
405 fadeout.reset_soft();
406 fadeout2.reset_soft();
407 porta_time = 0.f;
408 start_freq = freq;
409 target_freq = freq = 440 * pow(2.0, (queue_note_on - 69) / 12.0);
410 velocity = queue_vel;
411 ampctl = 1.0 + (queue_vel - 1.0) * *params[par_vel2amp];
412 fltctl = 1.0 + (queue_vel - 1.0) * *params[par_vel2filter];
413 bool starting = false;
415 if (!running)
417 starting = true;
418 if (legato >= 2)
419 porta_time = -1.f;
420 last_xfade = xfade;
421 unison_osc.phase = rand() << 16;
422 osc1.reset();
423 osc2.reset();
424 filter.reset();
425 filter2.reset();
426 if (*params[par_lfo1trig] <= 0)
427 lfo1.reset();
428 if (*params[par_lfo2trig] <= 0)
429 lfo2.reset();
430 switch((int)*params[par_oscmode])
432 case 1:
433 osc2.phase = 0x80000000;
434 break;
435 case 2:
436 osc2.phase = 0x40000000;
437 break;
438 case 3:
439 osc1.phase = osc2.phase = 0x40000000;
440 break;
441 case 4:
442 osc1.phase = 0x40000000;
443 osc2.phase = 0xC0000000;
444 break;
445 case 5:
446 // rand() is crap, but I don't have any better RNG in Calf yet
447 osc1.phase = rand() << 16;
448 osc2.phase = rand() << 16;
449 break;
450 default:
451 break;
453 running = true;
455 if (legato >= 2 && !gate)
456 porta_time = -1.f;
457 gate = true;
458 stopping = false;
459 if (starting || !(legato & 1) || envelope1.released())
460 envelope1.note_on();
461 if (starting || !(legato & 1) || envelope2.released())
462 envelope2.note_on();
463 if (!running || !(legato & 1))
464 lfo_clock = 0.f;
465 envelope1.advance();
466 envelope2.advance();
467 queue_note_on = -1;
468 float modsrc[modsrc_count] = { 1.f, velocity, (float)inertia_pressure.get_last(), modwheel_value, (float)envelope1.value, (float)envelope2.value, 0.5f+0.5f*lfo1.last, 0.5f+0.5f*lfo2.last};
469 calculate_modmatrix(moddest, moddest_count, modsrc);
470 set_frequency();
471 lookup_waveforms();
473 if (queue_note_on_and_off)
475 end_note();
476 queue_note_on_and_off = false;
480 void monosynth_audio_module::set_sample_rate(uint32_t sr) {
481 srate = sr;
482 crate = sr / step_size;
483 odcr = (float)(1.0 / crate);
484 fgain = 0.f;
485 fgain_delta = 0.f;
486 inertia_cutoff.ramp.set_length(crate / 30); // 1/30s
487 inertia_pitchbend.ramp.set_length(crate / 30); // 1/30s
488 master.set_sample_rate(sr);
491 float monosynth_audio_module::get_lfo(dsp::triangle_lfo &lfo, int param)
493 if (*params[param] <= 0)
494 return lfo.get();
495 float pt = lfo_clock / *params[param];
496 return lfo.get() * std::min(1.0f, pt);
499 void monosynth_audio_module::calculate_step()
501 if (queue_note_on != -1)
502 delayed_note_on();
503 else
504 if (stopping || !running)
506 running = false;
507 envelope1.advance();
508 envelope2.advance();
509 lfo1.get();
510 lfo2.get();
511 float modsrc[modsrc_count] = { 1.f, velocity, inertia_pressure.get_last(), modwheel_value, (float)envelope1.value, (float)envelope2.value, 0.5f+0.5f*lfo1.last, 0.5f+0.5f*lfo2.last};
512 calculate_modmatrix(moddest, moddest_count, modsrc);
513 last_stretch1 = (int32_t)(65536 * dsp::clip(*params[par_stretch1] + 0.01f * moddest[moddest_o1stretch], 1.f, 16.f));
514 return;
516 lfo1.set_freq(*params[par_lforate], crate);
517 lfo2.set_freq(*params[par_lfo2rate], crate);
518 float porta_total_time = *params[par_portamento] * 0.001f;
520 if (porta_total_time >= 0.00101f && porta_time >= 0) {
521 // XXXKF this is criminal, optimize!
522 float point = porta_time / porta_total_time;
523 if (point >= 1.0f) {
524 freq = target_freq;
525 porta_time = -1;
526 } else {
527 freq = start_freq + (target_freq - start_freq) * point;
528 // freq = start_freq * pow(target_freq / start_freq, point);
529 porta_time += odcr;
532 float lfov1 = get_lfo(lfo1, par_lfodelay);
533 lfov1 = lfov1 * dsp::lerp(1.f, modwheel_value, *params[par_mwhl_lfo]);
534 float lfov2 = get_lfo(lfo2, par_lfodelay);
535 lfo_clock += odcr;
536 if (fabs(*params[par_lfopitch]) > small_value<float>())
537 lfo_bend = pow(2.0f, *params[par_lfopitch] * lfov1 * (1.f / 1200.0f));
538 inertia_pitchbend.step();
539 envelope1.advance();
540 envelope2.advance();
541 float env1 = envelope1.value, env2 = envelope2.value;
542 float aenv1 = envelope1.get_amp_value(), aenv2 = envelope2.get_amp_value();
544 // mod matrix
545 // this should be optimized heavily; I think I'll do it when MIDI in Ardour 3 gets stable :>
546 float modsrc[modsrc_count] = { 1.f, velocity, inertia_pressure.get(), modwheel_value, env1, env2, 0.5f+0.5f*lfov1, 0.5f+0.5f*lfov2};
547 calculate_modmatrix(moddest, moddest_count, modsrc);
549 set_frequency();
550 inertia_cutoff.set_inertia(*params[par_cutoff]);
551 cutoff = inertia_cutoff.get() * pow(2.0f, (lfov1 * *params[par_lfofilter] + env1 * fltctl * *params[par_env1tocutoff] + env2 * fltctl * *params[par_env2tocutoff] + moddest[moddest_cutoff]) * (1.f / 1200.f));
552 if (*params[par_keyfollow] > 0.01f)
553 cutoff *= pow(freq / 264.f, *params[par_keyfollow]);
554 cutoff = dsp::clip(cutoff , 10.f, 18000.f);
555 float resonance = *params[par_resonance];
556 float e2r1 = *params[par_env1tores];
557 resonance = resonance * (1 - e2r1) + (0.7 + (resonance - 0.7) * env1 * env1) * e2r1;
558 float e2r2 = *params[par_env2tores];
559 resonance = resonance * (1 - e2r2) + (0.7 + (resonance - 0.7) * env2 * env2) * e2r2 + moddest[moddest_resonance];
560 float cutoff2 = dsp::clip(cutoff * separation, 10.f, 18000.f);
561 float newfgain = 0.f;
562 if (filter_type != last_filter_type)
564 filter.y2 = filter.y1 = filter.x2 = filter.x1 = filter.y1;
565 filter2.y2 = filter2.y1 = filter2.x2 = filter2.x1 = filter2.y1;
566 last_filter_type = filter_type;
568 switch(filter_type)
570 case flt_lp12:
571 filter.set_lp_rbj(cutoff, resonance, srate);
572 filter2.set_null();
573 newfgain = min(0.7f, 0.7f / resonance) * ampctl;
574 break;
575 case flt_hp12:
576 filter.set_hp_rbj(cutoff, resonance, srate);
577 filter2.set_null();
578 newfgain = min(0.7f, 0.7f / resonance) * ampctl;
579 break;
580 case flt_lp24:
581 filter.set_lp_rbj(cutoff, resonance, srate);
582 filter2.set_lp_rbj(cutoff2, resonance, srate);
583 newfgain = min(0.5f, 0.5f / resonance) * ampctl;
584 break;
585 case flt_lpbr:
586 filter.set_lp_rbj(cutoff, resonance, srate);
587 filter2.set_br_rbj(cutoff2, 1.0 / resonance, srate);
588 newfgain = min(0.5f, 0.5f / resonance) * ampctl;
589 break;
590 case flt_hpbr:
591 filter.set_hp_rbj(cutoff, resonance, srate);
592 filter2.set_br_rbj(cutoff2, 1.0 / resonance, srate);
593 newfgain = min(0.5f, 0.5f / resonance) * ampctl;
594 break;
595 case flt_2lp12:
596 filter.set_lp_rbj(cutoff, resonance, srate);
597 filter2.set_lp_rbj(cutoff2, resonance, srate);
598 newfgain = min(0.7f, 0.7f / resonance) * ampctl;
599 break;
600 case flt_bp6:
601 filter.set_bp_rbj(cutoff, resonance, srate);
602 filter2.set_null();
603 newfgain = ampctl;
604 break;
605 case flt_2bp6:
606 filter.set_bp_rbj(cutoff, resonance, srate);
607 filter2.set_bp_rbj(cutoff2, resonance, srate);
608 newfgain = ampctl;
609 break;
611 float e2a1 = *params[par_env1toamp];
612 float e2a2 = *params[par_env2toamp];
613 if (e2a1 > 0.f)
614 newfgain *= aenv1;
615 if (e2a2 > 0.f)
616 newfgain *= aenv2;
617 if (moddest[moddest_attenuation] != 0.f)
618 newfgain *= dsp::clip<float>(1 - moddest[moddest_attenuation] * moddest[moddest_attenuation], 0.f, 1.f);
619 fgain_delta = (newfgain - fgain) * (1.0 / step_size);
620 calculate_buffer_oscs(lfov1);
621 lfo1.last = lfov1;
622 lfo2.last = lfov2;
623 switch(filter_type)
625 case flt_lp24:
626 case flt_lpbr:
627 case flt_hpbr: // Oomek's wish
628 calculate_buffer_ser();
629 break;
630 case flt_lp12:
631 case flt_hp12:
632 case flt_bp6:
633 calculate_buffer_single();
634 break;
635 case flt_2lp12:
636 case flt_2bp6:
637 calculate_buffer_stereo();
638 break;
640 apply_fadeout();
643 void monosynth_audio_module::apply_fadeout()
645 if (fadeout.undoing)
647 fadeout.process(buffer2, step_size);
648 if (is_stereo_filter())
649 fadeout2.process(buffer2, step_size);
651 else
653 // stop the sound if the amplitude envelope is not running (if there's any)
654 bool aenv1_on = *params[par_env1toamp] > 0.f, aenv2_on = *params[par_env2toamp] > 0.f;
656 bool do_fadeout = force_fadeout;
658 // if there's no amplitude envelope at all, the fadeout starts at key release
659 if (!aenv1_on && !aenv2_on && !gate)
660 do_fadeout = true;
661 // if ENV1 modulates amplitude, the fadeout will start on ENV1 end too
662 if (aenv1_on && envelope1.state == adsr::STOP)
663 do_fadeout = true;
664 // if ENV2 modulates amplitude, the fadeout will start on ENV2 end too
665 if (aenv2_on && envelope2.state == adsr::STOP)
666 do_fadeout = true;
668 if (do_fadeout || fadeout.undoing || fadeout2.undoing)
670 fadeout.process(buffer, step_size);
671 if (is_stereo_filter())
672 fadeout2.process(buffer2, step_size);
673 if (fadeout.done)
674 stopping = true;
679 void monosynth_audio_module::note_on(int /*channel*/, int note, int vel)
681 queue_note_on = note;
682 queue_note_on_and_off = false;
683 last_key = note;
684 queue_vel = vel / 127.f;
685 stack.push(note);
688 void monosynth_audio_module::note_off(int /*channel*/, int note, int vel)
690 stack.pop(note);
691 if (note == queue_note_on)
693 queue_note_on_and_off = true;
694 return;
696 // If releasing the currently played note, try to get another one from note stack.
697 if (note == last_key) {
698 end_note();
702 void monosynth_audio_module::end_note()
704 if (stack.count())
706 int note;
707 last_key = note = stack.nth(stack.count() - 1);
708 start_freq = freq;
709 target_freq = freq = dsp::note_to_hz(note);
710 porta_time = 0;
711 set_frequency();
712 if (!(legato & 1)) {
713 envelope1.note_on();
714 envelope2.note_on();
715 stopping = false;
716 running = true;
718 return;
720 gate = false;
721 envelope1.note_off();
722 envelope2.note_off();
725 void monosynth_audio_module::channel_pressure(int /*channel*/, int value)
727 inertia_pressure.set_inertia(value * (1.0 / 127.0));
730 void monosynth_audio_module::control_change(int /*channel*/, int controller, int value)
732 switch(controller)
734 case 1:
735 modwheel_value_int = (modwheel_value_int & 127) | (value << 7);
736 modwheel_value = modwheel_value_int / 16383.0;
737 break;
738 case 33:
739 modwheel_value_int = (modwheel_value_int & (127 << 7)) | value;
740 modwheel_value = modwheel_value_int / 16383.0;
741 break;
742 case 120: // all sounds off
743 force_fadeout = true;
744 // fall through
745 case 123: // all notes off
746 gate = false;
747 queue_note_on = -1;
748 envelope1.note_off();
749 envelope2.note_off();
750 stack.clear();
751 break;
755 void monosynth_audio_module::deactivate()
757 gate = false;
758 running = false;
759 stopping = false;
760 envelope1.reset();
761 envelope2.reset();
762 stack.clear();
765 void monosynth_audio_module::set_frequency()
767 float detune_scaled = (detune - 1); // * log(freq / 440);
768 if (*params[par_scaledetune] > 0)
769 detune_scaled *= pow(20.0 / freq, (double)*params[par_scaledetune]);
770 float p1 = 1, p2 = 1;
771 if (moddest[moddest_o1detune] != 0)
772 p1 = pow(2.0, moddest[moddest_o1detune] * (1.0 / 1200.0));
773 if (moddest[moddest_o2detune] != 0)
774 p2 = pow(2.0, moddest[moddest_o2detune] * (1.0 / 1200.0));
775 osc1.set_freq(freq * (1 - detune_scaled) * p1 * inertia_pitchbend.get_last() * lfo_bend * xpose1, srate);
776 osc2.set_freq(freq * (1 + detune_scaled) * p2 * inertia_pitchbend.get_last() * lfo_bend * xpose2, srate);
780 void monosynth_audio_module::params_changed()
782 float sf = 0.001f;
783 envelope1.set(*params[par_env1attack] * sf, *params[par_env1decay] * sf, std::min(0.999f, *params[par_env1sustain]), *params[par_env1release] * sf, srate / step_size, *params[par_env1fade] * sf);
784 envelope2.set(*params[par_env2attack] * sf, *params[par_env2decay] * sf, std::min(0.999f, *params[par_env2sustain]), *params[par_env2release] * sf, srate / step_size, *params[par_env2fade] * sf);
785 filter_type = dsp::fastf2i_drm(*params[par_filtertype]);
786 separation = pow(2.0, *params[par_cutoffsep] / 1200.0);
787 wave1 = dsp::clip(dsp::fastf2i_drm(*params[par_wave1]), 0, (int)wave_count - 1);
788 wave2 = dsp::clip(dsp::fastf2i_drm(*params[par_wave2]), 0, (int)wave_count - 1);
789 detune = pow(2.0, *params[par_detune] / 1200.0);
790 xpose1 = pow(2.0, *params[par_osc1xpose] / 12.0);
791 xpose2 = pow(2.0, *params[par_osc2xpose] / 12.0);
792 xfade = *params[par_oscmix];
793 legato = dsp::fastf2i_drm(*params[par_legato]);
794 master.set_inertia(*params[par_master]);
795 if (running)
796 set_frequency();
797 if (wave1 != prev_wave1 || wave2 != prev_wave2)
798 lookup_waveforms();
802 uint32_t monosynth_audio_module::process(uint32_t offset, uint32_t nsamples, uint32_t inputs_mask, uint32_t outputs_mask)
804 uint32_t op = offset;
805 uint32_t op_end = offset + nsamples;
806 int had_data = 0;
807 while(op < op_end) {
808 if (output_pos == 0)
809 calculate_step();
810 if(op < op_end) {
811 uint32_t ip = output_pos;
812 uint32_t len = std::min(step_size - output_pos, op_end - op);
813 if (running)
815 had_data = 3;
816 if (is_stereo_filter())
817 for(uint32_t i = 0 ; i < len; i++) {
818 float vol = master.get();
819 outs[0][op + i] = buffer[ip + i] * vol;
820 outs[1][op + i] = buffer2[ip + i] * vol;
822 else
823 for(uint32_t i = 0 ; i < len; i++)
824 outs[0][op + i] = outs[1][op + i] = buffer[ip + i] * master.get();
826 else
828 dsp::zero(&outs[0][op], len);
829 dsp::zero(&outs[1][op], len);
831 op += len;
832 output_pos += len;
833 if (output_pos == step_size)
834 output_pos = 0;
838 return had_data;