From 51f970beececfdbcdd823c653a19414e210ff91b Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Mon, 29 Jul 2024 22:42:29 +0100 Subject: [PATCH] SegmentPlayer: Add start_offset input --- docs/library/buffer/segmentplayer/index.md | 2 +- .../signalflow/node/buffer/segment-player.h | 2 ++ source/src/node/buffer/segment-player.cpp | 15 ++++++++++++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/docs/library/buffer/segmentplayer/index.md b/docs/library/buffer/segmentplayer/index.md index 5185f74d..fa7cfe14 100644 --- a/docs/library/buffer/segmentplayer/index.md +++ b/docs/library/buffer/segmentplayer/index.md @@ -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. diff --git a/source/include/signalflow/node/buffer/segment-player.h b/source/include/signalflow/node/buffer/segment-player.h index c42b8214..1f2fb0cc 100644 --- a/source/include/signalflow/node/buffer/segment-player.h +++ b/source/include/signalflow/node/buffer/segment-player.h @@ -20,6 +20,7 @@ class SegmentPlayer : public Node std::vector onsets = {}, NodeRef index = nullptr, NodeRef rate = 1.0, + NodeRef start_offset = nullptr, NodeRef clock = nullptr, NodeRef continue_after_segment = 0); @@ -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; diff --git a/source/src/node/buffer/segment-player.cpp b/source/src/node/buffer/segment-player.cpp index 6b00251c..ae7efde9 100644 --- a/source/src/node/buffer/segment-player.cpp +++ b/source/src/node/buffer/segment-player.cpp @@ -8,8 +8,8 @@ namespace signalflow { SegmentPlayer::SegmentPlayer(BufferRef buffer, std::vector 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"; @@ -19,6 +19,7 @@ SegmentPlayer::SegmentPlayer(BufferRef buffer, std::vector 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); @@ -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 onsets = onsetsref->float_array_value(); @@ -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();