16 if (promise.should_terminate) {
26 for (
const auto& [time, callback] :
sequence) {
37 promise_ref.set_state(
"current_value", start_value);
38 promise_ref.set_state(
"end_value", end_value);
39 promise_ref.set_state(
"restart",
false);
41 const unsigned int sample_rate = scheduler.
get_rate();
42 if (step_duration < 1) {
46 uint64_t total_samples = (uint64_t)duration_seconds * sample_rate;
47 float per_sample_step = (end_value - start_value) / (
float)total_samples;
48 float sample_step = per_sample_step * (float)step_duration;
50 promise_ref.set_state(
"step", sample_step);
53 auto current_value = promise_ref.get_state<
float>(
"current_value");
54 auto last_value = promise_ref.get_state<
float>(
"end_value");
55 auto step = promise_ref.get_state<
float>(
"step");
57 if (!current_value || !last_value || !step) {
62 *current_value = start_value;
64 uint64_t samples_elapsed = 0;
68 while (samples_elapsed < total_samples) {
69 *current_value += *step;
71 if ((*step > 0 && *current_value >= *last_value) || (*step < 0 && *current_value <= *last_value)) {
72 *current_value = *last_value;
75 samples_elapsed += step_duration;
82 auto restart_requested = promise_ref.get_state<
bool>(
"restart");
83 if (restart_requested && *restart_requested) {
84 *restart_requested =
false;
88 promise_ref.auto_resume =
false;
89 co_await std::suspend_always {};
99 std::any value = pattern_func(step++);
107 std::function<
void()> callback,
108 std::shared_ptr<Nodes::Generator::Logic> logic_node,
114 logic_node = std::make_shared<Nodes::Generator::Logic>(0.5);
128 if (promise_ref.should_terminate) {
132 logic_node->process_sample(0.0);
141 std::function<
void()> callback,
142 std::shared_ptr<Nodes::Generator::Logic> logic_node)
147 logic_node = std::make_shared<Nodes::Generator::Logic>(0.5);
150 logic_node->on_change_to(target_state,
156 if (promise_ref.should_terminate) {
160 logic_node->process_sample(0.0);
168 std::function<
void()> callback,
169 std::shared_ptr<Nodes::Generator::Logic> logic_node)
174 logic_node = std::make_shared<Nodes::Generator::Logic>(0.5);
182 if (promise_ref.should_terminate) {
186 logic_node->process_sample(0.0);
#define MF_ERROR(comp, ctx,...)
Base context class for node callbacks.
A C++20 coroutine-based audio processing task with sample-accurate timing.
uint64_t seconds_to_samples(double seconds) const
Converts a time in seconds to a number of samples.
unsigned int get_rate(ProcessingToken token=ProcessingToken::SAMPLE_ACCURATE) const
Get processing rate for a domain.
Token-based multimodal task scheduling system for unified coroutine processing.
@ CoroutineScheduling
Coroutine scheduling and temporal coordination (Vruta::TaskScheduler)
@ Kriya
Automatable tasks and fluent scheduling api for Nodes and Buffers.
Vruta::SoundRoutine sequence(Vruta::TaskScheduler &scheduler, std::vector< std::pair< double, std::function< void()> > > sequence)
Creates a temporal sequence that executes callbacks at specified time offsets.
Vruta::SoundRoutine Trigger(Vruta::TaskScheduler &scheduler, bool target_state, std::function< void()> callback, std::shared_ptr< Nodes::Generator::Logic > logic_node)
Coroutine that executes callback when logic node changes to specific state.
Vruta::SoundRoutine pattern(Vruta::TaskScheduler &scheduler, std::function< std::any(uint64_t)> pattern_func, std::function< void(std::any)> callback, double interval_seconds)
Creates a generative algorithm that produces values based on a pattern function.
Vruta::SoundRoutine Toggle(Vruta::TaskScheduler &scheduler, std::function< void()> callback, std::shared_ptr< Nodes::Generator::Logic > logic_node)
Coroutine that executes callback on any logic node state change.
Vruta::SoundRoutine line(Vruta::TaskScheduler &scheduler, float start_value, float end_value, float duration_seconds, uint32_t step_duration, bool restartable)
Creates a continuous interpolation generator between two values over time.
Vruta::SoundRoutine Gate(Vruta::TaskScheduler &scheduler, std::function< void()> callback, std::shared_ptr< Nodes::Generator::Logic > logic_node, bool open)
Coroutine that executes callback continuously while logic node outputs true.
Vruta::SoundRoutine metro(Vruta::TaskScheduler &scheduler, double interval_seconds, std::function< void()> callback)
Creates a periodic event generator that executes a callback at regular intervals.
Templated awaitable for accessing a coroutine's promise object.
Awaitable object for precise sample-accurate timing delays.