Phase 26: add common parameter targets, step automation and MIDI control mapping

This commit is contained in:
Johannes Findeisen 2026-10-27 16:37:59 +01:00
commit 901a536f10
38 changed files with 740 additions and 67 deletions

130
src/control/model.c Normal file
View file

@ -0,0 +1,130 @@
#include "xtc/xtc_control.h"
#include "../project/project_internal.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
xtc_result xtc_project_get_control_map(const xtc_project *p, unsigned int channel, unsigned int out[3])
{
size_t i;
if (!p || !out || channel > 15) return XTC_ERR_INVALID_ARGUMENT;
for (i = 0; i < 3; ++i) out[i] = p->control_map[channel][i];
return XTC_OK;
}
xtc_result xtc_project_set_control_map(xtc_project *p, unsigned int channel, const unsigned int cc[3])
{
size_t i, j;
if (!p || !cc || channel > 15) return XTC_ERR_INVALID_ARGUMENT;
for (i = 0; i < 3; ++i) {
if (cc[i] > XTC_CC_DISABLED || cc[i] == 120 || cc[i] == 123) return XTC_ERR_INVALID_ARGUMENT;
for (j = 0; j < i; ++j) if (cc[i] != XTC_CC_DISABLED && cc[i] == cc[j]) return XTC_ERR_INVALID_ARGUMENT;
}
for (i = 0; i < 3; ++i) p->control_map[channel][i] = (unsigned char)cc[i];
return XTC_OK;
}
xtc_result xtc_project_parameter(const xtc_project *p, xtc_parameter_key key, double rate, xtc_parameter_state *out)
{
xtc_parameter_state state = {0};
size_t i;
if (!p || !out || !isfinite(rate) || rate < 1 || rate > 768000) return XTC_ERR_INVALID_ARGUMENT;
state.info.id = key.parameter;
if (key.kind == XTC_PARAMETER_TRACK) {
xtc_track_info track;
xtc_result r = XTC_ERR_NOT_FOUND;
if (key.parameter > 1) return XTC_ERR_NOT_FOUND;
for (i = 0; i < p->track_count; ++i) if (p->tracks[i].id == key.owner) { track = p->tracks[i]; r = XTC_OK; break; }
if (r != XTC_OK) return r;
snprintf(state.info.name,sizeof(state.info.name),"%s",key.parameter ? "Pan" : "Gain");
snprintf(state.info.unit,sizeof(state.info.unit),"%s",key.parameter ? "balance" : "linear");
state.info.minimum = key.parameter ? -1 : 0;
state.info.maximum = key.parameter ? 1 : 4;
state.info.default_value = key.parameter ? 0 : 1;
state.value = key.parameter ? track.pan : track.gain;
state.flags = XTC_PLUGIN_BOUNDED_BELOW | XTC_PLUGIN_BOUNDED_ABOVE;
for (i = 0; i < p->automation_count; ++i)
if (p->automation[i].track_id == key.owner && (unsigned int)p->automation[i].parameter == key.parameter) state.automated = 1;
} else if (key.kind == XTC_PARAMETER_PLUGIN) {
xtc_plugin_spec spec;
xtc_plugin_details details;
xtc_result r;
if (key.owner > SIZE_MAX) return XTC_ERR_NOT_FOUND;
r = xtc_graph_get_plugin(p->graph,(size_t)key.owner,&spec);
if (r != XTC_OK) return r;
r = xtc_plugin_describe(&spec,rate,&details);
if (r != XTC_OK) return r;
for (i = 0; i < spec.control_count && spec.ports[i] != key.parameter; ++i) {}
if (i == spec.control_count) return XTC_ERR_NOT_FOUND;
state.info = details.parameters[i]; state.flags = details.flags[i]; state.value = spec.values[i];
for (i = 0; i < spec.point_count; ++i) if (spec.points[i].port == key.parameter) state.automated = 1;
} else if (key.kind == XTC_PARAMETER_LIVE) {
if (key.owner > 15 || key.parameter > 2) return XTC_ERR_NOT_FOUND;
snprintf(state.info.name,sizeof(state.info.name),"%s",key.parameter == 0 ? "Live volume" : key.parameter == 1 ? "Live pan" : "Live sustain");
snprintf(state.info.unit,sizeof(state.info.unit),"%s",key.parameter == 0 ? "linear" : key.parameter == 1 ? "balance" : "boolean");
state.info.minimum = key.parameter == 1 ? -1 : 0;
state.info.maximum = 1;
state.info.default_value = key.parameter == 0 ? 1 : 0;
state.value = state.info.default_value;
state.flags = XTC_PLUGIN_BOUNDED_BELOW | XTC_PLUGIN_BOUNDED_ABOVE | (key.parameter == 2 ? XTC_PLUGIN_TOGGLED : 0U);
} else return XTC_ERR_INVALID_ARGUMENT;
/* Keep all output untouched on failure. */
*out = state;
return XTC_OK;
}
static int valid_value(const xtc_parameter_state *state, float value)
{
return isfinite(value) && value >= state->info.minimum && value <= state->info.maximum &&
(!(state->flags & XTC_PLUGIN_INTEGER) || value == floorf(value)) &&
(!(state->flags & XTC_PLUGIN_TOGGLED) || value == 0 || value == 1);
}
xtc_result xtc_project_set_parameter(xtc_project *p, xtc_parameter_key key, double rate, float value)
{
xtc_parameter_state state;
xtc_result r = xtc_project_parameter(p,key,rate,&state);
size_t i;
if (r != XTC_OK) return r;
if (!valid_value(&state,value)) return XTC_ERR_INVALID_ARGUMENT;
if (key.kind == XTC_PARAMETER_TRACK) {
for (i = 0; i < p->track_count; ++i) if (p->tracks[i].id == key.owner) {
if (key.parameter) p->tracks[i].pan = value; else p->tracks[i].gain = value;
return XTC_OK;
}
} else if (key.kind == XTC_PARAMETER_PLUGIN) {
xtc_plugin_spec spec;
r = xtc_graph_get_plugin(p->graph,(size_t)key.owner,&spec);
if (r != XTC_OK) return r;
for (i = 0; i < spec.control_count; ++i) if (spec.ports[i] == key.parameter) spec.values[i] = value;
return xtc_graph_set_plugin(p->graph,(size_t)key.owner,&spec);
}
return XTC_ERR_UNSUPPORTED;
}
xtc_result xtc_project_automate(xtc_project *p, xtc_parameter_key key, double rate,
uint64_t tick, unsigned int step, float value, int remove)
{
xtc_parameter_state state;
xtc_result r = xtc_project_parameter(p,key,rate,&state);
if (r != XTC_OK) return r;
if ((remove != 0 && remove != 1) || tick > XTC_PROJECT_MAX_TICK) return XTC_ERR_INVALID_ARGUMENT;
if (key.kind == XTC_PARAMETER_LIVE) return XTC_ERR_UNSUPPORTED;
if (!remove && (step > 1 || !valid_value(&state,value) ||
(!step && (state.flags & (XTC_PLUGIN_INTEGER|XTC_PLUGIN_TOGGLED))))) return XTC_ERR_INVALID_ARGUMENT;
if (key.kind == XTC_PARAMETER_TRACK) {
xtc_automation_point point = {key.owner,tick,(xtc_automation_parameter)key.parameter,value,step};
return remove ? xtc_project_remove_automation(p,key.owner,point.parameter,tick) : xtc_project_set_automation(p,&point);
} else {
xtc_plugin_spec spec;
unsigned int i;
r = xtc_graph_get_plugin(p->graph,(size_t)key.owner,&spec);
if (r != XTC_OK) return r;
for (i = 0; i < spec.point_count; ++i) if (spec.points[i].port == key.parameter && spec.points[i].tick == tick) break;
if (remove) {
if (i == spec.point_count) return XTC_ERR_NOT_FOUND;
memmove(spec.points+i,spec.points+i+1,(spec.point_count-i-1)*sizeof(*spec.points));
--spec.point_count;
} else {
if (i == XTC_PLUGIN_POINTS) return XTC_ERR_CAPACITY;
spec.points[i] = (xtc_plugin_point){tick,key.parameter,step,value};
if (i == spec.point_count) ++spec.point_count;
}
return xtc_graph_set_plugin(p->graph,(size_t)key.owner,&spec);
}
}