clang format
This commit is contained in:
parent
62acece204
commit
a6970aeed3
7 changed files with 315 additions and 342 deletions
23
delay.c
23
delay.c
|
@ -1,12 +1,12 @@
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "delay.h"
|
#include "delay.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
void delay_init(
|
void delay_init(
|
||||||
struct delay * delay
|
struct delay* delay)
|
||||||
) {
|
{
|
||||||
delay->mix = 0.8;
|
delay->mix = 0.8;
|
||||||
delay->feedback = 0.8;
|
delay->feedback = 0.8;
|
||||||
delay->duration = 0;
|
delay->duration = 0;
|
||||||
|
@ -17,8 +17,8 @@ void delay_init(
|
||||||
|
|
||||||
void delay_set_time(
|
void delay_set_time(
|
||||||
struct delay* delay,
|
struct delay* delay,
|
||||||
uint64_t new_duration
|
uint64_t new_duration)
|
||||||
) {
|
{
|
||||||
printf("set delay duration to %lu samples\n", new_duration);
|
printf("set delay duration to %lu samples\n", new_duration);
|
||||||
if (new_duration > delay->buf_capacity) {
|
if (new_duration > delay->buf_capacity) {
|
||||||
delay->buf_capacity = new_duration;
|
delay->buf_capacity = new_duration;
|
||||||
|
@ -39,12 +39,11 @@ void delay_process(
|
||||||
struct delay* delay,
|
struct delay* delay,
|
||||||
size_t frame_size,
|
size_t frame_size,
|
||||||
float const* in,
|
float const* in,
|
||||||
float * out
|
float* out)
|
||||||
) {
|
{
|
||||||
for (size_t i = 0; i < frame_size; ++i) {
|
for (size_t i = 0; i < frame_size; ++i) {
|
||||||
if (delay->duration > 0) {
|
if (delay->duration > 0) {
|
||||||
out[i] =
|
out[i] = in[i]
|
||||||
in[i]
|
|
||||||
+ delay->mix * delay->buf[delay->buf_idx];
|
+ delay->mix * delay->buf[delay->buf_idx];
|
||||||
|
|
||||||
delay->buf[delay->buf_idx] *= delay->feedback;
|
delay->buf[delay->buf_idx] *= delay->feedback;
|
||||||
|
|
11
delay.h
11
delay.h
|
@ -1,7 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
struct delay {
|
struct delay {
|
||||||
uint64_t duration;
|
uint64_t duration;
|
||||||
|
@ -14,17 +14,14 @@ struct delay {
|
||||||
};
|
};
|
||||||
|
|
||||||
void delay_init(
|
void delay_init(
|
||||||
struct delay * delay
|
struct delay* delay);
|
||||||
);
|
|
||||||
|
|
||||||
void delay_set_time(
|
void delay_set_time(
|
||||||
struct delay* delay,
|
struct delay* delay,
|
||||||
uint64_t new_duration
|
uint64_t new_duration);
|
||||||
);
|
|
||||||
|
|
||||||
void delay_process(
|
void delay_process(
|
||||||
struct delay* delay,
|
struct delay* delay,
|
||||||
size_t frame_size,
|
size_t frame_size,
|
||||||
float const* in,
|
float const* in,
|
||||||
float * out
|
float* out);
|
||||||
);
|
|
||||||
|
|
16
gate.c
16
gate.c
|
@ -1,13 +1,13 @@
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "gate.h"
|
#include "gate.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
void gate_init(
|
void gate_init(
|
||||||
struct gate * gate
|
struct gate* gate)
|
||||||
) {
|
{
|
||||||
gate->threshold = 0.0;
|
gate->threshold = 0.0;
|
||||||
gate->enable_calibration = false;
|
gate->enable_calibration = false;
|
||||||
|
|
||||||
|
@ -25,8 +25,8 @@ void gate_process(
|
||||||
struct gate* gate,
|
struct gate* gate,
|
||||||
size_t frame_size,
|
size_t frame_size,
|
||||||
float const* in,
|
float const* in,
|
||||||
float * out
|
float* out)
|
||||||
) {
|
{
|
||||||
|
|
||||||
bool act = false;
|
bool act = false;
|
||||||
|
|
||||||
|
|
7
gate.h
7
gate.h
|
@ -1,8 +1,8 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
struct gate {
|
struct gate {
|
||||||
float threshold;
|
float threshold;
|
||||||
|
@ -25,5 +25,4 @@ void gate_process(
|
||||||
struct gate* gate,
|
struct gate* gate,
|
||||||
size_t frame_size,
|
size_t frame_size,
|
||||||
float const* in,
|
float const* in,
|
||||||
float * out
|
float* out);
|
||||||
);
|
|
||||||
|
|
108
guitfx.c
108
guitfx.c
|
@ -2,20 +2,20 @@
|
||||||
#include "pipewire/port.h"
|
#include "pipewire/port.h"
|
||||||
#include "spa/pod/iter.h"
|
#include "spa/pod/iter.h"
|
||||||
#include "spa/utils/defs.h"
|
#include "spa/utils/defs.h"
|
||||||
#include <stdio.h>
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#include <spa/pod/pod.h>
|
|
||||||
#include <spa/pod/builder.h>
|
|
||||||
#include <spa/control/control.h>
|
#include <spa/control/control.h>
|
||||||
#include <spa/param/latency-utils.h>
|
#include <spa/param/latency-utils.h>
|
||||||
|
#include <spa/pod/builder.h>
|
||||||
|
#include <spa/pod/pod.h>
|
||||||
|
|
||||||
#include <pipewire/pipewire.h>
|
|
||||||
#include <pipewire/filter.h>
|
#include <pipewire/filter.h>
|
||||||
|
#include <pipewire/pipewire.h>
|
||||||
|
|
||||||
#include "delay.h"
|
#include "delay.h"
|
||||||
#include "sust.h"
|
|
||||||
#include "gate.h"
|
#include "gate.h"
|
||||||
|
#include "sust.h"
|
||||||
|
|
||||||
float envelope(float x);
|
float envelope(float x);
|
||||||
|
|
||||||
|
@ -63,27 +63,22 @@ static void on_process(void *userdata, struct spa_io_position *position)
|
||||||
struct spa_data* d = &buf->datas[0];
|
struct spa_data* d = &buf->datas[0];
|
||||||
|
|
||||||
if (d->data) {
|
if (d->data) {
|
||||||
struct spa_pod * pod =
|
struct spa_pod* pod = spa_pod_from_data(d->data, d->maxsize,
|
||||||
spa_pod_from_data(
|
d->chunk->offset, d->chunk->size);
|
||||||
d->data,
|
|
||||||
d->maxsize,
|
|
||||||
d->chunk->offset,
|
|
||||||
d->chunk->size
|
|
||||||
);
|
|
||||||
|
|
||||||
if (pod) {
|
if (pod) {
|
||||||
if (spa_pod_is_sequence(pod)) {
|
if (spa_pod_is_sequence(pod)) {
|
||||||
struct spa_pod_sequence* pod_seq = (struct spa_pod_sequence*)pod;
|
struct spa_pod_sequence* pod_seq = (struct spa_pod_sequence*)pod;
|
||||||
struct spa_pod_control* c;
|
struct spa_pod_control* c;
|
||||||
SPA_POD_SEQUENCE_FOREACH(pod_seq, c) {
|
SPA_POD_SEQUENCE_FOREACH(pod_seq, c)
|
||||||
|
{
|
||||||
if (c->type == SPA_CONTROL_Midi) {
|
if (c->type == SPA_CONTROL_Midi) {
|
||||||
unsigned sec =
|
unsigned sec = (frame + c->offset) / (float)position->clock.rate.denom;
|
||||||
(frame + c->offset)
|
|
||||||
/ (float) position->clock.rate.denom;
|
|
||||||
char* midi_data = SPA_POD_BODY(&c->value);
|
char* midi_data = SPA_POD_BODY(&c->value);
|
||||||
unsigned size = SPA_POD_BODY_SIZE(&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]);
|
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) {
|
switch (midi_data[0] & 0xff) {
|
||||||
case 0xb0:
|
case 0xb0:
|
||||||
|
@ -109,7 +104,8 @@ static void on_process(void *userdata, struct spa_io_position *position)
|
||||||
if (data->prog == 2) {
|
if (data->prog == 2) {
|
||||||
if (midi_data[2] >= 64) {
|
if (midi_data[2] >= 64) {
|
||||||
data->gate.threshold = data->gate.cur_block_sum * 0.8;
|
data->gate.threshold = data->gate.cur_block_sum * 0.8;
|
||||||
printf("calibrate noise gate: threshold = %f\n", data->gate.threshold);
|
printf("calibrate noise gate: threshold = %f\n",
|
||||||
|
data->gate.threshold);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,7 +131,6 @@ static void on_process(void *userdata, struct spa_io_position *position)
|
||||||
delay_set_time(&data->delay, duration);
|
delay_set_time(&data->delay, duration);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
sust_resize(&data->sust, duration);
|
sust_resize(&data->sust, duration);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,7 +159,8 @@ static void on_process(void *userdata, struct spa_io_position *position)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "on_process(): unexpected POD that is not a sequence (midi_in_port)\n");
|
fprintf(stderr, "on_process(): unexpected POD that is not a sequence "
|
||||||
|
"(midi_in_port)\n");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "on_process(): pod is NULL\n");
|
fprintf(stderr, "on_process(): pod is NULL\n");
|
||||||
|
@ -175,7 +171,6 @@ static void on_process(void *userdata, struct spa_io_position *position)
|
||||||
|
|
||||||
pw_filter_queue_buffer(data->midi_in_port, b);
|
pw_filter_queue_buffer(data->midi_in_port, b);
|
||||||
|
|
||||||
|
|
||||||
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 tmp1[n_samples];
|
float tmp1[n_samples];
|
||||||
float tmp2[n_samples];
|
float tmp2[n_samples];
|
||||||
|
@ -200,7 +195,9 @@ static void do_quit(void *userdata, int signal_number)
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
struct data data = { 0, };
|
struct data data = {
|
||||||
|
0,
|
||||||
|
};
|
||||||
|
|
||||||
gate_init(&data.gate);
|
gate_init(&data.gate);
|
||||||
delay_init(&data.delay);
|
delay_init(&data.delay);
|
||||||
|
@ -219,57 +216,36 @@ int main(int argc, char *argv[])
|
||||||
pw_loop_add_signal(pw_main_loop_get_loop(data.loop), SIGTERM, do_quit, &data);
|
pw_loop_add_signal(pw_main_loop_get_loop(data.loop), SIGTERM, do_quit, &data);
|
||||||
|
|
||||||
data.filter = pw_filter_new_simple(
|
data.filter = pw_filter_new_simple(
|
||||||
pw_main_loop_get_loop(data.loop),
|
pw_main_loop_get_loop(data.loop), "Guitar FX",
|
||||||
"Guitar FX",
|
pw_properties_new(PW_KEY_MEDIA_TYPE, "Audio", PW_KEY_MEDIA_CATEGORY,
|
||||||
pw_properties_new(
|
"Filter", PW_KEY_MEDIA_ROLE, "DSP", NULL),
|
||||||
PW_KEY_MEDIA_TYPE, "Audio",
|
&filter_events, &data);
|
||||||
PW_KEY_MEDIA_CATEGORY, "Filter",
|
|
||||||
PW_KEY_MEDIA_ROLE, "DSP",
|
|
||||||
NULL),
|
|
||||||
&filter_events,
|
|
||||||
&data);
|
|
||||||
|
|
||||||
data.midi_in_port = pw_filter_add_port(data.filter,
|
data.midi_in_port = pw_filter_add_port(data.filter, PW_DIRECTION_INPUT,
|
||||||
PW_DIRECTION_INPUT,
|
PW_FILTER_PORT_FLAG_MAP_BUFFERS, sizeof(struct port),
|
||||||
PW_FILTER_PORT_FLAG_MAP_BUFFERS,
|
pw_properties_new(PW_KEY_FORMAT_DSP, "8 bit raw midi",
|
||||||
sizeof(struct port),
|
PW_KEY_PORT_NAME, "midi in", NULL),
|
||||||
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);
|
NULL, 0);
|
||||||
|
|
||||||
data.out_port = pw_filter_add_port(data.filter,
|
data.guit_in_port = pw_filter_add_port(
|
||||||
PW_DIRECTION_OUTPUT,
|
data.filter, PW_DIRECTION_INPUT, PW_FILTER_PORT_FLAG_MAP_BUFFERS,
|
||||||
PW_FILTER_PORT_FLAG_MAP_BUFFERS,
|
|
||||||
sizeof(struct port),
|
sizeof(struct port),
|
||||||
pw_properties_new(
|
pw_properties_new(PW_KEY_FORMAT_DSP, "32 bit float mono audio",
|
||||||
PW_KEY_FORMAT_DSP, "32 bit float mono audio",
|
PW_KEY_PORT_NAME, "guitar in", NULL),
|
||||||
PW_KEY_PORT_NAME, "fx out",
|
|
||||||
NULL),
|
|
||||||
NULL, 0);
|
NULL, 0);
|
||||||
|
|
||||||
params[0] = spa_process_latency_build(&b,
|
data.out_port = pw_filter_add_port(
|
||||||
SPA_PARAM_ProcessLatency,
|
data.filter, PW_DIRECTION_OUTPUT, PW_FILTER_PORT_FLAG_MAP_BUFFERS,
|
||||||
&SPA_PROCESS_LATENCY_INFO_INIT(
|
sizeof(struct port),
|
||||||
.ns = 10 * SPA_NSEC_PER_MSEC
|
pw_properties_new(PW_KEY_FORMAT_DSP, "32 bit float mono audio",
|
||||||
));
|
PW_KEY_PORT_NAME, "fx out", NULL),
|
||||||
|
NULL, 0);
|
||||||
|
|
||||||
if (pw_filter_connect(data.filter,
|
params[0] = spa_process_latency_build(
|
||||||
PW_FILTER_FLAG_RT_PROCESS,
|
&b, SPA_PARAM_ProcessLatency,
|
||||||
params, 1) < 0) {
|
&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");
|
fprintf(stderr, "can't connect\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
22
sust.c
22
sust.c
|
@ -1,9 +1,10 @@
|
||||||
#include "sust.h"
|
#include "sust.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
void sust_init( struct sust * sust ) {
|
void sust_init(struct sust* sust)
|
||||||
|
{
|
||||||
sust->mode = MODE_Sostenuto;
|
sust->mode = MODE_Sostenuto;
|
||||||
|
|
||||||
sust->playing = false;
|
sust->playing = false;
|
||||||
|
@ -16,7 +17,8 @@ void sust_init( struct sust * sust ) {
|
||||||
sust->play_buf = malloc(sizeof(float) * sust->buf_len);
|
sust->play_buf = malloc(sizeof(float) * sust->buf_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
float envelope( float x ) {
|
float envelope(float x)
|
||||||
|
{
|
||||||
if (x < 0.4)
|
if (x < 0.4)
|
||||||
return (x / 0.4) * (x / 0.4);
|
return (x / 0.4) * (x / 0.4);
|
||||||
if (x < 0.6)
|
if (x < 0.6)
|
||||||
|
@ -26,11 +28,13 @@ float envelope( float x ) {
|
||||||
return v * v;
|
return v * v;
|
||||||
}
|
}
|
||||||
|
|
||||||
float softcos( float x ) {
|
float softcos(float x)
|
||||||
|
{
|
||||||
return sin(x * (3.141 / 2.0));
|
return sin(x * (3.141 / 2.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
void sust_resize( struct sust * sust, size_t new_len ) {
|
void sust_resize(struct sust* sust, size_t new_len)
|
||||||
|
{
|
||||||
sust->buf_len = new_len;
|
sust->buf_len = new_len;
|
||||||
sust->play_buf = realloc(sust->play_buf, sizeof(float) * new_len);
|
sust->play_buf = realloc(sust->play_buf, sizeof(float) * new_len);
|
||||||
sust->record_buf = realloc(sust->record_buf, sizeof(float) * new_len);
|
sust->record_buf = realloc(sust->record_buf, sizeof(float) * new_len);
|
||||||
|
@ -38,8 +42,8 @@ void sust_resize( struct sust * sust, size_t new_len ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void sust_swap(
|
void sust_swap(
|
||||||
struct sust * sust
|
struct sust* sust)
|
||||||
) {
|
{
|
||||||
float* tmp = sust->play_buf;
|
float* tmp = sust->play_buf;
|
||||||
sust->play_buf = sust->record_buf;
|
sust->play_buf = sust->record_buf;
|
||||||
sust->record_buf = tmp;
|
sust->record_buf = tmp;
|
||||||
|
@ -53,8 +57,8 @@ void sust_process(
|
||||||
struct sust* sust,
|
struct sust* sust,
|
||||||
size_t frame_size,
|
size_t frame_size,
|
||||||
float const* in,
|
float const* in,
|
||||||
float * out
|
float* out)
|
||||||
) {
|
{
|
||||||
for (size_t i = 0; i < frame_size; ++i) {
|
for (size_t i = 0; i < frame_size; ++i) {
|
||||||
|
|
||||||
if (sust->mode == MODE_Sustain && sust->idx == 0) {
|
if (sust->mode == MODE_Sustain && sust->idx == 0) {
|
||||||
|
|
4
sust.h
4
sust.h
|
@ -19,7 +19,6 @@ struct sust {
|
||||||
float* play_buf;
|
float* play_buf;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
void sust_init(struct sust* sust);
|
void sust_init(struct sust* sust);
|
||||||
void sust_resize(struct sust* sust, size_t new_len);
|
void sust_resize(struct sust* sust, size_t new_len);
|
||||||
void sust_swap(struct sust* sust);
|
void sust_swap(struct sust* sust);
|
||||||
|
@ -28,5 +27,4 @@ void sust_process(
|
||||||
struct sust* sust,
|
struct sust* sust,
|
||||||
size_t frame_size,
|
size_t frame_size,
|
||||||
float const* in,
|
float const* in,
|
||||||
float * out
|
float* out);
|
||||||
);
|
|
||||||
|
|
Loading…
Reference in a new issue