tibia/test/plugin.h

104 lines
2.8 KiB
C
Raw Normal View History

2024-01-13 11:07:00 +00:00
#include <string.h>
2024-01-05 14:35:39 +00:00
#include <math.h>
2024-01-05 13:45:14 +00:00
typedef struct plugin {
2024-01-13 11:07:00 +00:00
float sample_rate;
size_t delay_line_length;
2024-01-13 13:04:09 +00:00
float gain;
float delay;
2024-01-15 20:03:11 +00:00
float cutoff;
2024-01-13 13:04:09 +00:00
char bypass;
2024-01-13 11:07:00 +00:00
float * delay_line;
size_t delay_line_cur;
2024-01-15 20:03:11 +00:00
float z1;
2024-01-16 14:37:08 +00:00
float cutoff_k;
2024-01-05 13:45:14 +00:00
} plugin;
2024-01-11 05:54:34 +00:00
static void plugin_init(plugin *instance) {
2024-01-05 13:45:14 +00:00
}
2024-01-11 05:54:34 +00:00
static void plugin_fini(plugin *instance) {
2024-01-05 13:45:14 +00:00
}
2024-01-11 05:54:34 +00:00
static void plugin_set_sample_rate(plugin *instance, float sample_rate) {
2024-01-13 11:07:00 +00:00
instance->sample_rate = sample_rate;
instance->delay_line_length = ceilf(sample_rate) + 1;
}
static size_t plugin_mem_req(plugin *instance) {
return instance->delay_line_length * sizeof(float);
}
static void plugin_mem_set(plugin *instance, void *mem) {
instance->delay_line = mem;
2024-01-05 13:45:14 +00:00
}
2024-01-11 05:54:34 +00:00
static void plugin_reset(plugin *instance) {
2024-01-13 11:07:00 +00:00
memset(instance->delay_line, 0, instance->delay_line_length * sizeof(float));
instance->delay_line_cur = 0;
2024-01-15 20:03:11 +00:00
instance->z1 = 0.f;
2024-01-16 14:37:08 +00:00
instance->cutoff_k = 1.f;
2024-01-05 13:45:14 +00:00
}
2024-01-11 05:54:34 +00:00
static void plugin_set_parameter(plugin *instance, size_t index, float value) {
2024-01-05 13:45:14 +00:00
switch (index) {
case 0:
2024-01-13 13:04:09 +00:00
instance->gain = powf(10.f, 0.05f * value);
2024-01-05 13:45:14 +00:00
break;
case 1:
2024-01-13 13:04:09 +00:00
instance->delay = 0.001f * value;
2024-01-13 11:07:00 +00:00
break;
case 2:
2024-01-15 20:03:11 +00:00
instance->cutoff = value;
break;
case 3:
2024-01-05 13:45:14 +00:00
instance->bypass = value >= 0.5f;
break;
}
}
2024-01-11 05:54:34 +00:00
static float plugin_get_parameter(plugin *instance, size_t index) {
2024-01-09 10:47:25 +00:00
// no output parameters
return 0.f;
}
2024-01-13 11:07:00 +00:00
static size_t calc_index(size_t cur, size_t delay, size_t len) {
return (cur < delay ? cur + len : cur) - delay;
}
2024-01-11 05:54:34 +00:00
static void plugin_process(plugin *instance, const float **inputs, float **outputs, size_t n_samples) {
2024-01-13 13:04:09 +00:00
size_t delay = roundf(instance->sample_rate * instance->delay);
2024-01-16 14:37:08 +00:00
const float mA1 = instance->sample_rate / (instance->sample_rate + 6.283185307179586f * instance->cutoff * instance->cutoff_k);
2024-01-13 11:07:00 +00:00
for (size_t i = 0; i < n_samples; i++) {
instance->delay_line[instance->delay_line_cur] = inputs[0][i];
2024-01-15 20:03:11 +00:00
const float x = instance->delay_line[calc_index(instance->delay_line_cur, delay, instance->delay_line_length)];
2024-01-13 11:07:00 +00:00
instance->delay_line_cur++;
if (instance->delay_line_cur == instance->delay_line_length)
instance->delay_line_cur = 0;
2024-01-15 20:03:11 +00:00
const float y = x + mA1 * (instance->z1 - x);
instance->z1 = y;
2024-01-13 13:04:09 +00:00
outputs[0][i] = instance->bypass ? inputs[0][i] : instance->gain * y;
2024-01-13 11:07:00 +00:00
}
2024-01-05 13:45:14 +00:00
}
2024-01-16 14:37:08 +00:00
static void plugin_note_on(plugin *instance, size_t index, uint8_t note, float velocity) {
instance->cutoff_k = powf(2.f, (1.f / 12.f) * (note - 60));
}
static void plugin_note_off(plugin *instance, size_t index, uint8_t note, float velocity) {
}
static void plugin_all_sounds_off(plugin *instance, size_t index) {
}
static void plugin_all_notes_off(plugin *instance, size_t index) {
}
static void plugin_channel_pressure(plugin *instance, size_t index, float value) {
}
static void plugin_pitch_bend_change(plugin *instance, size_t index, float value) {
}