Merge branch 'master' of https://github.com/calf-studio-gear/calf
[calf.git] / src / calf / bypass.h
blobec5b841fb45d66d09755bf82ac4244fecb78a2fb
1 #ifndef CALF_BYPASS_H
2 #define CALF_BYPASS_H
4 #include "inertia.h"
6 namespace dsp {
8 class bypass
10 inertia<linear_ramp> ramp;
11 float first_value, next_value;
13 public:
14 bypass(int _ramp_len = 1024)
15 : ramp(linear_ramp(_ramp_len))
19 /// Pass the new state of the bypass button, and return the ramp-aware
20 /// bypass state
21 bool update(bool new_state, uint32_t nsamples)
23 ramp.set_inertia(new_state ? 1.f : 0.f);
24 first_value = ramp.get_last();
25 ramp.step_many(nsamples);
26 next_value = ramp.get_last();
27 return first_value >= 1 && next_value >= 1;
30 /// Apply ramp to prevent clicking
31 void crossfade(float *inputs[], float *outputs[], uint32_t nbuffers, uint32_t offset, uint32_t nsamples)
33 if (!nsamples || (first_value + next_value) == 0)
34 return;
35 float step = (next_value - first_value) / nsamples;
36 for (uint32_t b = 0; b < nbuffers; ++b)
38 float *out = outputs[b] + offset, *in = inputs[b] + offset;
39 if (first_value >= 1 && next_value >= 1)
40 memcpy(out, in, nsamples * sizeof(float));
41 else
43 for (uint32_t i = 0; i < nsamples; ++i)
45 float bypass_amt = first_value + i * step;
46 out[i] += (in[i] - out[i]) * bypass_amt;
55 #endif