From b4b79cea128b62c7eb8ed0836c6ba6094a296cbc Mon Sep 17 00:00:00 2001 From: klemek Date: Sat, 8 Nov 2025 19:29:04 +0100 Subject: [PATCH] refactor: use struct pointers everywhere and bump max sizes --- src/config.h | 10 +++++----- src/forge.c | 6 +++--- src/midi.c | 8 ++++---- src/midi.h | 2 +- src/state.c | 22 +++++++++++----------- src/state.h | 4 ++-- src/video.c | 12 ++++++------ src/video.h | 2 +- 8 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/config.h b/src/config.h index e252110..bdd01c2 100644 --- a/src/config.h +++ b/src/config.h @@ -18,7 +18,7 @@ /* STRINGS */ #ifndef STR_LEN -#define STR_LEN 1024 +#define STR_LEN 2048 #endif /* TYPES */ @@ -32,7 +32,7 @@ #endif #ifndef MAX_SUB_FILE -#define MAX_SUB_FILE 32 +#define MAX_SUB_FILE 63 #endif /* MIDI */ @@ -48,7 +48,7 @@ /* ARRAY */ #ifndef ARRAY_SIZE -#define ARRAY_SIZE 512 +#define ARRAY_SIZE 1024 #endif #ifndef ARRAY_NOT_FOUND @@ -63,8 +63,8 @@ #endif #ifndef MIN_BEAT_LENGTH -// 240.0 bpm -#define MIN_BEAT_LENGTH 250 +// 480.0 bpm +#define MIN_BEAT_LENGTH 125 #endif #ifndef BEATS_UNTIL_CHAIN_RESET diff --git a/src/forge.c b/src/forge.c index 21fc154..5068112 100644 --- a/src/forge.c +++ b/src/forge.c @@ -99,7 +99,7 @@ static bool start_video_captures(unsigned int video_count, bool trace_fps) { for (i = 0; i < video_count; i++) { if (!inputs.values[i].error && - !video_background_read(inputs.values[i], context, i, trace_fps)) { + !video_background_read(&inputs.values[i], context, i, trace_fps)) { return false; } } @@ -207,12 +207,12 @@ void forge_run(Parameters *params) { } else { trace_midi = params->trace_midi; - if (!midi_background_listen(midi, context, midi_callback)) { + if (!midi_background_listen(&midi, context, midi_callback)) { return; } } - if (!state_background_write(context, project.state_config, midi)) { + if (!state_background_write(context, &project.state_config, &midi)) { return; } diff --git a/src/midi.c b/src/midi.c index 167b2d8..5463ac7 100644 --- a/src/midi.c +++ b/src/midi.c @@ -28,7 +28,7 @@ void midi_write(MidiDevice *device, unsigned char code, unsigned char value) { snd_rawmidi_write(device->output, buffer, 3); } -bool midi_background_listen(MidiDevice device, SharedContext *context, +bool midi_background_listen(MidiDevice *device, SharedContext *context, void (*event_callback)(unsigned char code, unsigned char value)) { pid_t pid; @@ -43,16 +43,16 @@ bool midi_background_listen(MidiDevice device, SharedContext *context, if (pid == 0) { return true; } - log_info("(%s) background acquisition started (pid: %d)", device.name, pid); + log_info("(%s) background acquisition started (pid: %d)", device->name, pid); while (!context->stop) { - bytes_read = snd_rawmidi_read(device.input, buffer, 3); + bytes_read = snd_rawmidi_read(device->input, buffer, 3); if (bytes_read == 3) { event_callback(buffer[1], buffer[2]); } } log_info("(%s) background acquisition stopped by main thread (pid: %d)", - device.name, pid); + device->name, pid); return false; } diff --git a/src/midi.h b/src/midi.h index 7a9569c..8d85cd3 100644 --- a/src/midi.h +++ b/src/midi.h @@ -5,7 +5,7 @@ void midi_open(MidiDevice *device, char *name); void midi_write(MidiDevice *device, unsigned char code, unsigned char value); -bool midi_background_listen(MidiDevice device, SharedContext *context, +bool midi_background_listen(MidiDevice *device, SharedContext *context, void (*event_callback)(unsigned char code, unsigned char value)); diff --git a/src/state.c b/src/state.c index a18cd73..5284252 100644 --- a/src/state.c +++ b/src/state.c @@ -270,8 +270,8 @@ void state_apply_event(SharedContext *context, StateConfig *state_config, } } -bool state_background_write(SharedContext *context, StateConfig state_config, - MidiDevice midi) { +bool state_background_write(SharedContext *context, StateConfig *state_config, + MidiDevice *midi) { pid_t pid; bool beat_active, last_active, change, last_change; @@ -285,10 +285,10 @@ bool state_background_write(SharedContext *context, StateConfig state_config, } log_info("(state) background writing started (pid: %d)", pid); - if (!midi.error) { - update_page(context, &state_config, &midi); - update_active(context, &state_config, &midi); - update_values(context, &state_config, &midi); + if (!midi->error) { + update_page(context, state_config, midi); + update_active(context, state_config, midi); + update_values(context, state_config, midi); } last_active = false; @@ -297,12 +297,12 @@ bool state_background_write(SharedContext *context, StateConfig state_config, while (!context->stop) { beat_active = tempo_progress(&context->tempo, 1.0) < 0.25; - if (!midi.error && beat_active != last_active) { - safe_midi_write(&midi, state_config.tap_tempo_code, + if (!midi->error && beat_active != last_active) { + safe_midi_write(midi, state_config->tap_tempo_code, beat_active ? MIDI_MAX : 0); - safe_midi_write(&midi, - state_config.select_frag_codes.values[context->selected], + safe_midi_write(midi, + state_config->select_frag_codes.values[context->selected], beat_active ? MIDI_MAX : 0); } @@ -311,7 +311,7 @@ bool state_background_write(SharedContext *context, StateConfig state_config, change = tempo_progress(&context->tempo, 4.0) < 0.25; if (context->auto_random && change && !last_change) { - state_randomize(context, &state_config); + state_randomize(context, state_config); } last_change = change; diff --git a/src/state.h b/src/state.h index 412ab1d..9e4adde 100644 --- a/src/state.h +++ b/src/state.h @@ -9,8 +9,8 @@ void state_apply_event(SharedContext *context, StateConfig *state_config, MidiDevice *midi, unsigned char code, unsigned char value, bool trace_midi); -bool state_background_write(SharedContext *context, StateConfig state_config, - MidiDevice midi); +bool state_background_write(SharedContext *context, StateConfig *state_config, + MidiDevice *midi); void state_init(SharedContext *context, StateConfig *state_config, bool demo, bool auto_random, unsigned int base_tempo, char *state_file, diff --git a/src/video.c b/src/video.c index d3c18a0..433f3cb 100644 --- a/src/video.c +++ b/src/video.c @@ -322,7 +322,7 @@ static bool read_video(VideoCapture *video_capture) { return true; } -bool video_background_read(VideoCapture video_capture, SharedContext *context, +bool video_background_read(VideoCapture *video_capture, SharedContext *context, int input_index, bool trace_fps) { pid_t pid; Timer timer; @@ -336,27 +336,27 @@ bool video_background_read(VideoCapture video_capture, SharedContext *context, if (pid == 0) { return true; } - log_info("(%s) background acquisition started (pid: %d)", video_capture.name, + log_info("(%s) background acquisition started (pid: %d)", video_capture->name, pid); timer_init(&timer, 30); - while (!context->stop && read_video(&video_capture)) { + while (!context->stop && read_video(video_capture)) { // repeat infinitely if (timer_inc(&timer)) { fps = timer_reset(&timer); context->input_fps[input_index] = (unsigned int)round(fps); if (trace_fps) { - log_trace("(%s) %.2ffps", video_capture.name, fps); + log_trace("(%s) %.2ffps", video_capture->name, fps); } } } if (context->stop) { log_info("(%s) background acquisition stopped by main thread (pid: %d)", - video_capture.name, pid); + video_capture->name, pid); } else { log_info("(%s) background acquisition stopped after error (pid: %d)", - video_capture.name, pid); + video_capture->name, pid); } exit(context->stop ? EXIT_SUCCESS : EXIT_FAILURE); return false; diff --git a/src/video.h b/src/video.h index 4307fd1..6840fd9 100644 --- a/src/video.h +++ b/src/video.h @@ -6,7 +6,7 @@ void video_init(VideoCapture *video_capture, char *name, unsigned int preferred_height); -bool video_background_read(VideoCapture video_capture, SharedContext *context, +bool video_background_read(VideoCapture *video_capture, SharedContext *context, int input_index, bool trace_fps); void video_free(VideoCapture *video_capture);