Skip to content

Commit

Permalink
Add TriggerRoundRobin node; because this requires an ordered sequence…
Browse files Browse the repository at this point in the history
…, store Node outputs as a vector, not a set
  • Loading branch information
ideoforms committed Aug 13, 2024
1 parent d119f96 commit f6f7161
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 14 deletions.
8 changes: 7 additions & 1 deletion auxiliary/libs/signalflow-stubs/signalflow.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ from __future__ import annotations
import numpy
import typing
import typing_extensions
__all__ = ['ADSREnvelope', 'ASREnvelope', 'Abs', 'Add', 'AllpassDelay', 'AmplitudeToDecibels', 'AudioGraph', 'AudioGraphConfig', 'AudioIOException', 'AudioIn', 'AudioOut', 'AudioOut_Abstract', 'AudioOut_Dummy', 'AzimuthPanner', 'BeatCutter', 'BiquadFilter', 'Buffer', 'Buffer2D', 'BufferLooper', 'BufferPlayer', 'BufferRecorder', 'CPUUsageAboveLimitException', 'ChannelArray', 'ChannelCrossfade', 'ChannelMixer', 'ChannelPanner', 'ChannelSelect', 'Clip', 'ClockDivider', 'CombDelay', 'Compressor', 'Constant', 'Cos', 'Counter', 'CrossCorrelate', 'DCFilter', 'DecibelsToAmplitude', 'DetectSilence', 'DeviceNotFoundException', 'Divide', 'EQ', 'Envelope', 'EnvelopeBuffer', 'Equal', 'Euclidean', 'FFT', 'FFTBuffer', 'FFTBufferPlayer', 'FFTContinuousPhaseVocoder', 'FFTContrast', 'FFTConvolve', 'FFTCrossFade', 'FFTFindPeaks', 'FFTLFO', 'FFTLPF', 'FFTMagnitudePhaseArray', 'FFTNode', 'FFTNoiseGate', 'FFTOpNode', 'FFTPhaseVocoder', 'FFTRandomPhase', 'FFTScaleMagnitudes', 'FFTTonality', 'FFTTransform', 'FeedbackBufferReader', 'FeedbackBufferWriter', 'FlipFlop', 'Fold', 'FrequencyToMidiNote', 'Gate', 'Granulator', 'GraphAlreadyCreatedException', 'GraphNotCreatedException', 'GreaterThan', 'GreaterThanOrEqual', 'IFFT', 'If', 'Impulse', 'ImpulseSequence', 'Index', 'InsufficientBufferSizeException', 'InvalidChannelCountException', 'KDTree', 'KDTreeMatch', 'LFO', 'Latch', 'LessThan', 'LessThanOrEqual', 'Line', 'Logistic', 'Maximiser', 'MidiNoteToFrequency', 'Modulo', 'MoogVCF', 'MouseDown', 'MouseX', 'MouseY', 'Multiply', 'NearestNeighbour', 'Node', 'NodeAlreadyPlayingException', 'NodeNotPlayingException', 'NodeRegistry', 'NotEqual', 'OneTapDelay', 'OnsetDetector', 'Patch', 'PatchFinishedPlaybackException', 'PatchRegistry', 'PatchSpec', 'PinkNoise', 'Pow', 'RMS', 'RandomBrownian', 'RandomChoice', 'RandomCoin', 'RandomExponential', 'RandomExponentialDist', 'RandomGaussian', 'RandomImpulse', 'RandomImpulseSequence', 'RandomUniform', 'RectangularEnvelope', 'Resample', 'Round', 'RoundToScale', 'SIGNALFLOW_DEFAULT_BLOCK_SIZE', 'SIGNALFLOW_DEFAULT_FFT_HOP_SIZE', 'SIGNALFLOW_DEFAULT_FFT_SIZE', 'SIGNALFLOW_DEFAULT_SAMPLE_RATE', 'SIGNALFLOW_DEFAULT_TRIGGER', 'SIGNALFLOW_EVENT_DISTRIBUTION_POISSON', 'SIGNALFLOW_EVENT_DISTRIBUTION_UNIFORM', 'SIGNALFLOW_FILTER_TYPE_BAND_PASS', 'SIGNALFLOW_FILTER_TYPE_HIGH_PASS', 'SIGNALFLOW_FILTER_TYPE_HIGH_SHELF', 'SIGNALFLOW_FILTER_TYPE_LOW_PASS', 'SIGNALFLOW_FILTER_TYPE_LOW_SHELF', 'SIGNALFLOW_FILTER_TYPE_NOTCH', 'SIGNALFLOW_FILTER_TYPE_PEAK', 'SIGNALFLOW_INTERPOLATION_MODE_COSINE', 'SIGNALFLOW_INTERPOLATION_MODE_LINEAR', 'SIGNALFLOW_INTERPOLATION_MODE_NONE', 'SIGNALFLOW_MAX_CHANNELS', 'SIGNALFLOW_MAX_FFT_SIZE', 'SIGNALFLOW_NODE_BUFFER_SIZE', 'SIGNALFLOW_NODE_INITIAL_OUTPUT_CHANNELS', 'SIGNALFLOW_NODE_STATE_ACTIVE', 'SIGNALFLOW_NODE_STATE_STOPPED', 'SIGNALFLOW_PATCH_STATE_ACTIVE', 'SIGNALFLOW_PATCH_STATE_STOPPED', 'SVFilter', 'SampleAndHold', 'SawLFO', 'SawOscillator', 'ScaleLinExp', 'ScaleLinLin', 'SegmentPlayer', 'SegmentedGranulator', 'Sequence', 'Sin', 'SineLFO', 'SineOscillator', 'Smooth', 'SpatialEnvironment', 'SpatialPanner', 'SpatialSpeaker', 'SquareLFO', 'SquareOscillator', 'Squiz', 'StereoBalance', 'StereoPanner', 'StereoWidth', 'StochasticNode', 'Stutter', 'Subtract', 'Sum', 'Tan', 'Tanh', 'TimeShift', 'TriangleLFO', 'TriangleOscillator', 'TriggerMult', 'UnknownTriggerNameException', 'VampAnalysis', 'WaveShaper', 'WaveShaperBuffer', 'Wavetable', 'Wavetable2D', 'WavetableBuffer', 'WetDry', 'WhiteNoise', 'Wrap', 'amplitude_to_db', 'clip', 'db_to_amplitude', 'fold', 'frequency_to_midi_note', 'midi_note_to_frequency', 'random_exponential', 'random_integer', 'random_seed', 'random_uniform', 'save_block_to_text_file', 'save_block_to_wav_file', 'scale_exp_lin', 'scale_lin_exp', 'scale_lin_lin', 'signalflow_event_distribution_t', 'signalflow_filter_type_t', 'signalflow_interpolation_mode_t', 'signalflow_node_state_t', 'signalflow_patch_state_t', 'wrap']
__all__ = ['ADSREnvelope', 'ASREnvelope', 'Abs', 'Add', 'AllpassDelay', 'AmplitudeToDecibels', 'AudioGraph', 'AudioGraphConfig', 'AudioIOException', 'AudioIn', 'AudioOut', 'AudioOut_Abstract', 'AudioOut_Dummy', 'AzimuthPanner', 'BeatCutter', 'BiquadFilter', 'Buffer', 'Buffer2D', 'BufferLooper', 'BufferPlayer', 'BufferRecorder', 'CPUUsageAboveLimitException', 'ChannelArray', 'ChannelCrossfade', 'ChannelMixer', 'ChannelPanner', 'ChannelSelect', 'Clip', 'ClockDivider', 'CombDelay', 'Compressor', 'Constant', 'Cos', 'Counter', 'CrossCorrelate', 'DCFilter', 'DecibelsToAmplitude', 'DetectSilence', 'DeviceNotFoundException', 'Divide', 'EQ', 'Envelope', 'EnvelopeBuffer', 'Equal', 'Euclidean', 'FFT', 'FFTBuffer', 'FFTBufferPlayer', 'FFTContinuousPhaseVocoder', 'FFTContrast', 'FFTConvolve', 'FFTCrossFade', 'FFTFindPeaks', 'FFTLFO', 'FFTLPF', 'FFTMagnitudePhaseArray', 'FFTNode', 'FFTNoiseGate', 'FFTOpNode', 'FFTPhaseVocoder', 'FFTRandomPhase', 'FFTScaleMagnitudes', 'FFTTonality', 'FFTTransform', 'FeedbackBufferReader', 'FeedbackBufferWriter', 'FlipFlop', 'Fold', 'FrequencyToMidiNote', 'Gate', 'Granulator', 'GraphAlreadyCreatedException', 'GraphNotCreatedException', 'GreaterThan', 'GreaterThanOrEqual', 'IFFT', 'If', 'Impulse', 'ImpulseSequence', 'Index', 'InsufficientBufferSizeException', 'InvalidChannelCountException', 'KDTree', 'KDTreeMatch', 'LFO', 'Latch', 'LessThan', 'LessThanOrEqual', 'Line', 'Logistic', 'Maximiser', 'MidiNoteToFrequency', 'Modulo', 'MoogVCF', 'MouseDown', 'MouseX', 'MouseY', 'Multiply', 'NearestNeighbour', 'Node', 'NodeAlreadyPlayingException', 'NodeNotPlayingException', 'NodeRegistry', 'NotEqual', 'OneTapDelay', 'OnsetDetector', 'Patch', 'PatchFinishedPlaybackException', 'PatchRegistry', 'PatchSpec', 'PinkNoise', 'Pow', 'RMS', 'RandomBrownian', 'RandomChoice', 'RandomCoin', 'RandomExponential', 'RandomExponentialDist', 'RandomGaussian', 'RandomImpulse', 'RandomImpulseSequence', 'RandomUniform', 'RectangularEnvelope', 'Resample', 'Round', 'RoundToScale', 'SIGNALFLOW_DEFAULT_BLOCK_SIZE', 'SIGNALFLOW_DEFAULT_FFT_HOP_SIZE', 'SIGNALFLOW_DEFAULT_FFT_SIZE', 'SIGNALFLOW_DEFAULT_SAMPLE_RATE', 'SIGNALFLOW_DEFAULT_TRIGGER', 'SIGNALFLOW_EVENT_DISTRIBUTION_POISSON', 'SIGNALFLOW_EVENT_DISTRIBUTION_UNIFORM', 'SIGNALFLOW_FILTER_TYPE_BAND_PASS', 'SIGNALFLOW_FILTER_TYPE_HIGH_PASS', 'SIGNALFLOW_FILTER_TYPE_HIGH_SHELF', 'SIGNALFLOW_FILTER_TYPE_LOW_PASS', 'SIGNALFLOW_FILTER_TYPE_LOW_SHELF', 'SIGNALFLOW_FILTER_TYPE_NOTCH', 'SIGNALFLOW_FILTER_TYPE_PEAK', 'SIGNALFLOW_INTERPOLATION_MODE_COSINE', 'SIGNALFLOW_INTERPOLATION_MODE_LINEAR', 'SIGNALFLOW_INTERPOLATION_MODE_NONE', 'SIGNALFLOW_MAX_CHANNELS', 'SIGNALFLOW_MAX_FFT_SIZE', 'SIGNALFLOW_NODE_BUFFER_SIZE', 'SIGNALFLOW_NODE_INITIAL_OUTPUT_CHANNELS', 'SIGNALFLOW_NODE_STATE_ACTIVE', 'SIGNALFLOW_NODE_STATE_STOPPED', 'SIGNALFLOW_PATCH_STATE_ACTIVE', 'SIGNALFLOW_PATCH_STATE_STOPPED', 'SVFilter', 'SampleAndHold', 'SawLFO', 'SawOscillator', 'ScaleLinExp', 'ScaleLinLin', 'SegmentPlayer', 'SegmentedGranulator', 'Sequence', 'Sin', 'SineLFO', 'SineOscillator', 'Smooth', 'SpatialEnvironment', 'SpatialPanner', 'SpatialSpeaker', 'SquareLFO', 'SquareOscillator', 'Squiz', 'StereoBalance', 'StereoPanner', 'StereoWidth', 'StochasticNode', 'Stutter', 'Subtract', 'Sum', 'Tan', 'Tanh', 'TimeShift', 'TriangleLFO', 'TriangleOscillator', 'TriggerMult', 'TriggerRoundRobin', 'UnknownTriggerNameException', 'VampAnalysis', 'WaveShaper', 'WaveShaperBuffer', 'Wavetable', 'Wavetable2D', 'WavetableBuffer', 'WetDry', 'WhiteNoise', 'Wrap', 'amplitude_to_db', 'clip', 'db_to_amplitude', 'fold', 'frequency_to_midi_note', 'midi_note_to_frequency', 'random_exponential', 'random_integer', 'random_seed', 'random_uniform', 'save_block_to_text_file', 'save_block_to_wav_file', 'scale_exp_lin', 'scale_lin_exp', 'scale_lin_lin', 'signalflow_event_distribution_t', 'signalflow_filter_type_t', 'signalflow_interpolation_mode_t', 'signalflow_node_state_t', 'signalflow_patch_state_t', 'wrap']
class ADSREnvelope(Node):
"""
Attack-decay-sustain-release envelope. Sustain portion is held until gate is zero.
Expand Down Expand Up @@ -1780,6 +1780,12 @@ class TriggerMult(Node):
"""
def __init__(self, a: Node = 0) -> None:
...
class TriggerRoundRobin(Node):
"""
Relay trigger() events to a single node from the list of connected outputs, with `direction` determining the direction: 1 (or above) = move forwards by N, -1 = move backwards by N, 0 = stationary.
"""
def __init__(self, direction: Node = 1) -> None:
...
class UnknownTriggerNameException(Exception):
pass
class VampAnalysis(Node):
Expand Down
6 changes: 6 additions & 0 deletions source/include/signalflow/buffer/wavetable-buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ namespace signalflow
* A WavetableBuffer is a mono buffer with a fixed number of samples,
* which can be sampled at a position [0,1] to give a bipolar [-1, 1]
* amplitude value, intended for use with the Wavetable node.
*
* At some point, it would be nice to support multi-sampled wavetables
* band-limited per octave.
*
* Some useful notes:
* https://www.earlevel.com/main/category/digital-audio/oscillators/wavetable-oscillators/
*-----------------------------------------------------------------------*/
class WavetableBuffer : public Buffer
{
Expand Down
4 changes: 2 additions & 2 deletions source/include/signalflow/node/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class Node
virtual void set_value(float value);

std::map<std::string, NodeRef *> get_inputs();
std::set<std::pair<Node *, std::string>> get_outputs();
std::vector<std::pair<Node *, std::string>> get_outputs();
std::map<std::string, PropertyRef *> get_properties();
std::map<std::string, BufferRef *> get_buffers();

Expand Down Expand Up @@ -347,7 +347,7 @@ class Node
* Note that a node may modulate two different parameters of the same
* node.
*-----------------------------------------------------------------------*/
std::set<std::pair<Node *, std::string>> outputs;
std::vector<std::pair<Node *, std::string>> outputs;

/*------------------------------------------------------------------------
* Hash table of properties: (name, PropertyRef *)
Expand Down
31 changes: 31 additions & 0 deletions source/include/signalflow/node/sequencing/trigger-round-robin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

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

namespace signalflow
{

/**--------------------------------------------------------------------------------*
* Relay trigger() events to a single node from the list of connected outputs,
* with `direction` determining the direction: 1 (or above) = move forwards by N,
* -1 = move backwards by N, 0 = stationary.
*---------------------------------------------------------------------------------*/
class TriggerRoundRobin : public Node
{

public:
TriggerRoundRobin(NodeRef direction = 1);

virtual void process(Buffer &out, int num_frames) override;
virtual void trigger(std::string = SIGNALFLOW_DEFAULT_TRIGGER,
float value = SIGNALFLOW_NULL_FLOAT) override;

private:
unsigned int current_output_index = 0;
NodeRef direction;
};

REGISTER(TriggerRoundRobin, "trigger-round-robin")

}
3 changes: 2 additions & 1 deletion source/include/signalflow/signalflow.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
#include <signalflow/node/operators/subtract.h>
#include <signalflow/node/operators/sum.h>
#include <signalflow/node/operators/time-shift.h>
#include <signalflow/node/operators/trigger-mult.h>
#include <signalflow/node/operators/trigonometry.h>

