simplify noise gate

This commit is contained in:
Michael Sippel 2025-01-27 23:16:59 +01:00
parent f4bbf94e30
commit e36a4635e8
Signed by: senvas
GPG key ID: 060F22F65102F95C
3 changed files with 21 additions and 57 deletions

52
gate.c
View file

@ -12,15 +12,12 @@ void gate_init(
gate->enable_calibration = false;
gate->block_size = 1024;
gate->hist_size = 256;
gate->hist_idx = 0;
gate->hist = malloc( sizeof(float) * gate->hist_size );
gate->cur_block_count = 0;
gate->cur_block_sum = 0.0;
gate->is_active = false;
gate->cur_gain = 1.0;
gate->attack = 1.0 / 4096.0;
gate->attack = 1.0 / 512.0;
gate->release = 1.0 / 4096.0;
}
@ -30,62 +27,33 @@ void gate_process(
float const * in,
float * out
) {
float sum = 0.0;
int last_frame = 0;
bool act = false;
for( size_t i = 0; i < frame_size; ++i ) {
gate->cur_block_sum += fabs( in[i] );
gate->cur_block_count += 1;
if( gate->cur_block_count >= gate->block_size ) {
printf("new block sum [%lu] (frame %lu) : %f\n", gate->hist_idx, i, gate->cur_block_sum);
//printf("new block sum %f, gain = %f\n", gate->cur_block_sum, gate->cur_gain);
if( gate->cur_block_sum > 20.0 ) {
gate->is_active = true;
} else {
gate->is_active = false;
}
gate->hist[ gate->hist_idx ] = gate->cur_block_sum;
gate->hist_idx = (gate->hist_idx + 1) % gate->hist_size;
gate->last_block_sum = gate->cur_block_sum;
gate->cur_block_sum = 0.0;
gate->cur_block_count = 0;
last_frame = i;
if( gate->last_block_sum > 0.03 ) {
act = true;
} else {
act = false;
}
}
/*
unsigned enable_pos = gate->is_active ? 0 : frame_size;
unsigned disable_pos = gate->is_active ? frame_size : 0;
unsigned blocks_per_buffer = frame_size / gate->block_size;
/* start from last block sum and iterate backwards to find
* the timepoint where a block sum crosses the threshold
* /
unsigned i = gate->hist_idx;
while( last_frame > 0 ) {
if( gate->hist[ i ] < gate->threshold ) {
disable_pos = last_frame;
}
last_frame -= gate->block_size;
i = (i - 1) % gate->hist_size;
}
*/
for( size_t i = 0; i < frame_size; ++i ) {
if( gate->is_active ) {
if( act ) {
gate->cur_gain += gate->attack;
if( gate->cur_gain > 1.0 ) {
gate->cur_gain = 1.0;
}
} else {
gate->cur_gain -= gate->attack;
gate->cur_gain -= gate->release;
if( gate->cur_gain < 0.0 ) {
gate->cur_gain = 0.0;
}

6
gate.h
View file

@ -9,13 +9,9 @@ struct gate {
bool enable_calibration;
unsigned block_size;
unsigned hist_idx;
unsigned hist_size;
float * hist;
unsigned cur_block_count;
float cur_block_sum;
float last_block_sum;
bool is_active;
float cur_gain;

View file

@ -107,7 +107,7 @@ static void on_process(void *userdata, struct spa_io_position *position)
// noise gate calibration
if( data->prog == 2 ) {
if( midi_data[2] >= 64) {
data->gate.threshold = 0.0;//data->gate.cur_avg;
data->gate.threshold = data->gate.cur_block_sum * 0.8;
printf("calibrate noise gate: threshold = %f\n", data->gate.threshold);
}
}