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
])
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
;
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
);
155 L
*= (1 - floor(*params
[param_mute_l
] + 0.5));
156 R
*= (1 - floor(*params
[param_mute_r
] + 0.5));
159 L
*= (2 * (1 - floor(*params
[param_phase_l
] + 0.5))) - 1;
160 R
*= (2 * (1 - floor(*params
[param_phase_r
] + 0.5))) - 1;
166 int nbuf
= srate
* (fabs(*params
[param_delay
]) / 1000.f
);
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
];
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
;
185 __l
= L
* _phase_cos_coef
- R
* _phase_sin_coef
;
186 __r
= L
* _phase_sin_coef
+ R
* _phase_cos_coef
;
191 pos
= (pos
+ 2) % buffer_size
;
194 L
*= (1.f
- std::max(0.f
, *params
[param_balance_out
]));
195 R
*= (1.f
+ std::min(0.f
, *params
[param_balance_out
]));
198 L
*= *params
[param_level_out
];
199 R
*= *params
[param_level_out
];
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
);
215 float values
[] = {meter_inL
, meter_inR
, meter_outL
, meter_outR
};
216 meters
.process(values
);
219 bypass
.crossfade(ins
, outs
, 2, orig_offset
, numsamples
);
220 meters
.fall(numsamples
);
224 void stereo_audio_module::set_sample_rate(uint32_t sr
)
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));
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() {
247 mono_audio_module::~mono_audio_module() {
250 void mono_audio_module::activate() {
254 void mono_audio_module::deactivate() {
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
++) {
275 outs
[0][i
] = ins
[0][i
];
276 outs
[1][i
] = ins
[0][i
];
288 L
*= *params
[param_level_in
];
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
);
303 L
*= (1 - floor(*params
[param_mute_l
] + 0.5));
304 R
*= (1 - floor(*params
[param_mute_r
] + 0.5));
307 L
*= (2 * (1 - floor(*params
[param_phase_l
] + 0.5))) - 1;
308 R
*= (2 * (1 - floor(*params
[param_phase_r
] + 0.5))) - 1;
314 int nbuf
= srate
* (fabs(*params
[param_delay
]) / 1000.f
);
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
];
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
;
333 __l
= L
* _phase_cos_coef
- R
* _phase_sin_coef
;
334 __r
= L
* _phase_sin_coef
+ R
* _phase_cos_coef
;
339 pos
= (pos
+ 2) % buffer_size
;
342 L
*= (1.f
- std::max(0.f
, *params
[param_balance_out
]));
343 R
*= (1.f
+ std::min(0.f
, *params
[param_balance_out
]));
346 L
*= *params
[param_level_out
];
347 R
*= *params
[param_level_out
];
356 float values
[] = {meter_in
, meter_outL
, meter_outR
};
357 meters
.process(values
);
360 bypass
.crossfade(ins
, outs
, 2, orig_offset
, numsamples
);
361 meters
.fall(numsamples
);
365 void mono_audio_module::set_sample_rate(uint32_t sr
)
369 buffer_size
= (int)srate
* 0.05 * 2; // delay buffer size multiplied by 2 channels
370 buffer
= (float*) calloc(buffer_size
, sizeof(float));
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() {
391 phase_buffer
= (float*) calloc(max_phase_buffer_size
, sizeof(float));
393 analyzer_audio_module::~analyzer_audio_module() {
396 void analyzer_audio_module::activate() {
400 void analyzer_audio_module::deactivate() {
404 void analyzer_audio_module::params_changed() {
405 float resolution
, offset
;
406 switch((int)*params
[param_analyzer_mode
]) {
413 resolution
= pow(64, *params
[param_analyzer_level
]);
417 // we want to draw Stereo Image
418 resolution
= pow(64, *params
[param_analyzer_level
] * 1.75);
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
);
430 _analyzer
.set_params(
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
);
458 if(L
> 1.f
) clip_L
= srate
>> 3;
459 if(R
> 1.f
) clip_R
= srate
>> 3;
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;
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);
482 ppos
%= (phase_buffer_size
- 2);
485 _analyzer
.process(L
, R
);
496 SET_IF_CONNECTED(clip_L
);
497 SET_IF_CONNECTED(clip_R
);
498 SET_IF_CONNECTED(meter_L
);
499 SET_IF_CONNECTED(meter_R
);
503 void analyzer_audio_module::set_sample_rate(uint32_t 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];
515 *_use_fade
= *params
[param_gonio_use_fade
];
517 *_mode
= *params
[param_gonio_mode
];
518 *_accuracy
= *params
[param_gonio_accuracy
];
519 *_display
= *params
[param_gonio_display
];
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
);
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
);
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 /**********************************************************************
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
++) {
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
);
576 void widgets_audio_module::set_sample_rate(uint32_t 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
);