brickworks/examples/fxpp_noise_gate/src/impl.cpp
2024-02-23 14:17:01 +01:00

98 lines
2.5 KiB
C++

/*
* Brickworks
*
* Copyright (C) 2022-2024 Orastron Srl unipersonale
*
* Brickworks is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* Brickworks is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Brickworks. If not, see <http://www.gnu.org/licenses/>.
*
* File author: Stefano D'Angelo
*/
#include "impl.h"
#include "common.h"
#include <bw_noise_gate.h>
using namespace Brickworks;
class Engine {
public:
NoiseGate<1> noise_gate;
bool extSidechain;
};
extern "C" {
impl impl_new(void) {
Engine *instance = new Engine();
return reinterpret_cast<impl>(instance);
}
void impl_free(impl handle) {
Engine *instance = reinterpret_cast<Engine *>(handle);
delete instance;
}
void impl_set_sample_rate(impl handle, float sample_rate) {
Engine *instance = reinterpret_cast<Engine *>(handle);
instance->noise_gate.setSampleRate(sample_rate);
}
void impl_reset(impl handle) {
Engine *instance = reinterpret_cast<Engine *>(handle);
instance->noise_gate.reset();
}
void impl_set_parameter(impl handle, size_t index, float value) {
Engine *instance = reinterpret_cast<Engine *>(handle);
switch (index) {
case 0:
instance->noise_gate.setThreshDBFS(value);
break;
case 1:
instance->noise_gate.setRatio(value);
break;
case 2:
// using rise time 10% -> 90%: tau = rise time / log(9)
instance->noise_gate.setAttackTau((0.001f * 0.4551196133134186f) * value);
break;
case 3:
// as before
instance->noise_gate.setReleaseTau((0.001f * 0.4551196133134186f) * value);
break;
case 4:
instance->extSidechain = value >= 0.5f;
break;
}
}
float impl_get_parameter(impl handle, size_t index) {
(void)handle;
(void)index;
return 0.f;
}
void impl_process(impl handle, const float **inputs, float **outputs, size_t n_samples) {
Engine *instance = reinterpret_cast<Engine *>(handle);
#ifdef WASM
const float *x[1] = {inputs[0]};
const float *xSc[1] = {instance->extSidechain ? inputs[1] : inputs[0]};
float *y[1] = {outputs[0]};
instance->noise_gate.process(x, xSc, y, n_samples);
#else
instance->noise_gate.process({inputs[0]}, {instance->extSidechain ? inputs[1] : inputs[0]}, {outputs[0]}, n_samples);
#endif
}
}