1 /* Calf DSP plugin pack
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
24 #include <calf/giface.h>
25 #include <calf/modules_tools.h>
26 #include <calf/modules_dev.h>
28 #include <calf/utils.h>
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() {
45 stereo_audio_module::~stereo_audio_module() {
48 void stereo_audio_module::activate() {
52 void stereo_audio_module::deactivate() {
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
++) {
73 outs
[0][i
] = ins
[0][i
];
74 outs
[1][i
] = ins
[1][i
];
89 L
*= *params
[param_level_in
];
90 R
*= *params
[param_level_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
]));
97 if(*params
[param_softclip
]) {
98 R
= _inv_atan_shape
* atan(R
* _sc_level
);
99 L
= _inv_atan_shape
* atan(L
* _sc_level
);
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 )
114 switch((int)*params
[param_mode
])
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
);
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
;
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
);
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
);
167 L
*= (1 - floor(*params
[param_mute_l
] + 0.5));
168 R
*= (1 - floor(*params
[param_mute_r
] + 0.5));
171 L
*= (2 * (1 - floor(*params
[param_phase_l
] + 0.5))) - 1;
172 R
*= (2 * (1 - floor(*params
[param_phase_r
] + 0.5))) - 1;
178 int nbuf
= srate
* (fabs(*params
[param_delay
]) / 1000.f
);
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
];
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
;
197 __l
= L
* _phase_cos_coef
- R
* _phase_sin_coef
;
198 __r
= L
* _phase_sin_coef
+ R
* _phase_cos_coef
;
203 pos
= (pos
+ 2) % buffer_size
;
206 L
*= (1.f
- std::max(0.f
, *params
[param_balance_out
]));
207 R
*= (1.f
+ std::min(0.f
, *params
[param_balance_out
]));
210 L
*= *params
[param_level_out
];
211 R
*= *params
[param_level_out
];
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
);
227 float values
[] = {meter_inL
, meter_inR
, meter_outL
, meter_outR
};
228 meters
.process(values
);
231 bypass
.crossfade(ins
, outs
, 2, orig_offset
, numsamples
);
232 meters
.fall(numsamples
);
236 void stereo_audio_module::set_sample_rate(uint32_t sr
)
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));
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() {
259 mono_audio_module::~mono_audio_module() {
262 void mono_audio_module::activate() {
266 void mono_audio_module::deactivate() {
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
++) {
287 outs
[0][i
] = ins
[0][i
];
288 outs
[1][i
] = ins
[0][i
];
300 L
*= *params
[param_level_in
];
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
);
315 L
*= (1 - floor(*params
[param_mute_l
] + 0.5));
316 R
*= (1 - floor(*params
[param_mute_r
] + 0.5));
319 L
*= (2 * (1 - floor(*params
[param_phase_l
] + 0.5))) - 1;
320 R
*= (2 * (1 - floor(*params
[param_phase_r
] + 0.5))) - 1;
326 int nbuf
= srate
* (fabs(*params
[param_delay
]) / 1000.f
);
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
];
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
;
345 __l
= L
* _phase_cos_coef
- R
* _phase_sin_coef
;
346 __r
= L
* _phase_sin_coef
+ R
* _phase_cos_coef
;
351 pos
= (pos
+ 2) % buffer_size
;
354 L
*= (1.f
- std::max(0.f
, *params
[param_balance_out
]));
355 R
*= (1.f
+ std::min(0.f
, *params
[param_balance_out
]));
358 L
*= *params
[param_level_out
];
359 R
*= *params
[param_level_out
];
368 float values
[] = {meter_in
, meter_outL
, meter_outR
};
369 meters
.process(values
);
372 bypass
.crossfade(ins
, outs
, 2, orig_offset
, numsamples
);
373 meters
.fall(numsamples
);
377 void mono_audio_module::set_sample_rate(uint32_t sr
)
381 buffer_size
= (int)srate
* 0.05 * 2; // delay buffer size multiplied by 2 channels
382 buffer
= (float*) calloc(buffer_size
, sizeof(float));
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() {
403 phase_buffer
= (float*) calloc(max_phase_buffer_size
, sizeof(float));
405 analyzer_audio_module::~analyzer_audio_module() {
408 void analyzer_audio_module::activate() {
412 void analyzer_audio_module::deactivate() {
416 void analyzer_audio_module::params_changed() {
417 float resolution
, offset
;
418 switch((int)*params
[param_analyzer_mode
]) {
425 resolution
= pow(64, *params
[param_analyzer_level
]);
429 // we want to draw Stereo Image
430 resolution
= pow(64, *params
[param_analyzer_level
] * 1.75);
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
);
442 _analyzer
.set_params(
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
);
470 if(L
> 1.f
) clip_L
= srate
>> 3;
471 if(R
> 1.f
) clip_R
= srate
>> 3;
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;
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);
494 ppos
%= (phase_buffer_size
- 2);
497 _analyzer
.process(L
, R
);
508 SET_IF_CONNECTED(clip_L
);
509 SET_IF_CONNECTED(clip_R
);
510 SET_IF_CONNECTED(meter_L
);
511 SET_IF_CONNECTED(meter_R
);
515 void analyzer_audio_module::set_sample_rate(uint32_t 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];
527 *_use_fade
= *params
[param_gonio_use_fade
];
529 *_mode
= *params
[param_gonio_mode
];
530 *_accuracy
= *params
[param_gonio_accuracy
];
531 *_display
= *params
[param_gonio_display
];
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
);
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
);
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 /**********************************************************************
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
++) {
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
);
588 void widgets_audio_module::set_sample_rate(uint32_t 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
);