2 * Reusable audio effect classes - implementation.
4 * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
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
22 #include <calf/audio_fx.h>
23 #include <calf/giface.h>
29 using namespace calf_plugins
;
32 simple_phaser::simple_phaser(int _max_stages
, float *x1vals
, float *y1vals
)
34 max_stages
= _max_stages
;
44 set_stages(_max_stages
);
47 void simple_phaser::set_stages(int _stages
)
51 assert(_stages
<= max_stages
);
52 if (_stages
> max_stages
)
54 for (int i
= stages
; i
< _stages
; i
++)
63 void simple_phaser::reset()
68 for (int i
= 0; i
< max_stages
; i
++)
73 void simple_phaser::control_step()
76 int v
= phase
.get() + 0x40000000;
79 // triangle wave, range from 0 to INT_MAX
80 double vf
= (double)((v
>> 16) * (1.0 / 16384.0) - 1);
82 float freq
= base_frq
* pow(2.0, vf
* mod_depth
/ 1200.0);
83 freq
= dsp::clip
<float>(freq
, 10.0, 0.49 * sample_rate
);
84 stage1
.set_ap_w(freq
* (M_PI
/ 2.0) * odsr
);
86 for (int i
= 0; i
< stages
; i
++)
94 void simple_phaser::process(float *buf_out
, float *buf_in
, int nsamples
)
96 for (int i
=0; i
<nsamples
; i
++) {
100 float in
= *buf_in
++;
101 float fd
= in
+ state
* fb
;
102 for (int j
= 0; j
< stages
; j
++)
103 fd
= stage1
.process_ap(fd
, x1
[j
], y1
[j
]);
106 float sdry
= in
* gs_dry
.get();
107 float swet
= fd
* gs_wet
.get();
108 *buf_out
++ = sdry
+ swet
;
112 float simple_phaser::freq_gain(float freq
, float sr
) const
114 typedef std::complex<double> cfloat
;
115 freq
*= 2.0 * M_PI
/ sr
;
116 cfloat z
= 1.0 / exp(cfloat(0.0, freq
)); // z^-1
118 cfloat p
= cfloat(1.0);
119 cfloat stg
= stage1
.h_z(z
);
121 for (int i
= 0; i
< stages
; i
++)
124 p
= p
/ (cfloat(1.0) - cfloat(fb
) * p
);
125 return std::abs(cfloat(gs_dry
.get_last()) + cfloat(gs_wet
.get_last()) * p
);
128 ///////////////////////////////////////////////////////////////////////////////////
130 void biquad_filter_module::calculate_filter(float freq
, float q
, int mode
, float gain
)
132 if (mode
<= mode_36db_lp
) {
134 left
[0].set_lp_rbj(freq
, pow(q
, 1.0 / order
), srate
, gain
);
135 } else if ( mode_12db_hp
<= mode
&& mode
<= mode_36db_hp
) {
136 order
= mode
- mode_12db_hp
+ 1;
137 left
[0].set_hp_rbj(freq
, pow(q
, 1.0 / order
), srate
, gain
);
138 } else if ( mode_6db_bp
<= mode
&& mode
<= mode_18db_bp
) {
139 order
= mode
- mode_6db_bp
+ 1;
140 left
[0].set_bp_rbj(freq
, pow(q
, 1.0 / order
), srate
, gain
);
141 } else { // mode_6db_br <= mode <= mode_18db_br
142 order
= mode
- mode_6db_br
+ 1;
143 left
[0].set_br_rbj(freq
, order
* 0.1 * q
, srate
, gain
);
146 right
[0].copy_coeffs(left
[0]);
147 for (int i
= 1; i
< order
; i
++) {
148 left
[i
].copy_coeffs(left
[0]);
149 right
[i
].copy_coeffs(left
[0]);
153 void biquad_filter_module::filter_activate()
155 for (int i
=0; i
< order
; i
++) {
161 void biquad_filter_module::sanitize()
163 for (int i
=0; i
< order
; i
++) {
169 int biquad_filter_module::process_channel(uint16_t channel_no
, const float *in
, float *out
, uint32_t numsamples
, int inmask
) {
170 dsp::biquad_d1
*filter
;
171 switch (channel_no
) {
188 for (uint32_t i
= 0; i
< numsamples
; i
++)
189 out
[i
] = filter
[0].process(in
[i
]);
192 for (uint32_t i
= 0; i
< numsamples
; i
++)
193 out
[i
] = filter
[1].process(filter
[0].process(in
[i
]));
196 for (uint32_t i
= 0; i
< numsamples
; i
++)
197 out
[i
] = filter
[2].process(filter
[1].process(filter
[0].process(in
[i
])));
201 if (filter
[order
- 1].empty())
205 for (uint32_t i
= 0; i
< numsamples
; i
++)
206 out
[i
] = filter
[0].process_zeroin();
209 if (filter
[0].empty())
210 for (uint32_t i
= 0; i
< numsamples
; i
++)
211 out
[i
] = filter
[1].process_zeroin();
213 for (uint32_t i
= 0; i
< numsamples
; i
++)
214 out
[i
] = filter
[1].process(filter
[0].process_zeroin());
217 if (filter
[1].empty())
218 for (uint32_t i
= 0; i
< numsamples
; i
++)
219 out
[i
] = filter
[2].process_zeroin();
221 for (uint32_t i
= 0; i
< numsamples
; i
++)
222 out
[i
] = filter
[2].process(filter
[1].process(filter
[0].process_zeroin()));
226 for (int i
= 0; i
< order
; i
++)
227 filter
[i
].sanitize();
228 return filter
[order
- 1].empty() ? 0 : inmask
;
231 float biquad_filter_module::freq_gain(int subindex
, float freq
, float srate
) const
234 for (int j
= 0; j
< order
; j
++)
235 level
*= left
[j
].freq_gain(freq
, srate
);
239 /////////////////////////////////////////////////////////////////////////////////////////////////////////
241 void reverb::update_times()
246 tl
[0] = 397 << 16, tr
[0] = 383 << 16;
247 tl
[1] = 457 << 16, tr
[1] = 429 << 16;
248 tl
[2] = 549 << 16, tr
[2] = 631 << 16;
249 tl
[3] = 649 << 16, tr
[3] = 756 << 16;
250 tl
[4] = 773 << 16, tr
[4] = 803 << 16;
251 tl
[5] = 877 << 16, tr
[5] = 901 << 16;
254 tl
[0] = 697 << 16, tr
[0] = 783 << 16;
255 tl
[1] = 957 << 16, tr
[1] = 929 << 16;
256 tl
[2] = 649 << 16, tr
[2] = 531 << 16;
257 tl
[3] = 1049 << 16, tr
[3] = 1177 << 16;
258 tl
[4] = 473 << 16, tr
[4] = 501 << 16;
259 tl
[5] = 587 << 16, tr
[5] = 681 << 16;
263 tl
[0] = 697 << 16, tr
[0] = 783 << 16;
264 tl
[1] = 957 << 16, tr
[1] = 929 << 16;
265 tl
[2] = 649 << 16, tr
[2] = 531 << 16;
266 tl
[3] = 1249 << 16, tr
[3] = 1377 << 16;
267 tl
[4] = 1573 << 16, tr
[4] = 1671 << 16;
268 tl
[5] = 1877 << 16, tr
[5] = 1781 << 16;
271 tl
[0] = 1097 << 16, tr
[0] = 1087 << 16;
272 tl
[1] = 1057 << 16, tr
[1] = 1031 << 16;
273 tl
[2] = 1049 << 16, tr
[2] = 1039 << 16;
274 tl
[3] = 1083 << 16, tr
[3] = 1055 << 16;
275 tl
[4] = 1075 << 16, tr
[4] = 1099 << 16;
276 tl
[5] = 1003 << 16, tr
[5] = 1073 << 16;
279 tl
[0] = 197 << 16, tr
[0] = 133 << 16;
280 tl
[1] = 357 << 16, tr
[1] = 229 << 16;
281 tl
[2] = 549 << 16, tr
[2] = 431 << 16;
282 tl
[3] = 949 << 16, tr
[3] = 1277 << 16;
283 tl
[4] = 1173 << 16, tr
[4] = 1671 << 16;
284 tl
[5] = 1477 << 16, tr
[5] = 1881 << 16;
287 tl
[0] = 197 << 16, tr
[0] = 133 << 16;
288 tl
[1] = 257 << 16, tr
[1] = 179 << 16;
289 tl
[2] = 549 << 16, tr
[2] = 431 << 16;
290 tl
[3] = 619 << 16, tr
[3] = 497 << 16;
291 tl
[4] = 1173 << 16, tr
[4] = 1371 << 16;
292 tl
[5] = 1577 << 16, tr
[5] = 1881 << 16;
296 float fDec
=1000 + 2400.f
* diffusion
;
297 for (int i
= 0 ; i
< 6; i
++) {
298 ldec
[i
]=exp(-float(tl
[i
] >> 16) / fDec
),
299 rdec
[i
]=exp(-float(tr
[i
] >> 16) / fDec
);
305 apL1
.reset();apR1
.reset();
306 apL2
.reset();apR2
.reset();
307 apL3
.reset();apR3
.reset();
308 apL4
.reset();apR4
.reset();
309 apL5
.reset();apR5
.reset();
310 apL6
.reset();apR6
.reset();
311 lp_left
.reset();lp_right
.reset();
312 old_left
= 0; old_right
= 0;
315 void reverb::process(float &left
, float &right
)
317 unsigned int ipart
= phase
.ipart();
319 // the interpolated LFO might be an overkill here
320 int lfo
= phase
.lerp_by_fract_int
<int, 14, int>(sine
.data
[ipart
], sine
.data
[ipart
+1]) >> 2;
324 left
= apL1
.process_allpass_comb_lerp16(left
, tl
[0] - 45*lfo
, ldec
[0]);
325 left
= apL2
.process_allpass_comb_lerp16(left
, tl
[1] + 47*lfo
, ldec
[1]);
326 float out_left
= left
;
327 left
= apL3
.process_allpass_comb_lerp16(left
, tl
[2] + 54*lfo
, ldec
[2]);
328 left
= apL4
.process_allpass_comb_lerp16(left
, tl
[3] - 69*lfo
, ldec
[3]);
329 left
= apL5
.process_allpass_comb_lerp16(left
, tl
[4] + 69*lfo
, ldec
[4]);
330 left
= apL6
.process_allpass_comb_lerp16(left
, tl
[5] - 46*lfo
, ldec
[5]);
331 old_left
= lp_left
.process(left
* fb
);
335 right
= apR1
.process_allpass_comb_lerp16(right
, tr
[0] - 45*lfo
, rdec
[0]);
336 right
= apR2
.process_allpass_comb_lerp16(right
, tr
[1] + 47*lfo
, rdec
[1]);
337 float out_right
= right
;
338 right
= apR3
.process_allpass_comb_lerp16(right
, tr
[2] + 54*lfo
, rdec
[2]);
339 right
= apR4
.process_allpass_comb_lerp16(right
, tr
[3] - 69*lfo
, rdec
[3]);
340 right
= apR5
.process_allpass_comb_lerp16(right
, tr
[4] + 69*lfo
, rdec
[4]);
341 right
= apR6
.process_allpass_comb_lerp16(right
, tr
[5] - 46*lfo
, rdec
[5]);
342 old_right
= lp_right
.process(right
* fb
);
345 left
= out_left
, right
= out_right
;
348 /// Distortion Module by Tom Szilagyi
350 /// This module provides a blendable saturation stage
351 ///////////////////////////////////////////////////////////////////////////////////////////////
353 tap_distortion::tap_distortion()
358 rdrive
= rbdr
= kpa
= kpb
= kna
= knb
= ap
= an
= imr
= kc
= srct
= sq
= pwrq
= prev_med
= prev_out
= 0.f
;
359 drive_old
= blend_old
= -1.f
;
363 void tap_distortion::activate()
366 set_params(0.f
, 0.f
);
368 void tap_distortion::deactivate()
373 void tap_distortion::set_params(float blend
, float drive
)
375 // set distortion coeffs
376 if ((drive_old
!= drive
) || (blend_old
!= blend
)) {
377 rdrive
= 12.0f
/ drive
;
378 rbdr
= rdrive
/ (10.5f
- blend
) * 780.0f
/ 33.0f
;
379 kpa
= D(2.0f
* (rdrive
*rdrive
) - 1.0f
) + 1.0f
;
380 kpb
= (2.0f
- kpa
) / 2.0f
;
381 ap
= ((rdrive
*rdrive
) - kpa
+ 1.0f
) / 2.0f
;
382 kc
= kpa
/ D(2.0f
* D(2.0f
* (rdrive
*rdrive
) - 1.0f
) - 2.0f
* rdrive
*rdrive
);
384 srct
= (0.1f
* srate
) / (0.1f
* srate
+ 1.0f
);
386 knb
= -1.0f
* rbdr
/ D(sq
);
387 kna
= 2.0f
* kc
* rbdr
/ D(sq
);
389 imr
= 2.0f
* knb
+ D(2.0f
* kna
+ 4.0f
* an
- 1.0f
);
390 pwrq
= 2.0f
/ (imr
+ 1.0f
);
397 void tap_distortion::set_sample_rate(uint32_t sr
)
400 over
= srate
* 2 > 96000 ? 1 : 2;
401 resampler
.set_params(srate
, over
, 2);
404 float tap_distortion::process(float in
)
406 double *samples
= resampler
.upsample((double)in
);
408 for (int o
= 0; o
< over
; o
++) {
409 float proc
= samples
[o
];
412 med
= (D(ap
+ proc
* (kpa
- proc
)) + kpb
) * pwrq
;
414 med
= (D(an
- proc
* (kna
+ proc
)) + knb
) * pwrq
* -1.0f
;
416 proc
= srct
* (med
- prev_med
+ prev_out
);
420 meter
= std::max(meter
, proc
);
422 float out
= (float)resampler
.downsample(samples
);
426 float tap_distortion::get_distortion_level()
431 ////////////////////////////////////////////////////////////////////////////////
433 simple_lfo::simple_lfo()
439 void simple_lfo::activate()
445 void simple_lfo::deactivate()
450 float simple_lfo::get_value()
452 return get_value_from_phase(phase
, offset
) * amount
;
455 float simple_lfo::get_value_from_phase(float ph
, float off
) const
458 float phs
= ph
+ off
;
460 phs
= fmod(phs
, 1.f
);
465 val
= sin((phs
* 360.f
) * M_PI
/ 180);
470 val
= (phs
- 0.75) * 4 - 1;
472 val
= (phs
- 0.5) * 4 * -1;
474 val
= 1 - (phs
- 0.25) * 4;
480 val
= (phs
< 0.5) ? -1 : +1;
494 void simple_lfo::advance(uint32_t count
)
496 //this function walks from 0.f to 1.f and starts all over again
497 phase
+= count
* freq
* (1.0 / srate
);
499 phase
= fmod(phase
, 1.f
);
502 void simple_lfo::set_phase(float ph
)
504 //set the phase from outsinde
507 phase
= fmod(phase
, 1.f
);
510 void simple_lfo::set_params(float f
, int m
, float o
, uint32_t sr
, float a
)
512 // freq: a value in Hz
513 // mode: sine=0, triangle=1, square=2, saw_up=3, saw_down=4
514 // offset: value between 0.f and 1.f to offset the lfo in time
521 void simple_lfo::set_freq(float f
)
525 void simple_lfo::set_mode(int m
)
529 void simple_lfo::set_offset(float o
)
533 void simple_lfo::set_amount(float a
)
537 bool simple_lfo::get_graph(float *data
, int points
, cairo_iface
*context
, int *mode
) const
541 for (int i
= 0; i
< points
; i
++) {
542 float ph
= (float)i
/ (float)points
;
543 data
[i
] = get_value_from_phase(ph
, offset
) * amount
;
548 bool simple_lfo::get_dot(float &x
, float &y
, int &size
, cairo_iface
*context
) const
552 float phs
= phase
+ offset
;
554 phs
= fmod(phs
, 1.f
);
556 y
= get_value_from_phase(phase
, offset
) * amount
;
561 /// Lookahead Limiter by Christian Holschuh and Markus Schmidt
563 lookahead_limiter::lookahead_limiter() {
568 overall_buffer_size
= 0;
581 auto_release
= false;
591 lookahead_limiter::~lookahead_limiter()
598 void lookahead_limiter::activate()
605 void lookahead_limiter::set_multi(bool set
) { use_multi
= set
; }
607 void lookahead_limiter::deactivate()
612 float lookahead_limiter::get_attenuation()
619 void lookahead_limiter::set_sample_rate(uint32_t sr
)
623 overall_buffer_size
= (int)(srate
* (100.f
/ 1000.f
) * channels
) + channels
; // buffer size attack rate multiplied by 2 channels
624 buffer
= (float*) calloc(overall_buffer_size
, sizeof(float));
627 nextdelta
= (float*) calloc(overall_buffer_size
, sizeof(float));
628 nextpos
= (int*) malloc(overall_buffer_size
* sizeof(int));
629 memset(nextpos
, -1, overall_buffer_size
* sizeof(int));
634 void lookahead_limiter::set_params(float l
, float a
, float r
, float w
, bool ar
, float arc
, bool d
)
638 release
= r
/ 1000.f
;
645 void lookahead_limiter::reset() {
646 int bs
= (int)(srate
* attack
* channels
);
647 buffer_size
= bs
- bs
% channels
; // buffer size attack rate
658 void lookahead_limiter::reset_asc() {
665 float lookahead_limiter::get_rdelta(float peak
, float _limit
, float _att
, bool _asc
) {
667 // calc the att for average input to walk to if we use asc (att of average signal)
668 float _a_att
= (limit
* weight
) / (asc_coeff
* asc
) * (float)asc_c
;
670 // calc a release delta from this attenuation
671 float _rdelta
= (1.0 - _att
) / (srate
* release
);
672 if(_asc
and auto_release
and asc_c
> 0 and _a_att
> _att
) {
673 // check if releasing to average level of peaks is steeper than
675 float _delta
= std::max((_a_att
- _att
) / (srate
* release
), _rdelta
/ 10);
676 if(_delta
< _rdelta
) {
685 void lookahead_limiter::process(float &left
, float &right
, float * multi_buffer
)
687 // PROTIP: harming paying customers enough to make them develop a competing
688 // product may be considered an example of a less than sound business practice.
690 // fill lookahead buffer
692 // if we're sanitizing (zeroing) the buffer on attack time change,
693 // don't write the samples to the buffer
695 buffer
[pos
+ 1] = 0.f
;
698 buffer
[pos
+ 1] = right
;
701 // are we using multiband? get the multiband coefficient or use 1.f
702 float multi_coeff
= (use_multi
) ? multi_buffer
[pos
] : 1.f
;
704 // calc the real limit including weight and multi coeff
705 float _limit
= limit
* multi_coeff
* weight
;
707 // input peak - impact higher in left or right channel?
708 peak
= fabs(left
) > fabs(right
) ? fabs(left
) : fabs(right
);
710 // add an eventually appearing peak to the asc fake buffer if asc active
711 if(auto_release
and peak
> _limit
) {
716 if(peak
> _limit
or multi_coeff
< 1.0) {
717 float _multi_coeff
= 1.f
;
720 // calc the attenuation needed to reduce incoming peak
721 float _att
= std::min(_limit
/ peak
, 1.f
);
722 // calc release without any asc to keep all relevant peaks
723 float _rdelta
= get_rdelta(peak
, _limit
, _att
, false);
725 // calc the delta for walking to incoming peak attenuation
726 float _delta
= (_limit
/ peak
- att
) / buffer_size
* channels
;
729 // is the delta more important than the actual one?
730 // if so, we can forget about all stored deltas (because they can't
731 // be more important - we already checked that earlier) and use this
732 // delta now. and we have to create a release delta in nextpos buffer
735 nextdelta
[0] = _rdelta
;
740 // we have a peak on input its delta is less important than the
741 // actual delta. But what about the stored deltas we're following?
744 for(i
= nextiter
; i
< nextiter
+ nextlen
; i
++) {
745 // walk through our nextpos buffer
746 int j
= i
% buffer_size
;
747 // calculate a delta for the next stored peak
748 // are we using multiband? then get the multi_coeff for the
750 _multi_coeff
= (use_multi
) ? multi_buffer
[nextpos
[j
]] : 1.f
;
751 // is the left or the right channel on this position more
753 _peak
= fabs(buffer
[nextpos
[j
]]) > fabs(buffer
[nextpos
[j
] + 1]) ? fabs(buffer
[nextpos
[j
]]) : fabs(buffer
[nextpos
[j
] + 1]);
754 // calc a delta to use to reach our incoming peak from the
756 _delta
= (_limit
/ peak
- (limit
* _multi_coeff
* weight
) / _peak
) / (((buffer_size
- nextpos
[j
] + pos
) % buffer_size
) / channels
);
757 if(_delta
< nextdelta
[j
]) {
758 // if the buffered delta is more important than the delta
759 // used to reach our peak from the stored position, store
760 // the new delta at that position and stop the loop
761 nextdelta
[j
] = _delta
;
767 // there was something more important in the next-buffer.
768 // throw away any position and delta after the important
769 // position and add a new release delta
770 nextlen
= i
- nextiter
+ 1;
771 nextpos
[(nextiter
+ nextlen
) % buffer_size
] = pos
;
772 nextdelta
[(nextiter
+ nextlen
) % buffer_size
] = _rdelta
;
773 // set the next following position value to -1 (cleaning up the
775 nextpos
[(nextiter
+ nextlen
+ 1) % buffer_size
] = -1;
776 // and raise the length of our nextpos buffer for keeping the
783 // switch left and right pointers in buffer to output position
784 left
= buffer
[(pos
+ channels
) % buffer_size
];
785 right
= buffer
[(pos
+ channels
+ 1) % buffer_size
];
787 // if a peak leaves the buffer, remove it from asc fake buffer
788 // but only if we're not sanitizing asc buffer
789 float _peak
= fabs(left
) > fabs(right
) ? fabs(left
) : fabs(right
);
790 float _multi_coeff
= (use_multi
) ? multi_buffer
[(pos
+ channels
) % buffer_size
] : 1.f
;
791 if(pos
== asc_pos
and !asc_changed
) {
794 if(auto_release
and asc_pos
== -1 and _peak
> (limit
* weight
* _multi_coeff
)) {
799 // change the attenuation level
802 // ...and calculate outpout from it
806 if((pos
+ channels
) % buffer_size
== nextpos
[nextiter
]) {
807 // if we reach a buffered position, change the actual delta and erase
808 // this (the first) element from nextpos and nextdelta buffer
810 // set delta to asc influenced release delta
811 delta
= get_rdelta(_peak
, (limit
* weight
* _multi_coeff
), att
);
813 // if there are more positions to walk to, calc delta to next
814 // position in buffer and compare it to release delta (keep
815 // changes between peaks below asc steepness)
816 int _nextpos
= nextpos
[(nextiter
+ 1) % buffer_size
];
817 float __peak
= fabs(buffer
[_nextpos
]) > fabs(buffer
[_nextpos
+ 1]) ? fabs(buffer
[_nextpos
]) : fabs(buffer
[_nextpos
+ 1]);
818 float __multi_coeff
= (use_multi
) ? multi_buffer
[_nextpos
] : 1.f
;
819 float __delta
= ((limit
* __multi_coeff
* weight
) / __peak
- att
) / (((buffer_size
+ _nextpos
- ((pos
+ channels
) % buffer_size
)) % buffer_size
) / channels
);
820 if(__delta
< delta
) {
825 // if no asc set delta from nextdelta buffer and fix the attenuation
826 delta
= nextdelta
[nextiter
];
827 att
= (limit
* weight
* _multi_coeff
) / _peak
;
829 // remove first element from circular nextpos buffer
831 nextpos
[nextiter
] = -1;
832 nextiter
= (nextiter
+ 1) % buffer_size
;
836 // release time seems over, reset attenuation and delta
844 // main limiting party is over, let's cleanup the puke
847 // we're sanitizing? then send 0.f as output
852 // security personnel pawing your values
854 // if this happens we're doomed!!
855 // may happen on manually lowering attack
856 att
= 0.0000000000001;
857 delta
= (1.0f
- att
) / (srate
* release
);
860 if(att
!= 1.f
and 1 - att
< 0.0000000000001) {
865 if(delta
!= 0.f
and fabs(delta
) < 0.00000000000001) {
870 // post treatment (denormal, limit)
874 // store max attenuation for meter output
875 att_max
= (att
< att_max
) ? att
: att_max
;
877 // step forward in our sample ring buffer
878 pos
= (pos
+ channels
) % buffer_size
;
880 // sanitizing is always done after a full cycle through the lookahead buffer
881 if(_sanitize
and pos
== 0) _sanitize
= false;
886 bool lookahead_limiter::get_asc() {
887 if(!asc_active
) return false;
893 ////////////////////////////////////////////////////////////////////////////////
895 transients::transients() {
914 sustain_ended
= false;
916 transients::~transients()
920 void transients::set_channels(int ch
) {
922 lookbuf
= (float*) calloc(looksize
* channels
, sizeof(float));
925 void transients::set_sample_rate(uint32_t sr
) {
927 attack_coef
= exp(log(0.01) / (0.001 * srate
));
928 release_coef
= exp(log(0.01) / (0.2f
* srate
));
929 // due to new calculation in attack, we sometimes get harsh
930 // gain reduction/boost.
931 // to prevent "clicks" a maxdelta is set, which allows the signal
932 // to raise/fall ~6dB/ms.
933 maxdelta
= pow(4, 1.f
/ (0.001 * srate
));
936 void transients::set_params(float att_t
, float att_l
, float rel_t
, float rel_l
, float sust_th
, int look
, float mx
) {
939 sust_thres
= sust_th
;
942 att_level
= att_l
> 0 ? 0.25f
* pow(att_l
* 8, 2)
943 : -0.25f
* pow(att_l
* 4, 2);
944 rel_level
= rel_l
> 0 ? 0.5f
* pow(rel_l
* 8, 2)
945 : -0.25f
* pow(rel_l
* 4, 2);
948 void transients::calc_relfac()
950 relfac
= pow(0.5f
, 1.f
/ (0.001 * rel_time
* srate
));
952 void transients::process(float *in
) {
953 // fill lookahead buffer
955 for (int i
= 0; i
< channels
; i
++) {
956 lookbuf
[lookpos
+ i
] = in
[i
];
962 // this is the real envelope follower curve. It raises as
963 // fast as the signal is raising and falls much slower
964 // depending on the sample rate and the ffactor
965 // (the falling factor)
967 envelope
= attack_coef
* (envelope
- s
) + s
;
969 envelope
= release_coef
* (envelope
- s
) + s
;
972 // this is a curve which follows the envelope slowly.
973 // It never can rise above the envelope. It reaches 70.7%
974 // of the envelope in a certain amount of time set by the user
976 float attdelta
= (envelope
- attack
)
978 / (srate
* att_time
* 0.001);
979 if (sustain_ended
== true and envelope
/ attack
- 1 > 0.2f
)
980 sustain_ended
= false;
983 // never raise above envelope
984 attack
= std::min(envelope
, attack
);
987 // this is a curve which is always above the envelope. It
988 // starts to fall when the envelope falls beneath the
991 if ((envelope
/ release
) - sust_thres
< 0 and sustain_ended
== false)
992 sustain_ended
= true;
993 double reldelta
= sustain_ended
? relfac
: 1;
995 // release delta can never raise above 0
998 // never fall below envelope
999 release
= std::max(envelope
, release
);
1001 // difference between attack and envelope
1002 float attdiff
= attack
> 0 ? log(envelope
/ attack
) : 0;
1004 // difference between release and envelope
1005 float reldiff
= envelope
> 0 ? log(release
/ envelope
) : 0;
1007 // amplification factor from attack and release curve
1008 float ampfactor
= attdiff
* att_level
+ reldiff
* rel_level
;
1009 old_return
= new_return
;
1010 new_return
= 1 + (ampfactor
< 0 ? exp(ampfactor
) - 1 : ampfactor
);
1011 if (new_return
/ old_return
> maxdelta
)
1012 new_return
= old_return
* maxdelta
;
1013 if (new_return
/ old_return
< 1 / maxdelta
)
1014 new_return
= old_return
/ maxdelta
;
1016 int pos
= (lookpos
+ looksize
* channels
- lookahead
* channels
) % (looksize
* channels
);
1017 for (int i
= 0; i
< channels
; i
++) {
1018 in
[i
] = lookbuf
[pos
+ i
] * new_return
* mix
+ lookbuf
[pos
+ i
] * (mix
* -1 + 1);
1022 lookpos
= (lookpos
+ channels
) % (looksize
* channels
);
1028 //////////////////////////////////////////////////////////////////
1030 crossover::crossover() {
1035 void crossover::set_sample_rate(uint32_t sr
) {
1038 void crossover::init(int c
, int b
, uint32_t sr
) {
1039 channels
= std::min(8, c
);
1040 bands
= std::min(8, b
);
1042 for(int b
= 0; b
< bands
; b
++) {
1043 // reset frequency settings
1047 for (int c
= 0; c
< channels
; c
++) {
1053 float crossover::set_filter(int b
, float f
, bool force
) {
1054 // keep between neighbour bands
1056 f
= std::max((float)freq
[b
-1] * 1.1f
, f
);
1058 f
= std::min((float)freq
[b
+1] * 0.9f
, f
);
1059 // restrict to 10-20k
1060 f
= std::max(10.f
, std::min(20000.f
, f
));
1061 // nothing changed? return
1062 if (freq
[b
] == f
and !force
)
1072 q
= 0.7071068123730965;
1078 for (int c
= 0; c
< channels
; c
++) {
1080 lp
[c
][b
][0].set_lp_rbj(freq
[b
], q
, (float)srate
);
1081 hp
[c
][b
][0].set_hp_rbj(freq
[b
], q
, (float)srate
);
1083 lp
[c
][b
][0].copy_coeffs(lp
[c
-1][b
][0]);
1084 hp
[c
][b
][0].copy_coeffs(hp
[c
-1][b
][0]);
1088 lp
[c
][b
][1].set_lp_rbj(freq
[b
], 1.34, (float)srate
);
1089 hp
[c
][b
][1].set_hp_rbj(freq
[b
], 1.34, (float)srate
);
1091 lp
[c
][b
][1].copy_coeffs(lp
[c
-1][b
][1]);
1092 hp
[c
][b
][1].copy_coeffs(hp
[c
-1][b
][1]);
1094 lp
[c
][b
][2].copy_coeffs(lp
[c
][b
][0]);
1095 hp
[c
][b
][2].copy_coeffs(hp
[c
][b
][0]);
1096 lp
[c
][b
][3].copy_coeffs(lp
[c
][b
][1]);
1097 hp
[c
][b
][3].copy_coeffs(hp
[c
][b
][1]);
1099 lp
[c
][b
][1].copy_coeffs(lp
[c
][b
][0]);
1100 hp
[c
][b
][1].copy_coeffs(hp
[c
][b
][0]);
1103 redraw_graph
= std::min(2, redraw_graph
+ 1);
1106 void crossover::set_mode(int m
) {
1110 for(int i
= 0; i
< bands
- 1; i
++) {
1111 set_filter(i
, freq
[i
], true);
1113 redraw_graph
= std::min(2, redraw_graph
+ 1);
1115 void crossover::set_active(int b
, bool a
) {
1119 redraw_graph
= std::min(2, redraw_graph
+ 1);
1121 void crossover::set_level(int b
, float l
) {
1125 redraw_graph
= std::min(2, redraw_graph
+ 1);
1127 void crossover::process(float *data
) {
1128 for (int c
= 0; c
< channels
; c
++) {
1129 for(int b
= 0; b
< bands
; b
++) {
1130 out
[c
][b
] = data
[c
];
1131 for (int f
= 0; f
< get_filter_count(); f
++){
1133 out
[c
][b
] = lp
[c
][b
][f
].process(out
[c
][b
]);
1134 lp
[c
][b
][f
].sanitize();
1137 out
[c
][b
] = hp
[c
][b
- 1][f
].process(out
[c
][b
]);
1138 hp
[c
][b
- 1][f
].sanitize();
1141 out
[c
][b
] *= level
[b
];
1145 float crossover::get_value(int c
, int b
) {
1148 bool crossover::get_graph(int subindex
, int phase
, float *data
, int points
, cairo_iface
*context
, int *mode
) const
1150 if (subindex
>= bands
) {
1151 redraw_graph
= std::max(0, redraw_graph
- 1);
1156 for (int i
= 0; i
< points
; i
++) {
1158 freq
= 20.0 * pow (20000.0 / 20.0, i
* 1.0 / points
);
1159 for(int f
= 0; f
< get_filter_count(); f
++) {
1160 if(subindex
< bands
-1)
1161 ret
*= lp
[0][subindex
][f
].freq_gain(freq
, (float)srate
);
1163 ret
*= hp
[0][subindex
- 1][f
].freq_gain(freq
, (float)srate
);
1165 ret
*= level
[subindex
];
1166 context
->set_source_rgba(0.15, 0.2, 0.0, !active
[subindex
] ? 0.3 : 0.8);
1167 data
[i
] = dB_grid(ret
);
1171 bool crossover::get_layers(int index
, int generation
, unsigned int &layers
) const
1173 layers
= 0 | (redraw_graph
or !generation
? LG_CACHE_GRAPH
: 0) | (!generation
? LG_CACHE_GRID
: 0);
1174 return redraw_graph
or !generation
;
1177 int crossover::get_filter_count() const
1190 //////////////////////////////////////////////////////////////////
1192 bitreduction::bitreduction()
1201 redraw_graph
= true;
1204 void bitreduction::set_sample_rate(uint32_t sr
)
1208 void bitreduction::set_params(float b
, float mo
, bool bp
, uint32_t md
, float d
, float a
)
1215 coeff
= powf(2.0f
, b
) - 1;
1216 sqr
= sqrt(coeff
/ 2);
1217 aa1
= (1.f
- aa
) / 2.f
;
1218 redraw_graph
= true;
1220 float bitreduction::add_dc(float s
, float dc
) const
1222 return s
> 0 ? s
*= dc
: s
/= dc
;
1224 float bitreduction::remove_dc(float s
, float dc
) const
1226 return s
> 0 ? s
/= dc
: s
*= dc
;
1228 float bitreduction::waveshape(float in
) const
1234 in
= add_dc(in
, dc
);
1236 // main rounding calculation depending on mode
1238 // the idea for anti-aliasing:
1239 // you need a function f which brings you to the scale, where you want to round
1240 // and the function f_b (with f(f_b)=id) which brings you back to your original scale.
1242 // then you can use the logic below in the following way:
1243 // y = f(in) and k = roundf(y)
1245 // k = f_b(k) + ( f_b(k+1) - f_b(k) ) *0.5 * (sin(x - PI/2) + 1)
1247 // k = f_b(k) - ( f_b(k+1) - f_b(k) ) *0.5 * (sin(x - PI/2) + 1)
1249 // whereas x = (fabs(f(in) - k) - aa1) * PI / aa
1258 if (k
- aa1
<= y
and y
<= k
+ aa1
) {
1260 } else if (y
> k
+ aa1
) {
1261 k
= k
/ coeff
+ ((k
+ 1) / coeff
- k
/ coeff
) * 0.5 * (sin(M_PI
* (fabs(y
- k
) - aa1
) / aa
- M_PI_2
) + 1);
1263 k
= k
/ coeff
- (k
/ coeff
- (k
- 1) / coeff
) * 0.5 * (sin(M_PI
* (fabs(y
- k
) - aa1
) / aa
- M_PI_2
) + 1);
1268 y
= sqr
* log(fabs(in
)) + sqr
* sqr
;
1272 } else if (k
- aa1
<= y
and y
<= k
+ aa1
) {
1273 k
= in
/ fabs(in
) * exp(k
/ sqr
- sqr
);
1274 } else if (y
> k
+ aa1
) {
1275 k
= in
/ fabs(in
) * (exp(k
/ sqr
- sqr
) + (exp((k
+ 1) / sqr
- sqr
) - exp(k
/ sqr
- sqr
)) * 0.5 * (sin((fabs(y
- k
) - aa1
) / aa
* M_PI
- M_PI_2
) + 1));
1277 k
= in
/ fabs(in
) * (exp(k
/ sqr
- sqr
) - (exp(k
/ sqr
- sqr
) - exp((k
- 1) / sqr
- sqr
)) * 0.5 * (sin((fabs(y
- k
) - aa1
) / aa
* M_PI
- M_PI_2
) + 1));
1282 // morph between dry and wet signal
1283 k
+= (in
- k
) * morph
;
1286 k
= remove_dc(k
, dc
);
1290 float bitreduction::process(float in
)
1292 return waveshape(in
);
1295 bool bitreduction::get_layers(int index
, int generation
, unsigned int &layers
) const
1297 layers
= redraw_graph
or !generation
? LG_CACHE_GRAPH
| LG_CACHE_GRID
: 0;
1298 return redraw_graph
or !generation
;
1300 bool bitreduction::get_graph(int subindex
, int phase
, float *data
, int points
, cairo_iface
*context
, int *mode
) const
1302 if (subindex
>= 2) {
1303 redraw_graph
= false;
1306 for (int i
= 0; i
< points
; i
++) {
1307 data
[i
] = sin(((float)i
/ (float)points
* 360.) * M_PI
/ 180.);
1308 if (subindex
and !bypass
)
1309 data
[i
] = waveshape(data
[i
]);
1311 context
->set_line_width(1);
1312 context
->set_source_rgba(0.15, 0.2, 0.0, 0.15);
1317 bool bitreduction::get_gridline(int subindex
, int phase
, float &pos
, bool &vertical
, std::string
&legend
, cairo_iface
*context
) const
1319 if (phase
or subindex
)
1326 //////////////////////////////////////////////////////////////////
1328 resampleN::resampleN()
1334 resampleN::~resampleN()
1338 void resampleN::set_params(uint32_t sr
, int fctr
= 2, int fltrs
= 2)
1341 factor
= std::min(16, std::max(1, fctr
));
1342 filters
= std::min(4, std::max(1, fltrs
));
1344 filter
[0][0].set_lp_rbj(std::max(25000., (double)srate
/ 2), 0.8, (float)srate
* factor
);
1345 for (int i
= 1; i
< filters
; i
++) {
1346 filter
[0][i
].copy_coeffs(filter
[0][0]);
1347 filter
[1][i
].copy_coeffs(filter
[0][0]);
1350 double *resampleN::upsample(double sample
)
1354 for (int f
= 0; f
< filters
; f
++)
1355 tmp
[0] = filter
[0][f
].process(sample
);
1356 for (int i
= 1; i
< factor
; i
++) {
1358 for (int f
= 0; f
< filters
; f
++)
1359 tmp
[i
] = filter
[0][f
].process(sample
);
1364 double resampleN::downsample(double *sample
)
1367 for(int i
= 0; i
< factor
; i
++) {
1368 for (int f
= 0; f
< filters
; f
++) {
1369 sample
[i
] = filter
[1][f
].process(sample
[i
]);
1376 //////////////////////////////////////////////////////////////////
1378 samplereduction::samplereduction()
1387 void samplereduction::set_params(float am
)
1390 round
= roundf(amount
);
1393 double samplereduction::process(double in
)
1396 if (samples
>= round
) {
1399 if (target
+ amount
>= real
+ 1) {