New mini toggles
[calf.git] / src / modules_tools.cpp
blob026ef58226c8040b93242f9b4a358a362f105baf
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 <calf/giface.h>
25 #include <calf/modules_tools.h>
26 #include <calf/modules_dev.h>
27 #include <sys/time.h>
28 #include <calf/utils.h>
31 using namespace dsp;
32 using namespace calf_plugins;
34 #define SET_IF_CONNECTED(name) if (params[AM::param_##name] != NULL) *params[AM::param_##name] = name;
35 #define sinc(x) (!x) ? 1 : sin(M_PI * x)/(M_PI * x);
37 /**********************************************************************
38 * STEREO TOOLS by Markus Schmidt
39 **********************************************************************/
41 stereo_audio_module::stereo_audio_module() {
42 active = false;
43 _phase = -1;
45 stereo_audio_module::~stereo_audio_module() {
46 free(buffer);
48 void stereo_audio_module::activate() {
49 active = true;
52 void stereo_audio_module::deactivate() {
53 active = false;
56 void stereo_audio_module::params_changed() {
57 float slev = 2 * *params[param_slev]; // stereo level ( -2 -> 2 )
58 float sbal = 1 + *params[param_sbal]; // stereo balance ( 0 -> 2 )
59 float mlev = 2 * *params[param_mlev]; // mono level ( -2 -> 2 )
60 float mpan = 1 + *params[param_mpan]; // mono pan ( 0 -> 2 )
62 switch((int)*params[param_mode])
64 case 0:
65 default:
66 //LR->LR
67 LL = (mlev * (2.f - mpan) + slev * (2.f - sbal));
68 LR = (mlev * mpan - slev * sbal);
69 RL = (mlev * (2.f - mpan) - slev * (2.f - sbal));
70 RR = (mlev * mpan + slev * sbal);
71 break;
72 case 1:
73 //LR->MS
74 LL = (2.f - mpan) * (2.f - sbal);
75 LR = mpan * (2.f - sbal) * -1;
76 RL = (2.f - mpan) * sbal;
77 RR = mpan * sbal;
78 break;
79 case 2:
80 //MS->LR
81 LL = mlev * (2.f - sbal);
82 LR = mlev * mpan;
83 RL = slev * (2.f - sbal);
84 RR = slev * sbal * -1;
85 break;
86 case 3:
87 case 4:
88 case 5:
89 case 6:
90 //LR->LL
91 LL = 0.f;
92 LR = 0.f;
93 RL = 0.f;
94 RR = 0.f;
95 break;
97 if(*params[param_stereo_phase] != _phase) {
98 _phase = *params[param_stereo_phase];
99 _phase_cos_coef = cos(_phase / 180 * M_PI);
100 _phase_sin_coef = sin(_phase / 180 * M_PI);
102 if(*params[param_sc_level] != _sc_level) {
103 _sc_level = *params[param_sc_level];
104 _inv_atan_shape = 1.0 / atan(_sc_level);
108 uint32_t stereo_audio_module::process(uint32_t offset, uint32_t numsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
109 bool bypassed = bypass.update(*params[param_bypass] > 0.5f, numsamples);
110 uint32_t orig_offset = offset;
111 for(uint32_t i = offset; i < offset + numsamples; i++) {
112 if(bypassed) {
113 outs[0][i] = ins[0][i];
114 outs[1][i] = ins[1][i];
115 meter_inL = 0.f;
116 meter_inR = 0.f;
117 meter_outL = 0.f;
118 meter_outR = 0.f;
119 } else {
120 meter_inL = 0.f;
121 meter_inR = 0.f;
122 meter_outL = 0.f;
123 meter_outR = 0.f;
125 float L = ins[0][i];
126 float R = ins[1][i];
128 // levels in
129 L *= *params[param_level_in];
130 R *= *params[param_level_in];
132 // balance in
133 L *= (1.f - std::max(0.f, *params[param_balance_in]));
134 R *= (1.f + std::min(0.f, *params[param_balance_in]));
136 // copy / flip / mono ...
137 switch((int)*params[param_mode])
139 case 0:
140 default:
141 // LR > LR
142 break;
143 case 1:
144 // LR > MS
145 break;
146 case 2:
147 // MS > LR
148 break;
149 case 3:
150 // LR > LL
151 R = L;
152 break;
153 case 4:
154 // LR > RR
155 L = R;
156 break;
157 case 5:
158 // LR > L+R
159 L = (L + R) / 2;
160 R = L;
161 break;
162 case 6:
163 // LR > RL
164 float tmp = L;
165 L = R;
166 R = tmp;
167 break;
170 // softclip
171 if(*params[param_softclip]) {
172 // int ph;
173 // ph = L / fabs(L);
174 // L = L > 0.63 ? ph * (0.63 + 0.36 * (1 - pow(MATH_E, (1.f / 3) * (0.63 + L * ph)))) : L;
175 // ph = R / fabs(R);
176 // R = R > 0.63 ? ph * (0.63 + 0.36 * (1 - pow(MATH_E, (1.f / 3) * (0.63 + R * ph)))) : R;
177 R = _inv_atan_shape * atan(R * _sc_level);
178 L = _inv_atan_shape * atan(L * _sc_level);
181 // GUI stuff
182 meter_inL = L;
183 meter_inR = R;
185 // mute
186 L *= (1 - floor(*params[param_mute_l] + 0.5));
187 R *= (1 - floor(*params[param_mute_r] + 0.5));
189 // phase
190 L *= (2 * (1 - floor(*params[param_phase_l] + 0.5))) - 1;
191 R *= (2 * (1 - floor(*params[param_phase_r] + 0.5))) - 1;
193 // LR/MS
194 L += LL*L + RL*R;
195 R += RR*R + LR*L;
197 // delay
198 buffer[pos] = L;
199 buffer[pos + 1] = R;
201 int nbuf = srate * (fabs(*params[param_delay]) / 1000.f);
202 nbuf -= nbuf % 2;
203 if(*params[param_delay] > 0.f) {
204 R = buffer[(pos - (int)nbuf + 1 + buffer_size) % buffer_size];
205 } else if (*params[param_delay] < 0.f) {
206 L = buffer[(pos - (int)nbuf + buffer_size) % buffer_size];
209 // stereo base
210 float _sb = *params[param_stereo_base];
211 if(_sb < 0) _sb *= 0.5;
213 float __l = L + _sb * L - _sb * R;
214 float __r = R + _sb * R - _sb * L;
216 L = __l;
217 R = __r;
219 // stereo phase
220 __l = L * _phase_cos_coef - R * _phase_sin_coef;
221 __r = L * _phase_sin_coef + R * _phase_cos_coef;
223 L = __l;
224 R = __r;
226 pos = (pos + 2) % buffer_size;
228 // balance out
229 L *= (1.f - std::max(0.f, *params[param_balance_out]));
230 R *= (1.f + std::min(0.f, *params[param_balance_out]));
232 // level
233 L *= *params[param_level_out];
234 R *= *params[param_level_out];
236 //output
237 outs[0][i] = L;
238 outs[1][i] = R;
240 meter_outL = L;
241 meter_outR = R;
243 // phase meter
244 if(fabs(L) > 0.001 and fabs(R) > 0.001) {
245 meter_phase = fabs(fabs(L+R) > 0.000000001 ? sin(fabs((L-R)/(L+R))) : 0.f);
246 } else {
247 meter_phase = 0.f;
250 float values[] = {meter_inL, meter_inR, meter_outL, meter_outR};
251 meters.process(values);
253 if (!bypassed)
254 bypass.crossfade(ins, outs, 2, orig_offset, numsamples);
255 meters.fall(numsamples);
256 return outputs_mask;
259 void stereo_audio_module::set_sample_rate(uint32_t sr)
261 srate = sr;
262 // rebuild buffer
263 buffer_size = (int)(srate * 0.05 * 2.f); // buffer size attack rate multiplied by 2 channels
264 buffer = (float*) calloc(buffer_size, sizeof(float));
265 pos = 0;
266 int meter[] = {param_meter_inL, param_meter_inR, param_meter_outL, param_meter_outR};
267 int clip[] = {param_clip_inL, param_clip_inR, param_clip_outL, param_clip_outR};
268 meters.init(params, meter, clip, 4, sr);
271 /**********************************************************************
272 * MONO INPUT by Markus Schmidt
273 **********************************************************************/
275 mono_audio_module::mono_audio_module() {
276 active = false;
277 meter_in = 0.f;
278 meter_outL = 0.f;
279 meter_outR = 0.f;
280 _phase = -1.f;
282 mono_audio_module::~mono_audio_module() {
283 free(buffer);
285 void mono_audio_module::activate() {
286 active = true;
289 void mono_audio_module::deactivate() {
290 active = false;
293 void mono_audio_module::params_changed() {
294 if(*params[param_sc_level] != _sc_level) {
295 _sc_level = *params[param_sc_level];
296 _inv_atan_shape = 1.0 / atan(_sc_level);
298 if(*params[param_stereo_phase] != _phase) {
299 _phase = *params[param_stereo_phase];
300 _phase_cos_coef = cos(_phase / 180 * M_PI);
301 _phase_sin_coef = sin(_phase / 180 * M_PI);
305 uint32_t mono_audio_module::process(uint32_t offset, uint32_t numsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
306 bool bypassed = bypass.update(*params[param_bypass] > 0.5f, numsamples);
307 uint32_t orig_offset = offset;
308 for(uint32_t i = offset; i < offset + numsamples; i++) {
309 if(bypassed) {
310 outs[0][i] = ins[0][i];
311 outs[1][i] = ins[0][i];
312 meter_in = 0.f;
313 meter_outL = 0.f;
314 meter_outR = 0.f;
315 } else {
316 meter_in = 0.f;
317 meter_outL = 0.f;
318 meter_outR = 0.f;
320 float L = ins[0][i];
322 // levels in
323 L *= *params[param_level_in];
325 // softclip
326 if(*params[param_softclip]) {
327 //int ph = L / fabs(L);
328 //L = L > 0.63 ? ph * (0.63 + 0.36 * (1 - pow(MATH_E, (1.f / 3) * (0.63 + L * ph)))) : L;
329 L = _inv_atan_shape * atan(L * _sc_level);
332 // GUI stuff
333 meter_in = L;
335 float R = L;
337 // mute
338 L *= (1 - floor(*params[param_mute_l] + 0.5));
339 R *= (1 - floor(*params[param_mute_r] + 0.5));
341 // phase
342 L *= (2 * (1 - floor(*params[param_phase_l] + 0.5))) - 1;
343 R *= (2 * (1 - floor(*params[param_phase_r] + 0.5))) - 1;
345 // delay
346 buffer[pos] = L;
347 buffer[pos + 1] = R;
349 int nbuf = srate * (fabs(*params[param_delay]) / 1000.f);
350 nbuf -= nbuf % 2;
351 if(*params[param_delay] > 0.f) {
352 R = buffer[(pos - (int)nbuf + 1 + buffer_size) % buffer_size];
353 } else if (*params[param_delay] < 0.f) {
354 L = buffer[(pos - (int)nbuf + buffer_size) % buffer_size];
357 // stereo base
358 float _sb = *params[param_stereo_base];
359 if(_sb < 0) _sb *= 0.5;
361 float __l = L +_sb * L - _sb * R;
362 float __r = R + _sb * R - _sb * L;
364 L = __l;
365 R = __r;
367 // stereo phase
368 __l = L * _phase_cos_coef - R * _phase_sin_coef;
369 __r = L * _phase_sin_coef + R * _phase_cos_coef;
371 L = __l;
372 R = __r;
374 pos = (pos + 2) % buffer_size;
376 // balance out
377 L *= (1.f - std::max(0.f, *params[param_balance_out]));
378 R *= (1.f + std::min(0.f, *params[param_balance_out]));
380 // level
381 L *= *params[param_level_out];
382 R *= *params[param_level_out];
384 //output
385 outs[0][i] = L;
386 outs[1][i] = R;
388 meter_outL = L;
389 meter_outR = R;
391 float values[] = {meter_in, meter_outL, meter_outR};
392 meters.process(values);
394 if (!bypassed)
395 bypass.crossfade(ins, outs, 2, orig_offset, numsamples);
396 meters.fall(numsamples);
397 return outputs_mask;
400 void mono_audio_module::set_sample_rate(uint32_t sr)
402 srate = sr;
403 // rebuild buffer
404 buffer_size = (int)srate * 0.05 * 2; // delay buffer size multiplied by 2 channels
405 buffer = (float*) calloc(buffer_size, sizeof(float));
406 pos = 0;
407 int meter[] = {param_meter_in, param_meter_outL, param_meter_outR};
408 int clip[] = {param_clip_in, param_clip_outL, param_clip_outR};
409 meters.init(params, meter, clip, 3, sr);
412 /**********************************************************************
413 * ANALYZER by Markus Schmidt and Christian Holschuh
414 **********************************************************************/
416 analyzer_audio_module::analyzer_audio_module() {
418 active = false;
419 clip_L = 0.f;
420 clip_R = 0.f;
421 meter_L = 0.f;
422 meter_R = 0.f;
423 envelope = 0.f;
424 ppos = 0;
425 plength = 0;
426 phase_buffer = (float*) calloc(max_phase_buffer_size, sizeof(float));
428 analyzer_audio_module::~analyzer_audio_module() {
429 free(phase_buffer);
431 void analyzer_audio_module::activate() {
432 active = true;
435 void analyzer_audio_module::deactivate() {
436 active = false;
439 void analyzer_audio_module::params_changed() {
440 float resolution, offset;
441 switch((int)*params[param_analyzer_mode]) {
442 case 0:
443 case 1:
444 case 2:
445 case 3:
446 default:
447 // analyzer
448 resolution = pow(64, *params[param_analyzer_level]);
449 offset = 0.75;
450 break;
451 case 4:
452 // we want to draw Stereo Image
453 resolution = pow(64, *params[param_analyzer_level] * 1.75);
454 offset = 1.f;
455 break;
456 case 5:
457 // We want to draw Stereo Difference
458 offset = *params[param_analyzer_level] > 1
459 ? 1 + (*params[param_analyzer_level] - 1) / 4
460 : *params[param_analyzer_level];
461 resolution = pow(64, 2 * offset);
462 break;
465 _analyzer.set_params(
466 resolution,
467 offset,
468 *params[param_analyzer_accuracy],
469 *params[param_analyzer_hold],
470 *params[param_analyzer_smoothing],
471 *params[param_analyzer_mode],
472 *params[param_analyzer_scale],
473 *params[param_analyzer_post],
474 *params[param_analyzer_speed],
475 *params[param_analyzer_windowing],
476 *params[param_analyzer_view],
477 *params[param_analyzer_freeze]
481 uint32_t analyzer_audio_module::process(uint32_t offset, uint32_t numsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
482 for(uint32_t i = offset; i < offset + numsamples; i++) {
483 // let meters fall a bit
484 clip_L -= std::min(clip_L, numsamples);
485 clip_R -= std::min(clip_R, numsamples);
486 meter_L = 0.f;
487 meter_R = 0.f;
489 float L = ins[0][i];
490 float R = ins[1][i];
492 // GUI stuff
493 if(L > 1.f) clip_L = srate >> 3;
494 if(R > 1.f) clip_R = srate >> 3;
496 // goniometer
497 //the goniometer tries to show the signal in maximum
498 //size. therefor an envelope with fast attack and slow
499 //release is calculated with the max value of left and right.
500 float lemax = fabs(L) > fabs(R) ? fabs(L) : fabs(R);
501 attack_coef = exp(log(0.01)/(0.01 * srate * 0.001));
502 release_coef = exp(log(0.01)/(2000 * srate * 0.001));
503 if( lemax > envelope)
504 envelope = lemax; //attack_coef * (envelope - lemax) + lemax;
505 else
506 envelope = release_coef * (envelope - lemax) + lemax;
508 //use the envelope to bring biggest signal to 1. the biggest
509 //enlargement of the signal is 4.
511 phase_buffer[ppos] = L / std::max(0.25f , (envelope));
512 phase_buffer[ppos + 1] = R / std::max(0.25f , (envelope));
515 plength = std::min(phase_buffer_size, plength + 2);
516 ppos += 2;
517 ppos %= (phase_buffer_size - 2);
519 // analyzer
520 _analyzer.process(L, R);
522 // meter
523 meter_L = L;
524 meter_R = R;
526 //output
527 outs[0][i] = L;
528 outs[1][i] = R;
530 // draw meters
531 SET_IF_CONNECTED(clip_L);
532 SET_IF_CONNECTED(clip_R);
533 SET_IF_CONNECTED(meter_L);
534 SET_IF_CONNECTED(meter_R);
535 return outputs_mask;
538 void analyzer_audio_module::set_sample_rate(uint32_t sr)
540 srate = sr;
541 phase_buffer_size = srate / 30 * 2;
542 phase_buffer_size -= phase_buffer_size % 2;
543 phase_buffer_size = std::min(phase_buffer_size, (int)max_phase_buffer_size);
544 _analyzer.set_sample_rate(sr);
547 bool analyzer_audio_module::get_phase_graph(float ** _buffer, int *_length, int * _mode, bool * _use_fade, float * _fade, int * _accuracy, bool * _display) const {
548 *_buffer = &phase_buffer[0];
549 *_length = plength;
550 *_use_fade = *params[param_gonio_use_fade];
551 *_fade = 0.6;
552 *_mode = *params[param_gonio_mode];
553 *_accuracy = *params[param_gonio_accuracy];
554 *_display = *params[param_gonio_display];
555 return false;
558 bool analyzer_audio_module::get_graph(int index, int subindex, int phase, float *data, int points, cairo_iface *context, int *mode) const
560 if (*params[param_analyzer_display])
561 return _analyzer.get_graph(subindex, phase, data, points, context, mode);
562 return false;
564 bool analyzer_audio_module::get_moving(int index, int subindex, int &direction, float *data, int x, int y, int &offset, uint32_t &color) const
566 if (*params[param_analyzer_display])
567 return _analyzer.get_moving(subindex, direction, data, x, y, offset, color);
568 return false;
570 bool analyzer_audio_module::get_gridline(int index, int subindex, int phase, float &pos, bool &vertical, std::string &legend, cairo_iface *context) const
572 return _analyzer.get_gridline(subindex, phase, pos, vertical, legend, context);
574 bool analyzer_audio_module::get_layers(int index, int generation, unsigned int &layers) const
576 return _analyzer.get_layers(generation, layers);