Phase 23: refine routing inspection, signal-flow layout and atomic wire insertion
This commit is contained in:
parent
01d8c1d661
commit
ba5c2503a7
15 changed files with 533 additions and 22 deletions
|
|
@ -498,6 +498,14 @@ xtc_result xtc_graph_apply_edit(const xtc_graph *source, const xtc_graph_edit *e
|
|||
case XTC_EDIT_DISCONNECT:
|
||||
result = disconnect_ports(graph, &edit->data.connection);
|
||||
break;
|
||||
case XTC_EDIT_INSERT_GAIN: {
|
||||
const xtc_connection_info *c = &edit->data.connection;
|
||||
result = disconnect_ports(graph, c);
|
||||
if (result == XTC_OK) result = xtc_graph_add_gain(graph, 1.0f, &added_id);
|
||||
if (result == XTC_OK) result = xtc_graph_connect_ports(graph, c->source_node, c->source_port, added_id, 0);
|
||||
if (result == XTC_OK) result = xtc_graph_connect_ports(graph, added_id, 1, c->destination_node, c->destination_port);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
result = XTC_ERR_INVALID_ARGUMENT;
|
||||
break;
|
||||
|
|
@ -506,7 +514,7 @@ xtc_result xtc_graph_apply_edit(const xtc_graph *source, const xtc_graph_edit *e
|
|||
xtc_graph_destroy(graph);
|
||||
return result;
|
||||
}
|
||||
if (out_added_id != NULL && edit->kind == XTC_EDIT_ADD_NODE)
|
||||
if (out_added_id != NULL && (edit->kind == XTC_EDIT_ADD_NODE || edit->kind == XTC_EDIT_INSERT_GAIN))
|
||||
*out_added_id = added_id;
|
||||
*out_graph = graph;
|
||||
return XTC_OK;
|
||||
|
|
@ -588,3 +596,45 @@ xtc_result xtc_graph_set_plugin(xtc_graph *g, size_t id, const xtc_plugin_spec *
|
|||
memcmp(s->ports,g->nodes[i].plugin.ports,s->control_count*sizeof(*s->ports))) return XTC_ERR_UNSUPPORTED;
|
||||
g->nodes[i].plugin = *s; return XTC_OK;
|
||||
}
|
||||
|
||||
xtc_result xtc_graph_analyze(const xtc_graph *graph, xtc_routing_info *items, size_t capacity, size_t *count)
|
||||
{
|
||||
xtc_routing_info info[XTC_MAX_NODES] = {{0}};
|
||||
size_t order[XTC_MAX_NODES], i, j;
|
||||
int explicit_outputs = 0;
|
||||
xtc_result result;
|
||||
if (!graph || !count || (!items && graph->node_count)) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (capacity < graph->node_count) return XTC_ERR_CAPACITY;
|
||||
result = xtc_graph_order(graph, order);
|
||||
if (result != XTC_OK) return result;
|
||||
for (i = 0; i < graph->node_count; ++i) {
|
||||
info[i].id = graph->nodes[i].id;
|
||||
if (graph->nodes[i].type == XTC_NODE_OUTPUT) explicit_outputs = 1;
|
||||
}
|
||||
for (i = 0; i < graph->edge_count; ++i) {
|
||||
++info[graph->edges[i].source].outputs;
|
||||
++info[graph->edges[i].destination].inputs;
|
||||
}
|
||||
for (i = 0; i < graph->node_count; ++i) {
|
||||
xtc_port_info port;
|
||||
info[i].external_input = !info[i].inputs && xtc_graph_get_port(graph, info[i].id, 0, &port) == XTC_OK;
|
||||
info[i].reaches_output = graph->nodes[i].type == XTC_NODE_OUTPUT ||
|
||||
(!explicit_outputs && !graph->require_output_nodes && !info[i].outputs);
|
||||
}
|
||||
for (i = 0; i < graph->node_count; ++i) {
|
||||
size_t node = order[i];
|
||||
for (j = 0; j < graph->edge_count; ++j) if (graph->edges[j].source == node) {
|
||||
size_t destination = graph->edges[j].destination;
|
||||
if (info[destination].level <= info[node].level) info[destination].level = info[node].level + 1;
|
||||
}
|
||||
}
|
||||
for (i = graph->node_count; i > 0; --i) {
|
||||
size_t node = order[i-1];
|
||||
if (info[node].reaches_output)
|
||||
for (j = 0; j < graph->edge_count; ++j) if (graph->edges[j].destination == node)
|
||||
info[graph->edges[j].source].reaches_output = 1;
|
||||
}
|
||||
if (graph->node_count) memcpy(items, info, graph->node_count * sizeof(*items));
|
||||
*count = graph->node_count;
|
||||
return XTC_OK;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@
|
|||
#include "xtc/xtc_plugin.h"
|
||||
#include <FL/Fl.H>
|
||||
#include <FL/Fl_Box.H>
|
||||
#include <FL/Fl_Group.H>
|
||||
#include <FL/fl_draw.H>
|
||||
#include <FL/Fl_Menu_Item.H>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
|
|
@ -22,7 +24,7 @@ const char *node_name(xtc_node_type type)
|
|||
case XTC_NODE_OSCILLATOR:
|
||||
return "Oscillator";
|
||||
case XTC_NODE_GAIN:
|
||||
return "Gain";
|
||||
return "Gain / bus";
|
||||
case XTC_NODE_METER:
|
||||
return "Meter";
|
||||
case XTC_NODE_PLUGIN:
|
||||
|
|
@ -30,11 +32,11 @@ const char *node_name(xtc_node_type type)
|
|||
case XTC_NODE_NATIVE:
|
||||
return "Native processor";
|
||||
case XTC_NODE_INSTRUMENT:
|
||||
return "Instrument";
|
||||
return "MIDI mix";
|
||||
case XTC_NODE_AUDIO:
|
||||
return "Audio clips";
|
||||
return "Audio mix";
|
||||
case XTC_NODE_OUTPUT:
|
||||
return "Output";
|
||||
return "Output sum";
|
||||
}
|
||||
return "Unknown";
|
||||
}
|
||||
|
|
@ -75,6 +77,8 @@ GraphWidget::GraphWidget(int left, int top, int width, int height, const xtc_gra
|
|||
void GraphWidget::set_graph(const xtc_graph *graph)
|
||||
{
|
||||
graph_ = graph;
|
||||
routing_count_ = 0;
|
||||
(void)xtc_graph_analyze(graph_, routing_.data(), routing_.size(), &routing_count_);
|
||||
size_t nodes = 0U, edges = 0U;
|
||||
if (xtc_graph_get_counts(graph_, &nodes, &edges) != XTC_OK) {
|
||||
count_ = 0U;
|
||||
|
|
@ -106,6 +110,10 @@ void GraphWidget::set_graph(const xtc_graph *graph)
|
|||
if (std::abs(old[j].left - position.left) < node_width + 12 &&
|
||||
std::abs(old[j].top - position.top) < node_height + 12)
|
||||
occupied = true;
|
||||
for (size_t j = 0U; j < count_; ++j)
|
||||
if (std::abs(positions_[j].left - position.left) < node_width + 12 &&
|
||||
std::abs(positions_[j].top - position.top) < node_height + 12)
|
||||
occupied = true;
|
||||
if (!occupied)
|
||||
break;
|
||||
}
|
||||
|
|
@ -117,6 +125,7 @@ void GraphWidget::set_graph(const xtc_graph *graph)
|
|||
selected_node_ = SIZE_MAX;
|
||||
if (edge_selected_ && !select_connection(selected_edge_.source_node, selected_edge_.destination_node))
|
||||
edge_selected_ = false;
|
||||
grow_canvas();
|
||||
redraw();
|
||||
}
|
||||
|
||||
|
|
@ -174,6 +183,7 @@ bool GraphWidget::select_node(size_t id)
|
|||
return false;
|
||||
selected_node_ = id;
|
||||
edge_selected_ = false;
|
||||
describe_selection();
|
||||
redraw();
|
||||
return true;
|
||||
}
|
||||
|
|
@ -197,6 +207,7 @@ bool GraphWidget::select_connection(size_t source, size_t destination)
|
|||
selected_edge_ = edge;
|
||||
edge_selected_ = true;
|
||||
selected_node_ = SIZE_MAX;
|
||||
describe_selection();
|
||||
redraw();
|
||||
return true;
|
||||
}
|
||||
|
|
@ -219,8 +230,10 @@ void GraphWidget::feedback(xtc_result result, const char *success)
|
|||
std::snprintf(
|
||||
text, sizeof(text),
|
||||
"Graph unchanged: capacity exceeded (node limit or pending edit). Retry after the next audio block.");
|
||||
else if (result == XTC_ERR_GRAPH_CYCLE)
|
||||
std::snprintf(text, sizeof(text), "Graph unchanged: %s. Feedback loops are unsupported.", xtc_result_string(result));
|
||||
else
|
||||
std::snprintf(text, sizeof(text), "Graph unchanged: %s.", xtc_result_string(result));
|
||||
std::snprintf(text, sizeof(text), "Graph unchanged: %s. Connect an audio output (right) to an input (left); duplicate wires are rejected.", xtc_result_string(result));
|
||||
feedback_->copy_label(text);
|
||||
}
|
||||
|
||||
|
|
@ -282,7 +295,7 @@ xtc_result GraphWidget::delete_selected()
|
|||
return XTC_ERR_INVALID_STATE;
|
||||
}
|
||||
xtc_result result = handler_(context_, edit, nullptr);
|
||||
feedback(result, "Selection deleted.");
|
||||
feedback(result, "Selection deleted. Unwired inputs use external audio; select a node to inspect routing.");
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -348,11 +361,21 @@ void GraphWidget::draw()
|
|||
if (xtc_graph_get_connection(graph_, i, &edge) != XTC_OK || !port_position(edge.source_node, 1U, sx, sy) ||
|
||||
!port_position(edge.destination_node, 0U, dx, dy))
|
||||
continue;
|
||||
bool selected = edge_selected_ && selected_edge_.source_node == edge.source_node &&
|
||||
selected_edge_.destination_node == edge.destination_node;
|
||||
fl_color(selected ? FL_YELLOW : fl_rgb_color(100, 200, 190));
|
||||
bool selected = (edge_selected_ && selected_edge_.source_node == edge.source_node &&
|
||||
selected_edge_.destination_node == edge.destination_node) ||
|
||||
selected_node_ == edge.source_node || selected_node_ == edge.destination_node;
|
||||
xtc_routing_info route{};
|
||||
bool audible_path = routing(edge.destination_node, route) && route.reaches_output;
|
||||
fl_color(selected ? FL_YELLOW : audible_path ? fl_rgb_color(100, 200, 190) : fl_rgb_color(110, 115, 125));
|
||||
fl_line_style(FL_SOLID, selected ? 3 : 2);
|
||||
fl_line(x() + sx, y() + sy, x() + dx, y() + dy);
|
||||
double length = std::hypot(dx-sx, dy-sy);
|
||||
if (length > 20) {
|
||||
double ux = (dx-sx)/length, uy = (dy-sy)/length;
|
||||
int ax = x() + (sx+dx)/2, ay = y() + (sy+dy)/2;
|
||||
fl_line(ax, ay, ax-static_cast<int>(ux*9-uy*5), ay-static_cast<int>(uy*9+ux*5));
|
||||
fl_line(ax, ay, ax-static_cast<int>(ux*9+uy*5), ay-static_cast<int>(uy*9-ux*5));
|
||||
}
|
||||
}
|
||||
fl_line_style(0);
|
||||
for (size_t i = 0U; i < count_; ++i) {
|
||||
|
|
@ -360,7 +383,10 @@ void GraphWidget::draw()
|
|||
if (xtc_graph_get_node(graph_, i, &node) != XTC_OK)
|
||||
continue;
|
||||
int left = x() + positions_[i].left, top = y() + positions_[i].top;
|
||||
fl_color(fl_rgb_color(49, 54, 62));
|
||||
fl_color(node.type == XTC_NODE_OUTPUT ? fl_rgb_color(100, 50, 55)
|
||||
: node.type == XTC_NODE_GAIN ? fl_rgb_color(75, 55, 95)
|
||||
: node.type == XTC_NODE_OSCILLATOR || node.type == XTC_NODE_AUDIO || node.type == XTC_NODE_INSTRUMENT
|
||||
? fl_rgb_color(35, 80, 75) : fl_rgb_color(49, 65, 85));
|
||||
fl_rectf(left, top, node_width, node_height);
|
||||
fl_color(selected_node_ == node.id ? FL_YELLOW : fl_rgb_color(115, 130, 145));
|
||||
fl_rect(left, top, node_width, node_height);
|
||||
|
|
@ -368,6 +394,7 @@ void GraphWidget::draw()
|
|||
fl_font(FL_HELVETICA_BOLD, 12);
|
||||
char text[96];
|
||||
std::snprintf(text, sizeof(text), "%s #%zu", node_name(node.type), node.id);
|
||||
fl_push_clip(left+6, top+2, node_width-12, 40);
|
||||
fl_draw(text, left + 10, top + 20);
|
||||
fl_font(FL_HELVETICA, 12);
|
||||
if (node.type == XTC_NODE_OSCILLATOR)
|
||||
|
|
@ -383,8 +410,14 @@ void GraphWidget::draw()
|
|||
node.type == XTC_NODE_METER ? "Peak pass-through"
|
||||
: node.type == XTC_NODE_INSTRUMENT ? "MIDI sine source"
|
||||
: node.type == XTC_NODE_AUDIO ? "Timeline mix source"
|
||||
: "Mono audio output");
|
||||
: "Summed engine output");
|
||||
fl_draw(text, left + 10, top + 37);
|
||||
fl_pop_clip();
|
||||
xtc_routing_info route{};
|
||||
if (routing(node.id, route) && !route.reaches_output) {
|
||||
fl_color(FL_YELLOW);
|
||||
fl_draw("! no output path", left + 10, top + 72);
|
||||
}
|
||||
for (size_t port = 0U; port < 2U; ++port) {
|
||||
int px, py;
|
||||
if (!port_position(node.id, port, px, py))
|
||||
|
|
@ -392,7 +425,7 @@ void GraphWidget::draw()
|
|||
fl_color(fl_rgb_color(110, 220, 185));
|
||||
fl_pie(x() + px - 5, y() + py - 5, 10, 10, 0, 360);
|
||||
fl_color(FL_WHITE);
|
||||
fl_draw(port == 0U ? "in" : "out", left + (port == 0U ? 10 : 118), top + 62);
|
||||
fl_draw(port == 0U ? (route.external_input ? "ext in" : "sum in") : "audio out", left + (port == 0U ? 10 : 90), top + 56);
|
||||
}
|
||||
}
|
||||
if (wiring_) {
|
||||
|
|
@ -420,8 +453,20 @@ int GraphWidget::handle(int event)
|
|||
case FL_UNFOCUS:
|
||||
return 1;
|
||||
case FL_PUSH: {
|
||||
if (Fl::event_button() != FL_LEFT_MOUSE)
|
||||
return 0;
|
||||
if (Fl::event_button() == FL_RIGHT_MOUSE) {
|
||||
Fl_Menu_Item items[5]{};
|
||||
items[0].text = "Inspect selection";
|
||||
items[1].text = "Arrange by signal flow (L)";
|
||||
items[2].text = "Insert unity gain in selected wire (I)";
|
||||
items[3].text = "Delete selection";
|
||||
const auto *picked = items->popup(Fl::event_x(), Fl::event_y());
|
||||
if (picked == items) describe_selection();
|
||||
else if (picked == items+1) auto_layout();
|
||||
else if (picked == items+2) insert_gain();
|
||||
else if (picked == items+3) delete_selected();
|
||||
return 1;
|
||||
}
|
||||
if (Fl::event_button() != FL_LEFT_MOUSE) return 0;
|
||||
take_focus();
|
||||
mouse_x_ = left;
|
||||
mouse_y_ = top;
|
||||
|
|
@ -485,6 +530,16 @@ int GraphWidget::handle(int event)
|
|||
}
|
||||
return 1;
|
||||
case FL_KEYDOWN:
|
||||
if (Fl::event_key() == 'l' && !Fl::event_state(FL_CTRL | FL_ALT)) { auto_layout(); return 1; }
|
||||
if (Fl::event_key() == 'i' && !Fl::event_state(FL_CTRL | FL_ALT)) { insert_gain(); return 1; }
|
||||
if ((Fl::event_key() == FL_Left || Fl::event_key() == FL_Right) && count_) {
|
||||
size_t selected = 0;
|
||||
for (size_t i = 0; i < count_; ++i) if (positions_[i].id == selected_node_) {
|
||||
selected = Fl::event_key() == FL_Left ? (i+count_-1)%count_ : (i+1)%count_; break;
|
||||
}
|
||||
select_node(positions_[selected].id);
|
||||
return 1;
|
||||
}
|
||||
if (Fl::event_key() == FL_Delete || Fl::event_key() == FL_BackSpace) {
|
||||
delete_selected();
|
||||
return 1;
|
||||
|
|
@ -500,3 +555,70 @@ int GraphWidget::handle(int event)
|
|||
}
|
||||
return Fl_Widget::handle(event);
|
||||
}
|
||||
|
||||
bool GraphWidget::routing(size_t id, xtc_routing_info &info) const
|
||||
{
|
||||
for (size_t i = 0; i < routing_count_; ++i) if (routing_[i].id == id) { info = routing_[i]; return true; }
|
||||
return false;
|
||||
}
|
||||
|
||||
void GraphWidget::grow_canvas()
|
||||
{
|
||||
int width = 2000, height = 4000;
|
||||
for (size_t i = 0; i < count_; ++i) {
|
||||
width = std::max(width, positions_[i].left + node_width + 40);
|
||||
height = std::max(height, positions_[i].top + node_height + 40);
|
||||
}
|
||||
size(width, height);
|
||||
if (parent()) parent()->redraw();
|
||||
}
|
||||
|
||||
bool GraphWidget::auto_layout()
|
||||
{
|
||||
if (routing_count_ != count_) return false;
|
||||
std::array<size_t, 256> rows{};
|
||||
for (size_t i = 0; i < count_; ++i) {
|
||||
xtc_routing_info info{};
|
||||
if (!routing(positions_[i].id, info) || info.level >= rows.size()) return false;
|
||||
positions_[i].left = 40 + static_cast<int>(info.level) * 210;
|
||||
positions_[i].top = 40 + static_cast<int>(rows[info.level]++) * 115;
|
||||
}
|
||||
wiring_ = dragging_ = false;
|
||||
grow_canvas();
|
||||
feedback_->copy_label("Arranged left to right by signal flow. Layout is session-only; no audio or routing changes.");
|
||||
redraw();
|
||||
return true;
|
||||
}
|
||||
|
||||
xtc_result GraphWidget::insert_gain()
|
||||
{
|
||||
if (!edge_selected_) {
|
||||
feedback_->copy_label("Select a wire first, then insert a unity gain/bus node.");
|
||||
return XTC_ERR_INVALID_STATE;
|
||||
}
|
||||
xtc_graph_edit edit{};
|
||||
edit.kind = XTC_EDIT_INSERT_GAIN;
|
||||
edit.data.connection = selected_edge_;
|
||||
size_t added = SIZE_MAX;
|
||||
xtc_result result = handler_(context_, edit, &added);
|
||||
if (result == XTC_OK) select_node(added);
|
||||
feedback(result, "Unity gain inserted into wire as one edit. Inputs sum; fan-out copies the signal without normalization.");
|
||||
return result;
|
||||
}
|
||||
|
||||
void GraphWidget::describe_selection()
|
||||
{
|
||||
char text[320];
|
||||
if (edge_selected_) {
|
||||
std::snprintf(text, sizeof(text), "Wire: #%zu audio out -> #%zu sum in. Delete disconnects; I inserts a unity gain. Destination sums its inputs.",
|
||||
selected_edge_.source_node, selected_edge_.destination_node);
|
||||
} else {
|
||||
xtc_routing_info info{};
|
||||
if (!routing(selected_node_, info)) { feedback_->copy_label("Select a node or wire to inspect routing."); return; }
|
||||
std::snprintf(text, sizeof(text), "Node #%zu: %zu incoming, %zu outgoing | %s | %s. Ports are audio lanes; stereo uses paired independent processing.",
|
||||
info.id, info.inputs, info.outputs,
|
||||
info.reaches_output ? "path to engine output" : "NO output path",
|
||||
info.external_input ? "unwired input uses external audio" : "wired sum or source");
|
||||
}
|
||||
feedback_->copy_label(text);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -80,6 +80,10 @@ class GraphWidget : public Fl_Widget {
|
|||
* @return Result reported by the edit handler.
|
||||
*/
|
||||
xtc_result add_node(xtc_node_type type);
|
||||
xtc_result insert_gain();
|
||||
bool auto_layout(); // Presentation only, no engine publication or undo step.
|
||||
bool routing(size_t id, xtc_routing_info &info) const;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Requests a connection between two ports through the edit handler.
|
||||
|
|
@ -156,7 +160,10 @@ class GraphWidget : public Fl_Widget {
|
|||
};
|
||||
|
||||
std::array<Position, 256> positions_{};
|
||||
size_t count_ = 0U;
|
||||
size_t count_ = 0U, routing_count_ = 0U;
|
||||
std::array<xtc_routing_info, 256> routing_{};
|
||||
void describe_selection();
|
||||
void grow_canvas();
|
||||
const xtc_graph *graph_;
|
||||
EditHandler handler_;
|
||||
void *context_;
|
||||
|
|
|
|||
|
|
@ -217,6 +217,8 @@ MainWindow::MainWindow(bool prompt_unsaved)
|
|||
auto *scroll = new Fl_Scroll(20, 212, 956, 328);
|
||||
graph_widget_ = new GraphWidget(20, 212, 2000, 4000, xtc_project_graph(project_.get()), edit_graph, this, feedback);
|
||||
scroll->end();
|
||||
menu->add("View/Arrange graph by signal flow", 0, [](Fl_Widget *, void *c) { (void)static_cast<GraphWidget *>(c)->auto_layout(); }, graph_widget_);
|
||||
menu->add("Edit/Insert unity gain in selected wire", 0, [](Fl_Widget *, void *c) { (void)static_cast<GraphWidget *>(c)->insert_gain(); }, graph_widget_);
|
||||
auto *oscillator = new Fl_Button(20, 150, 130, 26, "Add oscillator");
|
||||
oscillator->callback(
|
||||
[](Fl_Widget *, void *context) {
|
||||
|
|
@ -363,7 +365,7 @@ xtc_result MainWindow::edit_graph(void *context, const xtc_graph_edit &edit, siz
|
|||
window->graph_widget_->set_graph(xtc_project_graph(replacement));
|
||||
window->sequencer_->set_project(replacement, false);
|
||||
window->mark_dirty();
|
||||
if (out_added_id != nullptr && edit.kind == XTC_EDIT_ADD_NODE)
|
||||
if (out_added_id != nullptr && (edit.kind == XTC_EDIT_ADD_NODE || edit.kind == XTC_EDIT_INSERT_GAIN))
|
||||
*out_added_id = added_id;
|
||||
window->refresh_status();
|
||||
return XTC_OK;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue