Skip to content

Commit

Permalink
NodeConnection presets relative loading
Browse files Browse the repository at this point in the history
  • Loading branch information
benkuper committed Nov 28, 2024
1 parent a4f3206 commit 9e49380
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
35 changes: 34 additions & 1 deletion Source/Node/Connection/NodeConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ void NodeAudioConnection::connectChannels(int sourceChannel, int destChannel)
else jassertfalse;
}

if (connectionExists(sourceChannel, destChannel)) return;

if (sourceChannel >= sourceNode->getNumAudioOutputs() || destChannel >= destNode->getNumAudioInputs() || sourceChannel < 0 || destChannel < 0)
{
Expand All @@ -219,6 +220,7 @@ void NodeAudioConnection::connectChannels(int sourceChannel, int destChannel)
return;
}


channelMap.add({ sourceChannel, destChannel });
ghostChannelMap.removeAllInstancesOf({ sourceChannel, destChannel });

Expand Down Expand Up @@ -283,6 +285,12 @@ void NodeAudioConnection::offsetChannels(bool input, int offset, bool notify)

}

bool NodeAudioConnection::connectionExists(int sourceChannel, int destChannel)
{
for (auto& c : channelMap) if (c.sourceChannel == sourceChannel && c.destChannel == destChannel) return true;
return false;
}

void NodeAudioConnection::updateConnections()
{
Array<ChannelMap> toRemove;
Expand Down Expand Up @@ -332,9 +340,34 @@ var NodeAudioConnection::getChannelMapData()

void NodeAudioConnection::loadChannelMapData(var data)
{
if (!data.isVoid()) clearConnections();
if (data.isVoid()) return;

//check existing connections
Array<ChannelMap> toRemove;
for (auto& c : channelMap)
{
bool found = false;
for (int i = 0; i < data.size(); i++)
{
if (c.sourceChannel == (int)data[i][0] && c.destChannel == (int)data[i][1])
{
found = true;
break;
}
}
if (!found) toRemove.add(c);
}

for (auto& r : toRemove)
{
//NLOG(niceName, "Disconnect channel " << r.sourceChannel << " > " << r.destChannel);
disconnectChannels(r.sourceChannel, r.destChannel, true, false);
}

//add new connections
for (int i = 0; i < data.size(); i++)
{
//NLOG(niceName, "Connect channel " << (int)data[i][0] << " > " << (int)data[i][1]);
connectChannels(data[i][0], data[i][1]);
}
}
Expand Down
2 changes: 2 additions & 0 deletions Source/Node/Connection/NodeConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class NodeAudioConnection :

void offsetChannels(bool input, int offset, bool notify = true);

bool connectionExists(int sourceChannel, int destChannel);

void updateConnections();

void audioInputsChanged(Node* n) override;
Expand Down
56 changes: 56 additions & 0 deletions Source/Node/Connection/NodeConnectionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,62 @@ Array<NodeConnection*> NodeConnectionManager::addItemsFromData(var data, bool ad
}


void NodeConnectionManager::loadJSONDataInternal(var data)
{
if (Engine::mainEngine->isLoadingFile || data.isVoid())
{
BaseManager::loadJSONDataInternal(data);
return;
}

Array<NodeConnection*> connectionsToAdd;
Array<NodeConnection*> newConnections;

var itemsData = data.getProperty("items", var());


for (int i = 0; i < itemsData.size(); i++)
{
var iData = itemsData[i];
NodeConnection::ConnectionType t = (NodeConnection::ConnectionType)(int)(iData.getProperty("connectionType", NodeConnection::AUDIO));
Node* sourceNode = nodeManager->getItemWithName(iData.getProperty("sourceNode", ""));
Node* destNode = nodeManager->getItemWithName(iData.getProperty("destNode", ""));

if (sourceNode && destNode)
{
NodeConnection* c = getConnectionForSourceAndDest(sourceNode, destNode, t);
if (c != nullptr)
{
// Reconfigure existing connection if needed
if (t == NodeConnection::AUDIO && iData.hasProperty("channels"))
{
var channelMapData = iData.getProperty("channels", var());
static_cast<NodeAudioConnection*>(c)->loadChannelMapData(channelMapData);
}
}
else
{
// Add new connection
NodeConnection* c = createConnectionForType(t);
c->loadJSONData(iData);
connectionsToAdd.add(c);
}

newConnections.add(c);
}
}

// Add new connections
Array<NodeConnection*> toRemove;
for (auto& c : items) if (!newConnections.contains(c)) toRemove.add(c);


//NLOG(niceName, "Removing " << toRemove.size() << " connections");
removeItems(toRemove, false);
//NLOG(niceName, "Adding " << connectionsToAdd.size() << " connections");
addItems(connectionsToAdd, var(), false);
}

void NodeConnectionManager::afterLoadJSONDataInternal()
{
BaseManager::afterLoadJSONDataInternal();
Expand Down
1 change: 1 addition & 0 deletions Source/Node/Connection/NodeConnectionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ class NodeConnectionManager :
NodeConnection* addItemFromData(var data, bool addToUndo = true) override;
Array<NodeConnection *> addItemsFromData(var data, bool addToUndo = true) override;

void loadJSONDataInternal(var data) override;
void afterLoadJSONDataInternal() override;
};
1 change: 1 addition & 0 deletions Source/Node/NodeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ NodeManager::NodeManager(AudioProcessorGraph* graph,
AudioProcessorGraph::NodeID midiInputNodeID, AudioProcessorGraph::NodeID midiOutputNodeID) :
BaseManager("Nodes"),
graph(graph),
cpuUsage(nullptr),
audioInputNodeID(audioInputNodeID),
audioOutputNodeID(audioOutputNodeID),
midiInputNodeID(midiInputNodeID),
Expand Down

0 comments on commit 9e49380

Please sign in to comment.