Phase 1 plus some refactoring.

This commit is contained in:
Johannes Findeisen 2026-10-26 23:20:00 +01:00
commit 8075e1b79d
17 changed files with 465 additions and 266 deletions

View file

@ -1,54 +1,27 @@
#include "xtc/xtc_engine.h"
#include "graph_internal.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
size_t source;
size_t destination;
} xt_edge;
typedef struct {
float gain;
} xt_gain_node;
struct xt_graph {
size_t max_nodes;
size_t node_count;
size_t edge_count;
xt_gain_node *nodes;
xt_edge *edges;
};
xt_result xt_graph_create(size_t max_nodes, xt_graph **out_graph)
xtc_result xtc_graph_create(size_t max_nodes, xtc_graph **out_graph)
{
xt_graph *graph;
if (out_graph == NULL || max_nodes == 0U) {
return XT_ERR_INVALID_ARGUMENT;
}
xtc_graph *graph;
if (out_graph == NULL) return XTC_ERR_INVALID_ARGUMENT;
*out_graph = NULL;
graph = (xt_graph *)calloc(1U, sizeof(*graph));
if (graph == NULL) {
return XT_ERR_OUT_OF_MEMORY;
}
graph->nodes = (xt_gain_node *)calloc(max_nodes, sizeof(*graph->nodes));
graph->edges = (xt_edge *)calloc(max_nodes * max_nodes, sizeof(*graph->edges));
if (max_nodes == 0U || max_nodes > XTC_MAX_NODES) return XTC_ERR_INVALID_ARGUMENT;
graph = calloc(1U, sizeof(*graph));
if (graph == NULL) return XTC_ERR_OUT_OF_MEMORY;
graph->nodes = calloc(max_nodes, sizeof(*graph->nodes));
graph->edges = calloc(max_nodes * max_nodes, sizeof(*graph->edges));
if (graph->nodes == NULL || graph->edges == NULL) {
free(graph->nodes);
free(graph->edges);
free(graph);
return XT_ERR_OUT_OF_MEMORY;
xtc_graph_destroy(graph);
return XTC_ERR_OUT_OF_MEMORY;
}
graph->max_nodes = max_nodes;
*out_graph = graph;
return XT_OK;
return XTC_OK;
}
void xt_graph_destroy(xt_graph *graph)
void xtc_graph_destroy(xtc_graph *graph)
{
if (graph != NULL) {
free(graph->nodes);
@ -57,133 +30,93 @@ void xt_graph_destroy(xt_graph *graph)
}
}
xt_result xt_graph_add_gain(xt_graph *graph, float gain, size_t *out_node_id)
static xtc_result add_node(xtc_graph *graph, xtc_node_type type, float value,
size_t *out_node_id)
{
if (graph == NULL || out_node_id == NULL) {
return XT_ERR_INVALID_ARGUMENT;
}
if (graph->node_count >= graph->max_nodes) {
return XT_ERR_CAPACITY;
}
if (graph == NULL || out_node_id == NULL || !isfinite(value))
return XTC_ERR_INVALID_ARGUMENT;
if (graph->node_count == graph->max_nodes) return XTC_ERR_CAPACITY;
graph->nodes[graph->node_count].type = type;
graph->nodes[graph->node_count].value = value;
*out_node_id = graph->node_count++;
return XTC_OK;
}
xtc_result xtc_graph_add_gain(xtc_graph *graph, float gain, size_t *out_node_id)
{ return add_node(graph, XTC_NODE_GAIN, gain, out_node_id); }
xtc_result xtc_graph_add_oscillator(xtc_graph *graph, float frequency, size_t *out_node_id)
{
if (frequency < 0.0f) return XTC_ERR_INVALID_ARGUMENT;
return add_node(graph, XTC_NODE_OSCILLATOR, frequency, out_node_id);
}
xtc_result xtc_graph_add_meter(xtc_graph *graph, size_t *out_node_id)
{ return add_node(graph, XTC_NODE_METER, 0.0f, out_node_id); }
xtc_result xtc_graph_add_output(xtc_graph *graph, size_t *out_node_id)
{ return add_node(graph, XTC_NODE_OUTPUT, 0.0f, out_node_id); }
graph->nodes[graph->node_count].gain = gain;
*out_node_id = graph->node_count;
graph->node_count++;
return XT_OK;
xtc_result xtc_graph_get_port(const xtc_graph *graph, size_t node_id,
size_t port_id, xtc_port_info *out_port)
{
xtc_node_type type;
if (graph == NULL || out_port == NULL) return XTC_ERR_INVALID_ARGUMENT;
if (node_id >= graph->node_count) return XTC_ERR_NOT_FOUND;
type = graph->nodes[node_id].type;
if (port_id > 1U || (port_id == 0U && type == XTC_NODE_OSCILLATOR) ||
(port_id == 1U && type == XTC_NODE_OUTPUT)) return XTC_ERR_NOT_FOUND;
out_port->direction = port_id == 0U ? XTC_PORT_INPUT : XTC_PORT_OUTPUT;
out_port->signal_type = XTC_SIGNAL_AUDIO;
out_port->channel_count = 1U;
return XTC_OK;
}
xt_result xt_graph_connect(xt_graph *graph, size_t source_node,
size_t destination_node)
xtc_result xtc_graph_order(const xtc_graph *graph, size_t *order)
{
size_t i;
size_t edge_capacity;
if (graph == NULL) {
return XT_ERR_INVALID_ARGUMENT;
}
if (source_node >= graph->node_count || destination_node >= graph->node_count) {
return XT_ERR_NOT_FOUND;
}
edge_capacity = graph->max_nodes * graph->max_nodes;
if (graph->edge_count >= edge_capacity) {
return XT_ERR_CAPACITY;
}
for (i = 0U; i < graph->edge_count; i++) {
if (graph->edges[i].source == source_node &&
graph->edges[i].destination == destination_node) {
return XT_ERR_INVALID_STATE;
}
}
graph->edges[graph->edge_count].source = source_node;
graph->edges[graph->edge_count].destination = destination_node;
graph->edge_count++;
return XT_OK;
}
xt_result xt_graph_validate(const xt_graph *graph)
{
size_t *indegree;
size_t *queue;
size_t head = 0U;
size_t tail = 0U;
size_t visited = 0U;
size_t i;
if (graph == NULL) {
return XT_ERR_INVALID_ARGUMENT;
}
if (graph->node_count == 0U) {
return XT_OK;
}
indegree = (size_t *)calloc(graph->node_count, sizeof(*indegree));
queue = (size_t *)calloc(graph->node_count, sizeof(*queue));
if (indegree == NULL || queue == NULL) {
free(indegree);
free(queue);
return XT_ERR_OUT_OF_MEMORY;
}
for (i = 0U; i < graph->edge_count; i++) {
if (graph->edges[i].source >= graph->node_count ||
graph->edges[i].destination >= graph->node_count) {
free(indegree);
free(queue);
return XT_ERR_INVALID_STATE;
}
indegree[graph->edges[i].destination]++;
}
for (i = 0U; i < graph->node_count; i++) {
if (indegree[i] == 0U) {
queue[tail++] = i;
}
}
size_t indegree[XTC_MAX_NODES] = {0U};
size_t head = 0U, tail = 0U, i;
if (graph == NULL || order == NULL) return XTC_ERR_INVALID_ARGUMENT;
for (i = 0U; i < graph->edge_count; ++i)
++indegree[graph->edges[i].destination];
for (i = 0U; i < graph->node_count; ++i)
if (indegree[i] == 0U) order[tail++] = i;
while (head < tail) {
size_t node = queue[head++];
visited++;
for (i = 0U; i < graph->edge_count; i++) {
size_t node = order[head++];
for (i = 0U; i < graph->edge_count; ++i) {
if (graph->edges[i].source == node) {
size_t dest = graph->edges[i].destination;
if (indegree[dest] > 0U) {
indegree[dest]--;
if (indegree[dest] == 0U) {
queue[tail++] = dest;
}
}
if (--indegree[dest] == 0U) order[tail++] = dest;
}
}
}
free(indegree);
free(queue);
return visited == graph->node_count ? XT_OK : XT_ERR_GRAPH_CYCLE;
return tail == graph->node_count ? XTC_OK : XTC_ERR_GRAPH_CYCLE;
}
xt_result xt_graph_process_offline(xt_graph *graph, const float *input,
float *output, size_t frame_count,
float input_gain)
xtc_result xtc_graph_validate(const xtc_graph *graph)
{
size_t i;
float cumulative_gain = input_gain;
if (graph == NULL || input == NULL || output == NULL) {
return XT_ERR_INVALID_ARGUMENT;
}
if (xt_graph_validate(graph) != XT_OK) {
return XT_ERR_INVALID_STATE;
}
/* Temporary starter behavior: apply gains in node insertion order.
This is not yet graph-plan execution and is only for offline tests. */
for (i = 0U; i < graph->node_count; i++) {
cumulative_gain *= graph->nodes[i].gain;
}
for (i = 0U; i < frame_count; i++) {
output[i] = input[i] * cumulative_gain;
}
return XT_OK;
size_t order[XTC_MAX_NODES];
return xtc_graph_order(graph, order);
}
xtc_result xtc_graph_connect_ports(xtc_graph *graph, size_t source_node,
size_t source_port, size_t destination_node,
size_t destination_port)
{
xtc_port_info source, dest;
size_t i;
xtc_result result = xtc_graph_get_port(graph, source_node, source_port, &source);
if (result != XTC_OK) return result;
result = xtc_graph_get_port(graph, destination_node, destination_port, &dest);
if (result != XTC_OK) return result;
if (source.direction != XTC_PORT_OUTPUT || dest.direction != XTC_PORT_INPUT ||
source.signal_type != dest.signal_type || source.channel_count != dest.channel_count)
return XTC_ERR_INVALID_ARGUMENT;
for (i = 0U; i < graph->edge_count; ++i)
if (graph->edges[i].source == source_node && graph->edges[i].destination == destination_node)
return XTC_ERR_INVALID_STATE;
if (graph->edge_count == graph->max_nodes * graph->max_nodes) return XTC_ERR_CAPACITY;
graph->edges[graph->edge_count].source = source_node;
graph->edges[graph->edge_count++].destination = destination_node;
result = xtc_graph_validate(graph);
if (result != XTC_OK) --graph->edge_count;
return result;
}
xtc_result xtc_graph_connect(xtc_graph *graph, size_t source_node, size_t destination_node)
{ return xtc_graph_connect_ports(graph, source_node, 1U, destination_node, 0U); }