/
tracktion_SummingNode.h
323 lines (255 loc) · 11.1 KB
/
tracktion_SummingNode.h
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
/*
,--. ,--. ,--. ,--.
,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2018
'-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
| | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
`---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
*/
#pragma once
namespace tracktion { inline namespace graph
{
//==============================================================================
//==============================================================================
/**
An Node which sums together the multiple inputs adding additional latency
to provide a coherent output.
*/
class SummingNode final : public Node
{
public:
SummingNode() = default;
SummingNode (std::vector<std::unique_ptr<Node>> inputs)
: ownedNodes (std::move (inputs))
{
for (auto& ownedNode : ownedNodes)
nodes.push_back (ownedNode.get());
assert (std::find (nodes.begin(), nodes.end(), nullptr) == nodes.end());
}
SummingNode (std::vector<Node*> inputs)
: nodes (std::move (inputs))
{
assert (std::find (nodes.begin(), nodes.end(), nullptr) == nodes.end());
}
SummingNode (std::vector<std::unique_ptr<Node>> ownedInputs,
std::vector<Node*> referencedInputs)
: SummingNode (std::move (ownedInputs))
{
nodes.insert (nodes.begin(), referencedInputs.begin(), referencedInputs.end());
assert (std::find (nodes.begin(), nodes.end(), nullptr) == nodes.end());
}
//==============================================================================
/** Adds an input to be summed in this Node. */
void addInput (std::unique_ptr<Node> newInput)
{
assert (newInput != nullptr);
nodes.push_back (newInput.get());
ownedNodes.push_back (std::move (newInput));
}
/** Enables Nodes to be intermediately summed using double precision.
This must be set before the node is initialised.
N.B. The output will still be single-precision floats.
*/
void setDoubleProcessingPrecision (bool shouldSumInDoublePrecision)
{
useDoublePrecision = shouldSumInDoublePrecision;
}
//==============================================================================
NodeProperties getNodeProperties() override
{
NodeProperties props;
props.hasAudio = false;
props.hasMidi = false;
props.numberOfChannels = 0;
for (auto& node : nodes)
{
auto nodeProps = node->getNodeProperties();
props.hasAudio = props.hasAudio || nodeProps.hasAudio;
props.hasMidi = props.hasMidi || nodeProps.hasMidi;
props.numberOfChannels = std::max (props.numberOfChannels, nodeProps.numberOfChannels);
props.latencyNumSamples = std::max (props.latencyNumSamples, nodeProps.latencyNumSamples);
hash_combine (props.nodeID, nodeProps.nodeID);
}
return props;
}
std::vector<Node*> getDirectInputNodes() override
{
std::vector<Node*> inputNodes;
for (auto& node : nodes)
inputNodes.push_back (node);
return inputNodes;
}
bool transform (Node&) override
{
const bool hasFlattened = flattenSummingNodes();
const bool hasCreatedLatency = createLatencyNodes();
return hasFlattened || hasCreatedLatency;
}
void prepareToPlay (const PlaybackInitialisationInfo& info) override
{
useDoublePrecision = useDoublePrecision && nodes.size() > 1;
if (useDoublePrecision)
tempDoubleBuffer.resize ({ (choc::buffer::ChannelCount) getNodeProperties().numberOfChannels,
(choc::buffer::FrameCount) info.blockSize });
}
bool isReadyToProcess() override
{
for (auto& node : nodes)
if (! node->hasProcessed())
return false;
return true;
}
void process (ProcessContext& pc) override
{
if (useDoublePrecision)
processDoublePrecision (pc);
else
processSinglePrecision (pc);
}
private:
//==============================================================================
std::vector<std::unique_ptr<Node>> ownedNodes;
std::vector<Node*> nodes;
bool useDoublePrecision = false;
choc::buffer::ChannelArrayBuffer<double> tempDoubleBuffer;
static void sortByTimestampUnstable (tracktion_engine::MidiMessageArray& messages) noexcept
{
std::sort (messages.begin(), messages.end(), [] (const juce::MidiMessage& a, const juce::MidiMessage& b)
{
auto t1 = a.getTimeStamp();
auto t2 = b.getTimeStamp();
if (t1 == t2)
{
if (a.isNoteOff() && b.isNoteOn()) return true;
if (a.isNoteOn() && b.isNoteOff()) return false;
}
return t1 < t2;
});
}
//==============================================================================
void processSinglePrecision (const ProcessContext& pc)
{
const auto numChannels = pc.buffers.audio.getNumChannels();
int nodesWithMidi = pc.buffers.midi.isEmpty() ? 0 : 1;
// Get each of the inputs and add them to dest
for (auto& node : nodes)
{
auto inputFromNode = node->getProcessedOutput();
if (auto numChannelsToAdd = std::min (inputFromNode.audio.getNumChannels(), numChannels))
add (pc.buffers.audio.getFirstChannels (numChannelsToAdd),
inputFromNode.audio.getFirstChannels (numChannelsToAdd));
if (inputFromNode.midi.isNotEmpty())
nodesWithMidi++;
pc.buffers.midi.mergeFrom (inputFromNode.midi);
}
if (nodesWithMidi > 1)
sortByTimestampUnstable (pc.buffers.midi);
}
void processDoublePrecision (const ProcessContext& pc)
{
const auto numChannels = pc.buffers.audio.getNumChannels();
auto doubleView = tempDoubleBuffer.getView().getStart (pc.buffers.audio.getNumFrames());
doubleView.clear();
int nodesWithMidi = pc.buffers.midi.isEmpty() ? 0 : 1;
// Get each of the inputs and add them to dest
for (auto& node : nodes)
{
auto inputFromNode = node->getProcessedOutput();
if (auto numChannelsToAdd = std::min (inputFromNode.audio.getNumChannels(), numChannels))
add (doubleView.getFirstChannels (numChannelsToAdd),
inputFromNode.audio.getFirstChannels (numChannelsToAdd));
if (inputFromNode.midi.isNotEmpty())
nodesWithMidi++;
pc.buffers.midi.mergeFrom (inputFromNode.midi);
}
assert (doubleView.getNumChannels() == (choc::buffer::ChannelCount) numChannels);
if (numChannels != 0)
add (pc.buffers.audio.getFirstChannels (numChannels), doubleView);
if (nodesWithMidi > 1)
sortByTimestampUnstable (pc.buffers.midi);
}
//==============================================================================
bool flattenSummingNodes()
{
bool hasChanged = false;
std::vector<std::unique_ptr<Node>> ownedNodesToAdd;
std::vector<Node*> nodesToAdd, nodesToErase;
for (auto& n : ownedNodes)
{
if (auto summingNode = dynamic_cast<SummingNode*> (n.get()))
{
for (auto& ownedNodeToAdd : summingNode->ownedNodes)
ownedNodesToAdd.push_back (std::move (ownedNodeToAdd));
for (auto& nodeToAdd : summingNode->nodes)
nodesToAdd.push_back (std::move (nodeToAdd));
nodesToErase.push_back (n.get());
hasChanged = true;
}
}
for (auto& n : ownedNodesToAdd)
ownedNodes.push_back (std::move (n));
for (auto& n : nodesToAdd)
nodes.push_back (std::move (n));
ownedNodes.erase (std::remove_if (ownedNodes.begin(), ownedNodes.end(),
[&] (auto& n) { return std::find (nodesToErase.begin(), nodesToErase.end(), n.get()) != nodesToErase.end(); }),
ownedNodes.end());
nodes.erase (std::remove_if (nodes.begin(), nodes.end(),
[&] (auto& n) { return std::find (nodesToErase.begin(), nodesToErase.end(), n) != nodesToErase.end(); }),
nodes.end());
return hasChanged;
}
bool createLatencyNodes()
{
bool topologyChanged = false;
const int maxLatency = getNodeProperties().latencyNumSamples;
std::vector<std::unique_ptr<Node>> ownedNodesToAdd;
for (auto& node : nodes)
{
const int nodeLatency = node->getNodeProperties().latencyNumSamples;
const int latencyToAdd = maxLatency - nodeLatency;
if (latencyToAdd == 0)
continue;
auto getOwnedNode = [this] (auto nodeToFind)
{
for (auto& ownedN : ownedNodes)
{
if (ownedN.get() == nodeToFind)
{
auto nodeToReturn = std::move (ownedN);
ownedN.reset();
return nodeToReturn;
}
}
return std::unique_ptr<Node>();
};
auto ownedNode = getOwnedNode (node);
auto latencyNode = ownedNode != nullptr ? makeNode<LatencyNode> (std::move (ownedNode), latencyToAdd)
: makeNode<LatencyNode> (node, latencyToAdd);
ownedNodesToAdd.push_back (std::move (latencyNode));
node = nullptr;
topologyChanged = true;
}
// Take ownership of any new nodes and also ensure they're reference in the raw array
for (auto& newNode : ownedNodesToAdd)
{
nodes.push_back (newNode.get());
ownedNodes.push_back (std::move (newNode));
}
nodes.erase (std::remove_if (nodes.begin(), nodes.end(),
[] (auto& n) { return n == nullptr; }),
nodes.end());
ownedNodes.erase (std::remove_if (ownedNodes.begin(), ownedNodes.end(),
[] (auto& n) { return n == nullptr; }),
ownedNodes.end());
return topologyChanged;
}
};
/** Creates a SummingNode from a number of Nodes. */
static inline std::unique_ptr<SummingNode> makeSummingNode (std::initializer_list<Node*> nodes)
{
std::vector<std::unique_ptr<Node>> nodeVector;
for (auto node : nodes)
nodeVector.push_back (std::unique_ptr<Node> (node));
return std::make_unique<SummingNode> (std::move (nodeVector));
}
}}