Make Analyzer UI require instance-access
[calf.git] / src / modules_tools.cpp
blob69edc4ec29c771e5a692641315ac1c70b842c515
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 bl, br;
114 switch((int)*params[param_mode])
116 case 0:
117 // LR > LR
118 break;
119 case 1:
120 // LR > MS
121 bl = L * std::min(1.f, 2.f - sbal);
122 br = R * std::min(1.f, sbal);
123 L = 0.5 * (bl + br) * mlev;
124 R = 0.5 * (bl - br) * slev;
125 break;
126 case 2:
127 // MS > LR
128 bl = L * mlev * std::min(1.f, 2.f - mpan) + R * slev * std::min(1.f, 2.f - sbal);
129 br = L * mlev * std::min(1.f, mpan) - R * slev * std::min(1.f, sbal);
130 L = bl;
131 R = br;
132 break;
133 case 3:
134 // LR > LL
135 R = L;
136 break;
137 case 4:
138 // LR > RR
139 L = R;
140 break;
141 case 5:
142 // LR > L+R
143 L = (L + R) / 2;
144 R = L;
145 break;
146 case 6:
147 // LR > RL
148 float tmp = L;
149 L = R;
150 R = tmp;
151 break;
154 // mute
155 L *= (1 - floor(*params[param_mute_l] + 0.5));
156 R *= (1 - floor(*params[param_mute_r] + 0.5));
158 // phase
159 L *= (2 * (1 - floor(*params[param_phase_l] + 0.5))) - 1;
160 R *= (2 * (1 - floor(*params[param_phase_r] + 0.5))) - 1;
162 // delay
163 buffer[pos] = L;
164 buffer[pos + 1] = R;
166 int nbuf = srate * (fabs(*params[param_delay]) / 1000.f);
167 nbuf -= nbuf % 2;
168 if(*params[param_delay] > 0.f) {
169 R = buffer[(pos - (int)nbuf + 1 + buffer_size) % buffer_size];
170 } else if (*params[param_delay] < 0.f) {
171 L = buffer[(pos - (int)nbuf + buffer_size) % buffer_size];
174 // stereo base
175 float _sb = *params[param_stereo_base];
176 if(_sb < 0) _sb *= 0.5;
178 float __l = L + _sb * L - _sb * R;
179 float __r = R + _sb * R - _sb * L;
181 L = __l;
182 R = __r;
184 // stereo phase
185 __l = L * _phase_cos_coef - R * _phase_sin_coef;
186 __r = L * _phase_sin_coef + R * _phase_cos_coef;
188 L = __l;
189 R = __r;
191 pos = (pos + 2) % buffer_size;
193 // balance out
194 L *= (1.f - std::max(0.f, *params[param_balance_out]));
195 R *= (1.f + std::min(0.f, *params[param_balance_out]));
197 // level
198 L *= *params[param_level_out];
199 R *= *params[param_level_out];
201 //output
202 outs[0][i] = L;
203 outs[1][i] = R;
205 meter_outL = L;
206 meter_outR = R;
208 // phase meter
209 if(fabs(L) > 0.001 and fabs(R) > 0.001) {
210 meter_phase = fabs(fabs(L+R) > 0.000000001 ? sin(fabs((L-R)/(L+R))) : 0.f);
211 } else {
212 meter_phase = 0.f;
215 float values[] = {meter_inL, meter_inR, meter_outL, meter_outR};
216 meters.process(values);
218 if (!bypassed)
219 bypass.crossfade(ins, outs, 2, orig_offset, numsamples);
220 meters.fall(numsamples);
221 return outputs_mask;
224 void stereo_audio_module::set_sample_rate(uint32_t sr)
226 srate = sr;
227 // rebuild buffer
228 buffer_size = (int)(srate * 0.05 * 2.f); // buffer size attack rate multiplied by 2 channels
229 buffer = (float*) calloc(buffer_size, sizeof(float));
230 pos = 0;
231 int meter[] = {param_meter_inL, param_meter_inR, param_meter_outL, param_meter_outR};
232 int clip[] = {param_clip_inL, param_clip_inR, param_clip_outL, param_clip_outR};
233 meters.init(params, meter, clip, 4, sr);
236 /**********************************************************************
237 * MONO INPUT by Markus Schmidt
238 **********************************************************************/
240 mono_audio_module::mono_audio_module() {
241 active = false;
242 meter_in = 0.f;
243 meter_outL = 0.f;
244 meter_outR = 0.f;
245 _phase = -1.f;
247 mono_audio_module::~mono_audio_module() {
248 free(buffer);
250 void mono_audio_module::activate() {
251 active = true;
254 void mono_audio_module::deactivate() {
255 active = false;
258 void mono_audio_module::params_changed() {
259 if(*params[param_sc_level] != _sc_level) {
260 _sc_level = *params[param_sc_level];
261 _inv_atan_shape = 1.0 / atan(_sc_level);
263 if(*params[param_stereo_phase] != _phase) {
264 _phase = *params[param_stereo_phase];
265 _phase_cos_coef = cos(_phase / 180 * M_PI);
266 _phase_sin_coef = sin(_phase / 180 * M_PI);
270 uint32_t mono_audio_module::process(uint32_t offset, uint32_t numsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
271 bool bypassed = bypass.update(*params[param_bypass] > 0.5f, numsamples);
272 uint32_t orig_offset = offset;
273 for(uint32_t i = offset; i < offset + numsamples; i++) {
274 if(bypassed) {
275 outs[0][i] = ins[0][i];
276 outs[1][i] = ins[0][i];
277 meter_in = 0.f;
278 meter_outL = 0.f;
279 meter_outR = 0.f;
280 } else {
281 meter_in = 0.f;
282 meter_outL = 0.f;
283 meter_outR = 0.f;
285 float L = ins[0][i];
287 // levels in
288 L *= *params[param_level_in];
290 // softclip
291 if(*params[param_softclip]) {
292 //int ph = L / fabs(L);
293 //L = L > 0.63 ? ph * (0.63 + 0.36 * (1 - pow(MATH_E, (1.f / 3) * (0.63 + L * ph)))) : L;
294 L = _inv_atan_shape * atan(L * _sc_level);
297 // GUI stuff
298 meter_in = L;
300 float R = L;
302 // mute
303 L *= (1 - floor(*params[param_mute_l] + 0.5));
304 R *= (1 - floor(*params[param_mute_r] + 0.5));
306 // phase
307 L *= (2 * (1 - floor(*params[param_phase_l] + 0.5))) - 1;
308 R *= (2 * (1 - floor(*params[param_phase_r] + 0.5))) - 1;
310 // delay
311 buffer[pos] = L;
312 buffer[pos + 1] = R;
314 int nbuf = srate * (fabs(*params[param_delay]) / 1000.f);
315 nbuf -= nbuf % 2;
316 if(*params[param_delay] > 0.f) {
317 R = buffer[(pos - (int)nbuf + 1 + buffer_size) % buffer_size];
318 } else if (*params[param_delay] < 0.f) {
319 L = buffer[(pos - (int)nbuf + buffer_size) % buffer_size];
322 // stereo base
323 float _sb = *params[param_stereo_base];
324 if(_sb < 0) _sb *= 0.5;
326 float __l = L +_sb * L - _sb * R;
327 float __r = R + _sb * R - _sb * L;
329 L = __l;
330 R = __r;
332 // stereo phase
333 __l = L * _phase_cos_coef - R * _phase_sin_coef;
334 __r = L * _phase_sin_coef + R * _phase_cos_coef;
336 L = __l;
337 R = __r;
339 pos = (pos + 2) % buffer_size;
341 // balance out
342 L *= (1.f - std::max(0.f, *params[param_balance_out]));
343 R *= (1.f + std::min(0.f, *params[param_balance_out]));
345 // level
346 L *= *params[param_level_out];
347 R *= *params[param_level_out];
349 //output
350 outs[0][i] = L;
351 outs[1][i] = R;
353 meter_outL = L;
354 meter_outR = R;
356 float values[] = {meter_in, meter_outL, meter_outR};
357 meters.process(values);
359 if (!bypassed)
360 bypass.crossfade(ins, outs, 2, orig_offset, numsamples);
361 meters.fall(numsamples);
362 return outputs_mask;
365 void mono_audio_module::set_sample_rate(uint32_t sr)
367 srate = sr;
368 // rebuild buffer
369 buffer_size = (int)srate * 0.05 * 2; // delay buffer size multiplied by 2 channels
370 buffer = (float*) calloc(buffer_size, sizeof(float));
371 pos = 0;
372 int meter[] = {param_meter_in, param_meter_outL, param_meter_outR};
373 int clip[] = {param_clip_in, param_clip_outL, param_clip_outR};
374 meters.init(params, meter, clip, 3, sr);
377 /**********************************************************************
378 * ANALYZER by Markus Schmidt and Christian Holschuh
379 **********************************************************************/
381 analyzer_audio_module::analyzer_audio_module() {
383 active = false;
384 clip_L = 0.f;
385 clip_R = 0.f;
386 meter_L = 0.f;
387 meter_R = 0.f;
388 envelope = 0.f;
389 ppos = 0;
390 plength = 0;
391 phase_buffer = (float*) calloc(max_phase_buffer_size, sizeof(float));
393 analyzer_audio_module::~analyzer_audio_module() {
394 free(phase_buffer);
396 void analyzer_audio_module::activate() {
397 active = true;
400 void analyzer_audio_module::deactivate() {
401 active = false;
404 void analyzer_audio_module::params_changed() {
405 float resolution, offset;
406 switch((int)*params[param_analyzer_mode]) {
407 case 0:
408 case 1:
409 case 2:
410 case 3:
411 default:
412 // analyzer
413 resolution = pow(64, *params[param_analyzer_level]);
414 offset = 0.75;
415 break;
416 case 4:
417 // we want to draw Stereo Image
418 resolution = pow(64, *params[param_analyzer_level] * 1.75);
419 offset = 1.f;
420 break;
421 case 5:
422 // We want to draw Stereo Difference
423 offset = *params[param_analyzer_level] > 1
424 ? 1 + (*params[param_analyzer_level] - 1) / 4
425 : *params[param_analyzer_level];
426 resolution = pow(64, 2 * offset);
427 break;
430 _analyzer.set_params(
431 resolution,
432 offset,
433 *params[param_analyzer_accuracy],
434 *params[param_analyzer_hold],
435 *params[param_analyzer_smoothing],
436 *params[param_analyzer_mode],
437 *params[param_analyzer_scale],
438 *params[param_analyzer_post],
439 *params[param_analyzer_speed],
440 *params[param_analyzer_windowing],
441 *params[param_analyzer_view],
442 *params[param_analyzer_freeze]
446 uint32_t analyzer_audio_module::process(uint32_t offset, uint32_t numsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
447 for(uint32_t i = offset; i < offset + numsamples; i++) {
448 // let meters fall a bit
449 clip_L -= std::min(clip_L, numsamples);
450 clip_R -= std::min(clip_R, numsamples);
451 meter_L = 0.f;
452 meter_R = 0.f;
454 float L = ins[0][i];
455 float R = ins[1][i];
457 // GUI stuff
458 if(L > 1.f) clip_L = srate >> 3;
459 if(R > 1.f) clip_R = srate >> 3;
461 // goniometer
462 //the goniometer tries to show the signal in maximum
463 //size. therefor an envelope with fast attack and slow
464 //release is calculated with the max value of left and right.
465 float lemax = fabs(L) > fabs(R) ? fabs(L) : fabs(R);
466 attack_coef = exp(log(0.01)/(0.01 * srate * 0.001));
467 release_coef = exp(log(0.01)/(2000 * srate * 0.001));
468 if( lemax > envelope)
469 envelope = lemax; //attack_coef * (envelope - lemax) + lemax;
470 else
471 envelope = release_coef * (envelope - lemax) + lemax;
473 //use the envelope to bring biggest signal to 1. the biggest
474 //enlargement of the signal is 4.
476 phase_buffer[ppos] = L / std::max(0.25f , (envelope));
477 phase_buffer[ppos + 1] = R / std::max(0.25f , (envelope));
480 plength = std::min(phase_buffer_size, plength + 2);
481 ppos += 2;
482 ppos %= (phase_buffer_size - 2);
484 // analyzer
485 _analyzer.process(L, R);
487 // meter
488 meter_L = L;
489 meter_R = R;
491 //output
492 outs[0][i] = L;
493 outs[1][i] = R;
495 // draw meters
496 SET_IF_CONNECTED(clip_L);
497 SET_IF_CONNECTED(clip_R);
498 SET_IF_CONNECTED(meter_L);
499 SET_IF_CONNECTED(meter_R);
500 return outputs_mask;
503 void analyzer_audio_module::set_sample_rate(uint32_t sr)
505 srate = sr;
506 phase_buffer_size = srate / 30 * 2;
507 phase_buffer_size -= phase_buffer_size % 2;
508 phase_buffer_size = std::min(phase_buffer_size, (int)max_phase_buffer_size);
509 _analyzer.set_sample_rate(sr);
512 bool analyzer_audio_module::get_phase_graph(float ** _buffer, int *_length, int * _mode, bool * _use_fade, float * _fade, int * _accuracy, bool * _display) const {
513 *_buffer = &phase_buffer[0];
514 *_length = plength;
515 *_use_fade = *params[param_gonio_use_fade];
516 *_fade = 0.6;
517 *_mode = *params[param_gonio_mode];
518 *_accuracy = *params[param_gonio_accuracy];
519 *_display = *params[param_gonio_display];
520 return false;
523 bool analyzer_audio_module::get_graph(int index, int subindex, int phase, float *data, int points, cairo_iface *context, int *mode) const
525 if (*params[param_analyzer_display])
526 return _analyzer.get_graph(subindex, phase, data, points, context, mode);
527 return false;
529 bool analyzer_audio_module::get_moving(int index, int subindex, int &direction, float *data, int x, int y, int &offset, uint32_t &color) const
531 if (*params[param_analyzer_display])
532 return _analyzer.get_moving(subindex, direction, data, x, y, offset, color);
533 return false;
535 bool analyzer_audio_module::get_gridline(int index, int subindex, int phase, float &pos, bool &vertical, std::string &legend, cairo_iface *context) const
537 return _analyzer.get_gridline(subindex, phase, pos, vertical, legend, context);
539 bool analyzer_audio_module::get_layers(int index, int generation, unsigned int &layers) const
541 return _analyzer.get_layers(generation, layers);
546 /**********************************************************************
547 * WIDGETS TEST
548 **********************************************************************/
550 widgets_audio_module::widgets_audio_module() {
552 widgets_audio_module::~widgets_audio_module() {
555 void widgets_audio_module::params_changed() {
559 uint32_t widgets_audio_module::process(uint32_t offset, uint32_t numsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
560 float meter_1, meter_2, meter_3, meter_4;
561 for(uint32_t i = offset; i < offset + numsamples; i++) {
562 meter_1 = 0.f;
563 meter_2 = 0.f;
564 meter_3 = 0.f;
565 meter_4 = 0.f;
567 //float L = ins[0][i];
568 //float R = ins[1][i];
569 float values[] = {meter_1, meter_2, meter_3, meter_4};
570 meters.process(values);
572 meters.fall(numsamples);
573 return outputs_mask;
576 void widgets_audio_module::set_sample_rate(uint32_t sr)
578 srate = sr;
579 int meter[] = {param_meter1, param_meter2, param_meter3, param_meter4};
580 int clip[] = {0, 0, 0, 0};
581 meters.init(params, meter, clip, 4, sr);