diff --git a/Source/Node/Connection/NodeConnection.cpp b/Source/Node/Connection/NodeConnection.cpp index d8f593b..84062fa 100644 --- a/Source/Node/Connection/NodeConnection.cpp +++ b/Source/Node/Connection/NodeConnection.cpp @@ -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) { @@ -219,6 +220,7 @@ void NodeAudioConnection::connectChannels(int sourceChannel, int destChannel) return; } + channelMap.add({ sourceChannel, destChannel }); ghostChannelMap.removeAllInstancesOf({ sourceChannel, destChannel }); @@ -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 toRemove; @@ -332,9 +340,34 @@ var NodeAudioConnection::getChannelMapData() void NodeAudioConnection::loadChannelMapData(var data) { - if (!data.isVoid()) clearConnections(); + if (data.isVoid()) return; + + //check existing connections + Array 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]); } } diff --git a/Source/Node/Connection/NodeConnection.h b/Source/Node/Connection/NodeConnection.h index d478828..e49c0ba 100644 --- a/Source/Node/Connection/NodeConnection.h +++ b/Source/Node/Connection/NodeConnection.h @@ -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; diff --git a/Source/Node/Connection/NodeConnectionManager.cpp b/Source/Node/Connection/NodeConnectionManager.cpp index 00f58ff..ff24acb 100644 --- a/Source/Node/Connection/NodeConnectionManager.cpp +++ b/Source/Node/Connection/NodeConnectionManager.cpp @@ -109,6 +109,62 @@ Array NodeConnectionManager::addItemsFromData(var data, bool ad } +void NodeConnectionManager::loadJSONDataInternal(var data) +{ + if (Engine::mainEngine->isLoadingFile || data.isVoid()) + { + BaseManager::loadJSONDataInternal(data); + return; + } + + Array connectionsToAdd; + Array 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(c)->loadChannelMapData(channelMapData); + } + } + else + { + // Add new connection + NodeConnection* c = createConnectionForType(t); + c->loadJSONData(iData); + connectionsToAdd.add(c); + } + + newConnections.add(c); + } + } + + // Add new connections + Array 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(); diff --git a/Source/Node/Connection/NodeConnectionManager.h b/Source/Node/Connection/NodeConnectionManager.h index f4f7d05..7bfdf2b 100644 --- a/Source/Node/Connection/NodeConnectionManager.h +++ b/Source/Node/Connection/NodeConnectionManager.h @@ -34,5 +34,6 @@ class NodeConnectionManager : NodeConnection* addItemFromData(var data, bool addToUndo = true) override; Array addItemsFromData(var data, bool addToUndo = true) override; + void loadJSONDataInternal(var data) override; void afterLoadJSONDataInternal() override; }; \ No newline at end of file diff --git a/Source/Node/NodeManager.cpp b/Source/Node/NodeManager.cpp index e7db1c6..bc43254 100644 --- a/Source/Node/NodeManager.cpp +++ b/Source/Node/NodeManager.cpp @@ -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),