From f4bbf94e30fb54e16bfd9fc2cb54284cd885e73b Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Mon, 27 Jan 2025 21:27:49 +0100 Subject: [PATCH] noise gate: simple attack&release envelope --- gate.c | 37 ++++++++++++++++++++++++------------- gate.h | 4 ++++ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/gate.c b/gate.c index 9eb0949..a25a39c 100644 --- a/gate.c +++ b/gate.c @@ -8,7 +8,6 @@ void gate_init( struct gate * gate ) { - gate->threshold = 0.0; gate->enable_calibration = false; @@ -17,7 +16,12 @@ void gate_init( gate->hist_idx = 0; gate->hist = malloc( sizeof(float) * gate->hist_size ); gate->cur_block_count = 0; - gate->cur_block_sum = 0.0; + gate->cur_block_sum = 0.0; + + gate->is_active = false; + gate->cur_gain = 1.0; + gate->attack = 1.0 / 4096.0; + gate->release = 1.0 / 4096.0; } void gate_process( @@ -53,7 +57,7 @@ void gate_process( last_frame = i; } } - + /* unsigned enable_pos = gate->is_active ? 0 : frame_size; unsigned disable_pos = gate->is_active ? frame_size : 0; @@ -61,8 +65,8 @@ void gate_process( /* 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 ) { @@ -70,16 +74,23 @@ void gate_process( } last_frame -= gate->block_size; - i -= 1; + i = (i - 1) % gate->hist_size; } - */ - +*/ for( size_t i = 0; i < frame_size; ++i ) { - // if( i >= enable_pos && i < disable_pos ) - if( gate->is_active ) - out[i] = in[i]; - else - out[i] = 0.0; + if( gate->is_active ) { + 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 ) { + gate->cur_gain = 0.0; + } + } + + out[i] = in[i] * gate->cur_gain; } } diff --git a/gate.h b/gate.h index 9456ccd..c921d1b 100644 --- a/gate.h +++ b/gate.h @@ -18,6 +18,10 @@ struct gate { float cur_block_sum; bool is_active; + float cur_gain; + + float attack; + float release; }; void gate_init();