Skip to content

Commit

Permalink
SegmentPlayer: Add start_offset input
Browse files Browse the repository at this point in the history
  • Loading branch information
ideoforms committed Jul 29, 2024
1 parent 6ff5619 commit 51f970b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/library/buffer/segmentplayer/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: SegmentPlayer: Trigger segments of `buffer` at the given list of `o
# SegmentPlayer

```python
SegmentPlayer(buffer=None, onsets={}, index=None, rate=1.0, clock=None, continue_after_segment=0)
SegmentPlayer(buffer=None, onsets={}, index=None, rate=1.0, start_offset=None, clock=None, continue_after_segment=0)
```

Trigger segments of `buffer` at the given list of `onsets` positions, in seconds. `index` determines the index of the onset to play back at, which can also be passed as an argument to trigger(). `rate` determines the playback rate, and `clock` can be used to retrigger based on the output of another Node. If `continue_after_segment` is non-zero, playback will continue after the subsequent onset.
Expand Down
2 changes: 2 additions & 0 deletions source/include/signalflow/node/buffer/segment-player.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class SegmentPlayer : public Node
std::vector<float> onsets = {},
NodeRef index = nullptr,
NodeRef rate = 1.0,
NodeRef start_offset = nullptr,
NodeRef clock = nullptr,
NodeRef continue_after_segment = 0);

Expand All @@ -41,6 +42,7 @@ class SegmentPlayer : public Node
double segment_end_phase;
NodeRef index;
NodeRef rate;
NodeRef start_offset;
float rate_scale_factor;
NodeRef clock;
NodeRef continue_after_segment;
Expand Down
15 changes: 12 additions & 3 deletions source/src/node/buffer/segment-player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace signalflow
{

SegmentPlayer::SegmentPlayer(BufferRef buffer, std::vector<float> onsets, NodeRef index, NodeRef rate,
NodeRef clock, NodeRef continue_after_segment)
: buffer(buffer), index(index), rate(rate), clock(clock), continue_after_segment(continue_after_segment)
NodeRef start_offset, NodeRef clock, NodeRef continue_after_segment)
: buffer(buffer), index(index), rate(rate), start_offset(start_offset), clock(clock), continue_after_segment(continue_after_segment)
{
this->name = "segment-player";

Expand All @@ -19,6 +19,7 @@ SegmentPlayer::SegmentPlayer(BufferRef buffer, std::vector<float> onsets, NodeRe
this->set_property("onsets", onsets);
this->create_input("index", this->index);
this->create_input("rate", this->rate);
this->create_input("start_offset", this->start_offset);
this->create_input("clock", this->clock);
this->create_input("continue_after_segment", this->continue_after_segment);

Expand Down Expand Up @@ -85,6 +86,7 @@ void SegmentPlayer::trigger(std::string name, float value)
if (name == SIGNALFLOW_DEFAULT_TRIGGER)
{
PropertyRef onsetsref = this->get_property("onsets");

if (onsetsref)
{
std::vector<float> onsets = onsetsref->float_array_value();
Expand All @@ -107,7 +109,14 @@ void SegmentPlayer::trigger(std::string name, float value)
{
segment_index = random_integer(0, onsets.size());
}
this->phase = onsets[segment_index] * this->buffer->get_sample_rate();

float start_offset = 0.0f;
if (this->start_offset)
{
start_offset = this->start_offset->out[0][0];
}

this->phase = (onsets[segment_index] + start_offset) * this->buffer->get_sample_rate();
if (segment_index < onsets.size() - 1)
{
this->segment_end_phase = onsets[segment_index + 1] * this->buffer->get_sample_rate();
Expand Down

0 comments on commit 51f970b

Please sign in to comment.