From 963ccd2e678b753de93578963de96a2c1d3814d9 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Sun, 27 Oct 2024 12:01:52 +0000 Subject: [PATCH] AudioGraph: Convert to a singleton, and generate a warning if multiple AudioGraphs are created --- source/src/python/graph.cpp | 48 +++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/source/src/python/graph.cpp b/source/src/python/graph.cpp index e1ff39c1..4e16f51e 100644 --- a/source/src/python/graph.cpp +++ b/source/src/python/graph.cpp @@ -1,19 +1,47 @@ #include "signalflow/python/python.h" +void graph_created_warning() +{ + std::cerr << "AudioGraph: The global audio graph has already been created. To create a new graph, call .destroy() first." << std::endl; +} + void init_python_graph(py::module &m) { /*-------------------------------------------------------------------------------- - * Graph + * AudioGraph. + * + * This class is a singleton, which is handled by this block of constructors. + * If a shared_graph already exists, return it, with a warning. *-------------------------------------------------------------------------------*/ - py::class_(m, "AudioGraph", "The global audio signal processing graph") - .def(py::init(), "config"_a = nullptr, "output_device"_a = nullptr, - "start"_a = true) - .def(py::init(), "config"_a = nullptr, "output_device"_a = "", - "start"_a = true) - .def(py::init(), "config_name"_a = nullptr, "output_device"_a = nullptr, - "start"_a = true) - .def(py::init(), "config_name"_a = nullptr, "output_device"_a = "", - "start"_a = true) + py::class_>(m, "AudioGraph", "The global audio signal processing graph") + .def(py::init<>([](AudioGraphConfig *config, NodeRef output_device, bool start) { + AudioGraph *graph = AudioGraph::get_shared_graph(); + if (graph) + graph_created_warning(); + return graph ? graph : new AudioGraph(config, output_device, start); + }), + "config"_a = nullptr, "output_device"_a = nullptr, "start"_a = true) + .def(py::init<>([](AudioGraphConfig *config, std::string output_device, bool start) { + AudioGraph *graph = AudioGraph::get_shared_graph(); + if (graph) + graph_created_warning(); + return graph ? graph : new AudioGraph(config, output_device, start); + }), + "config"_a = nullptr, "output_device"_a = nullptr, "start"_a = true) + .def(py::init<>([](std::string config_name, NodeRef output_device, bool start) { + AudioGraph *graph = AudioGraph::get_shared_graph(); + if (graph) + graph_created_warning(); + return graph ? graph : new AudioGraph(config_name, output_device, start); + }), + "config"_a = nullptr, "output_device"_a = nullptr, "start"_a = true) + .def(py::init<>([](std::string config_name, NodeRef output_device, bool start) { + AudioGraph *graph = AudioGraph::get_shared_graph(); + if (graph) + graph_created_warning(); + return graph ? graph : new AudioGraph(config_name, output_device, start); + }), + "config"_a = nullptr, "output_device"_a = nullptr, "start"_a = true) .def_static("get_shared_graph", &AudioGraph::get_shared_graph) /*--------------------------------------------------------------------------------