-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathChannelSelectorNode.cpp
More file actions
406 lines (332 loc) · 12 KB
/
ChannelSelectorNode.cpp
File metadata and controls
406 lines (332 loc) · 12 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
/****************************************************************************
**
** Copyright 2019 neuromore co
** Contact: https://neuromore.com/contact
**
** Commercial License Usage
** Licensees holding valid commercial neuromore licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and neuromore. For licensing terms
** and conditions see https://neuromore.com/licensing. For further
** information use the contact form at https://neuromore.com/contact.
**
** neuromore Public License Usage
** Alternatively, this file may be used under the terms of the neuromore
** Public License version 1 as published by neuromore co with exceptions as
** appearing in the file neuromore-class-exception.md included in the
** packaging of this file. Please review the following information to
** ensure the neuromore Public License requirements will be met:
** https://neuromore.com/npl
**
****************************************************************************/
// include precompiled header
#include <Engine/Precompiled.h>
// include required headers
#include "ChannelSelectorNode.h"
#include "../Core/AttributeSettings.h"
#include "Graph.h"
using namespace Core;
// constructor
ChannelSelectorNode::ChannelSelectorNode(Graph* graph) : SPNode(graph)
{
}
// destructor
ChannelSelectorNode::~ChannelSelectorNode()
{
DeleteOutputChannels();
}
// initialize the node
void ChannelSelectorNode::Init()
{
// init base class first
SPNode::Init();
// configure SPNode behaviour
RequireMatchingSampleRates();
UseChannelMetadataPropagation(false); // Hack: disable metadata propagation, it interfers with the function of selector node
// PORTS
InitInputPorts(1);
GetInputPort(INPUTPORT_SET).SetupAsChannels<double>("In", "x1", INPUTPORT_SET);
// create temporary empty ports
InitOutputPorts(NUMPORTSDEFAULT);
for (uint32 i = 0; i < NUMPORTSDEFAULT; ++i)
{
mTempString.Format("y%i", i + 1);
GetOutputPort(i).SetupAsChannels<double>("-", mTempString.AsChar(), i);
}
// ATTRIBUTES
// hidden port number attribute
Core::AttributeSettings* attribNumPorts = RegisterAttribute("", "numOutputPorts", "", Core::ATTRIBUTE_INTERFACETYPE_INTSPINNER);
attribNumPorts->SetDefaultValue( Core::AttributeInt32::Create(NUMPORTSDEFAULT) );
attribNumPorts->SetMinValue( Core::AttributeInt32::Create(0) );
attribNumPorts->SetMaxValue( Core::AttributeInt32::Create(INT_MAX) );
attribNumPorts->SetVisible(false);
// channel names
Core::AttributeSettings* attribChannelNames = RegisterAttribute("Channels", "channels", "Names or Indexes of Channels to use", Core::ATTRIBUTE_INTERFACETYPE_STRINGARRAY);
attribChannelNames->SetDefaultValue( Core::AttributeStringArray::Create("*") );
// single output attribute
Core::AttributeSettings* attribSingleOutput = RegisterAttribute("Single Output", "singleOutput", "", Core::ATTRIBUTE_INTERFACETYPE_CHECKBOX);
attribSingleOutput->SetDefaultValue( Core::AttributeBool::Create(false) );
// quick configuration attribute
Core::AttributeSettings* attribQuickConfig = RegisterAttribute("Quick Config", "quickconfig", "Configurable in Experience Wizard", Core::ATTRIBUTE_INTERFACETYPE_CHECKBOX);
attribQuickConfig->SetDefaultValue(Core::AttributeBool::Create(false));
}
// important: a reset must not delete the output ports or sever the connections in any way
void ChannelSelectorNode::Reset()
{
SPNode::Reset();
// delete all output channels and disconnect them from the outputs, but leave the output ports intact
DeleteOutputChannels();
}
void ChannelSelectorNode::ReInit(const Time& elapsed, const Time& delta)
{
if (BaseReInit(elapsed, delta) == false)
return;
// reinit baseclass
SPNode::ReInit(elapsed, delta);
// stop node if it doesnt have any connection or no input channels
Port& inPort = GetInputPort(INPUTPORT_SET);
if (inPort.HasConnection() == true && inPort.GetChannels() != NULL)
{
if (inPort.GetChannels() != NULL && inPort.GetChannels()->GetNumChannels() == 0)
{
//mIsInitialized = false;
}
}
PostReInit(elapsed, delta);
}
void ChannelSelectorNode::Update(const Time& elapsed, const Time& delta)
{
if (BaseUpdate(elapsed, delta) == false)
return;
// update baseclass
SPNode::Update(elapsed, delta);
// nothing to do if node is not initialized
if (mIsInitialized == false)
return;
// forward all samples to the outputs
const uint32 numChannels = mMapping.Size();
for (uint32 i=0; i<numChannels; ++i)
{
const Mapping& m = mMapping[i];
ChannelReader* reader = mInputReader.FindReader(m.in);
// no input, output 0.0 at variable samplerate
if (!reader && m.out)
m.out->AddSample(0.0);
// no input and no output, do nothing
if (!reader || !m.out)
continue;
// forward samples from input to output
const uint32 numSamples = reader->GetNumNewSamples();
for (uint32 s = 0; s < numSamples; ++s)
{
// Note: don't pop samples here, just read them
// (as we might forward one channel multiple times to different outputs)
double sample = reader->GetSample<double>(s);
m.out->AddSample(sample);
}
}
// advance all input reader at once after samples have been processed
mInputReader.Flush(true);
}
// an attribute has changed
void ChannelSelectorNode::OnAttributesChanged()
{
// always reinit if a node attribute was changed
ResetAsync();
}
// start the node: create output ports and channels
void ChannelSelectorNode::Start(const Time& elapsed)
{
// remember number of ports in attribute
SetInt32Attribute("numOutputPorts", mInputReader.GetNumChannels());
DeleteOutputChannels();
CollectSelectedInputChannels(); // collect channels first
ReInitOutputPorts(); // init ports (empty, without channels)
ReInitOutputChannels(); // init output channels and connect them to the ports; also name the ports
// Note: we have to call SPNode::Start after we created the channels
SPNode::Start(elapsed);
}
// filter input channels by name
void ChannelSelectorNode::CollectSelectedInputChannels()
{
// specified channel names
const Array<String>& names = GetStringArrayAttribute(ATTRIB_CHANNELNAMES, Array<String>());
const bool hasNames = !names.IsEmpty();
const bool useSingleWildCardFeature = (hasNames && names[0].GetLength() == 1 && names[0].GetFirst() == StringCharacter::asterisk);
const uint32 numInputChannels = mInputReader.GetNumChannels();
// no channel names specified: don't output any channel
if (hasNames == false)
return;
// a single asterisk matches all channels
if (useSingleWildCardFeature == true)
{
// forward all channel
for (uint32 i = 0; i < numInputChannels; ++i)
{
Channel<double>* ch = mInputReader.GetChannel(i)->AsType<double>();
mMapping.Add(Mapping(ch, 0, ch->GetName()));
}
return;
}
// iterate through the channel names and search for the next input channel that matches the name specification
const uint32 numNames = names.Size();
for (uint32 i=0; i<numNames; ++i)
{
String name = names[i];
name.Trim();
// handle strings like *FOO* special (search for substring instead of exact compare)
const bool useWildCardFeature = (name.GetLength() >=3 &&
name.GetFirst() == StringCharacter::asterisk &&
name.GetLast() == StringCharacter::asterisk);
// normal mode: exact name matching
if (useWildCardFeature == false && useSingleWildCardFeature == false)
{
uint32 channelIndex = CORE_INVALIDINDEX32;
// find first channel that matches the name exactly
for (uint32 j=0; j<numInputChannels; ++j)
{
ChannelBase* channel = mInputReader.GetChannel(j);
if ((name.IsValidInt() && name.ToInt() == (int)(j+1)) || channel->GetNameString().IsEqual(name))
{
// found it
channelIndex = j;
break;
}
}
// add the channel
if (channelIndex != CORE_INVALIDINDEX32)
{
Channel<double>* ch = mInputReader.GetChannel(channelIndex)->AsType<double>();
mMapping.Add(Mapping(ch, 0, ch->GetName()));
}
else
{
mMapping.Add(Mapping(0, 0, name));
// node error when channel was not found (not in wild card mode, only in exact match mode)
String tmp; tmp.Format("Channel '%s' not found", name.AsChar());
SetError(ERROR_CHANNEL_NOT_FOUND, tmp.AsChar());
}
}
else // wildcard modes: use substring matching
{
// get wildcard string by stripping the first/last char (an asterisk)
name.Trim(StringCharacter::asterisk);
// find _ALL_ channels that contain the name and add them to the list
for (uint32 j=0; j<numInputChannels; ++j)
{
ChannelBase* channel = mInputReader.GetChannel(j);
if (channel->GetNameString().Contains(name) == true)
{
// found one, add it (don't break, keep processing all input channels)
mMapping.Add(Mapping(channel->AsType<double>(), 0, channel->GetNameString()));
}
}
}
}
}
// use this to check if an input channel is forwarded by the node
bool ChannelSelectorNode::IsChannelSelected(const ChannelBase* channel) const
{
if (!channel)
return false;
// compare pointer against all selected input channels
const uint32 numSelectedChannels = mMapping.Size();
for (uint32 i=0; i<numSelectedChannels; ++i)
{
if (channel == mMapping[i].in)
return true;
}
return false;
}
// create the output ports (with empty multi channels)
void ChannelSelectorNode::ReInitOutputPorts()
{
const bool singleOutput = GetBoolAttribute(ATTRIB_SINGLE_OUTPUT);
// get number of required output ports
if (singleOutput == true)
{
InitOutputPorts(1);
GetOutputPort(0).SetupAsChannels<double>("-", "y1", 0);
// update channel references
GetOutputPort(0).GetChannels()->Clear();
}
else
{
// get number of output channels we have to output
const uint32 numChannels = mMapping.Size();
// create empty ports
InitOutputPorts(numChannels);
for (uint32 i = 0; i < numChannels; ++i)
{
mTempString.Format("y%i", i + 1);
GetOutputPort(i).SetupAsChannels<double>("-", mTempString.AsChar(), i);
}
}
}
// create the output channels (one for each selected input channel) and connect them to the outputs
void ChannelSelectorNode::ReInitOutputChannels()
{
// create output channel array, for the selected channels given
const uint32 numChannels = mMapping.Size();
for (uint32 i = 0; i < numChannels; ++i)
{
// configure output channel to same parameters as input channel
mMapping[i].out = new Channel<double>();
mMapping[i].out->SetBufferSize(10); // set any buffersize > 0
mMapping[i].out->SetName(mMapping[i].name);
if (mMapping[i].in)
{
mMapping[i].out->SetSampleRate(mMapping[i].in->GetSampleRate());
mMapping[i].out->SetMinValue(mMapping[i].in->GetMinValue());
mMapping[i].out->SetMaxValue(mMapping[i].in->GetMaxValue());
mMapping[i].out->SetColor(mMapping[i].in->GetColor());
}
}
// now add connections to output ports
const bool singleOutput = GetBoolAttribute(ATTRIB_SINGLE_OUTPUT);
if (singleOutput == true)
{
CORE_ASSERT(GetNumOutputPorts() == 1);
// add all channels into one multichannel
MultiChannel* multichannel = GetOutputPort(0).GetChannels();
multichannel->Clear();
for (uint32 i = 0; i < numChannels; ++i)
multichannel->AddChannel(mMapping[i].out);
// set the port name
GetOutputPort(0).SetName("Out");
}
else
{
CORE_ASSERT(GetNumOutputPorts() >= numChannels);
// add each channel to its individual port
for (uint32 i = 0; i < numChannels; ++i)
{
MultiChannel* multichannel = GetOutputPort(i).GetChannels();
multichannel->Clear();
multichannel->AddChannel(mMapping[i].out);
// set the port name
GetOutputPort(i).SetName(mMapping[i].name);
}
}
}
// delete all output channels and remove them from ports
void ChannelSelectorNode::DeleteOutputChannels()
{
//const uint32 numChannels = mOutputChannels.Size();
const uint32 numOutputPorts = GetNumOutputPorts();
// clear output ports first
for (uint32 i = 0; i < numOutputPorts; ++i)
{
GetOutputPort(i).GetChannels()->Clear();
GetOutputPort(i).SetName("-");
}
// delete the output channels
const uint32 numMappings = mMapping.Size();
for (uint32 i = 0; i < numMappings; i++)
{
delete mMapping[i].out;
mMapping[i].out = 0;
}
mMapping.Clear();
}