noise gate: simple attack&release envelope

This commit is contained in:
Michael Sippel 2025-01-27 21:27:49 +01:00
parent 7e5a01f959
commit f4bbf94e30
Signed by: senvas
GPG key ID: 060F22F65102F95C
2 changed files with 28 additions and 13 deletions

35
gate.c
View file

@ -8,7 +8,6 @@
void gate_init( void gate_init(
struct gate * gate struct gate * gate
) { ) {
gate->threshold = 0.0; gate->threshold = 0.0;
gate->enable_calibration = false; gate->enable_calibration = false;
@ -18,6 +17,11 @@ void gate_init(
gate->hist = malloc( sizeof(float) * gate->hist_size ); 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->cur_gain = 1.0;
gate->attack = 1.0 / 4096.0;
gate->release = 1.0 / 4096.0;
} }
void gate_process( void gate_process(
@ -53,7 +57,7 @@ void gate_process(
last_frame = i; last_frame = i;
} }
} }
/*
unsigned enable_pos = gate->is_active ? 0 : frame_size; unsigned enable_pos = gate->is_active ? 0 : frame_size;
unsigned disable_pos = gate->is_active ? frame_size : 0; 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 /* start from last block sum and iterate backwards to find
* the timepoint where a block sum crosses the threshold * the timepoint where a block sum crosses the threshold
*/ * /
/*
unsigned i = gate->hist_idx; unsigned i = gate->hist_idx;
while( last_frame > 0 ) { while( last_frame > 0 ) {
if( gate->hist[ i ] < gate->threshold ) { if( gate->hist[ i ] < gate->threshold ) {
@ -70,16 +74,23 @@ void gate_process(
} }
last_frame -= gate->block_size; last_frame -= gate->block_size;
i -= 1; i = (i - 1) % gate->hist_size;
} }
*/ */
for( size_t i = 0; i < frame_size; ++i ) { for( size_t i = 0; i < frame_size; ++i ) {
// if( i >= enable_pos && i < disable_pos ) if( gate->is_active ) {
if( gate->is_active ) gate->cur_gain += gate->attack;
out[i] = in[i]; if( gate->cur_gain > 1.0 ) {
else gate->cur_gain = 1.0;
out[i] = 0.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;
} }
} }

4
gate.h
View file

@ -18,6 +18,10 @@ struct gate {
float cur_block_sum; float cur_block_sum;
bool is_active; bool is_active;
float cur_gain;
float attack;
float release;
}; };
void gate_init(); void gate_init();