refactor: staticify state local functions
This commit is contained in:
+42
-49
@@ -281,17 +281,50 @@ void state_midi_event(SharedContext *context, const StateConfig *state_config,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void reset(SharedContext *context) {
|
||||||
|
memset(context->values, 0, sizeof(context->values));
|
||||||
|
memset(context->state.values, 0, sizeof(context->state.values));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void randomize(SharedContext *context, const StateConfig *state_config) {
|
||||||
|
unsigned int j;
|
||||||
|
unsigned int l;
|
||||||
|
unsigned int part;
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < state_config->midi_codes.length; i++) {
|
||||||
|
j = i / 3;
|
||||||
|
part = arr_uint_remap_index(state_config->midi_offsets, &j);
|
||||||
|
for (unsigned int k = 0; k < state_config->midi_active_counts.values[part];
|
||||||
|
k++) {
|
||||||
|
l = state_config->values_offsets.values[part] +
|
||||||
|
k * state_config->midi_counts.values[part] + j;
|
||||||
|
|
||||||
|
if (arr_uint_index_of(state_config->fader_codes,
|
||||||
|
state_config->midi_codes.values[i]) !=
|
||||||
|
ARRAY_NOT_FOUND) {
|
||||||
|
context->values[l][i % 3] = (float)rand_uint(MIDI_MAX + 1) / MIDI_MAX;
|
||||||
|
} else {
|
||||||
|
context->values[l][i % 3] = rand_uint(2) == 1 ? 1 : 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < context->state.length; i++) {
|
||||||
|
context->state.values[i] = rand_uint(state_config->state_max);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void state_key_event(SharedContext *context, const StateConfig *state_config,
|
void state_key_event(SharedContext *context, const StateConfig *state_config,
|
||||||
unsigned int code, const MidiDevice *midi) {
|
unsigned int code, const MidiDevice *midi) {
|
||||||
if (code == 82) {
|
if (code == 82) {
|
||||||
// R: randomize
|
// R: randomize
|
||||||
log_info("[R] Randomized");
|
log_info("[R] Randomized");
|
||||||
state_randomize(context, state_config);
|
randomize(context, state_config);
|
||||||
state_apply(context, state_config, midi);
|
update_values(context, state_config, midi);
|
||||||
} else if (code == 48) {
|
} else if (code == 1082) {
|
||||||
log_info("[0] Reset");
|
log_info("[SHIFT+R] Reset");
|
||||||
state_reset(context);
|
reset(context);
|
||||||
state_apply(context, state_config, midi);
|
update_values(context, state_config, midi);
|
||||||
} else if (code == 68) {
|
} else if (code == 68) {
|
||||||
// D: demo on/off
|
// D: demo on/off
|
||||||
log_info((context->demo ? "[D] Demo OFF" : "[D] Demo ON"));
|
log_info((context->demo ? "[D] Demo OFF" : "[D] Demo ON"));
|
||||||
@@ -326,13 +359,6 @@ void state_key_event(SharedContext *context, const StateConfig *state_config,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void state_apply(const SharedContext *context, const StateConfig *state_config,
|
|
||||||
const MidiDevice *midi) {
|
|
||||||
if (!midi->error) {
|
|
||||||
update_values(context, state_config, midi);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool state_background_write(SharedContext *context,
|
bool state_background_write(SharedContext *context,
|
||||||
const StateConfig *state_config,
|
const StateConfig *state_config,
|
||||||
const MidiDevice *midi) {
|
const MidiDevice *midi) {
|
||||||
@@ -376,9 +402,9 @@ bool state_background_write(SharedContext *context,
|
|||||||
(double)context->auto_random_cycle) < 0.5;
|
(double)context->auto_random_cycle) < 0.5;
|
||||||
|
|
||||||
if (context->auto_random && change && !last_change) {
|
if (context->auto_random && change && !last_change) {
|
||||||
state_randomize(context, state_config);
|
randomize(context, state_config);
|
||||||
|
|
||||||
state_apply(context, state_config, midi);
|
update_values(context, state_config, midi);
|
||||||
}
|
}
|
||||||
|
|
||||||
last_change = change;
|
last_change = change;
|
||||||
@@ -442,7 +468,7 @@ void state_init(SharedContext *context, const StateConfig *state_config,
|
|||||||
memset(context->state.values, 0, sizeof(context->state.values));
|
memset(context->state.values, 0, sizeof(context->state.values));
|
||||||
|
|
||||||
if (auto_random) {
|
if (auto_random) {
|
||||||
state_randomize(context, state_config);
|
randomize(context, state_config);
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(context->active, 0, sizeof(context->active));
|
memset(context->active, 0, sizeof(context->active));
|
||||||
@@ -462,39 +488,6 @@ void state_init(SharedContext *context, const StateConfig *state_config,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void state_reset(SharedContext *context) {
|
|
||||||
memset(context->values, 0, sizeof(context->values));
|
|
||||||
memset(context->state.values, 0, sizeof(context->state.values));
|
|
||||||
}
|
|
||||||
|
|
||||||
void state_randomize(SharedContext *context, const StateConfig *state_config) {
|
|
||||||
unsigned int j;
|
|
||||||
unsigned int l;
|
|
||||||
unsigned int part;
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < state_config->midi_codes.length; i++) {
|
|
||||||
j = i / 3;
|
|
||||||
part = arr_uint_remap_index(state_config->midi_offsets, &j);
|
|
||||||
for (unsigned int k = 0; k < state_config->midi_active_counts.values[part];
|
|
||||||
k++) {
|
|
||||||
l = state_config->values_offsets.values[part] +
|
|
||||||
k * state_config->midi_counts.values[part] + j;
|
|
||||||
|
|
||||||
if (arr_uint_index_of(state_config->fader_codes,
|
|
||||||
state_config->midi_codes.values[i]) !=
|
|
||||||
ARRAY_NOT_FOUND) {
|
|
||||||
context->values[l][i % 3] = (float)rand_uint(MIDI_MAX + 1) / MIDI_MAX;
|
|
||||||
} else {
|
|
||||||
context->values[l][i % 3] = rand_uint(2) == 1 ? 1 : 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < context->state.length; i++) {
|
|
||||||
context->state.values[i] = rand_uint(state_config->state_max);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void state_save(const SharedContext *context, const StateConfig *state_config,
|
void state_save(const SharedContext *context, const StateConfig *state_config,
|
||||||
const char *state_file) {
|
const char *state_file) {
|
||||||
StringArray lines;
|
StringArray lines;
|
||||||
|
|||||||
@@ -21,13 +21,6 @@ void state_init(SharedContext *context, const StateConfig *state_config,
|
|||||||
unsigned int base_tempo, const char *state_file,
|
unsigned int base_tempo, const char *state_file,
|
||||||
bool load_state);
|
bool load_state);
|
||||||
|
|
||||||
void state_reset(SharedContext *context);
|
|
||||||
|
|
||||||
void state_randomize(SharedContext *context, const StateConfig *state_config);
|
|
||||||
|
|
||||||
void state_apply(const SharedContext *context, const StateConfig *state_config,
|
|
||||||
const MidiDevice *midi);
|
|
||||||
|
|
||||||
void state_save(const SharedContext *context, const StateConfig *state_config,
|
void state_save(const SharedContext *context, const StateConfig *state_config,
|
||||||
const char *state_file);
|
const char *state_file);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user