feat: auto-reconnect midi
Clang Lint CI / lint-no-video (push) Successful in 59s
Clang Build CI / run-no-video (push) Successful in 59s
Clang Build CI / run-video (push) Successful in 59s
Clang Build CI / build-release (push) Successful in 1m31s
Clang Lint CI / lint-video (push) Successful in 1m7s

This commit is contained in:
2026-05-14 15:28:38 +02:00
parent adc520bc8b
commit 28b87d316a
8 changed files with 100 additions and 48 deletions
+19 -25
View File
@@ -13,7 +13,7 @@
#include "state.h"
#include "tempo.h"
static void safe_midi_write(const MidiDevice *midi, unsigned int code,
static void safe_midi_write(MidiDevice midi, unsigned int code,
unsigned char value) {
if (code != UNSET_MIDI_CODE) {
midi_write(midi, code, value);
@@ -21,7 +21,7 @@ static void safe_midi_write(const MidiDevice *midi, unsigned int code,
}
static void update_page(const Context *context, StateConfig state_config,
const MidiDevice *midi) {
MidiDevice midi) {
unsigned int page_item_min;
unsigned int page_item_max;
// SHOW PAGE
@@ -51,7 +51,7 @@ static void update_page(const Context *context, StateConfig state_config,
}
static void update_active(const Context *context, StateConfig state_config,
const MidiDevice *midi) {
MidiDevice midi, bool beat_active) {
unsigned int k;
for (unsigned int i = 0; i < state_config.midi_active_counts.length; i++) {
@@ -59,13 +59,13 @@ static void update_active(const Context *context, StateConfig state_config,
j++) {
k = state_config.midi_active_offsets.values[i] + j;
safe_midi_write(midi, state_config.midi_active_codes.values[k],
context->active[i] == j ? MIDI_MAX : 0);
context->active[i] == j && beat_active ? MIDI_MAX : 0);
}
}
}
static void update_values(const Context *context, StateConfig state_config,
const MidiDevice *midi) {
MidiDevice midi) {
unsigned int j;
unsigned int k;
unsigned int part;
@@ -407,8 +407,8 @@ void state_parse_config(StateConfig *state_config, const ConfigFile *config) {
}
void state_midi_event(Context *context, StateConfig state_config,
const MidiDevice *midi, unsigned char code,
unsigned char value, bool trace_midi) {
MidiDevice midi, unsigned char code, unsigned char value,
bool trace_midi) {
unsigned int i;
unsigned int j;
unsigned int k;
@@ -455,7 +455,7 @@ void state_midi_event(Context *context, StateConfig state_config,
if (value > 0) {
part = arr_uint_remap_index(state_config.midi_active_offsets, &i);
context->active[part] = i;
update_active(context, state_config, midi);
update_active(context, state_config, midi, true);
update_values(context, state_config, midi);
}
}
@@ -474,17 +474,17 @@ void state_midi_event(Context *context, StateConfig state_config,
} else if (value > 0) {
if (context->values[k][i % 3] > 0.5) {
context->values[k][i % 3] = 0;
midi_write(midi, code, 0);
safe_midi_write(midi, code, 0);
} else {
context->values[k][i % 3] = 1;
midi_write(midi, code, MIDI_MAX);
safe_midi_write(midi, code, MIDI_MAX);
}
}
}
if (code == state_config.tap_tempo_code) {
found = true;
midi_write(midi, code, value);
safe_midi_write(midi, code, value);
if (value > 0) {
tempo_tap(&context->tempo);
}
@@ -494,14 +494,14 @@ void state_midi_event(Context *context, StateConfig state_config,
if (trace_midi) {
log_trace("unknown midi: %d %d", code, value);
}
midi_write(midi, code, value);
safe_midi_write(midi, code, value);
} else if (trace_midi) {
log_trace("midi: %d %d", code, value);
}
}
void state_key_event(Context *context, StateConfig state_config,
unsigned int code, const MidiDevice *midi) {
unsigned int code, MidiDevice midi) {
unsigned int index;
if (code == state_config.hotkey_randomize) {
@@ -570,24 +570,18 @@ void *state_background_write(void *args) {
log_info("(state) background writing started");
if (!midi->error) {
update_page(context, state_config, midi);
update_active(context, state_config, midi);
update_values(context, state_config, midi);
}
last_active = false;
last_change = false;
while (!context->stop) {
beat_active = tempo_progress(&context->tempo, 1.0) < 0.5;
if (!midi->error && beat_active != last_active) {
safe_midi_write(midi, state_config.tap_tempo_code,
beat_active ? MIDI_MAX : 0);
if (midi->connected && beat_active != last_active) {
update_values(context, state_config, *midi);
update_page(context, state_config, *midi);
update_active(context, state_config, *midi, beat_active);
safe_midi_write(midi,
state_config.select_frag_codes.values[context->selected],
safe_midi_write(*midi, state_config.tap_tempo_code,
beat_active ? MIDI_MAX : 0);
}
@@ -599,7 +593,7 @@ void *state_background_write(void *args) {
if (context->auto_random && change && !last_change) {
randomize(context, state_config);
update_values(context, state_config, midi);
update_values(context, state_config, *midi);
}
last_change = change;