Vocoder: bugfixes
[calf.git] / src / modules_tools.cpp
bloba90ae527e62ce8249a910fa4485231c9290ca9ca
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 for(uint32_t i = offset; i < offset + numsamples; i++) {
111 if(*params[param_bypass] > 0.5) {
112 outs[0][i] = ins[0][i];
113 outs[1][i] = ins[1][i];
114 meter_inL = 0.f;
115 meter_inR = 0.f;
116 meter_outL = 0.f;
117 meter_outR = 0.f;
118 } else {
119 meter_inL = 0.f;
120 meter_inR = 0.f;
121 meter_outL = 0.f;
122 meter_outR = 0.f;
124 float L = ins[0][i];
125 float R = ins[1][i];
127 // levels in
128 L *= *params[param_level_in];
129 R *= *params[param_level_in];
131 // balance in
132 L *= (1.f - std::max(0.f, *params[param_balance_in]));
133 R *= (1.f + std::min(0.f, *params[param_balance_in]));
135 // copy / flip / mono ...
136 switch((int)*params[param_mode])
138 case 0:
139 default:
140 // LR > LR
141 break;
142 case 1:
143 // LR > MS
144 break;
145 case 2:
146 // MS > LR
147 break;
148 case 3:
149 // LR > LL
150 R = L;
151 break;
152 case 4:
153 // LR > RR
154 L = R;
155 break;
156 case 5:
157 // LR > L+R
158 L = (L + R) / 2;
159 R = L;
160 break;
161 case 6:
162 // LR > RL
163 float tmp = L;
164 L = R;
165 R = tmp;
166 break;
169 // softclip
170 if(*params[param_softclip]) {
171 // int ph;
172 // ph = L / fabs(L);
173 // L = L > 0.63 ? ph * (0.63 + 0.36 * (1 - pow(MATH_E, (1.f / 3) * (0.63 + L * ph)))) : L;
174 // ph = R / fabs(R);
175 // R = R > 0.63 ? ph * (0.63 + 0.36 * (1 - pow(MATH_E, (1.f / 3) * (0.63 + R * ph)))) : R;
176 R = _inv_atan_shape * atan(R * _sc_level);
177 L = _inv_atan_shape * atan(L * _sc_level);
180 // GUI stuff
181 meter_inL = L;
182 meter_inR = R;
184 // mute
185 L *= (1 - floor(*params[param_mute_l] + 0.5));
186 R *= (1 - floor(*params[param_mute_r] + 0.5));
188 // phase
189 L *= (2 * (1 - floor(*params[param_phase_l] + 0.5))) - 1;
190 R *= (2 * (1 - floor(*params[param_phase_r] + 0.5))) - 1;
192 // LR/MS
193 L += LL*L + RL*R;
194 R += RR*R + LR*L;
196 // delay
197 buffer[pos] = L;
198 buffer[pos + 1] = R;
200 int nbuf = srate * (fabs(*params[param_delay]) / 1000.f);
201 nbuf -= nbuf % 2;
202 if(*params[param_delay] > 0.f) {
203 R = buffer[(pos - (int)nbuf + 1 + buffer_size) % buffer_size];
204 } else if (*params[param_delay] < 0.f) {
205 L = buffer[(pos - (int)nbuf + buffer_size) % buffer_size];
208 // stereo base
209 float _sb = *params[param_stereo_base];
210 if(_sb < 0) _sb *= 0.5;
212 float __l = L +_sb * L - _sb * R;
213 float __r = R + _sb * R - _sb * L;
215 L = __l;
216 R = __r;
218 // stereo phase
219 __l = L * _phase_cos_coef - R * _phase_sin_coef;
220 __r = L * _phase_sin_coef + R * _phase_cos_coef;
222 L = __l;
223 R = __r;
225 pos = (pos + 2) % buffer_size;
227 // balance out
228 L *= (1.f - std::max(0.f, *params[param_balance_out]));
229 R *= (1.f + std::min(0.f, *params[param_balance_out]));
231 // level
232 L *= *params[param_level_out];
233 R *= *params[param_level_out];
235 //output
236 outs[0][i] = L;
237 outs[1][i] = R;
239 meter_outL = L;
240 meter_outR = R;
242 // phase meter
243 if(fabs(L) > 0.001 and fabs(R) > 0.001) {
244 meter_phase = fabs(fabs(L+R) > 0.000000001 ? sin(fabs((L-R)/(L+R))) : 0.f);
245 } else {
246 meter_phase = 0.f;
249 float values[] = {meter_inL, meter_inR, meter_outL, meter_outR};
250 meters.process(values);
252 meters.fall(numsamples);
253 return outputs_mask;
256 void stereo_audio_module::set_sample_rate(uint32_t sr)
258 srate = sr;
259 // rebuild buffer
260 buffer_size = (int)(srate * 0.05 * 2.f); // buffer size attack rate multiplied by 2 channels
261 buffer = (float*) calloc(buffer_size, sizeof(float));
262 pos = 0;
263 int meter[] = {param_meter_inL, param_meter_inR, param_meter_outL, param_meter_outR};
264 int clip[] = {param_clip_inL, param_clip_inR, param_clip_outL, param_clip_outR};
265 meters.init(params, meter, clip, 4, sr);
268 /**********************************************************************
269 * MONO INPUT by Markus Schmidt
270 **********************************************************************/
272 mono_audio_module::mono_audio_module() {
273 active = false;
274 meter_in = 0.f;
275 meter_outL = 0.f;
276 meter_outR = 0.f;
277 _phase = -1.f;
279 mono_audio_module::~mono_audio_module() {
280 free(buffer);
282 void mono_audio_module::activate() {
283 active = true;
286 void mono_audio_module::deactivate() {
287 active = false;
290 void mono_audio_module::params_changed() {
291 if(*params[param_sc_level] != _sc_level) {
292 _sc_level = *params[param_sc_level];
293 _inv_atan_shape = 1.0 / atan(_sc_level);
295 if(*params[param_stereo_phase] != _phase) {
296 _phase = *params[param_stereo_phase];
297 _phase_cos_coef = cos(_phase / 180 * M_PI);
298 _phase_sin_coef = sin(_phase / 180 * M_PI);
302 uint32_t mono_audio_module::process(uint32_t offset, uint32_t numsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
303 for(uint32_t i = offset; i < offset + numsamples; i++) {
304 if(*params[param_bypass] > 0.5) {
305 outs[0][i] = ins[0][i];
306 outs[1][i] = ins[0][i];
307 meter_in = 0.f;
308 meter_outL = 0.f;
309 meter_outR = 0.f;
310 } else {
311 meter_in = 0.f;
312 meter_outL = 0.f;
313 meter_outR = 0.f;
315 float L = ins[0][i];
317 // levels in
318 L *= *params[param_level_in];
320 // softclip
321 if(*params[param_softclip]) {
322 //int ph = L / fabs(L);
323 //L = L > 0.63 ? ph * (0.63 + 0.36 * (1 - pow(MATH_E, (1.f / 3) * (0.63 + L * ph)))) : L;
324 L = _inv_atan_shape * atan(L * _sc_level);
327 // GUI stuff
328 meter_in = L;
330 float R = L;
332 // mute
333 L *= (1 - floor(*params[param_mute_l] + 0.5));
334 R *= (1 - floor(*params[param_mute_r] + 0.5));
336 // phase
337 L *= (2 * (1 - floor(*params[param_phase_l] + 0.5))) - 1;
338 R *= (2 * (1 - floor(*params[param_phase_r] + 0.5))) - 1;
340 // delay
341 buffer[pos] = L;
342 buffer[pos + 1] = R;
344 int nbuf = srate * (fabs(*params[param_delay]) / 1000.f);
345 nbuf -= nbuf % 2;
346 if(*params[param_delay] > 0.f) {
347 R = buffer[(pos - (int)nbuf + 1 + buffer_size) % buffer_size];
348 } else if (*params[param_delay] < 0.f) {
349 L = buffer[(pos - (int)nbuf + buffer_size) % buffer_size];
352 // stereo base
353 float _sb = *params[param_stereo_base];
354 if(_sb < 0) _sb *= 0.5;
356 float __l = L +_sb * L - _sb * R;
357 float __r = R + _sb * R - _sb * L;
359 L = __l;
360 R = __r;
362 // stereo phase
363 __l = L * _phase_cos_coef - R * _phase_sin_coef;
364 __r = L * _phase_sin_coef + R * _phase_cos_coef;
366 L = __l;
367 R = __r;
369 pos = (pos + 2) % buffer_size;
371 // balance out
372 L *= (1.f - std::max(0.f, *params[param_balance_out]));
373 R *= (1.f + std::min(0.f, *params[param_balance_out]));
375 // level
376 L *= *params[param_level_out];
377 R *= *params[param_level_out];
379 //output
380 outs[0][i] = L;
381 outs[1][i] = R;
383 meter_outL = L;
384 meter_outR = R;
386 float values[] = {meter_in, meter_outL, meter_outR};
387 meters.process(values);
389 meters.fall(numsamples);
390 return outputs_mask;
393 void mono_audio_module::set_sample_rate(uint32_t sr)
395 srate = sr;
396 // rebuild buffer
397 buffer_size = (int)srate * 0.05 * 2; // delay buffer size multiplied by 2 channels
398 buffer = (float*) calloc(buffer_size, sizeof(float));
399 pos = 0;
400 int meter[] = {param_meter_in, param_meter_outL, param_meter_outR};
401 int clip[] = {param_clip_in, param_clip_outL, param_clip_outR};
402 meters.init(params, meter, clip, 3, sr);
405 /**********************************************************************
406 * ANALYZER by Markus Schmidt and Christian Holschuh
407 **********************************************************************/
409 analyzer_audio_module::analyzer_audio_module() {
411 active = false;
412 clip_L = 0.f;
413 clip_R = 0.f;
414 meter_L = 0.f;
415 meter_R = 0.f;
416 envelope = 0.f;
417 ppos = 0;
418 plength = 0;
419 phase_buffer = (float*) calloc(max_phase_buffer_size, sizeof(float));
421 analyzer_audio_module::~analyzer_audio_module() {
422 free(phase_buffer);
424 void analyzer_audio_module::activate() {
425 active = true;
428 void analyzer_audio_module::deactivate() {
429 active = false;
432 void analyzer_audio_module::params_changed() {
433 float resolution, offset;
434 switch((int)*params[param_analyzer_mode]) {
435 case 0:
436 case 1:
437 case 2:
438 case 3:
439 default:
440 // analyzer
441 resolution = pow(64, *params[param_analyzer_level]);
442 offset = 0.75;
443 break;
444 case 4:
445 // we want to draw Stereo Image
446 resolution = pow(64, *params[param_analyzer_level] * 1.75);
447 offset = 1.f;
448 break;
449 case 5:
450 // We want to draw Stereo Difference
451 offset = *params[param_analyzer_level] > 1
452 ? 1 + (*params[param_analyzer_level] - 1) / 4
453 : *params[param_analyzer_level];
454 resolution = pow(64, 2 * offset);
455 break;
458 _analyzer.set_params(
459 resolution,
460 offset,
461 *params[param_analyzer_accuracy],
462 *params[param_analyzer_hold],
463 *params[param_analyzer_smoothing],
464 *params[param_analyzer_mode],
465 *params[param_analyzer_scale],
466 *params[param_analyzer_post],
467 *params[param_analyzer_speed],
468 *params[param_analyzer_windowing],
469 *params[param_analyzer_view],
470 *params[param_analyzer_freeze]
474 uint32_t analyzer_audio_module::process(uint32_t offset, uint32_t numsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
475 for(uint32_t i = offset; i < offset + numsamples; i++) {
476 // let meters fall a bit
477 clip_L -= std::min(clip_L, numsamples);
478 clip_R -= std::min(clip_R, numsamples);
479 meter_L = 0.f;
480 meter_R = 0.f;
482 float L = ins[0][i];
483 float R = ins[1][i];
485 // GUI stuff
486 if(L > 1.f) clip_L = srate >> 3;
487 if(R > 1.f) clip_R = srate >> 3;
489 // goniometer
490 //the goniometer tries to show the signal in maximum
491 //size. therefor an envelope with fast attack and slow
492 //release is calculated with the max value of left and right.
493 float lemax = fabs(L) > fabs(R) ? fabs(L) : fabs(R);
494 attack_coef = exp(log(0.01)/(0.01 * srate * 0.001));
495 release_coef = exp(log(0.01)/(2000 * srate * 0.001));
496 if( lemax > envelope)
497 envelope = lemax; //attack_coef * (envelope - lemax) + lemax;
498 else
499 envelope = release_coef * (envelope - lemax) + lemax;
501 //use the envelope to bring biggest signal to 1. the biggest
502 //enlargement of the signal is 4.
504 phase_buffer[ppos] = L / std::max(0.25f , (envelope));
505 phase_buffer[ppos + 1] = R / std::max(0.25f , (envelope));
508 plength = std::min(phase_buffer_size, plength + 2);
509 ppos += 2;
510 ppos %= (phase_buffer_size - 2);
512 // analyzer
513 _analyzer.process(L, R);
515 // meter
516 meter_L = L;
517 meter_R = R;
519 //output
520 outs[0][i] = L;
521 outs[1][i] = R;
523 // draw meters
524 SET_IF_CONNECTED(clip_L);
525 SET_IF_CONNECTED(clip_R);
526 SET_IF_CONNECTED(meter_L);
527 SET_IF_CONNECTED(meter_R);
528 return outputs_mask;
531 void analyzer_audio_module::set_sample_rate(uint32_t sr)
533 srate = sr;
534 phase_buffer_size = srate / 30 * 2;
535 phase_buffer_size -= phase_buffer_size % 2;
536 phase_buffer_size = std::min(phase_buffer_size, (int)max_phase_buffer_size);
537 _analyzer.set_sample_rate(sr);
540 bool analyzer_audio_module::get_phase_graph(float ** _buffer, int *_length, int * _mode, bool * _use_fade, float * _fade, int * _accuracy, bool * _display) const {
541 *_buffer = &phase_buffer[0];
542 *_length = plength;
543 *_use_fade = *params[param_gonio_use_fade];
544 *_fade = 0.6;
545 *_mode = *params[param_gonio_mode];
546 *_accuracy = *params[param_gonio_accuracy];
547 *_display = *params[param_gonio_display];
548 return false;
551 bool analyzer_audio_module::get_graph(int index, int subindex, int phase, float *data, int points, cairo_iface *context, int *mode) const
553 if (*params[param_analyzer_display])
554 return _analyzer.get_graph(subindex, phase, data, points, context, mode);
555 return false;
557 bool analyzer_audio_module::get_moving(int index, int subindex, int &direction, float *data, int x, int y, int &offset, uint32_t &color) const
559 if (*params[param_analyzer_display])
560 return _analyzer.get_moving(subindex, direction, data, x, y, offset, color);
561 return false;
563 bool analyzer_audio_module::get_gridline(int index, int subindex, int phase, float &pos, bool &vertical, std::string &legend, cairo_iface *context) const
565 return _analyzer.get_gridline(subindex, phase, pos, vertical, legend, context);
567 bool analyzer_audio_module::get_layers(int index, int generation, unsigned int &layers) const
569 return _analyzer.get_layers(generation, layers);