From e36a4635e8632b12819eac3d1f417c722c00c237 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Mon, 27 Jan 2025 23:16:59 +0100 Subject: [PATCH] simplify noise gate --- gate.c | 70 +++++++++++++++----------------------------------------- gate.h | 6 +---- guitfx.c | 2 +- 3 files changed, 21 insertions(+), 57 deletions(-) diff --git a/gate.c b/gate.c index a25a39c..bb11f25 100644 --- a/gate.c +++ b/gate.c @@ -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,67 +27,38 @@ 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; + 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; + gate->last_block_sum = gate->cur_block_sum; + gate->cur_block_sum = 0.0; + + if( gate->last_block_sum > 0.03 ) { + act = true; } 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; - - /* 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 ) { - gate->cur_gain += gate->attack; - if( gate->cur_gain > 1.0 ) { + if( act ) { + gate->cur_gain += gate->attack; + if( gate->cur_gain > 1.0 ) { gate->cur_gain = 1.0; } - } else { - gate->cur_gain -= gate->attack; - if( gate->cur_gain < 0.0 ) { + } else { + gate->cur_gain -= gate->release; + if( gate->cur_gain < 0.0 ) { gate->cur_gain = 0.0; } - } + } - out[i] = in[i] * gate->cur_gain; - } + out[i] = in[i] * gate->cur_gain; + } } diff --git a/gate.h b/gate.h index c921d1b..7f96d6a 100644 --- a/gate.h +++ b/gate.h @@ -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; diff --git a/guitfx.c b/guitfx.c index 824310a..11ffe2c 100644 --- a/guitfx.c +++ b/guitfx.c @@ -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); } }