260 lines
8.7 KiB
C
260 lines
8.7 KiB
C
#include "pipewire/keys.h"
|
|
#include "pipewire/port.h"
|
|
#include "spa/pod/iter.h"
|
|
#include "spa/utils/defs.h"
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
|
|
#include <spa/control/control.h>
|
|
#include <spa/param/latency-utils.h>
|
|
#include <spa/pod/builder.h>
|
|
#include <spa/pod/pod.h>
|
|
|
|
#include <pipewire/filter.h>
|
|
#include <pipewire/pipewire.h>
|
|
|
|
#include "delay.h"
|
|
#include "gate.h"
|
|
#include "sust.h"
|
|
|
|
float envelope(float x);
|
|
|
|
struct data;
|
|
|
|
struct port {
|
|
struct data* data;
|
|
};
|
|
|
|
struct data {
|
|
struct pw_main_loop* loop;
|
|
struct pw_filter* filter;
|
|
struct port* guit_in_port;
|
|
struct port* midi_in_port;
|
|
struct port* out_port;
|
|
|
|
//! effect data
|
|
uint64_t last_tap;
|
|
struct delay delay;
|
|
struct sust sust;
|
|
struct gate gate;
|
|
|
|
unsigned prog;
|
|
|
|
//! elapsed time in number of samples
|
|
uint64_t time;
|
|
};
|
|
|
|
static void on_process(void* userdata, struct spa_io_position* position)
|
|
{
|
|
struct data* data = userdata;
|
|
|
|
uint32_t n_samples = position->clock.duration;
|
|
uint64_t frame = data->time;
|
|
data->time += n_samples;
|
|
|
|
struct pw_buffer* b = pw_filter_dequeue_buffer(data->midi_in_port);
|
|
if (b == NULL) {
|
|
fprintf(stderr, "on_process(): no buffer for midi_in_port\n");
|
|
return;
|
|
}
|
|
|
|
struct spa_buffer* buf = b->buffer;
|
|
spa_assert(buf->n_datas == 1);
|
|
struct spa_data* d = &buf->datas[0];
|
|
|
|
if (d->data) {
|
|
struct spa_pod* pod = spa_pod_from_data(d->data, d->maxsize,
|
|
d->chunk->offset, d->chunk->size);
|
|
|
|
if (pod) {
|
|
if (spa_pod_is_sequence(pod)) {
|
|
struct spa_pod_sequence* pod_seq = (struct spa_pod_sequence*)pod;
|
|
struct spa_pod_control* c;
|
|
SPA_POD_SEQUENCE_FOREACH(pod_seq, c)
|
|
{
|
|
if (c->type == SPA_CONTROL_Midi) {
|
|
unsigned sec = (frame + c->offset) / (float)position->clock.rate.denom;
|
|
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,
|
|
midi_data[0], midi_data[1], midi_data[2]);
|
|
|
|
switch (midi_data[0] & 0xff) {
|
|
case 0xb0:
|
|
switch (midi_data[1]) {
|
|
case 0x0b:
|
|
// expr pedal
|
|
float val_f = ((float)midi_data[2]) / 128.0;
|
|
|
|
float thres = 0.5;
|
|
|
|
if (val_f > thres) {
|
|
float expr_mix = (val_f - thres) / (1.0 - thres);
|
|
printf("Expr Pedal %f\n", expr_mix);
|
|
data->delay.mix = expr_mix;
|
|
} else {
|
|
data->delay.mix = 0.0;
|
|
}
|
|
break;
|
|
|
|
case 0x42:
|
|
|
|
// noise gate calibration
|
|
if (data->prog == 2) {
|
|
if (midi_data[2] >= 64) {
|
|
data->gate.threshold = data->gate.cur_block_sum * 0.8;
|
|
printf("calibrate noise gate: threshold = %f\n",
|
|
data->gate.threshold);
|
|
}
|
|
}
|
|
|
|
// sust pedal
|
|
if (data->prog == 1) {
|
|
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;
|
|
}
|
|
}
|
|
|
|
// tap tempo
|
|
if (data->prog == 0) {
|
|
uint64_t cur_tap = frame + c->offset;
|
|
uint64_t duration = cur_tap - data->last_tap;
|
|
data->last_tap = cur_tap;
|
|
if (duration < (4 * position->clock.rate.denom)) {
|
|
delay_set_time(&data->delay, duration);
|
|
}
|
|
|
|
sust_resize(&data->sust, duration);
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
break;
|
|
|
|
case 0xc0:
|
|
// 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]);
|
|
data->prog = midi_data[1];
|
|
break;
|
|
}
|
|
} else {
|
|
printf("on_process(): non midi-control\n");
|
|
}
|
|
}
|
|
} else {
|
|
fprintf(stderr, "on_process(): unexpected POD that is not a sequence "
|
|
"(midi_in_port)\n");
|
|
}
|
|
} else {
|
|
fprintf(stderr, "on_process(): pod is NULL\n");
|
|
}
|
|
} else {
|
|
fprintf(stderr, "on_process(): no data in buffer of midi_in_port\n");
|
|
}
|
|
|
|
pw_filter_queue_buffer(data->midi_in_port, b);
|
|
|
|
float* const in = pw_filter_get_dsp_buffer(data->guit_in_port, n_samples);
|
|
float tmp1[n_samples];
|
|
float tmp2[n_samples];
|
|
float* out = pw_filter_get_dsp_buffer(data->out_port, n_samples);
|
|
if (in && out) {
|
|
gate_process(&data->gate, n_samples, in, tmp1);
|
|
sust_process(&data->sust, n_samples, tmp1, tmp2);
|
|
delay_process(&data->delay, n_samples, tmp2, out);
|
|
}
|
|
}
|
|
|
|
static const struct pw_filter_events filter_events = {
|
|
PW_VERSION_FILTER_EVENTS,
|
|
.process = on_process,
|
|
};
|
|
|
|
static void do_quit(void* userdata, int signal_number)
|
|
{
|
|
struct data* data = userdata;
|
|
pw_main_loop_quit(data->loop);
|
|
}
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
struct data data = {
|
|
0,
|
|
};
|
|
|
|
gate_init(&data.gate);
|
|
delay_init(&data.delay);
|
|
sust_init(&data.sust);
|
|
data.prog = 0;
|
|
|
|
const struct spa_pod* params[1];
|
|
uint8_t buffer[1024];
|
|
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
|
|
|
|
pw_init(&argc, &argv);
|
|
|
|
data.loop = pw_main_loop_new(NULL);
|
|
|
|
pw_loop_add_signal(pw_main_loop_get_loop(data.loop), SIGINT, do_quit, &data);
|
|
pw_loop_add_signal(pw_main_loop_get_loop(data.loop), SIGTERM, do_quit, &data);
|
|
|
|
data.filter = pw_filter_new_simple(
|
|
pw_main_loop_get_loop(data.loop), "Guitar FX",
|
|
pw_properties_new(PW_KEY_MEDIA_TYPE, "Audio", PW_KEY_MEDIA_CATEGORY,
|
|
"Filter", PW_KEY_MEDIA_ROLE, "DSP", NULL),
|
|
&filter_events, &data);
|
|
|
|
data.midi_in_port = pw_filter_add_port(data.filter, PW_DIRECTION_INPUT,
|
|
PW_FILTER_PORT_FLAG_MAP_BUFFERS, sizeof(struct port),
|
|
pw_properties_new(PW_KEY_FORMAT_DSP, "8 bit raw midi",
|
|
PW_KEY_PORT_NAME, "midi in", NULL),
|
|
NULL, 0);
|
|
|
|
data.guit_in_port = pw_filter_add_port(
|
|
data.filter, PW_DIRECTION_INPUT, PW_FILTER_PORT_FLAG_MAP_BUFFERS,
|
|
sizeof(struct port),
|
|
pw_properties_new(PW_KEY_FORMAT_DSP, "32 bit float mono audio",
|
|
PW_KEY_PORT_NAME, "guitar in", NULL),
|
|
NULL, 0);
|
|
|
|
data.out_port = pw_filter_add_port(
|
|
data.filter, PW_DIRECTION_OUTPUT, PW_FILTER_PORT_FLAG_MAP_BUFFERS,
|
|
sizeof(struct port),
|
|
pw_properties_new(PW_KEY_FORMAT_DSP, "32 bit float mono audio",
|
|
PW_KEY_PORT_NAME, "fx out", NULL),
|
|
NULL, 0);
|
|
|
|
params[0] = spa_process_latency_build(
|
|
&b, SPA_PARAM_ProcessLatency,
|
|
&SPA_PROCESS_LATENCY_INFO_INIT(.ns = 10 * SPA_NSEC_PER_MSEC));
|
|
|
|
if (pw_filter_connect(data.filter, PW_FILTER_FLAG_RT_PROCESS, params, 1) < 0) {
|
|
fprintf(stderr, "can't connect\n");
|
|
return -1;
|
|
}
|
|
|
|
pw_main_loop_run(data.loop);
|
|
|
|
pw_filter_destroy(data.filter);
|
|
pw_main_loop_destroy(data.loop);
|
|
pw_deinit();
|
|
|
|
return 0;
|
|
}
|