diff --git a/delay.c b/delay.c index d878fd1..c73889f 100644 --- a/delay.c +++ b/delay.c @@ -1,17 +1,36 @@ #include #include #include +#include #include "delay.h" void delay_init( struct delay * delay ) { - delay->duration = 15000; delay->mix = 0.8; delay->feedback = 0.6; - + delay->duration = 0; delay->buf_idx = 0; - delay->buf = calloc( delay->duration, sizeof(float) ); + delay->buf_capacity = 0; + delay->buf = NULL; +} + +void delay_set_time( + struct delay * delay, + uint64_t new_duration +) { + printf("set delay duration to %lu samples\n", new_duration); + if( new_duration > delay->buf_capacity ) { + delay->buf_capacity = new_duration; + delay->buf = realloc( delay->buf, sizeof(float) * new_duration ); + } + + for( int i = delay->duration; i < new_duration; ++i ) { + delay->buf[i] = delay->buf[i - delay->duration]; + } + + delay->duration = new_duration; + delay->buf_idx %= new_duration; } void delay_process( @@ -21,12 +40,16 @@ void delay_process( float * out ) { for( size_t i = 0; i < frame_size; ++i ) { - out[i] = - (1.0 - delay->mix) * in[i] - + delay->mix * delay->buf[ delay->buf_idx ]; + if( delay->duration > 0 ) { + out[i] = + (1.0 - delay->mix) * in[i] + + delay->mix * delay->buf[ delay->buf_idx ]; - delay->buf[ delay->buf_idx ] *= delay->feedback; - delay->buf[ delay->buf_idx ] += (1.0 - delay->feedback) * in[i]; - delay->buf_idx = (delay->buf_idx + 1) % delay->duration; + delay->buf[ delay->buf_idx ] *= delay->feedback; + delay->buf[ delay->buf_idx ] += (1.0 - delay->feedback) * in[i]; + delay->buf_idx = (delay->buf_idx + 1) % delay->duration; + } else { + out[i] = in[i]; + } } } diff --git a/delay.h b/delay.h index fc004e6..4ed2cf5 100644 --- a/delay.h +++ b/delay.h @@ -9,6 +9,7 @@ struct delay { float mix; uint64_t buf_idx; + size_t buf_capacity; float * buf; }; @@ -16,6 +17,11 @@ void delay_init( struct delay * delay ); +void delay_set_time( + struct delay * delay, + uint64_t new_duration +); + void delay_process( struct delay * delay, size_t frame_size, diff --git a/guitfx.c b/guitfx.c index ad35445..71bd06a 100644 --- a/guitfx.c +++ b/guitfx.c @@ -29,6 +29,7 @@ struct data { struct port * out_port; //! effect data + uint64_t last_tap; struct delay delay; //! elapsed time in number of samples @@ -71,10 +72,18 @@ static void on_process(void *userdata, struct spa_io_position *position) unsigned sec = (frame + c->offset) / (float) position->clock.rate.denom; - char * data = SPA_POD_BODY(&c->value); + char * midi_data = SPA_POD_BODY(&c->value); unsigned size = SPA_POD_BODY_SIZE(&c->value); - printf("[%d] MIDI message (%d bytes) : %x, %x, %x\n", sec, size, data[0], data[1], data[2]); + uint64_t cur_tap = frame + c->offset; + uint64_t duration = cur_tap - data->last_tap; + data->last_tap = cur_tap; + + printf("duration = %lu\n", duration); + if( duration < (4*position->clock.rate.denom) ) { + delay_set_time( &data->delay, duration ); + } + printf("[%d] MIDI message (%d bytes) : %x, %x, %x\n", sec, size, midi_data[0], midi_data[1], midi_data[2]); } else { printf("on_process(): non midi-control\n"); }