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

70
gate.c
View file

@ -12,15 +12,12 @@ void gate_init(
gate->enable_calibration = false; gate->enable_calibration = false;
gate->block_size = 1024; 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_count = 0;
gate->cur_block_sum = 0.0; gate->cur_block_sum = 0.0;
gate->is_active = false; gate->is_active = false;
gate->cur_gain = 1.0; gate->cur_gain = 1.0;
gate->attack = 1.0 / 4096.0; gate->attack = 1.0 / 512.0;
gate->release = 1.0 / 4096.0; gate->release = 1.0 / 4096.0;
} }
@ -30,67 +27,38 @@ void gate_process(
float const * in, float const * in,
float * out float * out
) { ) {
float sum = 0.0;
bool act = false;
int last_frame = 0;
for( size_t i = 0; i < frame_size; ++i ) { for( size_t i = 0; i < frame_size; ++i ) {
gate->cur_block_sum += fabs( in[i] ); gate->cur_block_sum += fabs( in[i] );
gate->cur_block_count += 1; gate->cur_block_count += 1;
if( gate->cur_block_count >= gate->block_size ) { 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->last_block_sum = gate->cur_block_sum;
gate->is_active = true; gate->cur_block_sum = 0.0;
if( gate->last_block_sum > 0.03 ) {
act = true;
} else { } else {
gate->is_active = false; act = false;
} }
gate->hist[ gate->hist_idx ] = gate->cur_block_sum;
gate->hist_idx = (gate->hist_idx + 1) % gate->hist_size;
gate->cur_block_sum = 0.0;
gate->cur_block_count = 0;
last_frame = i;
} }
}
/*
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; if( act ) {
gate->cur_gain += gate->attack;
/* start from last block sum and iterate backwards to find if( gate->cur_gain > 1.0 ) {
* 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 ) {
gate->cur_gain += gate->attack;
if( gate->cur_gain > 1.0 ) {
gate->cur_gain = 1.0; gate->cur_gain = 1.0;
} }
} else { } else {
gate->cur_gain -= gate->attack; gate->cur_gain -= gate->release;
if( gate->cur_gain < 0.0 ) { if( gate->cur_gain < 0.0 ) {
gate->cur_gain = 0.0; gate->cur_gain = 0.0;
} }
} }
out[i] = in[i] * gate->cur_gain; out[i] = in[i] * gate->cur_gain;
} }
} }

6
gate.h
View file

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

View file

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