guitfx/delay.c

56 lines
1.4 KiB
C
Raw Normal View History

2024-12-08 15:57:19 +01:00
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
2024-12-08 16:40:53 +01:00
#include <stdio.h>
2024-12-08 15:57:19 +01:00
#include "delay.h"
void delay_init(
struct delay * delay
) {
delay->mix = 0.8;
delay->feedback = 0.6;
2024-12-08 16:40:53 +01:00
delay->duration = 0;
2024-12-08 15:57:19 +01:00
delay->buf_idx = 0;
2024-12-08 16:40:53 +01:00
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;
2024-12-08 15:57:19 +01:00
}
void delay_process(
struct delay * delay,
size_t frame_size,
float const * in,
float * out
) {
for( size_t i = 0; i < frame_size; ++i ) {
2024-12-08 16:40:53 +01:00
if( delay->duration > 0 ) {
out[i] =
(1.0 - delay->mix) * in[i]
+ delay->mix * delay->buf[ delay->buf_idx ];
2024-12-08 15:57:19 +01:00
2024-12-08 16:40:53 +01:00
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];
}
2024-12-08 15:57:19 +01:00
}
}