Merge branch 'master' of https://github.com/calf-studio-gear/calf
[calf.git] / src / modules_tools.cpp
blobffdfd51fd8229eb043ecb31be9d89eb11b122501
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 if(*params[param_stereo_phase] != _phase) {
58 _phase = *params[param_stereo_phase];
59 _phase_cos_coef = cos(_phase / 180 * M_PI);
60 _phase_sin_coef = sin(_phase / 180 * M_PI);
62 if(*params[param_sc_level] != _sc_level) {
63 _sc_level = *params[param_sc_level];
64 _inv_atan_shape = 1.0 / atan(_sc_level);
68 uint32_t stereo_audio_module::process(uint32_t offset, uint32_t numsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
69 bool bypassed = bypass.update(*params[param_bypass] > 0.5f, numsamples);
70 uint32_t orig_offset = offset;
71 for(uint32_t i = offset; i < offset + numsamples; i++) {
72 if(bypassed) {
73 outs[0][i] = ins[0][i];
74 outs[1][i] = ins[1][i];
75 meter_inL = 0.f;
76 meter_inR = 0.f;
77 meter_outL = 0.f;
78 meter_outR = 0.f;
79 } else {
80 meter_inL = 0.f;
81 meter_inR = 0.f;
82 meter_outL = 0.f;
83 meter_outR = 0.f;
85 float L = ins[0][i];
86 float R = ins[1][i];
88 // levels in
89 L *= *params[param_level_in];
90 R *= *params[param_level_in];
92 // balance in
93 L *= (1.f - std::max(0.f, *params[param_balance_in]));
94 R *= (1.f + std::min(0.f, *params[param_balance_in]));
96 // softclip
97 if(*params[param_softclip]) {
98 R = _inv_atan_shape * atan(R * _sc_level);
99 L = _inv_atan_shape * atan(L * _sc_level);
102 // GUI stuff
103 meter_inL = L;
104 meter_inR = R;
106 // modes
107 float slev = *params[param_slev]; // slev - stereo level ( -2 -> 2 )
108 float sbal = 1 + *params[param_sbal]; // sbal - stereo balance ( 0 -> 2 )
109 float mlev = *params[param_mlev]; // mlev - mono level ( 0 -> 2 )
110 float mpan = (1 + *params[param_mpan]); // mpan - mono pan ( 0 -> 1 )
112 float l, r, m, s;
114 switch((int)*params[param_mode])
116 case 0:
117 // LR > LR
118 m = (L + R) * 0.5;
119 s = (L - R) * 0.5;
120 l = m * mlev * std::min(1.f, 2.f - mpan) + s * slev * std::min(1.f, 2.f - sbal);
121 r = m * mlev * std::min(1.f, mpan) - s * slev * std::min(1.f, sbal);
122 L = l;
123 R = r;
124 break;
125 case 1:
126 // LR > MS
127 l = L * std::min(1.f, 2.f - sbal);
128 r = R * std::min(1.f, sbal);
129 L = 0.5 * (l + r) * mlev;
130 R = 0.5 * (l - r) * slev;
131 break;
132 case 2:
133 // MS > LR
134 l = L * mlev * std::min(1.f, 2.f - mpan) + R * slev * std::min(1.f, 2.f - sbal);
135 r = L * mlev * std::min(1.f, mpan) - R * slev * std::min(1.f, sbal);
136 L = l;
137 R = r;
138 break;
139 case 3:
140 // LR > LL
141 R = L;
142 break;
143 case 4:
144 // LR > RR
145 L = R;
146 break;
147 case 5:
148 // LR > L+R
149 L = (L + R) / 2;
150 R = L;
151 break;
152 case 6:
153 // LR > RL
154 l = L;
155 L = R;
156 R = l;
157 m = (L + R) * 0.5;
158 s = (L - R) * 0.5;
159 l = m * mlev * std::min(1.f, 2.f - mpan) + s * slev * std::min(1.f, 2.f - sbal);
160 r = m * mlev * std::min(1.f, mpan) - s * slev * std::min(1.f, sbal);
161 L = l;
162 R = r;
163 break;
166 // mute
167 L *= (1 - floor(*params[param_mute_l] + 0.5));
168 R *= (1 - floor(*params[param_mute_r] + 0.5));
170 // phase
171 L *= (2 * (1 - floor(*params[param_phase_l] + 0.5))) - 1;
172 R *= (2 * (1 - floor(*params[param_phase_r] + 0.5))) - 1;
174 // delay
175 buffer[pos] = L;
176 buffer[pos + 1] = R;
178 int nbuf = srate * (fabs(*params[param_delay]) / 1000.f);
179 nbuf -= nbuf % 2;
180 if(*params[param_delay] > 0.f) {
181 R = buffer[(pos - (int)nbuf + 1 + buffer_size) % buffer_size];
182 } else if (*params[param_delay] < 0.f) {
183 L = buffer[(pos - (int)nbuf + buffer_size) % buffer_size];
186 // stereo base
187 float _sb = *params[param_stereo_base];
188 if(_sb < 0) _sb *= 0.5;
190 float __l = L + _sb * L - _sb * R;
191 float __r = R + _sb * R - _sb * L;
193 L = __l;
194 R = __r;
196 // stereo phase
197 __l = L * _phase_cos_coef - R * _phase_sin_coef;
198 __r = L * _phase_sin_coef + R * _phase_cos_coef;
200 L = __l;
201 R = __r;
203 pos = (pos + 2) % buffer_size;
205 // balance out
206 L *= (1.f - std::max(0.f, *params[param_balance_out]));
207 R *= (1.f + std::min(0.f, *params[param_balance_out]));
209 // level
210 L *= *params[param_level_out];
211 R *= *params[param_level_out];
213 //output
214 outs[0][i] = L;
215 outs[1][i] = R;
217 meter_outL = L;
218 meter_outR = R;
220 // phase meter
221 if(fabs(L) > 0.001 and fabs(R) > 0.001) {
222 meter_phase = fabs(fabs(L+R) > 0.000000001 ? sin(fabs((L-R)/(L+R))) : 0.f);
223 } else {
224 meter_phase = 0.f;
227 float values[] = {meter_inL, meter_inR, meter_outL, meter_outR};
228 meters.process(values);
230 if (!bypassed)
231 bypass.crossfade(ins, outs, 2, orig_offset, numsamples);
232 meters.fall(numsamples);
233 return outputs_mask;
236 void stereo_audio_module::set_sample_rate(uint32_t sr)
238 srate = sr;
239 // rebuild buffer
240 buffer_size = (int)(srate * 0.05 * 2.f); // buffer size attack rate multiplied by 2 channels
241 buffer = (float*) calloc(buffer_size, sizeof(float));
242 pos = 0;
243 int meter[] = {param_meter_inL, param_meter_inR, param_meter_outL, param_meter_outR};
244 int clip[] = {param_clip_inL, param_clip_inR, param_clip_outL, param_clip_outR};
245 meters.init(params, meter, clip, 4, sr);
248 /**********************************************************************
249 * MONO INPUT by Markus Schmidt
250 **********************************************************************/
252 mono_audio_module::mono_audio_module() {
253 active = false;
254 meter_in = 0.f;
255 meter_outL = 0.f;
256 meter_outR = 0.f;
257 _phase = -1.f;
259 mono_audio_module::~mono_audio_module() {
260 free(buffer);
262 void mono_audio_module::activate() {
263 active = true;
266 void mono_audio_module::deactivate() {
267 active = false;
270 void mono_audio_module::params_changed() {
271 if(*params[param_sc_level] != _sc_level) {
272 _sc_level = *params[param_sc_level];
273 _inv_atan_shape = 1.0 / atan(_sc_level);
275 if(*params[param_stereo_phase] != _phase) {
276 _phase = *params[param_stereo_phase];
277 _phase_cos_coef = cos(_phase / 180 * M_PI);
278 _phase_sin_coef = sin(_phase / 180 * M_PI);
282 uint32_t mono_audio_module::process(uint32_t offset, uint32_t numsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
283 bool bypassed = bypass.update(*params[param_bypass] > 0.5f, numsamples);
284 uint32_t orig_offset = offset;
285 for(uint32_t i = offset; i < offset + numsamples; i++) {
286 if(bypassed) {
287 outs[0][i] = ins[0][i];
288 outs[1][i] = ins[0][i];
289 meter_in = 0.f;
290 meter_outL = 0.f;
291 meter_outR = 0.f;
292 } else {
293 meter_in = 0.f;
294 meter_outL = 0.f;
295 meter_outR = 0.f;
297 float L = ins[0][i];
299 // levels in
300 L *= *params[param_level_in];
302 // softclip
303 if(*params[param_softclip]) {
304 //int ph = L / fabs(L);
305 //L = L > 0.63 ? ph * (0.63 + 0.36 * (1 - pow(MATH_E, (1.f / 3) * (0.63 + L * ph)))) : L;
306 L = _inv_atan_shape * atan(L * _sc_level);
309 // GUI stuff
310 meter_in = L;
312 float R = L;
314 // mute
315 L *= (1 - floor(*params[param_mute_l] + 0.5));
316 R *= (1 - floor(*params[param_mute_r] + 0.5));
318 // phase
319 L *= (2 * (1 - floor(*params[param_phase_l] + 0.5))) - 1;
320 R *= (2 * (1 - floor(*params[param_phase_r] + 0.5))) - 1;
322 // delay
323 buffer[pos] = L;
324 buffer[pos + 1] = R;
326 int nbuf = srate * (fabs(*params[param_delay]) / 1000.f);
327 nbuf -= nbuf % 2;
328 if(*params[param_delay] > 0.f) {
329 R = buffer[(pos - (int)nbuf + 1 + buffer_size) % buffer_size];
330 } else if (*params[param_delay] < 0.f) {
331 L = buffer[(pos - (int)nbuf + buffer_size) % buffer_size];
334 // stereo base
335 float _sb = *params[param_stereo_base];
336 if(_sb < 0) _sb *= 0.5;
338 float __l = L +_sb * L - _sb * R;
339 float __r = R + _sb * R - _sb * L;
341 L = __l;
342 R = __r;
344 // stereo phase
345 __l = L * _phase_cos_coef - R * _phase_sin_coef;
346 __r = L * _phase_sin_coef + R * _phase_cos_coef;
348 L = __l;
349 R = __r;
351 pos = (pos + 2) % buffer_size;
353 // balance out
354 L *= (1.f - std::max(0.f, *params[param_balance_out]));
355 R *= (1.f + std::min(0.f, *params[param_balance_out]));
357 // level
358 L *= *params[param_level_out];
359 R *= *params[param_level_out];
361 //output
362 outs[0][i] = L;
363 outs[1][i] = R;
365 meter_outL = L;
366 meter_outR = R;
368 float values[] = {meter_in, meter_outL, meter_outR};
369 meters.process(values);
371 if (!bypassed)
372 bypass.crossfade(ins, outs, 2, orig_offset, numsamples);
373 meters.fall(numsamples);
374 return outputs_mask;
377 void mono_audio_module::set_sample_rate(uint32_t sr)
379 srate = sr;
380 // rebuild buffer
381 buffer_size = (int)srate * 0.05 * 2; // delay buffer size multiplied by 2 channels
382 buffer = (float*) calloc(buffer_size, sizeof(float));
383 pos = 0;
384 int meter[] = {param_meter_in, param_meter_outL, param_meter_outR};
385 int clip[] = {param_clip_in, param_clip_outL, param_clip_outR};
386 meters.init(params, meter, clip, 3, sr);
389 /**********************************************************************
390 * ANALYZER by Markus Schmidt and Christian Holschuh
391 **********************************************************************/
393 analyzer_audio_module::analyzer_audio_module() {
395 active = false;
396 clip_L = 0.f;
397 clip_R = 0.f;
398 meter_L = 0.f;
399 meter_R = 0.f;
400 envelope = 0.f;
401 ppos = 0;
402 plength = 0;
403 phase_buffer = (float*) calloc(max_phase_buffer_size, sizeof(float));
405 analyzer_audio_module::~analyzer_audio_module() {
406 free(phase_buffer);
408 void analyzer_audio_module::activate() {
409 active = true;
412 void analyzer_audio_module::deactivate() {
413 active = false;
416 void analyzer_audio_module::params_changed() {
417 float resolution, offset;
418 switch((int)*params[param_analyzer_mode]) {
419 case 0:
420 case 1:
421 case 2:
422 case 3:
423 default:
424 // analyzer
425 resolution = pow(64, *params[param_analyzer_level]);
426 offset = 0.75;
427 break;
428 case 4:
429 // we want to draw Stereo Image
430 resolution = pow(64, *params[param_analyzer_level] * 1.75);
431 offset = 1.f;
432 break;
433 case 5:
434 // We want to draw Stereo Difference
435 offset = *params[param_analyzer_level] > 1
436 ? 1 + (*params[param_analyzer_level] - 1) / 4
437 : *params[param_analyzer_level];
438 resolution = pow(64, 2 * offset);
439 break;
442 _analyzer.set_params(
443 resolution,
444 offset,
445 *params[param_analyzer_accuracy],
446 *params[param_analyzer_hold],
447 *params[param_analyzer_smoothing],
448 *params[param_analyzer_mode],
449 *params[param_analyzer_scale],
450 *params[param_analyzer_post],
451 *params[param_analyzer_speed],
452 *params[param_analyzer_windowing],
453 *params[param_analyzer_view],
454 *params[param_analyzer_freeze]
458 uint32_t analyzer_audio_module::process(uint32_t offset, uint32_t numsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
459 for(uint32_t i = offset; i < offset + numsamples; i++) {
460 // let meters fall a bit
461 clip_L -= std::min(clip_L, numsamples);
462 clip_R -= std::min(clip_R, numsamples);
463 meter_L = 0.f;
464 meter_R = 0.f;
466 float L = ins[0][i];
467 float R = ins[1][i];
469 // GUI stuff
470 if(L > 1.f) clip_L = srate >> 3;
471 if(R > 1.f) clip_R = srate >> 3;
473 // goniometer
474 //the goniometer tries to show the signal in maximum
475 //size. therefor an envelope with fast attack and slow
476 //release is calculated with the max value of left and right.
477 float lemax = fabs(L) > fabs(R) ? fabs(L) : fabs(R);
478 attack_coef = exp(log(0.01)/(0.01 * srate * 0.001));
479 release_coef = exp(log(0.01)/(2000 * srate * 0.001));
480 if( lemax > envelope)
481 envelope = lemax; //attack_coef * (envelope - lemax) + lemax;
482 else
483 envelope = release_coef * (envelope - lemax) + lemax;
485 //use the envelope to bring biggest signal to 1. the biggest
486 //enlargement of the signal is 4.
488 phase_buffer[ppos] = L / std::max(0.25f , (envelope));
489 phase_buffer[ppos + 1] = R / std::max(0.25f , (envelope));
492 plength = std::min(phase_buffer_size, plength + 2);
493 ppos += 2;
494 ppos %= (phase_buffer_size - 2);
496 // analyzer
497 _analyzer.process(L, R);
499 // meter
500 meter_L = L;
501 meter_R = R;
503 //output
504 outs[0][i] = L;
505 outs[1][i] = R;
507 // draw meters
508 SET_IF_CONNECTED(clip_L);
509 SET_IF_CONNECTED(clip_R);
510 SET_IF_CONNECTED(meter_L);
511 SET_IF_CONNECTED(meter_R);
512 return outputs_mask;
515 void analyzer_audio_module::set_sample_rate(uint32_t sr)
517 srate = sr;
518 phase_buffer_size = srate / 30 * 2;
519 phase_buffer_size -= phase_buffer_size % 2;
520 phase_buffer_size = std::min(phase_buffer_size, (int)max_phase_buffer_size);
521 _analyzer.set_sample_rate(sr);
524 bool analyzer_audio_module::get_phase_graph(float ** _buffer, int *_length, int * _mode, bool * _use_fade, float * _fade, int * _accuracy, bool * _display) const {
525 *_buffer = &phase_buffer[0];
526 *_length = plength;
527 *_use_fade = *params[param_gonio_use_fade];
528 *_fade = 0.6;
529 *_mode = *params[param_gonio_mode];
530 *_accuracy = *params[param_gonio_accuracy];
531 *_display = *params[param_gonio_display];
532 return false;
535 bool analyzer_audio_module::get_graph(int index, int subindex, int phase, float *data, int points, cairo_iface *context, int *mode) const
537 if (*params[param_analyzer_display])
538 return _analyzer.get_graph(subindex, phase, data, points, context, mode);
539 return false;
541 bool analyzer_audio_module::get_moving(int index, int subindex, int &direction, float *data, int x, int y, int &offset, uint32_t &color) const
543 if (*params[param_analyzer_display])
544 return _analyzer.get_moving(subindex, direction, data, x, y, offset, color);
545 return false;
547 bool analyzer_audio_module::get_gridline(int index, int subindex, int phase, float &pos, bool &vertical, std::string &legend, cairo_iface *context) const
549 return _analyzer.get_gridline(subindex, phase, pos, vertical, legend, context);
551 bool analyzer_audio_module::get_layers(int index, int generation, unsigned int &layers) const
553 return _analyzer.get_layers(generation, layers);
558 /**********************************************************************
559 * WIDGETS TEST
560 **********************************************************************/
562 widgets_audio_module::widgets_audio_module() {
564 widgets_audio_module::~widgets_audio_module() {
567 void widgets_audio_module::params_changed() {
571 uint32_t widgets_audio_module::process(uint32_t offset, uint32_t numsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
572 float meter_1, meter_2, meter_3, meter_4;
573 for(uint32_t i = offset; i < offset + numsamples; i++) {
574 meter_1 = 0.f;
575 meter_2 = 0.f;
576 meter_3 = 0.f;
577 meter_4 = 0.f;
579 //float L = ins[0][i];
580 //float R = ins[1][i];
581 float values[] = {meter_1, meter_2, meter_3, meter_4};
582 meters.process(values);
584 meters.fall(numsamples);
585 return outputs_mask;
588 void widgets_audio_module::set_sample_rate(uint32_t sr)
590 srate = sr;
591 int meter[] = {param_meter1, param_meter2, param_meter3, param_meter4};
592 int clip[] = {0, 0, 0, 0};
593 meters.init(params, meter, clip, 4, sr);