# Node graph clarifications

**URL:** <https://forum.juce.com/t/node-graph-clarifications/56170>\
**Category:** Tracktion Engine\
**Created:** [May 11, 2023, 12:33am UTC](https://forum.juce.com/t/node-graph-clarifications/56170 "2023-05-11T00:33:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ayra](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ayra/32/5775_2.png) [@Ayra](https://forum.juce.com/u/Ayra)\
**Post date:** [May 11, 2023, 12:33am UTC](https://forum.juce.com/t/node-graph-clarifications/56170/1 "2023-05-11T00:33:14Z")

</div>

Hi, I’m trying to understand tracktion::graph, my main goal is to recreate something like AudioPluginHost with node graph (not importing all tracktion::engine) to exploit the potential of multithreading.

Up to now it seems me that each node is something like an AudioProcessor and that an AudioProcessorGraph is a node (that can be converted to a NodeGraph), right?

if it’s so how to represent an AudioPluginInstance with a tracktion::graph::Node? how to attach a player to a DeviceManagerCallback?

I can’t find a stat point to understand how to exploit tracktion::graph for this purpose… I’m all wrong or my vision is right? There is some possible dialogue between tracktion::graph::Node and an AudioProcessor? Maybe the instance of AudioPluginInstance is something I need to wrap in a subclass of tracktion::graph::Node as decorator?

my idea of working as start point (pseudoCode):

```auto
    sineNode.reset(new SinNode{220});
    nodePlayer.reset(new SimpleNodePlayer{std::move(sineNode), sampleRate, blockSize});
    
    deviceManager.initialise(2, 2, nullptr, true);
    deviceManager.addAudioCallback (&nodePlayer);

```

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [May 11, 2023, 10:59am UTC](https://forum.juce.com/t/node-graph-clarifications/56170/2 "2023-05-11T10:59:04Z")

</div>

It’s a bit more complicated than that.

`tracktion::graph` is lower level than `juce::AudioProcessorGraph`. You build up a graph of `Node`s to represent your processing graph similar to `tracktion_EditNodeBuilder.cpp:createNodeForEdit` then hand that to a NodePlayer for playing.

There’s no automatic connection to a device manager or plugin instance etc. You’d have to wrap that yourself and process the player by manually calling its `process` function.

If you want to process the graph in a multi-threaded way, you need to use a `MultiThreadedNodePlayer` or `LockFreeMultiThreadedNodePlayer` to play it back.

---

<div class="post-metadata">

**Author:** ![Ayra](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ayra/32/5775_2.png) [@Ayra](https://forum.juce.com/u/Ayra)\
**Post date:** [May 11, 2023, 11:20am UTC](https://forum.juce.com/t/node-graph-clarifications/56170/3 "2023-05-11T11:20:22Z")

</div>

> [@dave96](#):
>
> tracktion\_EditNodeBuilder.cpp

Ok so, looking around code (before use those stuffs for my purpose) an AudioPluginInstance in tracktion is something more similar to PluginNode?

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [May 11, 2023, 11:47am UTC](https://forum.juce.com/t/node-graph-clarifications/56170/4 "2023-05-11T11:47:05Z")

</div>

Yes, but that’s very much a `tracktion::engine` concept as it wraps a `te::Plugin`.

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [May 11, 2023, 2:40pm UTC](https://forum.juce.com/t/node-graph-clarifications/56170/5 "2023-05-11T14:40:02Z")

</div>

The Tracktion graph module has just a few utility type of nodes built-in. If you want to host plugins without getting a dependency on the full the Tracktion Engine, you need to write your own Tracktion graph node subclass that wraps a juce::AudioPluginInstance. Things might be easier in the end if you just use the full engine even if you don’t need all the features it has. (But could be an interesting programming exercise to do the custom plugin hosting node, of course…)

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [May 11, 2023, 2:55pm UTC](https://forum.juce.com/t/node-graph-clarifications/56170/6 "2023-05-11T14:55:39Z")

</div>

What I always envisioned was a wrapper around a `juce::AudioProcessorGraph` that uses the topology of that and the plugins but converts the playback to use `tracktion::graph`. That would mean TG basically replaces the internal `juce::GraphRenderSequence`.

I think its doable but if it’s worthwhile is another question. You’d have to have a pretty big and complex graph to get decent utilisation of multiple cores.

---

<div class="post-metadata">

**Author:** ![Ayra](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ayra/32/5775_2.png) [@Ayra](https://forum.juce.com/u/Ayra)\
**Post date:** [May 12, 2023, 2:09am UTC](https://forum.juce.com/t/node-graph-clarifications/56170/7 "2023-05-12T02:09:19Z")

</div>

Does this make any sense?

```auto
using namespace tracktion::graph;

static MidiBuffer toMidiBuffers(tracktion_engine::MidiMessageArray& midiMessageArray)
{
    MidiBuffer midiMessages;
    
    for (int i = 0; i < midiMessageArray.size(); i++)
    {
        midiMessages.addEvent(midiMessageArray[i], i);
    }
    
    return midiMessages;
}

class NodeWrapper : public Node
{
    
public:
    
    NodeWrapper(std::unique_ptr<AudioProcessor> audioProcessor) : processor(std::move(audioProcessor)) {}
    
    std::vector<Node*> getDirectInputNodes() override { return inputNodes; }
    
    NodeProperties getNodeProperties() override
    {
        NodeProperties props;
        
        if (processor.get())
        {
            props.hasAudio = !processor->isMidiEffect();
            props.hasMidi = processor->acceptsMidi() || processor->producesMidi();
            props.numberOfChannels = processor->getTotalNumOutputChannels();
            props.nodeID = nodeID;
        }
        
        return props;
    }
    
    bool isReadyToProcess() override { return processor.get() ? true : false; }
    
    void prepareToPlay (const PlaybackInitialisationInfo& infos) override
    {
        jassert(processor.get());
        processor->prepareToPlay(infos.sampleRate, infos.blockSize);
    }
    
    void process (ProcessContext& pc) override
    {
        jassert(processor.get());
        
        auto audioBuffer = toAudioBuffer(pc.buffers.audio);
        auto midiBuffer = toMidiBuffers(pc.buffers.midi);
        
        processor->processBlock(audioBuffer, midiBuffer);
    }
    
private:

    std::unique_ptr<AudioProcessor> processor;
    size_t nodeID = 0; // MARK: 🔶 With AudioProcessorGraph it was managed by the graph ... Here?
    std::vector<Node*> inputNodes; // MARK: 🔶 Who will tell this to the node?
};

```

And if yes: I could process it with **LockFreeMultiThreadedNodePlayer**? and how can I pass the buffer the juce Device Manager to be rendered?

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [May 12, 2023, 2:17am UTC](https://forum.juce.com/t/node-graph-clarifications/56170/8 "2023-05-12T02:17:01Z")

</div>

It’s somewhat in the right direction but unfortunately needs quite a bit more work…You need to store the Node’s input node or nodes in your custom node, for starters. How the graph structure works in the Tracktion graph is a bit peculiar…I just today started looking more into it all and things can get a bit hairy. (I also need custom nodes for plugins etc, I can’t use the full Tracktion Engine for my purposes.)

---

<div class="post-metadata">

**Author:** ![Ayra](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ayra/32/5775_2.png) [@Ayra](https://forum.juce.com/u/Ayra)\
**Post date:** [May 12, 2023, 2:38am UTC](https://forum.juce.com/t/node-graph-clarifications/56170/9 "2023-05-12T02:38:34Z")

</div>

Ok… I don’t understand yet how connect nodes in trackion::graph: my approach, and what I’d like to achieve with some helper classes, is to connect and manage my nodes as AudioProcessorGraph actually does, so up to now I immagine to have a NodesManager that gives an abstract idea of Graph who actually has some func called connectNodeChannel or addNode ecc that at some point fills the InputSources of each WrapperNode (with nodes connected to this one) and gives it an unique id.

AudioProcessorGraph has IOAudio input IOAudio output and same for midi to pass data to audioprocessoraplayer and so to the devicemanager: Maybe customizing those classes I can pass and get data to the main node passed to MultithreadNodePlayer?

---

<div class="post-metadata">

**Author:** ![Ayra](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ayra/32/5775_2.png) [@Ayra](https://forum.juce.com/u/Ayra)\
**Post date:** [May 12, 2023, 2:45am UTC](https://forum.juce.com/t/node-graph-clarifications/56170/10 "2023-05-12T02:45:17Z")

</div>

Yes… Using all tracktion engine is really useless also for mine… Basically I want to recreate Audiopluginhost but I want to manage nodes in multithread way, so… “a node graph with in some nodes” 😅

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [May 12, 2023, 8:50am UTC](https://forum.juce.com/t/node-graph-clarifications/56170/11 "2023-05-12T08:50:07Z")

</div>

> [@xenakios](#):
>
> How the graph structure works in the Tracktion graph is a bit peculiar…I just today started looking more into it all and things can get a bit hairy.

Can you elaborate on this a bit please? There might be ways I can improve the API or examples.

It was mainly developed to run the Tracktion Engine processing so isn’t the best fleshed out for other uses just yet. It can definitely be used for other tasks though. All the tests are good examples and don’t use Engine.

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [May 12, 2023, 11:43am UTC](https://forum.juce.com/t/node-graph-clarifications/56170/12 "2023-05-12T11:43:45Z")

</div>

I guess it’s just about me having to wrap my head around the concept of having to rebuild the graph when the signal routing needs to change. And avoid pitfalls like what Ayra was getting into : in the code he/she posted a unique\_ptr to a juce AudioProcessor was taken into the node. That would not work with heavy to construct/destruct 3rd party plugins. It’s not too uncommon plugins may take even several seconds to construct/destruct, so that should be avoided at all costs if not absolutely necessary. So, lifetimes of things like plugins need to be handled somehow separately of the Tracktion graph node lifetimes, I would assume.

There’s also the additional problem I have to make this all work with Clap plugins, so I also need to take into consideration stuff relating to that. Wanting to use Clap plugins with the special features they make possible was the main reason it didn’t look feasible to use the full Tracktion engine.

---

<div class="post-metadata">

**Author:** ![Ayra](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ayra/32/5775_2.png) [@Ayra](https://forum.juce.com/u/Ayra)\
**Post date:** [May 12, 2023, 12:16pm UTC](https://forum.juce.com/t/node-graph-clarifications/56170/13 "2023-05-12T12:16:31Z")

</div>

m… But calling this (were **nodeWrapperContainer** is an OwnedArray that collect instances of **WrapperNode** ):

```auto
formatManager.createPluginInstanceAsync (desc.pluginDescription,
                                             sampleRate,
                                             blockSize,
                                             [this] (std::unique_ptr<AudioPluginInstance> instance, const String& error)
                                             { nodeWrapperContainer.add(new NodeWrapper(std::move (instance))) });

```

Cannot be a solution to be sure that the 3rd party plugin has been finished to be instantiated?

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [May 12, 2023, 12:39pm UTC](https://forum.juce.com/t/node-graph-clarifications/56170/14 "2023-05-12T12:39:22Z")

</div>

Yes, I think the main problem here is that people expect `tracktion::graph` to be of a similar abstraction level as `juce::AudioProcessorGraph`, it’s not, it’s much more like the internal `juce::GraphRenderSequence` as I mentioned above.

The way `graph` gets its speed is by being a static graph (or set of Nodes if you will), these are also relatively lightweight (most just contain some smart pointers to some data and a couple of allocated audio/MIDI buffers).  
Persistent data (plugins, automation, playheads, time-stretchers etc.) are stored with smart pointers and not re-created when the graph rebuilds.  
I spoke about this at length in this ADC talk:

[![](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/b/b4c1ffecbedae4415ac13661e5fdb45a257275fb.jpeg "Introducing Tracktion Graph: A Topological Processing Library for Audio - Dave Rowland - ADC20") ](https://www.youtube.com/watch?v=Mkz908eP_4g)

We do have a higher level concept in Tracktion Engine called Racks which are more similar to `juce::AudioProcessorGraph`. In my opinion separating the model (plugins and their connections) from how they are processed was one of the great wins of the `tracktion::graph` project. It makes things much easier to manipulate and optimise.

* * *

I think the bigger question with Clap plugins is: what unique features of them do you need? That might help drive the implementation decisions.

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [May 12, 2023, 12:48pm UTC](https://forum.juce.com/t/node-graph-clarifications/56170/15 "2023-05-12T12:48:14Z")

</div>

No, you need to keep your plugin instances completely separate from the graph. They can only be _referenced_ by the graph, not _owned_ by it.

Take a look at how `tracktion::engine::ExternalPlugin` owns the `juce::AudioPluginInstance` but the `PluginNode` of the graph just takes a reference to that `Plugin`.

* * *

I don’t want to speak out of turn but this stuff is extremely complex. It’s taken me many years of working on a DAW engine to properly grasp how all this ownership and processing semantics should work. There’s a lot of very tricky problems to solve. tracktion::engine does all these by utilising tracktion::graph internally.

It’s going to take a lot of work to replicate what tracktion::engine does (playhead, tempo changes, automation etc.) if you start with tracktion::graph (the graph construction alone is a couple of thousand lines of code).

I’d think about your _product_ first and then if tracktion::graph is the most efficient use of time. It could be that just using `juce::AudioProcessorGraph` single threaded is just fine for your use case. Or it could be that just using tracktion::engine is fine, there’s some overhead to it but not loads and tons of benefit.

It’s just difficult to know what advise to give without knowing what exactly people are building.

---

<div class="post-metadata">

**Author:** ![Ayra](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ayra/32/5775_2.png) [@Ayra](https://forum.juce.com/u/Ayra)\
**Post date:** [May 12, 2023, 12:56pm UTC](https://forum.juce.com/t/node-graph-clarifications/56170/16 "2023-05-12T12:56:42Z")

</div>

Yes… But juce::AudioProcessorGraph is perfect for my purposes… But it works in single thread (for what I understand…) so I can’t process the mum of plugins I need in my scenario… There’s a way to make AudioProcessorGraph multithread? (or maybe instantiate multiple AudioProcessorGraph and give to each one a separate thread?)

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [May 12, 2023, 1:33pm UTC](https://forum.juce.com/t/node-graph-clarifications/56170/17 "2023-05-12T13:33:31Z")

</div>

> [@dave96](#):
>
> I think the bigger question with Clap plugins is: what unique features of them do you need?

The most crucial one is the unified input and output event queues for the plugins that handle automation/modulation and note events (which are not completely equivalent to traditional MIDI events).

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [May 12, 2023, 5:57pm UTC](https://forum.juce.com/t/node-graph-clarifications/56170/18 "2023-05-12T17:57:52Z")

</div>

Thats really a question for you but it seems a bit of a strange one. Yes, a fully multi-threaded system will likely outperform a single threaded one but what systems will your product be running on?

- Is it a private tool, a commercial product etc.
- What plugins will be running? Are they your own ones or any 3rd party plugin?
- How will you know how many plugins users need to run? Have you benchmarked a single-threaded system (i.e. using `juce::AudioProcessorGraph `)?
- Is this a plugin or a stand-alone product? If a plugin, what host will it be run in? Is that multi-threaded?

I don’t mean to be discouraging, it’s just that if you have a limited amount of time and resources, finding the quickest route to an acceptable solution is often the better path. If this is the start of a much bigger, longer term project however then I can see the value in user a lower level library like Tracktion Graph.

Just my 2 cents

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [May 12, 2023, 6:01pm UTC](https://forum.juce.com/t/node-graph-clarifications/56170/19 "2023-05-12T18:01:09Z")

</div>

> [@xenakios](#):
>
> The most crucial one is the unified input and output event queues for the plugins that handle automation/modulation and note events (which are not completely equivalent to traditional MIDI events).

Ok, that’s not something I’ve really looked in to but presumably you’re tying this in to a larger project model with a concept of these events and then want to generate event queues to pass to the Clap plugins?

I think if this was me, and you’re building a DAW-like-thing (i.e. with a timeline and events), I’d probably fork Engine and add the capability rather than trying to build the DAW from scratch around Tracktion Graph. But again, it’s hard to know without having some kind spec or at least feature set to look at.

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [May 12, 2023, 7:20pm UTC](https://forum.juce.com/t/node-graph-clarifications/56170/20 "2023-05-12T19:20:47Z")

</div>

I have already spent months on other aspects of the software, the event sequencing stuff works reasonably well and was designed from the beginning to support the needed Clap specific things. Just haven’t been happy with the primitive and hacky single threaded audio graph I made for it. The whole audio etc routing stuff was bit of an afterthought, I initially thought I could get by just having a single Clap instrument hosted for the whole software… 😅 (It’s not a traditional DAW really.)

Anyway, I already got quite far with the custom Tracktion graph stuff too. Haven’t yet tested with actual Clap plugins or if there would be problems when using the multithreaded players. I guess it’s still a possibility to fork Engine too, but I will have to see about that.

[Next page](https://forum.juce.com/t/node-graph-clarifications/56170.md?page=2)
