wip noise gate

This commit is contained in:
Michael Sippel 2025-01-12 05:08:18 +01:00
parent bcead0e0b5
commit db9d2d4e9f
Signed by: senvas
GPG key ID: 060F22F65102F95C
4 changed files with 102 additions and 4 deletions

53
gate.c Normal file
View file

@ -0,0 +1,53 @@
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include "gate.h"
#include <math.h>
void gate_init(
struct gate * gate
) {
gate->threshold = 0.0;
gate->enable_calibration = false;
gate->cur_state = false;
gate->cur_avg = 0;
}
void gate_process(
struct gate * gate,
size_t frame_size,
float const * in,
float * out
) {
float sum = 0.0;
for( size_t i = 0; i < frame_size; ++i ) {
sum += fabs( in[i] );
}
gate->cur_avg = sum / frame_size;
if( gate->cur_avg > gate->threshold ) {
if( ! gate->cur_state ) {
gate->cur_state = true;
printf("GATE on\n");
}
for( size_t i = 0; i < frame_size; ++i ) {
out[i] = in[i];
}
}
else
{
if( gate->cur_state ) {
gate->cur_state = false;
printf("GATE off\n");
}
for( size_t i = 0; i < frame_size; ++i ) {
out[i] = 0.0;
}
}
}

21
gate.h Normal file
View file

@ -0,0 +1,21 @@
#pragma once
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
struct gate {
float threshold;
bool enable_calibration;
float cur_avg;
bool cur_state;
};
void gate_init();
void gate_process(
struct gate * gate,
size_t frame_size,
float const * in,
float * out
);

View file

@ -15,6 +15,7 @@
#include "delay.h" #include "delay.h"
#include "sust.h" #include "sust.h"
#include "gate.h"
float envelope( float x ); float envelope( float x );
struct data; struct data;
@ -34,6 +35,7 @@ struct data {
uint64_t last_tap; uint64_t last_tap;
struct delay delay; struct delay delay;
struct sust sust; struct sust sust;
struct gate gate;
unsigned prog; unsigned prog;
@ -100,6 +102,15 @@ static void on_process(void *userdata, struct spa_io_position *position)
break; break;
case 0x42: case 0x42:
// noise gate calibration
if( data->prog == 2 ) {
if( midi_data[2] >= 64) {
data->gate.threshold = 0.0;//data->gate.cur_avg;
printf("calibrate noise gate: threshold = %f\n", data->gate.threshold);
}
}
// sust pedal // sust pedal
if( data->prog == 1 ) { if( data->prog == 1 ) {
if( midi_data[2] >= 64) { if( midi_data[2] >= 64) {
@ -133,6 +144,15 @@ static void on_process(void *userdata, struct spa_io_position *position)
case 0xc0: case 0xc0:
// program change // program change
if( midi_data[2] >= 64) {
sust_swap( &data->sust );
data->sust.playing = true;
data->sust.start_idx = data->sust.idx;
data->sust.idx = 0;
} else {
data->sust.playing = false;
}
printf("program change: %u\n", midi_data[1]); printf("program change: %u\n", midi_data[1]);
data->prog = midi_data[1]; data->prog = midi_data[1];
break; break;
@ -155,11 +175,13 @@ static void on_process(void *userdata, struct spa_io_position *position)
float * const in = pw_filter_get_dsp_buffer(data->guit_in_port, n_samples); float * const in = pw_filter_get_dsp_buffer(data->guit_in_port, n_samples);
float tmp[n_samples]; float tmp1[n_samples];
float tmp2[n_samples];
float * out = pw_filter_get_dsp_buffer(data->out_port, n_samples); float * out = pw_filter_get_dsp_buffer(data->out_port, n_samples);
if( in && out ) { if( in && out ) {
sust_process( &data->sust, n_samples, in, tmp ); gate_process( &data->gate, n_samples, in, tmp1 );
delay_process( &data->delay, n_samples, tmp, out ); sust_process( &data->sust, n_samples, in, tmp2 );
delay_process( &data->delay, n_samples, tmp2, out );
} }
} }
@ -178,6 +200,7 @@ int main(int argc, char *argv[])
{ {
struct data data = { 0, }; struct data data = { 0, };
gate_init( &data.gate );
delay_init( &data.delay ); delay_init( &data.delay );
sust_init( &data.sust ); sust_init( &data.sust );
data.prog = 0; data.prog = 0;

View file

@ -9,5 +9,6 @@ executable(
'guitfx.c', 'guitfx.c',
'delay.c', 'delay.c',
'sust.c', 'sust.c',
'gate.c',
dependencies : [pipewire_dep, m_dep], dependencies : [pipewire_dep, m_dep],
) )