-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathnode.cpp
More file actions
1453 lines (1265 loc) · 44.7 KB
/
node.cpp
File metadata and controls
1453 lines (1265 loc) · 44.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2023 Kushview, LLC <info@kushview.net>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "nodes/baseprocessor.hpp" // for internal id macros
#include <element/node.hpp>
#include <element/session.hpp>
#include <element/script.hpp>
#include "engine/graphmanager.hpp"
#include "scopedflag.hpp"
namespace element {
//==============================================================================
struct NameSorter
{
NameSorter() {}
int compareElements (const Node& lhs, const Node& rhs)
{
return lhs.getName() < rhs.getName() ? -1 : lhs.getName() == rhs.getName() ? 0
: 1;
}
friend class NodeArray;
};
static void readPluginDescriptionForLoading (const ValueTree& p, PluginDescription& pd)
{
const auto& type = p.getProperty (tags::type);
if (type == types::Graph.toString())
{
pd.name = p.getProperty (tags::name);
pd.fileOrIdentifier = EL_NODE_ID_GRAPH;
pd.pluginFormatName = EL_NODE_FORMAT_NAME;
}
else
{
// plugins and io nodes
pd.name = p.getProperty (tags::pluginName);
pd.pluginFormatName = p.getProperty (tags::format);
pd.fileOrIdentifier = p.getProperty (tags::identifier);
if (pd.fileOrIdentifier.isEmpty())
pd.fileOrIdentifier = p.getProperty (tags::file);
}
}
static ValueTree getBlockValueTree (const Node& node)
{
return node.getUIValueTree().getOrCreateChildWithName (types::Block, nullptr);
}
static StringArray getHiddenPortsProperty (const Node& node)
{
const auto bv = getBlockValueTree (node);
auto result = StringArray::fromTokens (bv.getProperty ("hiddenPorts").toString(), ",", "\"\'");
result.trim();
return result;
}
static void setHiddenPortsProperty (const Node& node, const StringArray& symbols)
{
auto bv = getBlockValueTree (node);
bv.setProperty ("hiddenPorts", symbols.joinIntoString (","), nullptr);
}
//==============================================================================
uint32 Port::index() const noexcept
{
const int index = getProperty (tags::index, -1);
return index >= 0 ? static_cast<uint32> (index) : EL_INVALID_PORT;
}
const String Port::symbol() const noexcept { return getProperty (tags::symbol, String()); }
int Port::channel() const noexcept
{
const Node node (objectData.getParent().getParent());
if (auto* g = node.getObject())
return g->getChannelPort (index());
return -1;
}
Node Port::getNode() const
{
return Node (getNodeValueTree(), false);
}
void Port::setHiddenOnBlock (bool hidden)
{
const auto node (getNode());
auto symbols = getHiddenPortsProperty (node);
if (hidden)
{
symbols.addIfNotAlreadyThere (symbol());
}
else
{
symbols.removeString (symbol().toRawUTF8());
}
setHiddenPortsProperty (node, symbols);
setProperty ("hiddenOnBlock", hidden);
}
bool Port::isHiddenOnBlock() const
{
const auto parent (getNode());
if (! parent.isValid())
{
return true;
}
// TODO: hiddenOnBlock property can't be a 'once' object
if (! objectData.hasProperty ("hiddenOnBlock"))
{
const auto symbols = getHiddenPortsProperty (parent);
ValueTree (objectData).setProperty ("hiddenOnBlock", symbols.contains (symbol().toRawUTF8()), nullptr);
}
return (bool) getProperty ("hiddenOnBlock");
}
//==============================================================================
Node::Node() : Model() {}
Node::Node (const ValueTree& data, const bool setMissing)
: Model (data)
{
if (setMissing)
{
jassert (data.hasType (types::Node));
setMissingProperties();
}
}
// clang-format off
Node::Node (const Identifier& tp)
: Model (types::Node, (tp == types::Graph ? EL_GRAPH_VERSION : EL_NODE_VERSION))
{
#if JUCE_DEBUG
Array<Identifier> supported ({ types::Node, types::Graph });
jassert (supported.contains (tp));
#endif
objectData.setProperty (tags::type, tp.toString(), nullptr);
setMissingProperties();
}
// clang-format on
Node::~Node() noexcept {}
//=============================================================================
bool Node::isValid() const noexcept { return objectData.hasType (types::Node); }
const String Node::getName() const noexcept { return getProperty (tags::name).toString(); }
void Node::setName (const String& name) { setProperty (tags::name, name); }
const String Node::getPluginName() const noexcept
{
if (ProcessorPtr object = getObject())
return object->getName();
return {};
}
const String Node::getDisplayName() const noexcept
{
String name = getName();
if (name.isEmpty())
name = getPluginName();
return name;
}
bool Node::hasModifiedName() const noexcept
{
auto dname = getName();
return dname.isNotEmpty() && dname != getPluginName();
}
//=============================================================================
Node Node::createDefaultGraph (const String& name)
{
Node graph (types::Graph);
graph.setProperty (tags::name, name);
ValueTree gports = graph.getPortsValueTree();
int portIdx = 0;
gports.addChild (Port ("Audio In 1", tags::audio, tags::input, portIdx++).data(), -1, 0);
gports.addChild (Port ("Audio In 2", tags::audio, tags::input, portIdx++).data(), -1, 0);
gports.addChild (Port ("MIDI In", tags::midi, tags::input, portIdx++).data(), -1, 0);
gports.addChild (Port ("Audio Out 1", tags::audio, tags::output, portIdx++).data(), -1, 0);
gports.addChild (Port ("Audio Out 2", tags::audio, tags::output, portIdx++).data(), -1, 0);
gports.addChild (Port ("MIDI Out", tags::midi, tags::output, portIdx++).data(), -1, 0);
ValueTree nodes = graph.getNodesValueTree();
const auto types = StringArray ({ "audio.input", "audio.output", "midi.input", "midi.output" });
const auto names = StringArray ({ "Audio In", "Audio Out", "MIDI In", "MIDI Out" });
uint32 nodeId = 1;
for (const auto& t : types)
{
ValueTree ioNode (types::Node);
ValueTree ports = ioNode.getOrCreateChildWithName (tags::ports, 0);
portIdx = 0;
ioNode.setProperty (tags::id, static_cast<int64> (nodeId++), 0)
.setProperty (tags::type, "plugin", 0)
.setProperty (tags::format, "Internal", 0)
.setProperty (tags::identifier, t, 0)
.setProperty (tags::name, names[types.indexOf (t)], 0);
if (t == "audio.input")
{
ioNode.setProperty (tags::relativeX, 0.25f, 0)
.setProperty (tags::relativeY, 0.25f, 0)
.setProperty ("numAudioIns", 0, 0)
.setProperty ("numAudioOuts", 2, 0);
Port port ("Port", tags::audio, tags::output, (uint32) portIdx++);
ports.addChild (port.data(), -1, 0);
port = Port ("Port", tags::audio, tags::output, (int) portIdx++);
ports.addChild (port.data(), -1, 0);
}
else if (t == "audio.output")
{
ioNode.setProperty (tags::relativeX, 0.25f, 0)
.setProperty (tags::relativeY, 0.75f, 0)
.setProperty ("numAudioIns", 2, 0) // TODO: Needed?
.setProperty ("numAudioOuts", 0, 0); // TODO: Needed?
Port port ("Port", tags::audio, tags::input, (uint32) portIdx++);
ports.addChild (port.data(), -1, 0);
port = Port ("Port", tags::audio, tags::input, (int) portIdx++);
}
else if (t == "midi.input")
{
ioNode.setProperty (tags::relativeX, 0.75f, 0)
.setProperty (tags::relativeY, 0.25f, 0)
.setProperty ("numAudioIns", 0, 0)
.setProperty ("numAudioOuts", 0, 0);
Port port ("Port", tags::midi, tags::output, (uint32) portIdx++);
ports.addChild (port.data(), -1, 0);
}
else if (t == "midi.output")
{
ioNode.setProperty (tags::relativeX, 0.75f, 0)
.setProperty (tags::relativeY, 0.75f, 0)
.setProperty ("numAudioIns", 0, 0)
.setProperty ("numAudioOuts", 0, 0);
Port port ("Port", tags::midi, tags::input, (uint32) portIdx++);
ports.addChild (port.data(), -1, 0);
}
Node finalNode (ioNode, true);
nodes.addChild (finalNode.data(), -1, 0);
}
return graph;
}
bool Node::isProbablyGraphNode (const ValueTree& data)
{
// clang-format off
const var& tp = data.getProperty (tags::type);
return (data.hasType (types::Node) || data.hasType (tags::node)) &&
(tags::graph.toString() == tp.toString() ||
types::Graph.toString() == tp.toString() ||
String ("default") == tp.toString());
// clang-format on
}
ValueTree Node::resetIds (const ValueTree& data)
{
ValueTree result = data;
jassert (result.hasType (types::Node)); // must be a node
jassert (! result.getParent().isValid()); // cannot be part of another object tree
if (result.getParent().isValid())
return result;
result.removeProperty (tags::id, nullptr);
result.setProperty (tags::uuid, Uuid().toString(), nullptr);
return result;
}
ValueTree Node::parse (const File& file)
{
ValueTree sessionData = Session::readFromFile (file);
if (sessionData.isValid())
{
const auto graphs = sessionData.getChildWithName (tags::graphs);
const auto sessionNode = graphs.getChild (graphs.getProperty (tags::active, 0));
return sessionNode.createCopy();
}
ValueTree data;
ValueTree nodeData;
if (auto e = XmlDocument::parse (file))
{
data = ValueTree::fromXml (*e);
}
else
{
FileInputStream input (file);
data = ValueTree::readFromStream (input);
}
if (data.hasType (types::Node))
{
nodeData = data;
}
else
{
nodeData = data.getChildWithName (types::Node);
if (! nodeData.isValid())
{
nodeData = data.getChildWithName (tags::node);
if (nodeData.isValid())
{
String error;
nodeData = Node::migrate (nodeData, error);
if (error.isNotEmpty())
{
std::clog << "[element] error: " << error.toStdString() << std::endl;
}
}
}
// copy properties from preset if needed.
if (nodeData.isValid())
{
// Rename the node appropriately
if (data.hasProperty (tags::name))
nodeData.setProperty (tags::name, data.getProperty (tags::name), 0);
else
nodeData.setProperty (tags::name, file.getFileNameWithoutExtension(), 0);
}
}
if (nodeData.isValid() && nodeData.hasType (types::Node))
{
if (data.indexOf (nodeData) >= 0)
data.removeChild (nodeData, 0);
Node::sanitizeProperties (nodeData);
return nodeData;
}
return ValueTree();
}
void Node::sanitizeProperties (ValueTree node, const bool recursive)
{
node.removeProperty (tags::updater, nullptr);
node.removeProperty (tags::object, nullptr);
if (node.hasType (types::Node))
{
Array<Identifier> properties ({ tags::offline,
tags::placeholder,
tags::missing });
for (const auto& property : properties)
node.removeProperty (property, nullptr);
}
if (recursive)
for (int i = 0; i < node.getNumChildren(); ++i)
sanitizeProperties (node.getChild (i), recursive);
}
void Node::sanitizeRuntimeProperties (ValueTree node, const bool recursive)
{
Node::sanitizeProperties (node, recursive);
}
bool Node::writeToFile (const File& targetFile) const
{
ValueTree data = objectData.createCopy();
sanitizeProperties (data, true);
#if EL_SAVE_BINARY_FORMAT
TemporaryFile tempFile (targetFile);
if (auto out = std::unique_ptr<FileOutputStream> (tempFile.getFile().createOutputStream()))
{
data.writeToStream (*out);
out.reset();
return tempFile.overwriteTargetFileWithTemporary();
}
#else
if (auto e = data.createXml())
return e->writeTo (targetFile);
#endif
return false;
}
bool Node::savePresetTo (const DataPath& path, const String& name) const
{
{
// hack: ensure the plugin's state info is up-to-date
Node (*this).savePluginState();
}
ValueTree preset (tags::preset);
ValueTree data = objectData.createCopy();
sanitizeProperties (data, true);
preset.addChild (data, -1, 0);
const auto targetFile = path.getPresetFile (name);
if (! targetFile.getParentDirectory().exists())
targetFile.getParentDirectory().createDirectory();
data.setProperty (tags::version, EL_NODE_VERSION, 0);
data.setProperty (tags::name, targetFile.getFileNameWithoutExtension(), 0);
data.setProperty (tags::type, tags::node.toString(), 0);
#if EL_SAVE_BINARY_FORMAT
TemporaryFile tempFile (targetFile);
if (auto out = std::unique_ptr<FileOutputStream> (tempFile.getFile().createOutputStream()))
{
data.writeToStream (*out);
out.reset();
return tempFile.overwriteTargetFileWithTemporary();
}
#else
if (auto e = preset.createXml())
return e->writeTo (targetFile);
#endif
return false;
}
Node Node::createGraph (const String& name)
{
Node node (types::Graph);
ValueTree data = node.data();
data.setProperty (tags::name, name, nullptr);
data.getOrCreateChildWithName (tags::nodes, nullptr);
data.getOrCreateChildWithName (tags::arcs, nullptr);
return node;
}
ValueTree Node::makeArc (const Arc& arc)
{
ValueTree model (types::Arc);
model.setProperty (tags::sourceNode, (int) arc.sourceNode, nullptr);
model.setProperty (tags::sourcePort, (int) arc.sourcePort, nullptr);
model.setProperty (tags::destNode, (int) arc.destNode, nullptr);
model.setProperty (tags::destPort, (int) arc.destPort, nullptr);
return model;
}
const bool Node::canConnectTo (const Node& o) const
{
if (objectData.getParent() != o.objectData.getParent() || objectData == o.objectData)
{
return false;
}
return true;
}
ValueTree Node::getParentArcsNode() const
{
ValueTree tmp = objectData.getParent();
if (tmp.hasType (tags::nodes))
tmp = tmp.getParent();
if (! tmp.isValid())
return ValueTree();
jassert (tmp.hasType (types::Node));
return tmp.getOrCreateChildWithName (tags::arcs, nullptr);
}
void Node::getPluginDescription (PluginDescription& p) const
{
readPluginDescriptionForLoading (objectData, p);
}
ValueTree Node::addScript (const Script& script)
{
auto data = script.data();
if (data.getParent().isValid())
data = data.createCopy();
Script src (data);
if (src.valid())
getScriptsValueTree().addChild (src.data(), -1, nullptr);
return src.data();
}
void Node::setMissingProperties()
{
stabilizePropertyString (tags::uuid, Uuid().toString());
stabilizePropertyString (tags::type, types::Node.toString());
stabilizePropertyString (tags::name, "Node");
stabilizeProperty (tags::bypass, false);
stabilizeProperty (tags::persistent, true);
stabilizePropertyString (tags::renderMode, "single");
stabilizeProperty (tags::keyStart, 0);
stabilizeProperty (tags::keyEnd, 127);
stabilizeProperty (tags::transpose, 0);
stabilizeProperty (tags::delayCompensation, 0);
stabilizeProperty (tags::tempo, (double) 120.0);
objectData.getOrCreateChildWithName (tags::nodes, nullptr);
objectData.getOrCreateChildWithName (tags::ports, nullptr);
objectData.getOrCreateChildWithName (tags::scripts, nullptr);
auto ui = objectData.getOrCreateChildWithName (tags::ui, nullptr);
ui.getOrCreateChildWithName (types::Block, nullptr);
}
Processor* Node::getObject() const
{
return dynamic_cast<Processor*> (objectData.getProperty (tags::object, var()).getObject());
}
Processor* Node::getObjectForId (const uint32 nodeId) const
{
const Node node (getNodeById (nodeId));
return node.isValid() ? node.getObject() : nullptr;
}
void Node::getPorts (PortArray& ports, PortType type, bool isInput) const
{
const ValueTree portList (getPortsValueTree());
for (int i = 0; i < portList.getNumChildren(); ++i)
{
const Port port (portList.getChild (i));
if (port.isA (type, isInput))
ports.add (port);
}
}
void Node::getPorts (PortArray& ins, PortArray& outs, PortType type) const
{
const ValueTree portList (getPortsValueTree());
for (int i = 0; i < portList.getNumChildren(); ++i)
{
const Port port (portList.getChild (i));
if (port.isA (type, true))
ins.add (port);
else if (port.isA (type, false))
outs.add (port);
}
}
void Node::getAudioInputs (PortArray& ports) const
{
getPorts (ports, PortType::Audio, true);
}
void Node::getAudioOutputs (PortArray& ports) const
{
getPorts (ports, PortType::Audio, false);
}
//==============================================================================
// clang-format off
bool Node::isAudioIONode() const
{
return objectData.getProperty (tags::format) == "Internal" &&
(objectData.getProperty (tags::identifier) == "audio.input" ||
objectData.getProperty (tags::identifier) == "audio.output");
}
bool Node::isAudioInputNode() const
{
return objectData.getProperty (tags::format) == "Internal" &&
objectData.getProperty (tags::identifier) == "audio.input";
}
bool Node::isAudioOutputNode() const
{
return objectData.getProperty (tags::format) == "Internal" &&
objectData.getProperty (tags::identifier) == "audio.output";
}
bool Node::isMidiIONode() const
{
return objectData.getProperty (tags::format) == "Internal" &&
(objectData.getProperty (tags::identifier) == "midi.input" ||
objectData.getProperty (tags::identifier) == "midi.output");
}
/** Returns true if a global MIDI input node. e.g */
bool Node::isMidiInputNode() const
{
return objectData.getProperty (tags::format) == "Internal" &&
objectData.getProperty (tags::identifier) == "midi.input";
}
/** Returns true if a global MIDI output node. e.g */
bool Node::isMidiOutputNode() const
{
return objectData.getProperty (tags::format) == "Internal" &&
objectData.getProperty (tags::identifier) == "midi.output";
}
// clang-format on
//==============================================================================
bool Node::isMidiInputDevice() const
{
return objectData.getProperty (tags::format) == EL_NODE_FORMAT_NAME && objectData.getProperty (tags::identifier) == EL_NODE_ID_MIDI_INPUT_DEVICE;
}
bool Node::isMidiOutputDevice() const
{
return objectData.getProperty (tags::format) == EL_NODE_FORMAT_NAME && objectData.getProperty (tags::identifier) == EL_NODE_ID_MIDI_OUTPUT_DEVICE;
}
//==============================================================================
void Node::resetPorts()
{
if (ProcessorPtr ptr = getObject())
{
ptr->refreshPorts();
ValueTree newPorts = ptr->createPortsData();
ValueTree ports = getPortsValueTree();
objectData.removeChild (ports, nullptr);
objectData.addChild (newPorts, -1, nullptr);
}
}
void Node::getPossibleSources (NodeArray& a) const
{
ValueTree nodes = objectData.getParent();
if (! nodes.hasType (tags::nodes))
return;
for (int i = 0; i < nodes.getNumChildren(); ++i)
{
const Node child (nodes.getChild (i));
if (child.getNodeId() == getNodeId())
continue;
if (child.canConnectTo (*this))
a.add (child);
}
}
void Node::getPossibleDestinations (NodeArray& a) const
{
ValueTree nodes = objectData.getParent();
if (! nodes.hasType (tags::nodes))
return;
for (int i = 0; i < nodes.getNumChildren(); ++i)
{
const Node child (nodes.getChild (i));
if (child.getNodeId() == getNodeId())
continue;
if (canConnectTo (child))
a.add (child);
}
}
Arc Node::arcFromValueTree (const ValueTree& data)
{
Arc arc ((uint32) (int) data.getProperty (tags::sourceNode, (int) EL_INVALID_NODE),
(uint32) (int) data.getProperty (tags::sourcePort, (int) EL_INVALID_PORT),
(uint32) (int) data.getProperty (tags::destNode, (int) EL_INVALID_NODE),
(uint32) (int) data.getProperty (tags::destPort, (int) EL_INVALID_PORT));
return arc;
}
int Node::getNumConnections() const { return getArcsValueTree().getNumChildren(); }
ValueTree Node::getConnectionValueTree (const int index) const { return getArcsValueTree().getChild (index); }
void NodeArray::sortByName()
{
NameSorter sorter;
this->sort (sorter);
}
bool Node::connectionExists (const ValueTree& arcs,
const uint32 sourceNode,
const uint32 sourcePort,
const uint32 destNode,
const uint32 destPort,
const bool checkMissing)
{
for (int i = arcs.getNumChildren(); --i >= 0;)
{
const ValueTree arc (arcs.getChild (i));
if (static_cast<int> (sourceNode) == (int) arc.getProperty (tags::sourceNode) && static_cast<int> (sourcePort) == (int) arc.getProperty (tags::sourcePort) && static_cast<int> (destNode) == (int) arc.getProperty (tags::destNode) && static_cast<int> (destPort) == (int) arc.getProperty (tags::destPort))
{
return (checkMissing) ? ! arc.getProperty (tags::missing, false) : true;
}
}
return false;
}
Node Node::getNodeById (const uint32 nodeId) const
{
const ValueTree nodes = getNodesValueTree();
Node node (nodes.getChildWithProperty (tags::id, static_cast<int64> (nodeId)), false);
return node;
}
static Node findNodeRecursive (const Node& node, const Uuid& uuid)
{
Node found;
for (int i = node.getNumNodes(); --i >= 0;)
{
found = node.getNode (i);
if (found.getUuid() == uuid)
return found;
found = findNodeRecursive (found, uuid);
if (found.isValid())
break;
}
return found;
}
Node Node::getNodeByUuid (const Uuid& uuid, const bool recursive) const
{
if (! recursive)
{
const ValueTree nodes = getNodesValueTree();
Node node (nodes.getChildWithProperty (tags::uuid, uuid.toString()), false);
return node;
}
return findNodeRecursive (*this, uuid);
}
Port Node::getPort (const int index) const
{
Port port (getPortsValueTree().getChild (index));
return port;
}
bool Node::canConnect (const uint32 sourceNode, const uint32 sourcePort, const uint32 destNode, const uint32 destPort) const
{
const Node sn (getNodeById (sourceNode));
const Node dn (getNodeById (destNode));
if (! sn.isValid() || ! dn.isValid())
return false;
const Port dp (dn.getPort ((int) destPort));
const Port sp (sn.getPort ((int) sourcePort));
return sp.getType().canConnect (dp.getType());
}
void Node::setRelativePosition (const double x, const double y)
{
setProperty (tags::relativeX, x);
setProperty (tags::relativeY, y);
}
void Node::getRelativePosition (double& x, double& y) const
{
x = (double) getProperty (tags::relativeX, 0.5f);
y = (double) getProperty (tags::relativeY, 0.5f);
}
bool Node::hasPosition() const
{
return hasProperty (tags::x) && hasProperty (tags::y);
}
void Node::getPosition (double& x, double& y) const
{
x = (double) getProperty (tags::x, 0.0);
y = (double) getProperty (tags::y, 0.0);
}
void Node::setPosition (double x, double y)
{
setProperty (tags::x, x);
setProperty (tags::y, y);
}
Node Node::getParentGraph() const
{
ValueTree parent = objectData.getParent();
while (! isProbablyGraphNode (parent))
{
if (! parent.isValid())
break;
parent = parent.getParent();
}
return isProbablyGraphNode (parent) ? Node (parent, false)
: Node();
}
bool Node::descendsFrom (const Node& graph) const
{
auto parent = getParentGraph();
while (graph.isValid() && parent.isValid())
{
if (graph == parent)
return true;
parent = parent.getParentGraph();
}
return false;
}
bool Node::isChildOfRootGraph() const
{
const auto graph (getParentGraph());
return graph.isRootGraph();
}
MidiChannels Node::getMidiChannels() const
{
MidiChannels chans;
if (objectData.hasProperty (tags::midiChannels))
{
if (auto* const block = objectData.getProperty (tags::midiChannels).getBinaryData())
{
BigInteger data;
data.loadFromMemoryBlock (*block);
chans.setChannels (data);
}
}
else
{
const auto channel = (int) objectData.getProperty (tags::midiChannel, 0);
if (channel > 0)
chans.setChannel (channel);
else
chans.setOmni (true);
}
return chans;
}
void Node::restorePluginState()
{
if (! isValid())
return;
if (ProcessorPtr obj = getObject())
{
if (auto* const proc = obj->getAudioProcessor())
{
const int wantedProgram = objectData.getProperty (tags::program, -1);
const bool shouldSetProgram = proc->getNumPrograms() > 0 && isPositiveAndBelow (wantedProgram, proc->getNumPrograms());
if (shouldSetProgram)
proc->setCurrentProgram (wantedProgram);
auto data = getProperty (tags::state).toString().trim();
if (data.isNotEmpty())
{
MemoryBlock state;
state.fromBase64Encoding (data);
if (state.getSize() > 0)
{
proc->setStateInformation (state.getData(), (int) state.getSize());
}
}
data = getProperty (tags::programState).toString().trim();
if (shouldSetProgram && data.isNotEmpty())
{
MemoryBlock state;
state.fromBase64Encoding (data);
if (state.getSize() > 0)
{
proc->setCurrentProgramStateInformation (state.getData(),
(int) state.getSize());
}
}
}
else
{
const int wantedProgram = objectData.getProperty (tags::program, -1);
const bool shouldSetProgram = obj->getNumPrograms() > 0 && isPositiveAndBelow (wantedProgram, obj->getNumPrograms());
if (shouldSetProgram)
obj->setCurrentProgram (wantedProgram);
auto data = getProperty (tags::state).toString().trim();
if (data.isNotEmpty())
{
MemoryBlock state;
state.fromBase64Encoding (data);
if (state.getSize() > 0)
obj->setState (state.getData(), (int) state.getSize());
}
}
if (hasProperty (tags::bypass))
{
obj->suspendProcessing (isBypassed());
}
if (hasProperty (tags::gain))
{
obj->setGain (getProperty ("gain"));
}
if (hasProperty ("inputGain"))
{
obj->setInputGain (getProperty ("inputGain"));
}
if (hasProperty (tags::keyStart) && hasProperty (tags::keyEnd))
{
Range<int> range (getProperty (tags::keyStart, 0),
getProperty (tags::keyEnd, 127));
obj->setKeyRange (range);
}
if (hasProperty (tags::midiChannels))
{
const MidiChannels channels (getMidiChannels());
obj->setMidiChannels (channels.get());
}
if (hasProperty (tags::midiProgram))
{
obj->setMidiProgram ((int) getProperty (tags::midiProgram, -1));
}
if (hasProperty (tags::midiProgramsEnabled))
obj->setMidiProgramsEnabled ((bool) getProperty (tags::midiProgramsEnabled, true));
obj->setUseGlobalMidiPrograms ((bool) getProperty (tags::globalMidiPrograms, obj->useGlobalMidiPrograms()));
if (hasProperty (tags::midiProgramsState))
obj->setMidiProgramsState (getProperty (tags::midiProgramsState).toString().trim());
obj->setMuted ((bool) getProperty (tags::mute, obj->isMuted()));
obj->setMuteInput ((bool) getProperty ("muteInput", obj->isMutingInputs()));
if (hasProperty (tags::transpose))
obj->setTransposeOffset (getProperty (tags::transpose));
obj->setOversamplingFactor (jmax (1, (int) getProperty (tags::oversamplingFactor, 1)));
obj->setDelayCompensation (getProperty (tags::delayCompensation, 0.0));
}
// this was originally here to help reduce memory usage
// need another way to free this property without disturbing
// the normal flow of the app.
const bool clearStateProperty = false;
if (clearStateProperty)
objectData.removeProperty (tags::state, 0);
for (int i = 0; i < getNumNodes(); ++i)
getNode (i).restorePluginState();
}
void Node::savePluginState()
{
if (! isValid())
return;
ProcessorPtr obj = getObject();
if (obj && obj->isPrepared)
{
MemoryBlock state;
if (auto* proc = obj->getAudioProcessor())
{
proc->getStateInformation (state);
if (state.getSize() > 0)
{
objectData.setProperty (tags::state, state.toBase64Encoding(), nullptr);
}
else
{
const bool clearStateProperty = false;
if (clearStateProperty)
objectData.removeProperty (tags::state, 0);
}
state.reset();
proc->getCurrentProgramStateInformation (state);
if (state.getSize() > 0)
{
objectData.setProperty (tags::programState, state.toBase64Encoding(), 0);
}
setProperty (tags::bypass, proc->isSuspended());
setProperty (tags::program, proc->getCurrentProgram());
const auto layout = proc->getBusesLayout();
auto buses = objectData.getOrCreateChildWithName (tags::buses, nullptr);
buses.removeAllChildren (nullptr);
auto channelSetToData = [] (const AudioChannelSet& acs) -> ValueTree {
ValueTree bus (types::AudioChannelSet);
bus.setProperty (tags::arrangement, acs.getSpeakerArrangementAsString(), nullptr);
return bus;
};
auto bins = buses.getOrCreateChildWithName (tags::inputs, nullptr);
for (int i = 0; i < layout.inputBuses.size(); ++i)
{
auto data = channelSetToData (layout.inputBuses.getReference (i));
if (data.isValid())
bins.addChild (data, -1, nullptr);
}
auto bouts = buses.getOrCreateChildWithName (tags::outputs, nullptr);
for (int i = 0; i < layout.outputBuses.size(); ++i)
{
auto data = channelSetToData (layout.outputBuses.getReference (i));
if (data.isValid())
bouts.addChild (data, -1, nullptr);
}
}
else
{
obj->getState (state);