Button: emit slopes (fix for stuck modulaor plug-ins)
[calf.git] / src / modules_tools.cpp
blobdd7f792469d22d924f4c069b0d95a77f598b1b9c
1 /* Calf DSP plugin pack
2 * Assorted plugins
4 * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen
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 <limits.h>
22 #include <memory.h>
23 #include <math.h>
24 #include <fftw3.h>
25 #include <calf/giface.h>
26 #include <calf/modules_tools.h>
27 #include <calf/modules_dev.h>
28 #include <sys/time.h>
29 #include <calf/utils.h>
32 using namespace dsp;
33 using namespace calf_plugins;
35 #define SET_IF_CONNECTED(name) if (params[AM::param_##name] != NULL) *params[AM::param_##name] = name;
36 #define sinc(x) (!x) ? 1 : sin(M_PI * x)/(M_PI * x);
38 /**********************************************************************
39 * STEREO TOOLS by Markus Schmidt
40 **********************************************************************/
42 stereo_audio_module::stereo_audio_module() {
43 active = false;
44 _phase = -1;
46 stereo_audio_module::~stereo_audio_module() {
47 free(buffer);
49 void stereo_audio_module::activate() {
50 active = true;
53 void stereo_audio_module::deactivate() {
54 active = false;
57 void stereo_audio_module::params_changed() {
58 float slev = 2 * *params[param_slev]; // stereo level ( -2 -> 2 )
59 float sbal = 1 + *params[param_sbal]; // stereo balance ( 0 -> 2 )
60 float mlev = 2 * *params[param_mlev]; // mono level ( -2 -> 2 )
61 float mpan = 1 + *params[param_mpan]; // mono pan ( 0 -> 2 )
63 switch((int)*params[param_mode])
65 case 0:
66 default:
67 //LR->LR
68 LL = (mlev * (2.f - mpan) + slev * (2.f - sbal));
69 LR = (mlev * mpan - slev * sbal);
70 RL = (mlev * (2.f - mpan) - slev * (2.f - sbal));
71 RR = (mlev * mpan + slev * sbal);
72 break;
73 case 1:
74 //LR->MS
75 LL = (2.f - mpan) * (2.f - sbal);
76 LR = mpan * (2.f - sbal) * -1;
77 RL = (2.f - mpan) * sbal;
78 RR = mpan * sbal;
79 break;
80 case 2:
81 //MS->LR
82 LL = mlev * (2.f - sbal);
83 LR = mlev * mpan;
84 RL = slev * (2.f - sbal);
85 RR = slev * sbal * -1;
86 break;
87 case 3:
88 case 4:
89 case 5:
90 case 6:
91 //LR->LL
92 LL = 0.f;
93 LR = 0.f;
94 RL = 0.f;
95 RR = 0.f;
96 break;
98 if(*params[param_stereo_phase] != _phase) {
99 _phase = *params[param_stereo_phase];
100 _phase_cos_coef = cos(_phase / 180 * M_PI);
101 _phase_sin_coef = sin(_phase / 180 * M_PI);
103 if(*params[param_sc_level] != _sc_level) {
104 _sc_level = *params[param_sc_level];
105 _inv_atan_shape = 1.0 / atan(_sc_level);
109 uint32_t stereo_audio_module::process(uint32_t offset, uint32_t numsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
110 bool bypassed = bypass.update(*params[param_bypass] > 0.5f, numsamples);
111 uint32_t orig_offset = offset;
112 for(uint32_t i = offset; i < offset + numsamples; i++) {
113 if(bypassed) {
114 outs[0][i] = ins[0][i];
115 outs[1][i] = ins[1][i];
116 meter_inL = 0.f;
117 meter_inR = 0.f;
118 meter_outL = 0.f;
119 meter_outR = 0.f;
120 } else {
121 meter_inL = 0.f;
122 meter_inR = 0.f;
123 meter_outL = 0.f;
124 meter_outR = 0.f;
126 float L = ins[0][i];
127 float R = ins[1][i];
129 // levels in
130 L *= *params[param_level_in];
131 R *= *params[param_level_in];
133 // balance in
134 L *= (1.f - std::max(0.f, *params[param_balance_in]));
135 R *= (1.f + std::min(0.f, *params[param_balance_in]));
137 // copy / flip / mono ...
138 switch((int)*params[param_mode])
140 case 0:
141 default:
142 // LR > LR
143 break;
144 case 1:
145 // LR > MS
146 break;
147 case 2:
148 // MS > LR
149 break;
150 case 3:
151 // LR > LL
152 R = L;
153 break;
154 case 4:
155 // LR > RR
156 L = R;
157 break;
158 case 5:
159 // LR > L+R
160 L = (L + R) / 2;
161 R = L;
162 break;
163 case 6:
164 // LR > RL
165 float tmp = L;
166 L = R;
167 R = tmp;
168 break;
171 // softclip
172 if(*params[param_softclip]) {
173 // int ph;
174 // ph = L / fabs(L);
175 // L = L > 0.63 ? ph * (0.63 + 0.36 * (1 - pow(MATH_E, (1.f / 3) * (0.63 + L * ph)))) : L;
176 // ph = R / fabs(R);
177 // R = R > 0.63 ? ph * (0.63 + 0.36 * (1 - pow(MATH_E, (1.f / 3) * (0.63 + R * ph)))) : R;
178 R = _inv_atan_shape * atan(R * _sc_level);
179 L = _inv_atan_shape * atan(L * _sc_level);
182 // GUI stuff
183 meter_inL = L;
184 meter_inR = R;
186 // mute
187 L *= (1 - floor(*params[param_mute_l] + 0.5));
188 R *= (1 - floor(*params[param_mute_r] + 0.5));
190 // phase
191 L *= (2 * (1 - floor(*params[param_phase_l] + 0.5))) - 1;
192 R *= (2 * (1 - floor(*params[param_phase_r] + 0.5))) - 1;
194 // LR/MS
195 L += LL*L + RL*R;
196 R += RR*R + LR*L;
198 // delay
199 buffer[pos] = L;
200 buffer[pos + 1] = R;
202 int nbuf = srate * (fabs(*params[param_delay]) / 1000.f);
203 nbuf -= nbuf % 2;
204 if(*params[param_delay] > 0.f) {
205 R = buffer[(pos - (int)nbuf + 1 + buffer_size) % buffer_size];
206 } else if (*params[param_delay] < 0.f) {
207 L = buffer[(pos - (int)nbuf + buffer_size) % buffer_size];
210 // stereo base
211 float _sb = *params[param_stereo_base];
212 if(_sb < 0) _sb *= 0.5;
214 float __l = L +_sb * L - _sb * R;
215 float __r = R + _sb * R - _sb * L;
217 L = __l;
218 R = __r;
220 // stereo phase
221 __l = L * _phase_cos_coef - R * _phase_sin_coef;
222 __r = L * _phase_sin_coef + R * _phase_cos_coef;
224 L = __l;
225 R = __r;
227 pos = (pos + 2) % buffer_size;
229 // balance out
230 L *= (1.f - std::max(0.f, *params[param_balance_out]));
231 R *= (1.f + std::min(0.f, *params[param_balance_out]));
233 // level
234 L *= *params[param_level_out];
235 R *= *params[param_level_out];
237 //output
238 outs[0][i] = L;
239 outs[1][i] = R;
241 meter_outL = L;
242 meter_outR = R;
244 // phase meter
245 if(fabs(L) > 0.001 and fabs(R) > 0.001) {
246 meter_phase = fabs(fabs(L+R) > 0.000000001 ? sin(fabs((L-R)/(L+R))) : 0.f);
247 } else {
248 meter_phase = 0.f;
251 float values[] = {meter_inL, meter_inR, meter_outL, meter_outR};
252 meters.process(values);
254 if (!bypassed)
255 bypass.crossfade(ins, outs, 2, orig_offset, numsamples);
256 meters.fall(numsamples);
257 return outputs_mask;
260 void stereo_audio_module::set_sample_rate(uint32_t sr)
262 srate = sr;
263 // rebuild buffer
264 buffer_size = (int)(srate * 0.05 * 2.f); // buffer size attack rate multiplied by 2 channels
265 buffer = (float*) calloc(buffer_size, sizeof(float));
266 pos = 0;
267 int meter[] = {param_meter_inL, param_meter_inR, param_meter_outL, param_meter_outR};
268 int clip[] = {param_clip_inL, param_clip_inR, param_clip_outL, param_clip_outR};
269 meters.init(params, meter, clip, 4, sr);
272 /**********************************************************************
273 * MONO INPUT by Markus Schmidt
274 **********************************************************************/
276 mono_audio_module::mono_audio_module() {
277 active = false;
278 meter_in = 0.f;
279 meter_outL = 0.f;
280 meter_outR = 0.f;
281 _phase = -1.f;
283 mono_audio_module::~mono_audio_module() {
284 free(buffer);
286 void mono_audio_module::activate() {
287 active = true;
290 void mono_audio_module::deactivate() {
291 active = false;
294 void mono_audio_module::params_changed() {
295 if(*params[param_sc_level] != _sc_level) {
296 _sc_level = *params[param_sc_level];
297 _inv_atan_shape = 1.0 / atan(_sc_level);
299 if(*params[param_stereo_phase] != _phase) {
300 _phase = *params[param_stereo_phase];
301 _phase_cos_coef = cos(_phase / 180 * M_PI);
302 _phase_sin_coef = sin(_phase / 180 * M_PI);
306 uint32_t mono_audio_module::process(uint32_t offset, uint32_t numsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
307 bool bypassed = bypass.update(*params[param_bypass] > 0.5f, numsamples);
308 uint32_t orig_offset = offset;
309 for(uint32_t i = offset; i < offset + numsamples; i++) {
310 if(bypassed) {
311 outs[0][i] = ins[0][i];
312 outs[1][i] = ins[0][i];
313 meter_in = 0.f;
314 meter_outL = 0.f;
315 meter_outR = 0.f;
316 } else {
317 meter_in = 0.f;
318 meter_outL = 0.f;
319 meter_outR = 0.f;
321 float L = ins[0][i];
323 // levels in
324 L *= *params[param_level_in];
326 // softclip
327 if(*params[param_softclip]) {
328 //int ph = L / fabs(L);
329 //L = L > 0.63 ? ph * (0.63 + 0.36 * (1 - pow(MATH_E, (1.f / 3) * (0.63 + L * ph)))) : L;
330 L = _inv_atan_shape * atan(L * _sc_level);
333 // GUI stuff
334 meter_in = L;
336 float R = L;
338 // mute
339 L *= (1 - floor(*params[param_mute_l] + 0.5));
340 R *= (1 - floor(*params[param_mute_r] + 0.5));
342 // phase
343 L *= (2 * (1 - floor(*params[param_phase_l] + 0.5))) - 1;
344 R *= (2 * (1 - floor(*params[param_phase_r] + 0.5))) - 1;
346 // delay
347 buffer[pos] = L;
348 buffer[pos + 1] = R;
350 int nbuf = srate * (fabs(*params[param_delay]) / 1000.f);
351 nbuf -= nbuf % 2;
352 if(*params[param_delay] > 0.f) {
353 R = buffer[(pos - (int)nbuf + 1 + buffer_size) % buffer_size];
354 } else if (*params[param_delay] < 0.f) {
355 L = buffer[(pos - (int)nbuf + buffer_size) % buffer_size];
358 // stereo base
359 float _sb = *params[param_stereo_base];
360 if(_sb < 0) _sb *= 0.5;
362 float __l = L +_sb * L - _sb * R;
363 float __r = R + _sb * R - _sb * L;
365 L = __l;
366 R = __r;
368 // stereo phase
369 __l = L * _phase_cos_coef - R * _phase_sin_coef;
370 __r = L * _phase_sin_coef + R * _phase_cos_coef;
372 L = __l;
373 R = __r;
375 pos = (pos + 2) % buffer_size;
377 // balance out
378 L *= (1.f - std::max(0.f, *params[param_balance_out]));
379 R *= (1.f + std::min(0.f, *params[param_balance_out]));
381 // level
382 L *= *params[param_level_out];
383 R *= *params[param_level_out];
385 //output
386 outs[0][i] = L;
387 outs[1][i] = R;
389 meter_outL = L;
390 meter_outR = R;
392 float values[] = {meter_in, meter_outL, meter_outR};
393 meters.process(values);
395 if (!bypassed)
396 bypass.crossfade(ins, outs, 2, orig_offset, numsamples);
397 meters.fall(numsamples);
398 return outputs_mask;
401 void mono_audio_module::set_sample_rate(uint32_t sr)
403 srate = sr;
404 // rebuild buffer
405 buffer_size = (int)srate * 0.05 * 2; // delay buffer size multiplied by 2 channels
406 buffer = (float*) calloc(buffer_size, sizeof(float));
407 pos = 0;
408 int meter[] = {param_meter_in, param_meter_outL, param_meter_outR};
409 int clip[] = {param_clip_in, param_clip_outL, param_clip_outR};
410 meters.init(params, meter, clip, 3, sr);
413 /**********************************************************************
414 * ANALYZER by Markus Schmidt and Christian Holschuh
415 **********************************************************************/
417 analyzer_audio_module::analyzer_audio_module() {
419 active = false;
420 clip_L = 0.f;
421 clip_R = 0.f;
422 meter_L = 0.f;
423 meter_R = 0.f;
424 envelope = 0.f;
425 ppos = 0;
426 plength = 0;
427 phase_buffer = (float*) calloc(max_phase_buffer_size, sizeof(float));
429 analyzer_audio_module::~analyzer_audio_module() {
430 free(phase_buffer);
432 void analyzer_audio_module::activate() {
433 active = true;
436 void analyzer_audio_module::deactivate() {
437 active = false;
440 void analyzer_audio_module::params_changed() {
441 float resolution, offset;
442 switch((int)*params[param_analyzer_mode]) {
443 case 0:
444 case 1:
445 case 2:
446 case 3:
447 default:
448 // analyzer
449 resolution = pow(64, *params[param_analyzer_level]);
450 offset = 0.75;
451 break;
452 case 4:
453 // we want to draw Stereo Image
454 resolution = pow(64, *params[param_analyzer_level] * 1.75);
455 offset = 1.f;
456 break;
457 case 5:
458 // We want to draw Stereo Difference
459 offset = *params[param_analyzer_level] > 1
460 ? 1 + (*params[param_analyzer_level] - 1) / 4
461 : *params[param_analyzer_level];
462 resolution = pow(64, 2 * offset);
463 break;
466 _analyzer.set_params(
467 resolution,
468 offset,
469 *params[param_analyzer_accuracy],
470 *params[param_analyzer_hold],
471 *params[param_analyzer_smoothing],
472 *params[param_analyzer_mode],
473 *params[param_analyzer_scale],
474 *params[param_analyzer_post],
475 *params[param_analyzer_speed],
476 *params[param_analyzer_windowing],
477 *params[param_analyzer_view],
478 *params[param_analyzer_freeze]
482 uint32_t analyzer_audio_module::process(uint32_t offset, uint32_t numsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
483 for(uint32_t i = offset; i < offset + numsamples; i++) {
484 // let meters fall a bit
485 clip_L -= std::min(clip_L, numsamples);
486 clip_R -= std::min(clip_R, numsamples);
487 meter_L = 0.f;
488 meter_R = 0.f;
490 float L = ins[0][i];
491 float R = ins[1][i];
493 // GUI stuff
494 if(L > 1.f) clip_L = srate >> 3;
495 if(R > 1.f) clip_R = srate >> 3;
497 // goniometer
498 //the goniometer tries to show the signal in maximum
499 //size. therefor an envelope with fast attack and slow
500 //release is calculated with the max value of left and right.
501 float lemax = fabs(L) > fabs(R) ? fabs(L) : fabs(R);
502 attack_coef = exp(log(0.01)/(0.01 * srate * 0.001));
503 release_coef = exp(log(0.01)/(2000 * srate * 0.001));
504 if( lemax > envelope)
505 envelope = lemax; //attack_coef * (envelope - lemax) + lemax;
506 else
507 envelope = release_coef * (envelope - lemax) + lemax;
509 //use the envelope to bring biggest signal to 1. the biggest
510 //enlargement of the signal is 4.
512 phase_buffer[ppos] = L / std::max(0.25f , (envelope));
513 phase_buffer[ppos + 1] = R / std::max(0.25f , (envelope));
516 plength = std::min(phase_buffer_size, plength + 2);
517 ppos += 2;
518 ppos %= (phase_buffer_size - 2);
520 // analyzer
521 _analyzer.process(L, R);
523 // meter
524 meter_L = L;
525 meter_R = R;
527 //output
528 outs[0][i] = L;
529 outs[1][i] = R;
531 // draw meters
532 SET_IF_CONNECTED(clip_L);
533 SET_IF_CONNECTED(clip_R);
534 SET_IF_CONNECTED(meter_L);
535 SET_IF_CONNECTED(meter_R);
536 return outputs_mask;
539 void analyzer_audio_module::set_sample_rate(uint32_t sr)
541 srate = sr;
542 phase_buffer_size = srate / 30 * 2;
543 phase_buffer_size -= phase_buffer_size % 2;
544 phase_buffer_size = std::min(phase_buffer_size, (int)max_phase_buffer_size);
545 _analyzer.set_sample_rate(sr);
548 bool analyzer_audio_module::get_phase_graph(float ** _buffer, int *_length, int * _mode, bool * _use_fade, float * _fade, int * _accuracy, bool * _display) const {
549 *_buffer = &phase_buffer[0];
550 *_length = plength;
551 *_use_fade = *params[param_gonio_use_fade];
552 *_fade = 0.6;
553 *_mode = *params[param_gonio_mode];
554 *_accuracy = *params[param_gonio_accuracy];
555 *_display = *params[param_gonio_display];
556 return false;
559 bool analyzer_audio_module::get_graph(int index, int subindex, int phase, float *data, int points, cairo_iface *context, int *mode) const
561 if (*params[param_analyzer_display])
562 return _analyzer.get_graph(subindex, phase, data, points, context, mode);
563 return false;
565 bool analyzer_audio_module::get_moving(int index, int subindex, int &direction, float *data, int x, int y, int &offset, uint32_t &color) const
567 if (*params[param_analyzer_display])
568 return _analyzer.get_moving(subindex, direction, data, x, y, offset, color);
569 return false;
571 bool analyzer_audio_module::get_gridline(int index, int subindex, int phase, float &pos, bool &vertical, std::string &legend, cairo_iface *context) const
573 return _analyzer.get_gridline(subindex, phase, pos, vertical, legend, context);
575 bool analyzer_audio_module::get_layers(int index, int generation, unsigned int &layers) const
577 return _analyzer.get_layers(generation, layers);