Phase 4: Add modular graph editor and transactional graph edits
This commit is contained in:
parent
2ecc03b482
commit
5c41d7f6da
27 changed files with 1023 additions and 64 deletions
|
|
@ -35,13 +35,13 @@ xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate,
|
|||
{
|
||||
xtc_plan *plan;
|
||||
xtc_result result;
|
||||
size_t node, edge, cursor = 0U;
|
||||
size_t node, edge, storage_nodes, cursor = 0U;
|
||||
int has_output = 0;
|
||||
if (out_plan == NULL) return XTC_ERR_INVALID_ARGUMENT;
|
||||
*out_plan = NULL;
|
||||
if (graph == NULL || !isfinite(sample_rate) || sample_rate <= 0.0 ||
|
||||
max_frames == 0U || max_frames > XTC_MAX_FRAMES) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (graph->node_count == 0U) return XTC_ERR_INVALID_STATE;
|
||||
storage_nodes = graph->node_count == 0U ? 1U : graph->node_count;
|
||||
for (node = 0U; node < graph->node_count; ++node) {
|
||||
if (graph->nodes[node].type == XTC_NODE_OSCILLATOR &&
|
||||
(double)graph->nodes[node].value > sample_rate * 0.5)
|
||||
|
|
@ -55,14 +55,14 @@ xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate,
|
|||
plan->max_frames = max_frames;
|
||||
plan->sample_rate = sample_rate;
|
||||
plan->input_gain = 1.0f;
|
||||
plan->nodes = calloc(plan->node_count, sizeof(*plan->nodes));
|
||||
plan->order = calloc(plan->node_count, sizeof(*plan->order));
|
||||
plan->nodes = calloc(storage_nodes, sizeof(*plan->nodes));
|
||||
plan->order = calloc(storage_nodes, sizeof(*plan->order));
|
||||
plan->offsets = calloc(plan->node_count + 1U, sizeof(*plan->offsets));
|
||||
plan->sources = calloc(graph->edge_count + 1U, sizeof(*plan->sources));
|
||||
plan->render = calloc(plan->node_count, sizeof(*plan->render));
|
||||
plan->buffers = calloc(plan->node_count * max_frames, sizeof(*plan->buffers));
|
||||
plan->peaks = calloc(plan->node_count, sizeof(*plan->peaks));
|
||||
plan->phases = calloc(plan->node_count, sizeof(*plan->phases));
|
||||
plan->render = calloc(storage_nodes, sizeof(*plan->render));
|
||||
plan->buffers = calloc(storage_nodes * max_frames, sizeof(*plan->buffers));
|
||||
plan->peaks = calloc(storage_nodes, sizeof(*plan->peaks));
|
||||
plan->phases = calloc(storage_nodes, sizeof(*plan->phases));
|
||||
if (plan->nodes == NULL || plan->order == NULL || plan->offsets == NULL ||
|
||||
plan->sources == NULL || plan->render == NULL || plan->buffers == NULL ||
|
||||
plan->peaks == NULL || plan->phases == NULL) {
|
||||
|
|
@ -80,7 +80,7 @@ xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate,
|
|||
plan->sources[cursor++] = graph->edges[edge].source;
|
||||
if (graph->edges[edge].source == node) terminal = 0;
|
||||
}
|
||||
plan->render[node] = (unsigned char)(has_output ?
|
||||
plan->render[node] = (unsigned char)(has_output || graph->require_output_nodes ?
|
||||
plan->nodes[node].type == XTC_NODE_OUTPUT : terminal);
|
||||
}
|
||||
plan->offsets[plan->node_count] = cursor;
|
||||
|
|
@ -99,7 +99,11 @@ xtc_result xtc_plan_reset(xtc_plan *plan)
|
|||
xtc_result xtc_plan_meter_peak(const xtc_plan *plan, size_t node_id, float *out_peak)
|
||||
{
|
||||
if (plan == NULL || out_peak == NULL) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (node_id >= plan->node_count) return XTC_ERR_NOT_FOUND;
|
||||
size_t index;
|
||||
for (index = 0U; index < plan->node_count; ++index)
|
||||
if (plan->nodes[index].id == node_id) break;
|
||||
if (index == plan->node_count) return XTC_ERR_NOT_FOUND;
|
||||
node_id = index;
|
||||
if (plan->nodes[node_id].type != XTC_NODE_METER) return XTC_ERR_INVALID_ARGUMENT;
|
||||
*out_peak = plan->peaks[node_id];
|
||||
return XTC_OK;
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ struct xtc_realtime {
|
|||
xtc_plan *active; /* Audio owner only, until consumer is stopped/joined. */
|
||||
unsigned int blocks;
|
||||
unsigned int result;
|
||||
unsigned int submitted_revision, pending_revision, active_revision;
|
||||
};
|
||||
|
||||
xtc_result xtc_realtime_create(double sample_rate, size_t max_frames,
|
||||
|
|
@ -59,6 +60,11 @@ xtc_result xtc_realtime_submit(xtc_realtime *engine, const xtc_graph *graph)
|
|||
return XTC_ERR_CAPACITY;
|
||||
result = xtc_graph_compile(graph, engine->sample_rate, engine->max_frames, &plan);
|
||||
if (result != XTC_OK) return result;
|
||||
{
|
||||
unsigned int revision = __atomic_load_n(&engine->submitted_revision, __ATOMIC_RELAXED) + 1U;
|
||||
__atomic_store_n(&engine->pending_revision, revision, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&engine->submitted_revision, revision, __ATOMIC_RELAXED);
|
||||
}
|
||||
__atomic_store_n(&engine->pending, plan, __ATOMIC_RELEASE);
|
||||
return XTC_OK;
|
||||
}
|
||||
|
|
@ -68,6 +74,8 @@ xtc_result xtc_realtime_get_status(const xtc_realtime *engine,
|
|||
{
|
||||
if (engine == NULL || out_status == NULL) return XTC_ERR_INVALID_ARGUMENT;
|
||||
out_status->processed_blocks = __atomic_load_n(&engine->blocks, __ATOMIC_RELAXED);
|
||||
out_status->submitted_revision = __atomic_load_n(&engine->submitted_revision, __ATOMIC_RELAXED);
|
||||
out_status->active_revision = __atomic_load_n(&engine->active_revision, __ATOMIC_RELAXED);
|
||||
out_status->last_result = (xtc_result)__atomic_load_n(&engine->result, __ATOMIC_RELAXED);
|
||||
return XTC_OK;
|
||||
}
|
||||
|
|
@ -85,10 +93,15 @@ xtc_result xtc_realtime_process(xtc_realtime *engine, const float *input,
|
|||
/* A full retirement slot delays adoption without delaying processing.
|
||||
* acquire pairs with control's release exchange after its last read. */
|
||||
if (__atomic_load_n(&engine->retired, __ATOMIC_ACQUIRE) == NULL) {
|
||||
xtc_plan *next = __atomic_exchange_n(&engine->pending, NULL, __ATOMIC_ACQUIRE);
|
||||
xtc_plan *next = __atomic_load_n(&engine->pending, __ATOMIC_ACQUIRE);
|
||||
if (next != NULL) {
|
||||
xtc_plan *old = engine->active;
|
||||
/* Read revision BEFORE releasing the pending slot. The producer
|
||||
* cannot reuse pending_revision until its acquire observes NULL. */
|
||||
unsigned int revision = __atomic_load_n(&engine->pending_revision, __ATOMIC_RELAXED);
|
||||
(void)__atomic_exchange_n(&engine->pending, NULL, __ATOMIC_ACQ_REL);
|
||||
engine->active = next;
|
||||
__atomic_store_n(&engine->active_revision, revision, __ATOMIC_RELAXED);
|
||||
/* No reference to old is used after this release. */
|
||||
__atomic_store_n(&engine->retired, old, __ATOMIC_RELEASE);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue