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 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;
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);
325 o1phase
= 1 - o1phase
;
326 o1phase
= (o1phase
- rnd_start
) * scl
;
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
;
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
);
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
);
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
);
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
;
404 void monosynth_audio_module::delayed_note_on()
406 force_fadeout
= false;
407 fadeout
.reset_soft();
408 fadeout2
.reset_soft();
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;
423 unison_osc
.phase
= rand() << 16;
428 if (*params
[par_lfo1trig
] <= 0)
430 if (*params
[par_lfo2trig
] <= 0)
432 switch((int)*params
[par_oscmode
])
435 osc2
.phase
= 0x80000000;
438 osc2
.phase
= 0x40000000;
441 osc1
.phase
= osc2
.phase
= 0x40000000;
444 osc1
.phase
= 0x40000000;
445 osc2
.phase
= 0xC0000000;
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;
457 if (legato
>= 2 && !gate
)
461 if (starting
|| !(legato
& 1) || envelope1
.released())
463 if (starting
|| !(legato
& 1) || envelope2
.released())
465 if (!running
|| !(legato
& 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
);
475 if (queue_note_on_and_off
)
478 queue_note_on_and_off
= false;
482 void monosynth_audio_module::set_sample_rate(uint32_t sr
) {
484 crate
= sr
/ step_size
;
485 odcr
= (float)(1.0 / crate
);
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)
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)
506 if (stopping
|| !running
)
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
));
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
;
529 freq
= start_freq
+ (target_freq
- start_freq
) * point
;
530 // freq = start_freq * pow(target_freq / start_freq, point);
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
);
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();
543 float env1
= envelope1
.value
, env2
= envelope2
.value
;
544 float aenv1
= envelope1
.get_amp_value(), aenv2
= envelope2
.get_amp_value();
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
);
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
;
573 filter
.set_lp_rbj(cutoff
, resonance
, srate
);
575 newfgain
= min(0.7f
, 0.7f
/ resonance
) * ampctl
;
578 filter
.set_hp_rbj(cutoff
, resonance
, srate
);
580 newfgain
= min(0.7f
, 0.7f
/ resonance
) * ampctl
;
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
;
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
;
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
;
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
;
603 filter
.set_bp_rbj(cutoff
, resonance
, srate
);
608 filter
.set_bp_rbj(cutoff
, resonance
, srate
);
609 filter2
.set_bp_rbj(cutoff2
, resonance
, srate
);
613 float e2a1
= *params
[par_env1toamp
];
614 float e2a2
= *params
[par_env2toamp
];
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
);
629 case flt_hpbr
: // Oomek's wish
630 calculate_buffer_ser();
635 calculate_buffer_single();
639 calculate_buffer_stereo();
645 void monosynth_audio_module::apply_fadeout()
649 fadeout
.process(buffer2
, step_size
);
650 if (is_stereo_filter())
651 fadeout2
.process(buffer2
, step_size
);
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
)
663 // if ENV1 modulates amplitude, the fadeout will start on ENV1 end too
664 if (aenv1_on
&& envelope1
.state
== adsr::STOP
)
666 // if ENV2 modulates amplitude, the fadeout will start on ENV2 end too
667 if (aenv2_on
&& envelope2
.state
== adsr::STOP
)
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
);
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;
686 queue_vel
= vel
/ 127.f
;
690 void monosynth_audio_module::note_off(int /*channel*/, int note
, int vel
)
693 if (note
== queue_note_on
)
695 queue_note_on_and_off
= true;
698 // If releasing the currently played note, try to get another one from note stack.
699 if (note
== last_key
) {
704 void monosynth_audio_module::end_note()
709 last_key
= note
= stack
.nth(stack
.count() - 1);
711 target_freq
= freq
= dsp::note_to_hz(note
);
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
)
737 modwheel_value_int
= (modwheel_value_int
& 127) | (value
<< 7);
738 modwheel_value
= modwheel_value_int
/ 16383.0;
741 modwheel_value_int
= (modwheel_value_int
& (127 << 7)) | value
;
742 modwheel_value
= modwheel_value_int
/ 16383.0;
744 case 120: // all sounds off
745 force_fadeout
= true;
747 case 123: // all notes off
750 envelope1
.note_off();
751 envelope2
.note_off();
757 void monosynth_audio_module::deactivate()
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()
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
]);
799 if (wave1
!= prev_wave1
|| wave2
!= prev_wave2
)
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
;
813 uint32_t ip
= output_pos
;
814 uint32_t len
= std::min(step_size
- output_pos
, op_end
- op
);
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
;
825 for(uint32_t i
= 0 ; i
< len
; i
++)
826 outs
[0][op
+ i
] = outs
[1][op
+ i
] = buffer
[ip
+ i
] * master
.get();
830 dsp::zero(&outs
[0][op
], len
);
831 dsp::zero(&outs
[1][op
], len
);
835 if (output_pos
== step_size
)