guitfx/gate.c

65 lines
1.3 KiB
C
Raw Normal View History

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