From 2657c2b73275323b2edf9871e2ba1edf5319a6e1 Mon Sep 17 00:00:00 2001 From: klemek Date: Mon, 3 Nov 2025 21:46:14 +0100 Subject: [PATCH] feat(trace): trace fps and midi with args --- README.md | 2 ++ src/args.c | 12 ++++++++++-- src/forge.c | 23 ++++++++++++++--------- src/state.c | 10 ++++++---- src/state.h | 4 ++-- src/types.h | 2 ++ src/video.c | 6 ++++-- src/video.h | 2 +- 8 files changed, 41 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index f5a4881..aaad18e 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,8 @@ options: -t, --tempo base tempo (default: 60) --demo demonstration mode (assume --no-save-state and --no-load-state) -w, --windowed not fullscreen + --trace-midi print midi code and values + --trace-fps print fps status of subsystems ``` ## Release guide diff --git a/src/args.c b/src/args.c index dab732e..c7c9721 100644 --- a/src/args.c +++ b/src/args.c @@ -58,7 +58,9 @@ static void print_help(int status_code) { " -t, --tempo base tempo (default: 60)\n" " --demo demonstration mode (assume --no-save-state " "and --no-load-state)\n" - " -w, --windowed not fullscreen\n"); + " -w, --windowed not fullscreen\n" + " --trace-midi print midi code and values\n" + " --trace-fps print fps status of subsystems\n"); exit(status_code); } @@ -107,13 +109,15 @@ Parameters args_parse(int argc, char **argv) { strncpy(params.config_path, DATADIR "/default.cfg", STR_LEN); strncpy(params.state_file, "forge_saved_state.txt", STR_LEN); params.load_state = true; - params.save_state = false; + params.save_state = true; params.internal_size = 720; params.video_size = 0; params.base_tempo = 60.0f; params.demo = false; params.windowed = false; params.video_in.length = 0; + params.trace_midi = false; + params.trace_fps = false; for (i = 1; i < argc; i++) { arg = argv[i]; @@ -171,6 +175,10 @@ Parameters args_parse(int argc, char **argv) { params.save_state = false; } else if (is_arg(arg, "-w") || is_arg(arg, "--windowed")) { params.windowed = true; + } else if (is_arg(arg, "--trace-midi")) { + params.trace_midi = true; + } else if (is_arg(arg, "--trace-fps")) { + params.trace_fps = true; } else { invalid_arg(arg); } diff --git a/src/forge.c b/src/forge.c index 585812c..00ba393 100644 --- a/src/forge.c +++ b/src/forge.c @@ -31,15 +31,18 @@ static Timer timer; static ConfigFile config; static MidiDevice midi; static StateConfig state_config; +static bool trace_midi; -static void compute_fps() { +static void compute_fps(bool trace_fps) { double fps; char title[100]; if (timer_inc(&timer)) { fps = timer_reset(&timer); - log_trace("(main) %.2ffps", fps); + if (trace_fps) { + log_trace("(main) %.2ffps", fps); + } if (window_output != NULL) { sprintf(title, PACKAGE " " VERSION " - %.0ffps", fps); @@ -148,12 +151,12 @@ static void init_inputs(StringArray video_in, unsigned int video_size) { } } -static bool start_video_captures(unsigned int video_count) { +static bool start_video_captures(unsigned int video_count, bool trace_fps) { unsigned int i; for (i = 0; i < video_count; i++) { if (!inputs.values[i].error && - !video_background_read(&inputs.values[i], context, i)) { + !video_background_read(&inputs.values[i], context, i, trace_fps)) { return false; } } @@ -197,15 +200,15 @@ static void key_callback(Window *window, int key, } static void midi_callback(unsigned char code, unsigned char value) { - state_apply_event(context, state_config, midi, code, value); + state_apply_event(context, state_config, midi, code, value, trace_midi); } -static void loop(bool hr) { +static void loop(bool hr, bool trace_fps) { if (hr) { hot_reload(); } - compute_fps(); + compute_fps(trace_fps); context->time = window_get_time(); @@ -248,7 +251,7 @@ void forge_run(Parameters params) { init_context(params, in_count); - if (!start_video_captures(params.video_in.length)) { + if (!start_video_captures(params.video_in.length, params.trace_fps)) { return; } @@ -257,6 +260,8 @@ void forge_run(Parameters params) { if (midi.error) { params.demo = true; } else { + trace_midi = params.trace_midi; + if (!midi_background_listen(midi, context, midi_callback)) { return; } @@ -308,7 +313,7 @@ void forge_run(Parameters params) { while ((window_output == NULL || !window_should_close(window_output)) && (window_monitor == NULL || !window_should_close(window_monitor))) { - loop(params.hot_reload); + loop(params.hot_reload, params.trace_fps); } context->stop = true; diff --git a/src/state.c b/src/state.c index ec7c969..7a0f59f 100644 --- a/src/state.c +++ b/src/state.c @@ -182,8 +182,8 @@ static void update_values(SharedContext *context, StateConfig state_config, } void state_apply_event(SharedContext *context, StateConfig state_config, - MidiDevice midi, unsigned char code, - unsigned char value) { + MidiDevice midi, unsigned char code, unsigned char value, + bool trace_midi) { unsigned int i, j, k, part; bool found; @@ -263,9 +263,11 @@ void state_apply_event(SharedContext *context, StateConfig state_config, } if (!found) { - log_trace("unknown midi: %d %d", code, value); + if (trace_midi) { + log_trace("unknown midi: %d %d", code, value); + } midi_write(midi, code, value); - } else { + } else if (trace_midi) { log_trace("midi: %d %d", code, value); } } diff --git a/src/state.h b/src/state.h index 426c57e..732e6fe 100644 --- a/src/state.h +++ b/src/state.h @@ -6,8 +6,8 @@ StateConfig state_parse_config(ConfigFile config); void state_apply_event(SharedContext *context, StateConfig state_config, - MidiDevice midi, unsigned char code, - unsigned char value); + MidiDevice midi, unsigned char code, unsigned char value, + bool trace_midi); bool state_background_midi_write(SharedContext *context, StateConfig state_config, MidiDevice midi); diff --git a/src/types.h b/src/types.h index 63e01d0..9ee56c8 100644 --- a/src/types.h +++ b/src/types.h @@ -46,6 +46,8 @@ typedef struct Parameters { bool demo; bool windowed; StringArray video_in; + bool trace_midi; + bool trace_fps; } Parameters; typedef struct Vertex { diff --git a/src/video.c b/src/video.c index 7ef2392..0ce5a62 100644 --- a/src/video.c +++ b/src/video.c @@ -329,7 +329,7 @@ static bool read_video(VideoCapture *video_capture) { } bool video_background_read(VideoCapture *video_capture, SharedContext *context, - int input_index) { + int input_index, bool trace_fps) { pid_t pid; Timer timer; double fps; @@ -352,7 +352,9 @@ bool video_background_read(VideoCapture *video_capture, SharedContext *context, fps = timer_reset(&timer); context->input_fps[input_index] = (unsigned int)round(fps); - log_trace("(%s) %.2ffps", video_capture->name, fps); + if (trace_fps) { + log_trace("(%s) %.2ffps", video_capture->name, fps); + } } } if (context->stop) { diff --git a/src/video.h b/src/video.h index 7648786..0b5dd6a 100644 --- a/src/video.h +++ b/src/video.h @@ -6,7 +6,7 @@ VideoCapture video_init(char *name, unsigned int preferred_height); bool video_background_read(VideoCapture *video_capture, SharedContext *context, - int input_index); + int input_index, bool trace_fps); void video_free(VideoCapture video_capture);