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>
25 using namespace calf_plugins
;
30 monosynth_audio_module::monosynth_audio_module()
31 : mod_matrix_impl(mod_matrix_data
, &mm_metadata
)
33 , inertia_pitchbend(1)
34 , inertia_pressure(64)
38 void monosynth_audio_module::reset()
45 inertia_pitchbend
.set_now(1.f
);
48 modwheel_value_int
= 0;
49 inertia_cutoff
.set_now(*params
[par_cutoff
]);
50 inertia_pressure
.set_now(0);
56 last_pwshift1
= last_pwshift2
= 0;
57 last_stretch1
= 65536;
60 queue_note_on_and_off
= false;
66 last_filter_type
= -1;
70 void monosynth_audio_module::activate()
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
;
85 static waveform_family
<MONOSYNTH_WAVE_BITS
> waves_data
[wave_count
];
88 enum { S
= 1 << MONOSYNTH_WAVE_BITS
, HS
= S
/ 2, QS
= S
/ 4, QS3
= 3 * QS
};
92 reporter
->report_progress(0, "Precalculating waveforms");
94 // yes these waves don't have really perfect 1/x spectrum because of aliasing
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
++) {
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
;
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
);
141 reporter
->report_progress(50, "Precalculating waveforms");
143 for (int i
= 0; i
< S
; i
++) {
145 float p
= i
* 1.0 / QS3
;
146 data
[i
] = sin(M_PI
* p
* p
* p
);
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;
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
++) {
178 data
[i
] = sin(i
* 2 * M_PI
/ HS
);
181 data
[i
] = sin(i
* 4 * M_PI
/ HS
);
184 data
[i
] = sin(i
* 8 * M_PI
/ HS
);
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
);
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
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
) {
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
;
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
)
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
;
236 if (index
== par_wave1
)
238 float ph
= i
* 1.0 / points
;
241 ph
= (ph
- rnd_start
) * scl
;
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);
251 if (index
== par_filtertype
) {
254 if (subindex
> (is_stereo_filter() ? 1 : 0))
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
);
265 set_channel_color(context
, subindex
);
268 data
[i
] = log(level
) / log(1024.0) + 0.5;
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
;
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;
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);
324 o1phase
= 1 - o1phase
;
325 o1phase
= (o1phase
- rnd_start
) * scl
;
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
;
338 last_unison
+= unison_delta
;
339 last_unison_scale
+= unison_scale_delta
;
341 buffer
[i
] = lerp(r
* osc1val
, osc2val
, cur_xfade
);
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
);
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
);
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
;
402 void monosynth_audio_module::delayed_note_on()
404 force_fadeout
= false;
405 fadeout
.reset_soft();
406 fadeout2
.reset_soft();
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;
421 unison_osc
.phase
= rand() << 16;
426 if (*params
[par_lfo1trig
] <= 0)
428 if (*params
[par_lfo2trig
] <= 0)
430 switch((int)*params
[par_oscmode
])
433 osc2
.phase
= 0x80000000;
436 osc2
.phase
= 0x40000000;
439 osc1
.phase
= osc2
.phase
= 0x40000000;
442 osc1
.phase
= 0x40000000;
443 osc2
.phase
= 0xC0000000;
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;
455 if (legato
>= 2 && !gate
)
459 if (starting
|| !(legato
& 1) || envelope1
.released())
461 if (starting
|| !(legato
& 1) || envelope2
.released())
463 if (!running
|| !(legato
& 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
);
473 if (queue_note_on_and_off
)
476 queue_note_on_and_off
= false;
480 void monosynth_audio_module::set_sample_rate(uint32_t sr
) {
482 crate
= sr
/ step_size
;
483 odcr
= (float)(1.0 / crate
);
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)
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)
504 if (stopping
|| !running
)
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
));
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
;
527 freq
= start_freq
+ (target_freq
- start_freq
) * point
;
528 // freq = start_freq * pow(target_freq / start_freq, point);
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
);
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();
541 float env1
= envelope1
.value
, env2
= envelope2
.value
;
542 float aenv1
= envelope1
.get_amp_value(), aenv2
= envelope2
.get_amp_value();
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
);
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
;
571 filter
.set_lp_rbj(cutoff
, resonance
, srate
);
573 newfgain
= min(0.7f
, 0.7f
/ resonance
) * ampctl
;
576 filter
.set_hp_rbj(cutoff
, resonance
, srate
);
578 newfgain
= min(0.7f
, 0.7f
/ resonance
) * ampctl
;
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
;
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
;
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
;
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
;
601 filter
.set_bp_rbj(cutoff
, resonance
, srate
);
606 filter
.set_bp_rbj(cutoff
, resonance
, srate
);
607 filter2
.set_bp_rbj(cutoff2
, resonance
, srate
);
611 float e2a1
= *params
[par_env1toamp
];
612 float e2a2
= *params
[par_env2toamp
];
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
);
627 case flt_hpbr
: // Oomek's wish
628 calculate_buffer_ser();
633 calculate_buffer_single();
637 calculate_buffer_stereo();
643 void monosynth_audio_module::apply_fadeout()
647 fadeout
.process(buffer2
, step_size
);
648 if (is_stereo_filter())
649 fadeout2
.process(buffer2
, step_size
);
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
)
661 // if ENV1 modulates amplitude, the fadeout will start on ENV1 end too
662 if (aenv1_on
&& envelope1
.state
== adsr::STOP
)
664 // if ENV2 modulates amplitude, the fadeout will start on ENV2 end too
665 if (aenv2_on
&& envelope2
.state
== adsr::STOP
)
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
);
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;
684 queue_vel
= vel
/ 127.f
;
688 void monosynth_audio_module::note_off(int /*channel*/, int note
, int vel
)
691 if (note
== queue_note_on
)
693 queue_note_on_and_off
= true;
696 // If releasing the currently played note, try to get another one from note stack.
697 if (note
== last_key
) {
702 void monosynth_audio_module::end_note()
707 last_key
= note
= stack
.nth(stack
.count() - 1);
709 target_freq
= freq
= dsp::note_to_hz(note
);
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
)
735 modwheel_value_int
= (modwheel_value_int
& 127) | (value
<< 7);
736 modwheel_value
= modwheel_value_int
/ 16383.0;
739 modwheel_value_int
= (modwheel_value_int
& (127 << 7)) | value
;
740 modwheel_value
= modwheel_value_int
/ 16383.0;
742 case 120: // all sounds off
743 force_fadeout
= true;
745 case 123: // all notes off
748 envelope1
.note_off();
749 envelope2
.note_off();
755 void monosynth_audio_module::deactivate()
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()
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
]);
797 if (wave1
!= prev_wave1
|| wave2
!= prev_wave2
)
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
;
811 uint32_t ip
= output_pos
;
812 uint32_t len
= std::min(step_size
- output_pos
, op_end
- op
);
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
;
823 for(uint32_t i
= 0 ; i
< len
; i
++)
824 outs
[0][op
+ i
] = outs
[1][op
+ i
] = buffer
[ip
+ i
] * master
.get();
828 dsp::zero(&outs
[0][op
], len
);
829 dsp::zero(&outs
[1][op
], len
);
833 if (output_pos
== step_size
)