/*------------------------------------------------------------------------
Expand Down Expand Up @@ -168,6 +167,8 @@
#include <signalflow/node/sequencing/index.h>
#include <signalflow/node/sequencing/latch.h>
#include <signalflow/node/sequencing/sequence.h>
#include <signalflow/node/sequencing/trigger-mult.h>
#include <signalflow/node/sequencing/trigger-round-robin.h>

/*------------------------------------------------------------------------
* Analysis and MIR
Expand Down
3 changes: 2 additions & 1 deletion source/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ set(SRC ${SRC}
${CMAKE_CURRENT_SOURCE_DIR}/node/operators/subtract.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/operators/sum.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/operators/trigonometry.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/operators/trigger-mult.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/operators/comparison.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/operators/time-shift.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/oscillators/constant.cpp
Expand Down Expand Up @@ -136,6 +135,8 @@ set(SRC ${SRC}
${CMAKE_CURRENT_SOURCE_DIR}/node/sequencing/sequence.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/sequencing/impulse-sequence.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/sequencing/euclidean.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/sequencing/trigger-mult.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/sequencing/trigger-round-robin.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/analysis/vamp.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/analysis/cross-correlate.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/analysis/nearest-neighbour.cpp
Expand Down
17 changes: 14 additions & 3 deletions source/src/node/node.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <algorithm>

#include "signalflow/node/node.h"

#include "signalflow/node/operators/add.h"
Expand Down Expand Up @@ -308,7 +310,7 @@ std::map<std::string, NodeRef *> Node::get_inputs()
return this->inputs;
}

std::set<std::pair<Node *, std::string>> Node::get_outputs()
std::vector<std::pair<Node *, std::string>> Node::get_outputs()
{
return this->outputs;
}
Expand Down Expand Up @@ -413,12 +415,21 @@ void Node::remove_input(NodeRef input)

void Node::add_output(Node *target, std::string name)
{
this->outputs.insert(std::make_pair(target, name));
this->outputs.push_back(std::make_pair(target, name));
}

void Node::remove_output(Node *target, std::string name)
{
this->outputs.erase(std::make_pair(target, name));
std::vector<std::pair<Node *, std::string>>::iterator iter;

for (iter = this->outputs.begin(); iter != this->outputs.end();)
{
if (iter->second == name)
{
this->outputs.erase(iter);
break;
}
}
}

bool Node::get_matches_input_channels()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "signalflow/node/operators/trigger-mult.h"
#include "signalflow/node/sequencing/trigger-mult.h"
#include <math.h>

namespace signalflow
Expand All @@ -16,8 +16,7 @@ void TriggerMult::process(Buffer &out, int num_frames)
{
for (int frame = 0; frame < num_frames; frame++)
{
float value = this->input->out[channel][frame];
out[channel][frame] = value;
out[channel][frame] = this->input->out[channel][frame];
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions source/src/node/sequencing/trigger-round-robin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "signalflow/node/sequencing/trigger-round-robin.h"
#include <math.h>

namespace signalflow
{

TriggerRoundRobin::TriggerRoundRobin(NodeRef direction)
: Node(), direction(direction)
{
this->name = "trigger-round-robin";
this->create_input("direction", this->direction);
}

void TriggerRoundRobin::process(Buffer &out, int num_frames)
{
for (int channel = 0; channel < this->num_output_channels; channel++)
{
for (int frame = 0; frame < num_frames; frame++)
{
out[channel][frame] = 0.0;
}
}
}

void TriggerRoundRobin::trigger(std::string name, float value)
{
for (size_t index = 0; index < this->outputs.size(); index++)
{
if (index == current_output_index)
{
int direction = this->direction->out[0][0];
auto output_node = this->outputs[index].first;
output_node->trigger(name, value);
this->current_output_index = (this->current_output_index + direction) % outputs.size();
break;
}
}
}

}
9 changes: 6 additions & 3 deletions source/src/python/nodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,6 @@ void init_python_nodes(py::module &m)
py::class_<TimeShift, Node, NodeRefTemplate<TimeShift>>(m, "TimeShift", "TimeShift")
.def(py::init<NodeRef>(), "a"_a = 0);

py::class_<TriggerMult, Node, NodeRefTemplate<TriggerMult>>(m, "TriggerMult", "Distribute any triggers to all output nodes.")
.def(py::init<NodeRef>(), "a"_a = 0);

py::class_<Sin, Node, NodeRefTemplate<Sin>>(m, "Sin", "Outputs sin(a), per sample.")
.def(py::init<NodeRef>(), "a"_a = 0);

Expand Down Expand Up @@ -412,6 +409,12 @@ void init_python_nodes(py::module &m)
py::class_<Sequence, Node, NodeRefTemplate<Sequence>>(m, "Sequence", "Outputs the elements in `sequence`, incrementing position on each `clock`.")
.def(py::init<std::vector<float>, NodeRef>(), "sequence"_a = std::vector<float>(), "clock"_a = nullptr);

py::class_<TriggerMult, Node, NodeRefTemplate<TriggerMult>>(m, "TriggerMult", "Distribute any triggers to all output nodes.")
.def(py::init<NodeRef>(), "a"_a = 0);

py::class_<TriggerRoundRobin, Node, NodeRefTemplate<TriggerRoundRobin>>(m, "TriggerRoundRobin", "Relay trigger() events to a single node from the list of connected outputs, with `direction` determining the direction: 1 (or above) = move forwards by N, -1 = move backwards by N, 0 = stationary.")
.def(py::init<NodeRef>(), "direction"_a = 1);

py::class_<Logistic, Node, NodeRefTemplate<Logistic>>(m, "Logistic", "Logistic noise.")
.def(py::init<NodeRef, NodeRef>(), "chaos"_a = 3.7, "frequency"_a = 0.0);

Expand Down

0 comments on commit f6f7161

Please sign in to comment.