guitfx/gate.c

65 lines
1.4 KiB
C
Raw Normal View History

2025-01-12 05:08:18 +01:00
#include "gate.h"
#include <math.h>
2025-02-17 18:06:56 +01:00
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
2025-01-12 05:08:18 +01:00
void gate_init(
2025-02-17 18:06:56 +01:00
struct gate* gate)
{
2025-01-12 05:08:18 +01:00
gate->threshold = 0.0;
gate->enable_calibration = false;
gate->block_size = 1024;
gate->cur_block_count = 0;
gate->cur_block_sum = 0.0;
gate->is_active = false;
gate->cur_gain = 1.0;
gate->attack = 1.0 / 128.0;
gate->release = 1.0 / 8192.0;
2025-01-12 05:08:18 +01:00
}
void gate_process(
2025-02-17 18:06:56 +01:00
struct gate* gate,
2025-01-12 05:08:18 +01:00
size_t frame_size,
2025-02-17 18:06:56 +01:00
float const* in,
float* out)
{
2025-01-12 05:08:18 +01:00
2025-02-17 18:06:56 +01:00
bool act = false;
2025-01-12 05:08:18 +01:00
2025-02-17 18:06:56 +01:00
for (size_t i = 0; i < frame_size; ++i) {
gate->cur_block_sum += fabs(in[i]);
gate->cur_block_count += 1;
2025-01-12 05:08:18 +01:00
2025-02-17 18:06:56 +01:00
if (gate->cur_block_count >= gate->block_size) {
// printf("new block sum %f, gain = %f\n", gate->cur_block_sum, gate->cur_gain);
2025-01-27 23:16:59 +01:00
2025-02-17 18:06:56 +01:00
gate->last_block_sum = gate->cur_block_sum;
gate->cur_block_sum = 0.0;
2025-01-12 05:08:18 +01:00
2025-02-17 18:06:56 +01:00
if (gate->last_block_sum > 0.03) {
act = true;
} else {
act = false;
}
}
2025-01-12 05:08:18 +01:00
2025-02-17 18:06:56 +01:00
if (act) {
gate->cur_gain += gate->attack;
if (gate->cur_gain > 1.0) {
gate->cur_gain = 1.0;
}
2025-02-17 18:06:56 +01:00
} else {
gate->cur_gain -= gate->release;
if (gate->cur_gain < 0.0) {
gate->cur_gain = 0.0;
}
2025-02-17 18:06:56 +01:00
}
2025-01-27 23:16:59 +01:00
out[i] = in[i] * gate->cur_gain;
}
2025-01-12 05:08:18 +01:00
}