From e7ad7fabb596fea516fdccb388fd72b36460f9e0 Mon Sep 17 00:00:00 2001 From: klemek Date: Thu, 2 Oct 2025 23:40:37 +0200 Subject: [PATCH] demo mode change state at random each 4 beats --- src/state.c | 12 ++++++++++-- src/tempo.c | 5 +++-- src/tempo.h | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/state.c b/src/state.c index 6d2bba8..14759bb 100644 --- a/src/state.c +++ b/src/state.c @@ -270,7 +270,7 @@ void state_apply_event(SharedContext *context, StateConfig state_config, bool state_background_midi_write(SharedContext *context, StateConfig state_config, MidiDevice midi) { pid_t pid; - bool beat_active, last_active; + bool beat_active, last_active, change, last_change; pid = fork(); if (pid < 0) { @@ -289,7 +289,7 @@ bool state_background_midi_write(SharedContext *context, last_active = false; while (!context->stop) { - beat_active = tempo_progress(context->tempo) < 0.25; + beat_active = tempo_progress(context->tempo, 1.0) < 0.25; if (beat_active != last_active) { safe_midi_write(midi, state_config.tap_tempo_code, @@ -301,6 +301,14 @@ bool state_background_midi_write(SharedContext *context, last_active = beat_active; } + + change = tempo_progress(context->tempo, 4.0) < 0.25; + + if (change && !last_change && context->demo) { + state_randomize(context, state_config); + } + + last_change = change; } log_info("(state) background writing stopped by main thread (pid: %d)", pid); diff --git a/src/tempo.c b/src/tempo.c index a23f6dc..ba9e3bb 100644 --- a/src/tempo.c +++ b/src/tempo.c @@ -113,10 +113,11 @@ void tempo_tap(Tempo *tempo) { add_tap_to_chain(tempo, t); } -double tempo_progress(Tempo tempo) { +double tempo_progress(Tempo tempo, double modulo) { long t; t = now(); - return fmod((double)(t - tempo.last_reset) / (double)tempo.beat_length, 1.0); + return fmod((double)(t - tempo.last_reset) / (double)tempo.beat_length, + modulo); } \ No newline at end of file diff --git a/src/tempo.h b/src/tempo.h index 7cd0af5..cc73551 100644 --- a/src/tempo.h +++ b/src/tempo.h @@ -9,6 +9,6 @@ void tempo_tap(Tempo *tempo); void tempo_set(Tempo *tempo, float value); -double tempo_progress(Tempo tempo); +double tempo_progress(Tempo tempo, double modulo); #endif /* TEMPO_H */ \ No newline at end of file