Skip to content

Commit

Permalink
Add SIGNALFLOW_NULL_FLOAT, and use as default trigger() value argument
Browse files Browse the repository at this point in the history
  • Loading branch information
ideoforms committed Feb 14, 2024
1 parent 7397cda commit b59a394
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
4 changes: 2 additions & 2 deletions auxiliary/libs/signalflow-stubs/signalflow.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ class FFTRandomPhase(FFTOpNode):
"""
Randomise phase values.
"""
def __init__(self, input: Node = 0) -> None:
def __init__(self, input: Node = 0, level: Node = 1.0) -> None:
...
class FFTTonality(FFTOpNode):
"""
Expand Down Expand Up @@ -866,7 +866,7 @@ class LessThanOrEqual(Node):
...
class Line(Node):
"""
Line segment with the given start/end values and duration. If loop is true, repeats indefinitely. Retriggers on a clock signal.
Line segment with the given start/end values, and duration (in seconds). If loop is true, repeats indefinitely. Retriggers on a clock signal.
"""
def __init__(self, start: Node = 0.0, end: Node = 1.0, time: Node = 1.0, loop: Node = 0, clock: Node = None) -> None:
...
Expand Down
6 changes: 6 additions & 0 deletions source/include/signalflow/core/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "signalflow/buffer/ringbuffer.h"
#include "signalflow/core/exceptions.h"

#include <limits>
#include <map>
#include <stdlib.h>
#include <string>
Expand Down Expand Up @@ -31,6 +32,11 @@ typedef RingBuffer<sample> SampleRingBuffer;
(void) (expr); \
} while (0)

/*------------------------------------------------------------------------
* Sentinel value, used to represent an undefined or unset float value.
*-----------------------------------------------------------------------*/
#define SIGNALFLOW_NULL_FLOAT std::numeric_limits<float>::max()

/*------------------------------------------------------------------------
* Max supported number of output channels. Impacts memory usage.
* TODO: Retire this constant.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class SegmentedGranulator : public Node
NodeRef max_grains = 2048);

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

protected:
Expand All @@ -38,7 +39,7 @@ class SegmentedGranulator : public Node
std::vector<float> durations;
float rate_scale_factor;
std::vector<Grain *> grains;
std::mutex mutex;
bool triggered = false;
};

REGISTER(SegmentedGranulator, "segmented-granulator")
Expand Down
2 changes: 1 addition & 1 deletion source/include/signalflow/node/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class Node
* Generic trigger method.
*-----------------------------------------------------------------------*/
virtual void trigger(std::string name = SIGNALFLOW_DEFAULT_TRIGGER,
float value = 1);
float value = SIGNALFLOW_NULL_FLOAT);

/*------------------------------------------------------------------------
* Print the node's output value to stdout at a specified frequency.
Expand Down
2 changes: 1 addition & 1 deletion source/include/signalflow/patch/patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class Patch
* Trigger this Patch.
*--------------------------------------------------------------------------------*/
void trigger(std::string name = SIGNALFLOW_DEFAULT_TRIGGER,
float value = 1.0);
float value = SIGNALFLOW_NULL_FLOAT);

/**--------------------------------------------------------------------------------
* Parse a template from live Node objects to create a network of NodeDefs
Expand Down
24 changes: 14 additions & 10 deletions source/src/node/buffer/granulation/grainsegments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ void SegmentedGranulator::process(Buffer &out, int num_frames)

for (int frame = 0; frame < num_frames; frame++)
{
if (SIGNALFLOW_CHECK_TRIGGER(this->clock, frame))
if (SIGNALFLOW_CHECK_TRIGGER(this->clock, frame) || this->triggered)
{
this->triggered = false;
int index = (int) this->index->out[0][frame];
if (index < 0 || index >= this->onset_times.size())
{
Expand All @@ -78,9 +79,7 @@ void SegmentedGranulator::process(Buffer &out, int num_frames)
this->onset_times[index] * buffer->get_sample_rate(),
this->durations[index] * buffer->get_sample_rate(),
this->rate->out[0][frame] * this->rate_scale_factor);
this->mutex.lock();
this->grains.push_back(grain);
this->mutex.unlock();
}
}

Expand Down Expand Up @@ -109,9 +108,7 @@ void SegmentedGranulator::process(Buffer &out, int num_frames)
else
{
delete grain;
this->mutex.lock();
grains.erase(it);
this->mutex.unlock();
}
}
}
Expand All @@ -121,12 +118,21 @@ void SegmentedGranulator::trigger(std::string name, float value)
{
/*--------------------------------------------------------------------------------
* TODO: Don't repeat this same block of code from the trigger block above
* TODO: How to honour `value` here, if specified?
* Perhaps the default should be a null value, that gets ignored in favour
* of `index` if not specified.
*--------------------------------------------------------------------------------*/
if (name == SIGNALFLOW_DEFAULT_TRIGGER)
{
if (value == SIGNALFLOW_NULL_FLOAT)
{
/*--------------------------------------------------------------------------------
* If triggered without an explicitly-specified index, defer the trigger
* until the next process() block. This is because, if the .index property of
* the granulator is set at the same time as trigger() is called, the .index
* update won't take effect until the next process().
*--------------------------------------------------------------------------------*/
this->triggered = true;
return;
}

int index = (int) value;
if (index < 0 || index >= this->onset_times.size())
{
Expand All @@ -139,9 +145,7 @@ void SegmentedGranulator::trigger(std::string name, float value)
this->onset_times[index] * buffer->get_sample_rate(),
this->durations[index] * buffer->get_sample_rate(),
this->rate->out[0][0] * this->rate_scale_factor);
this->mutex.lock();
this->grains.push_back(grain);
this->mutex.unlock();
}
}
}
Expand Down

0 comments on commit b59a394

Please sign in to comment.