Skip to content

Commit

Permalink
Add SelectInput node
Browse files Browse the repository at this point in the history
  • Loading branch information
ideoforms committed Aug 15, 2024
1 parent b45d29a commit 0e74164
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
33 changes: 33 additions & 0 deletions source/include/signalflow/node/operators/select-input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include "signalflow/core/constants.h"
#include "signalflow/node/node.h"

#include <list>

namespace signalflow
{

/**--------------------------------------------------------------------------------*
* Generates the
*---------------------------------------------------------------------------------*/
class SelectInput : public VariableInputNode
{

public:
SelectInput(NodeRef index = 0);
SelectInput(std::initializer_list<NodeRef> inputs, NodeRef index = 0);
SelectInput(std::vector<NodeRef> inputs, NodeRef index = 0);
SelectInput(std::vector<int> inputs, NodeRef index = 0);
SelectInput(std::vector<float> inputs, NodeRef index = 0);

virtual void process(Buffer &out, int num_frames);

protected:
void init(NodeRef index);
NodeRef index;
};

REGISTER(SelectInput, "select-input")

}
69 changes: 69 additions & 0 deletions source/src/node/operators/select-input.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "signalflow/core/core.h"
#include "signalflow/node/operators/select-input.h"

namespace signalflow
{
void SelectInput::init(NodeRef index)
{
this->name = "select-input";

this->index = index;
this->create_input("index", this->index);
}

SelectInput::SelectInput(NodeRef index)
: VariableInputNode()
{
this->init(index);
}

SelectInput::SelectInput(std::initializer_list<NodeRef> inputs, NodeRef index)
: VariableInputNode(inputs)
{
this->init(index);
}

SelectInput::SelectInput(std::vector<NodeRef> inputs, NodeRef index)
: VariableInputNode(inputs)
{
this->init(index);
}

SelectInput::SelectInput(std::vector<float> inputs, NodeRef index)
: VariableInputNode(inputs)
{
this->init(index);
}

SelectInput::SelectInput(std::vector<int> inputs, NodeRef index)
: VariableInputNode(inputs)
{
this->init(index);
}

void SelectInput::process(Buffer &out, int num_frames)
{
if (this->input_list.size() == 0)
{
signalflow_audio_thread_error("SelectInput: No inputs were passed to select from");
}

int index = 0;
int last_index = -1;
NodeRef selected_node = nullptr;
for (int frame = 0; frame < num_frames; frame++)
{
index = ((int) this->index->out[0][frame]) % this->input_list.size();
if (index != last_index)
{
selected_node = *std::next(this->input_list.begin(), index);
last_index = index;
}
for (int channel = 0; channel < this->get_num_output_channels(); channel++)
{
out[channel][frame] = selected_node->out[channel][frame];
}
}
}

}

0 comments on commit 0e74164

Please sign in to comment.