Fix bypass crossfade read/write out of bounds
[calf.git] / src / monosynth.cpp
blob65982d154c4a11891c7a4200b3fbd45142b96748
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 uint32_t vlfo = this->lfo1.phase;
306 static const int muls[8] = { 33, -47, 53, -67, 87, -101, 121, -139 };
307 float unison = *params[par_o2unison] + moddest[moddest_o2unisonamp] * 0.01;
308 float unison_scale = 1.0, unison_delta = 0.0, last_unison_scale = 1.0, unison_scale_delta = 0.0;
309 if (unison > 0)
311 float freq = fabs(*params[par_o2unisonfrq] / muls[7]);
312 if (moddest[moddest_o2unisondetune] != 0)
313 freq *= pow(2.0, moddest[moddest_o2unisondetune]);
314 unison_osc.set_freq(freq, srate);
315 last_unison_scale = 1.0 / (1.0 + 2 * last_unison);
316 unison_scale = 1.0 / (1.0 + 2 * unison);
317 unison_delta = (unison - last_unison) * (1.0 / step_size);
318 unison_scale_delta = (unison_scale - last_unison_scale) * (1.0 / step_size);
320 for (uint32_t i = 0; i < step_size; i++)
322 //buffer[i] = lerp(osc1.get_phaseshifted(shift1, mix1), osc2.get_phaseshifted(shift2, mix2), cur_xfade);
323 float o1phase = osc1.phase / (65536.0 * 65536.0);
324 if (o1phase < 0.5)
325 o1phase = 1 - o1phase;
326 o1phase = (o1phase - rnd_start) * scl;
327 if (o1phase < 0)
328 o1phase = 0;
329 float r = 1.0 - o1phase * o1phase;
330 float osc1val = osc1.get_phasedist(stretch1, shift1, mix1);
331 float osc2val = osc2.get_phaseshifted(shift2, mix2);
332 if (unison > 0 || last_unison > 0)
334 for (int j = 0; j < 8; j++)
335 osc2val += last_unison * osc2.get_phaseshifted2(shift2, unison_osc.phase * muls[j], mix2);
336 osc2val *= last_unison_scale;
338 unison_osc.step();
339 last_unison += unison_delta;
340 last_unison_scale += unison_scale_delta;
342 vlfo += this->lfo1.phasedelta / step_size;
343 buffer[i] = lerp(r * osc1val, osc2val, cur_xfade);
344 osc1.advance();
345 osc2.advance();
346 shift1 += shift_delta1;
347 shift2 += shift_delta2;
348 stretch1 += stretch_delta1;
349 cur_xfade += xfade_step;
351 last_xfade = new_xfade;
352 last_unison = unison;
355 void monosynth_audio_module::calculate_buffer_ser()
357 filter.big_step(1.0 / step_size);
358 filter2.big_step(1.0 / step_size);
359 for (uint32_t i = 0; i < step_size; i++)
361 float wave = buffer[i] * fgain;
362 wave = filter.process(wave);
363 wave = filter2.process(wave);
364 buffer[i] = wave;
365 fgain += fgain_delta;
369 void monosynth_audio_module::calculate_buffer_single()
371 filter.big_step(1.0 / step_size);
372 for (uint32_t i = 0; i < step_size; i++)
374 float wave = buffer[i] * fgain;
375 wave = filter.process(wave);
376 buffer[i] = wave;
377 fgain += fgain_delta;
381 void monosynth_audio_module::calculate_buffer_stereo()
383 filter.big_step(1.0 / step_size);
384 filter2.big_step(1.0 / step_size);
385 for (uint32_t i = 0; i < step_size; i++)
387 float wave1 = buffer[i] * fgain;
388 buffer[i] = fgain * filter.process(wave1);
389 buffer2[i] = fgain * filter2.process(wave1);
390 fgain += fgain_delta;
394 void monosynth_audio_module::lookup_waveforms()
396 osc1.waveform = waves[wave1 == wave_sqr ? wave_saw : wave1].get_level((uint32_t)(((uint64_t)osc1.phasedelta) * last_stretch1 >> 16));
397 osc2.waveform = waves[wave2 == wave_sqr ? wave_saw : wave2].get_level(osc2.phasedelta);
398 if (!osc1.waveform) osc1.waveform = silence;
399 if (!osc2.waveform) osc2.waveform = silence;
400 prev_wave1 = wave1;
401 prev_wave2 = wave2;
404 void monosynth_audio_module::delayed_note_on()
406 force_fadeout = false;
407 fadeout.reset_soft();
408 fadeout2.reset_soft();
409 porta_time = 0.f;
410 start_freq = freq;
411 target_freq = freq = 440 * pow(2.0, (queue_note_on - 69) / 12.0);
412 velocity = queue_vel;
413 ampctl = 1.0 + (queue_vel - 1.0) * *params[par_vel2amp];
414 fltctl = 1.0 + (queue_vel - 1.0) * *params[par_vel2filter];
415 bool starting = false;
417 if (!running)
419 starting = true;
420 if (legato >= 2)
421 porta_time = -1.f;
422 last_xfade = xfade;
423 unison_osc.phase = rand() << 16;
424 osc1.reset();
425 osc2.reset();
426 filter.reset();
427 filter2.reset();
428 if (*params[par_lfo1trig] <= 0)
429 lfo1.reset();
430 if (*params[par_lfo2trig] <= 0)
431 lfo2.reset();
432 switch((int)*params[par_oscmode])
434 case 1:
435 osc2.phase = 0x80000000;
436 break;
437 case 2:
438 osc2.phase = 0x40000000;
439 break;
440 case 3:
441 osc1.phase = osc2.phase = 0x40000000;
442 break;
443 case 4:
444 osc1.phase = 0x40000000;
445 osc2.phase = 0xC0000000;
446 break;
447 case 5:
448 // rand() is crap, but I don't have any better RNG in Calf yet
449 osc1.phase = rand() << 16;
450 osc2.phase = rand() << 16;
451 break;
452 default:
453 break;
455 running = true;
457 if (legato >= 2 && !gate)
458 porta_time = -1.f;
459 gate = true;
460 stopping = false;
461 if (starting || !(legato & 1) || envelope1.released())
462 envelope1.note_on();
463 if (starting || !(legato & 1) || envelope2.released())
464 envelope2.note_on();
465 if (!running || !(legato & 1))
466 lfo_clock = 0.f;
467 envelope1.advance();
468 envelope2.advance();
469 queue_note_on = -1;
470 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};
471 calculate_modmatrix(moddest, moddest_count, modsrc);
472 set_frequency();
473 lookup_waveforms();
475 if (queue_note_on_and_off)
477 end_note();
478 queue_note_on_and_off = false;
482 void monosynth_audio_module::set_sample_rate(uint32_t sr) {
483 srate = sr;
484 crate = sr / step_size;
485 odcr = (float)(1.0 / crate);
486 fgain = 0.f;
487 fgain_delta = 0.f;
488 inertia_cutoff.ramp.set_length(crate / 30); // 1/30s
489 inertia_pitchbend.ramp.set_length(crate / 30); // 1/30s
490 master.set_sample_rate(sr);
493 float monosynth_audio_module::get_lfo(dsp::triangle_lfo &lfo, int param)
495 if (*params[param] <= 0)
496 return lfo.get();
497 float pt = lfo_clock / *params[param];
498 return lfo.get() * std::min(1.0f, pt);
501 void monosynth_audio_module::calculate_step()
503 if (queue_note_on != -1)
504 delayed_note_on();
505 else
506 if (stopping || !running)
508 running = false;
509 envelope1.advance();
510 envelope2.advance();
511 lfo1.get();
512 lfo2.get();
513 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};
514 calculate_modmatrix(moddest, moddest_count, modsrc);
515 last_stretch1 = (int32_t)(65536 * dsp::clip(*params[par_stretch1] + 0.01f * moddest[moddest_o1stretch], 1.f, 16.f));
516 return;
518 lfo1.set_freq(*params[par_lforate], crate);
519 lfo2.set_freq(*params[par_lfo2rate], crate);
520 float porta_total_time = *params[par_portamento] * 0.001f;
522 if (porta_total_time >= 0.00101f && porta_time >= 0) {
523 // XXXKF this is criminal, optimize!
524 float point = porta_time / porta_total_time;
525 if (point >= 1.0f) {
526 freq = target_freq;
527 porta_time = -1;
528 } else {
529 freq = start_freq + (target_freq - start_freq) * point;
530 // freq = start_freq * pow(target_freq / start_freq, point);
531 porta_time += odcr;
534 float lfov1 = get_lfo(lfo1, par_lfodelay);
535 lfov1 = lfov1 * dsp::lerp(1.f, modwheel_value, *params[par_mwhl_lfo]);
536 float lfov2 = get_lfo(lfo2, par_lfodelay);
537 lfo_clock += odcr;
538 if (fabs(*params[par_lfopitch]) > small_value<float>())
539 lfo_bend = pow(2.0f, *params[par_lfopitch] * lfov1 * (1.f / 1200.0f));
540 inertia_pitchbend.step();
541 envelope1.advance();
542 envelope2.advance();
543 float env1 = envelope1.value, env2 = envelope2.value;
544 float aenv1 = envelope1.get_amp_value(), aenv2 = envelope2.get_amp_value();
546 // mod matrix
547 // this should be optimized heavily; I think I'll do it when MIDI in Ardour 3 gets stable :>
548 float modsrc[modsrc_count] = { 1.f, velocity, inertia_pressure.get(), modwheel_value, env1, env2, 0.5f+0.5f*lfov1, 0.5f+0.5f*lfov2};
549 calculate_modmatrix(moddest, moddest_count, modsrc);
551 set_frequency();
552 inertia_cutoff.set_inertia(*params[par_cutoff]);
553 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));
554 if (*params[par_keyfollow] > 0.01f)
555 cutoff *= pow(freq / 264.f, *params[par_keyfollow]);
556 cutoff = dsp::clip(cutoff , 10.f, 18000.f);
557 float resonance = *params[par_resonance];
558 float e2r1 = *params[par_env1tores];
559 resonance = resonance * (1 - e2r1) + (0.7 + (resonance - 0.7) * env1 * env1) * e2r1;
560 float e2r2 = *params[par_env2tores];
561 resonance = resonance * (1 - e2r2) + (0.7 + (resonance - 0.7) * env2 * env2) * e2r2 + moddest[moddest_resonance];
562 float cutoff2 = dsp::clip(cutoff * separation, 10.f, 18000.f);
563 float newfgain = 0.f;
564 if (filter_type != last_filter_type)
566 filter.y2 = filter.y1 = filter.x2 = filter.x1 = filter.y1;
567 filter2.y2 = filter2.y1 = filter2.x2 = filter2.x1 = filter2.y1;
568 last_filter_type = filter_type;
570 switch(filter_type)
572 case flt_lp12:
573 filter.set_lp_rbj(cutoff, resonance, srate);
574 filter2.set_null();
575 newfgain = min(0.7f, 0.7f / resonance) * ampctl;
576 break;
577 case flt_hp12:
578 filter.set_hp_rbj(cutoff, resonance, srate);
579 filter2.set_null();
580 newfgain = min(0.7f, 0.7f / resonance) * ampctl;
581 break;
582 case flt_lp24:
583 filter.set_lp_rbj(cutoff, resonance, srate);
584 filter2.set_lp_rbj(cutoff2, resonance, srate);
585 newfgain = min(0.5f, 0.5f / resonance) * ampctl;
586 break;
587 case flt_lpbr:
588 filter.set_lp_rbj(cutoff, resonance, srate);
589 filter2.set_br_rbj(cutoff2, 1.0 / resonance, srate);
590 newfgain = min(0.5f, 0.5f / resonance) * ampctl;
591 break;
592 case flt_hpbr:
593 filter.set_hp_rbj(cutoff, resonance, srate);
594 filter2.set_br_rbj(cutoff2, 1.0 / resonance, srate);
595 newfgain = min(0.5f, 0.5f / resonance) * ampctl;
596 break;
597 case flt_2lp12:
598 filter.set_lp_rbj(cutoff, resonance, srate);
599 filter2.set_lp_rbj(cutoff2, resonance, srate);
600 newfgain = min(0.7f, 0.7f / resonance) * ampctl;
601 break;
602 case flt_bp6:
603 filter.set_bp_rbj(cutoff, resonance, srate);
604 filter2.set_null();
605 newfgain = ampctl;
606 break;
607 case flt_2bp6:
608 filter.set_bp_rbj(cutoff, resonance, srate);
609 filter2.set_bp_rbj(cutoff2, resonance, srate);
610 newfgain = ampctl;
611 break;
613 float e2a1 = *params[par_env1toamp];
614 float e2a2 = *params[par_env2toamp];
615 if (e2a1 > 0.f)
616 newfgain *= aenv1;
617 if (e2a2 > 0.f)
618 newfgain *= aenv2;
619 if (moddest[moddest_attenuation] != 0.f)
620 newfgain *= dsp::clip<float>(1 - moddest[moddest_attenuation] * moddest[moddest_attenuation], 0.f, 1.f);
621 fgain_delta = (newfgain - fgain) * (1.0 / step_size);
622 calculate_buffer_oscs(lfov1);
623 lfo1.last = lfov1;
624 lfo2.last = lfov2;
625 switch(filter_type)
627 case flt_lp24:
628 case flt_lpbr:
629 case flt_hpbr: // Oomek's wish
630 calculate_buffer_ser();
631 break;
632 case flt_lp12:
633 case flt_hp12:
634 case flt_bp6:
635 calculate_buffer_single();
636 break;
637 case flt_2lp12:
638 case flt_2bp6:
639 calculate_buffer_stereo();
640 break;
642 apply_fadeout();
645 void monosynth_audio_module::apply_fadeout()
647 if (fadeout.undoing)
649 fadeout.process(buffer2, step_size);
650 if (is_stereo_filter())
651 fadeout2.process(buffer2, step_size);
653 else
655 // stop the sound if the amplitude envelope is not running (if there's any)
656 bool aenv1_on = *params[par_env1toamp] > 0.f, aenv2_on = *params[par_env2toamp] > 0.f;
658 bool do_fadeout = force_fadeout;
660 // if there's no amplitude envelope at all, the fadeout starts at key release
661 if (!aenv1_on && !aenv2_on && !gate)
662 do_fadeout = true;
663 // if ENV1 modulates amplitude, the fadeout will start on ENV1 end too
664 if (aenv1_on && envelope1.state == adsr::STOP)
665 do_fadeout = true;
666 // if ENV2 modulates amplitude, the fadeout will start on ENV2 end too
667 if (aenv2_on && envelope2.state == adsr::STOP)
668 do_fadeout = true;
670 if (do_fadeout || fadeout.undoing || fadeout2.undoing)
672 fadeout.process(buffer, step_size);
673 if (is_stereo_filter())
674 fadeout2.process(buffer2, step_size);
675 if (fadeout.done)
676 stopping = true;
681 void monosynth_audio_module::note_on(int /*channel*/, int note, int vel)
683 queue_note_on = note;
684 queue_note_on_and_off = false;
685 last_key = note;
686 queue_vel = vel / 127.f;
687 stack.push(note);
690 void monosynth_audio_module::note_off(int /*channel*/, int note, int vel)
692 stack.pop(note);
693 if (note == queue_note_on)
695 queue_note_on_and_off = true;
696 return;
698 // If releasing the currently played note, try to get another one from note stack.
699 if (note == last_key) {
700 end_note();
704 void monosynth_audio_module::end_note()
706 if (stack.count())
708 int note;
709 last_key = note = stack.nth(stack.count() - 1);
710 start_freq = freq;
711 target_freq = freq = dsp::note_to_hz(note);
712 porta_time = 0;
713 set_frequency();
714 if (!(legato & 1)) {
715 envelope1.note_on();
716 envelope2.note_on();
717 stopping = false;
718 running = true;
720 return;
722 gate = false;
723 envelope1.note_off();
724 envelope2.note_off();
727 void monosynth_audio_module::channel_pressure(int /*channel*/, int value)
729 inertia_pressure.set_inertia(value * (1.0 / 127.0));
732 void monosynth_audio_module::control_change(int /*channel*/, int controller, int value)
734 switch(controller)
736 case 1:
737 modwheel_value_int = (modwheel_value_int & 127) | (value << 7);
738 modwheel_value = modwheel_value_int / 16383.0;
739 break;
740 case 33:
741 modwheel_value_int = (modwheel_value_int & (127 << 7)) | value;
742 modwheel_value = modwheel_value_int / 16383.0;
743 break;
744 case 120: // all sounds off
745 force_fadeout = true;
746 // fall through
747 case 123: // all notes off
748 gate = false;
749 queue_note_on = -1;
750 envelope1.note_off();
751 envelope2.note_off();
752 stack.clear();
753 break;
757 void monosynth_audio_module::deactivate()
759 gate = false;
760 running = false;
761 stopping = false;
762 envelope1.reset();
763 envelope2.reset();
764 stack.clear();
767 void monosynth_audio_module::set_frequency()
769 float detune_scaled = (detune - 1); // * log(freq / 440);
770 if (*params[par_scaledetune] > 0)
771 detune_scaled *= pow(20.0 / freq, (double)*params[par_scaledetune]);
772 float p1 = 1, p2 = 1;
773 if (moddest[moddest_o1detune] != 0)
774 p1 = pow(2.0, moddest[moddest_o1detune] * (1.0 / 1200.0));
775 if (moddest[moddest_o2detune] != 0)
776 p2 = pow(2.0, moddest[moddest_o2detune] * (1.0 / 1200.0));
777 osc1.set_freq(freq * (1 - detune_scaled) * p1 * inertia_pitchbend.get_last() * lfo_bend * xpose1, srate);
778 osc2.set_freq(freq * (1 + detune_scaled) * p2 * inertia_pitchbend.get_last() * lfo_bend * xpose2, srate);
782 void monosynth_audio_module::params_changed()
784 float sf = 0.001f;
785 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);
786 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);
787 filter_type = dsp::fastf2i_drm(*params[par_filtertype]);
788 separation = pow(2.0, *params[par_cutoffsep] / 1200.0);
789 wave1 = dsp::clip(dsp::fastf2i_drm(*params[par_wave1]), 0, (int)wave_count - 1);
790 wave2 = dsp::clip(dsp::fastf2i_drm(*params[par_wave2]), 0, (int)wave_count - 1);
791 detune = pow(2.0, *params[par_detune] / 1200.0);
792 xpose1 = pow(2.0, *params[par_osc1xpose] / 12.0);
793 xpose2 = pow(2.0, *params[par_osc2xpose] / 12.0);
794 xfade = *params[par_oscmix];
795 legato = dsp::fastf2i_drm(*params[par_legato]);
796 master.set_inertia(*params[par_master]);
797 if (running)
798 set_frequency();
799 if (wave1 != prev_wave1 || wave2 != prev_wave2)
800 lookup_waveforms();
804 uint32_t monosynth_audio_module::process(uint32_t offset, uint32_t nsamples, uint32_t inputs_mask, uint32_t outputs_mask)
806 uint32_t op = offset;
807 uint32_t op_end = offset + nsamples;
808 int had_data = 0;
809 while(op < op_end) {
810 if (output_pos == 0)
811 calculate_step();
812 if(op < op_end) {
813 uint32_t ip = output_pos;
814 uint32_t len = std::min(step_size - output_pos, op_end - op);
815 if (running)
817 had_data = 3;
818 if (is_stereo_filter())
819 for(uint32_t i = 0 ; i < len; i++) {
820 float vol = master.get();
821 outs[0][op + i] = buffer[ip + i] * vol;
822 outs[1][op + i] = buffer2[ip + i] * vol;
824 else
825 for(uint32_t i = 0 ; i < len; i++)
826 outs[0][op + i] = outs[1][op + i] = buffer[ip + i] * master.get();
828 else
830 dsp::zero(&outs[0][op], len);
831 dsp::zero(&outs[1][op], len);
833 op += len;
834 output_pos += len;
835 if (output_pos == step_size)
836 output_pos = 0;
840 return had_data;