MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Tasks.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include "Pattern.hpp"
6
7namespace MayaFlux {
8namespace Vruta {
9 class TaskScheduler;
10 class SoundRoutine;
11 class GraphicsRoutine;
12 class Routine;
13}
14
15namespace Nodes::Generator {
16 class Logic;
17}
18
19namespace Kriya {
20
21 /**
22 * @brief Creates a periodic event generator that executes a callback at regular intervals
23 * @param interval_seconds Time between callback executions in seconds
24 * @param callback Function to execute on each interval
25 * @param token Processing token to determine which scheduler rate to use (default: SAMPLE_ACCURATE)
26 * @return A Routine shared_ptr of type determined by the processing token of the scheduler (SoundRoutine, GraphicsRoutine, etc.)
27 *
28 * The metro task provides a fundamental temporal mechanism for creating
29 * time-based structures in computational systems. It executes the provided
30 * callback function at precise, regular intervals with sample-accurate timing,
31 * enabling the creation of rhythmic patterns, event sequences, and temporal
32 * frameworks that can synchronize across different domains.
33 *
34 * Unlike system timers which can drift due to processing load, this implementation
35 * maintains precise timing by synchronizing with the sample-rate clock, making it
36 * suitable for both audio and cross-domain applications where timing accuracy
37 * is critical.
38 *
39 * Example usage:
40 * ```cpp
41 * // Create a periodic event generator (2Hz)
42 * auto periodic_task = Kriya::metro(0.5, []() {
43 * trigger_event(); // Could affect audio, visuals, data, etc.
44 * });
45 * scheduler->add_task(periodic_task);
46 * ```
47 *
48 * The metro task continues indefinitely until explicitly cancelled, creating
49 * a persistent temporal structure within the computational system.
50 */
51 MAYAFLUX_API std::shared_ptr<Vruta::Routine> metro(double interval_seconds, std::function<void()> callback, Vruta::ProcessingToken token = Vruta::ProcessingToken::SAMPLE_ACCURATE);
52
53 /**
54 * @brief Creates a temporal sequence that executes callbacks at specified time offsets
55 * @param scheduler The task scheduler that will manage this sequence
56 * @param sequence Vector of (time_offset, callback) pairs to execute in order
57 * @param token Processing token to determine which scheduler rate to use (default: SAMPLE_ACCURATE)
58 * @return A Routine shared_ptr of type determined by the processing token of the scheduler (SoundRoutine, GraphicsRoutine, etc.)
59 *
60 * The sequence task enables the creation of precisely timed event chains with
61 * specific temporal relationships. Each event consists of a time offset (in seconds)
62 * and a callback function to execute at that precise moment.
63 *
64 * This mechanism is valuable for creating structured temporal progressions,
65 * algorithmic sequences, or any series of time-based events that require
66 * specific timing relationships. The sequence can coordinate events across
67 * multiple domains (audio, visual, data) with sample-accurate precision.
68 *
69 * Example usage:
70 * ```cpp
71 * // Create a temporal sequence of events
72 * auto event_sequence = Kriya::sequence({
73 * {0.0, []() { trigger_event_a(); }}, // Immediate
74 * {0.5, []() { trigger_event_b(); }}, // 0.5 seconds later
75 * {1.0, []() { trigger_event_c(); }}, // 1.0 seconds later
76 * {1.5, []() { trigger_event_d(); }} // 1.5 seconds later
77 * });
78 * scheduler->add_task(event_sequence);
79 * ```
80 *
81 * The sequence task completes after executing all events in the defined timeline.
82 */
83 MAYAFLUX_API std::shared_ptr<Vruta::Routine> sequence(std::vector<std::pair<double, std::function<void()>>> sequence, Vruta::ProcessingToken token = Vruta::ProcessingToken::SAMPLE_ACCURATE);
84
85 /**
86 * @brief Creates a continuous interpolation generator between two values over time
87 * @param start_value Initial value of the interpolation
88 * @param end_value Final value of the interpolation
89 * @param duration_seconds Total duration of the interpolation in seconds
90 * @param step_duration Number of samples between value updates (default: 5)
91 * @param restartable Whether the interpolation can be restarted after completion (default: false)
92 * @return A SoundRoutine that implements the interpolation behavior
93 *
94 * The line task generates a linear interpolation between two numerical values
95 * over a specified duration. This creates continuous, gradual transitions that
96 * can be applied to any parameter in a computational system - from audio
97 * parameters to visual properties, physical simulation values, or data
98 * transformation coefficients.
99 *
100 * The current value of the interpolation is stored in the task's state under the key
101 * "current_value" and can be accessed by external code using get_state<float>.
102 *
103 * Example usage:
104 * ```cpp
105 * // Create a 2-second interpolation from 0.0 to 1.0
106 * auto transition = Kriya::line(*scheduler, 0.0f, 1.0f, 2.0f);
107 * auto task_ptr = std::make_shared<SoundRoutine>(std::move(transition));
108 * scheduler->add_task(task_ptr);
109 *
110 * // In the processing loop:
111 * float* value = task_ptr->get_state<float>("current_value");
112 * if (value) {
113 * // Apply to any parameter in any domain
114 * apply_parameter(*value);
115 * }
116 * ```
117 *
118 * If restartable is true, the interpolation task will remain active after reaching the
119 * end value and can be restarted by calling restart() on the SoundRoutine.
120 */
121 MAYAFLUX_API Vruta::SoundRoutine line(float start_value, float end_value, float duration_seconds, uint32_t step_duration = 5, bool restartable = false);
122
123 /**
124 * @brief Coroutine that executes callback continuously while logic node outputs true
125 * @param callback Function to execute while condition is true
126 * @param logic_node Logic node to monitor (creates default threshold node if null)
127 * @param open Whether to subscribe to gate open (true) or close (false)
128 * @return SoundRoutine coroutine handle
129 * @deprecated Register Logic::while_true (or while_false) on the node instead.
130 * Scheduled for removal in the next release.
131 */
132 [[deprecated("Use Logic::while_true or Logic::while_false on the node; removal planned for the next release")]]
133 MAYAFLUX_API Vruta::SoundRoutine Gate(
134 std::function<void()> callback,
135 std::shared_ptr<Nodes::Generator::Logic> logic_node, bool open = true);
136
137 /**
138 * @brief Coroutine that executes callback when logic node changes to specific state
139 * @param logic_node Logic node to monitor (creates default threshold node if null)
140 * @param target_state State to trigger on (true/false)
141 * @param callback Function to execute on state change
142 * @return SoundRoutine coroutine handle
143 * @deprecated Register Logic::on_change_to on the node instead.
144 * Scheduled for removal in the next release.
145 */
146 [[deprecated("Use Logic::on_change_to on the node; removal planned for the next release")]]
147 MAYAFLUX_API Vruta::SoundRoutine Trigger(
148 bool target_state,
149 std::function<void()> callback,
150 std::shared_ptr<Nodes::Generator::Logic> logic_node);
151
152 /**
153 * @brief Coroutine that executes callback on any logic node state change
154 * @param logic_node Logic node to monitor (creates default threshold node if null)
155 * @param callback Function to execute on any state flip
156 * @return SoundRoutine coroutine handle
157 * @deprecated Register Logic::on_change on the node instead.
158 * Scheduled for removal in the next release.
159 */
160 [[deprecated("Use Logic::on_change on the node; removal planned for the next release")]]
161 MAYAFLUX_API Vruta::SoundRoutine Toggle(
162 std::function<void()> callback,
163 std::shared_ptr<Nodes::Generator::Logic> logic_node);
164}
165}
PickCallback callback
Definition Picker.cpp:22
@ Vruta
Coroutines, schedulers, clocks, task management.
@ Kriya
Automatable tasks and fluent scheduling api for Nodes and Buffers.
Vruta::SoundRoutine Gate(std::function< void()> callback, std::shared_ptr< Nodes::Generator::Logic > logic_node, bool open)
Coroutine that executes callback continuously while logic node outputs true.
Definition Tasks.cpp:126
Vruta::SoundRoutine Toggle(std::function< void()> callback, std::shared_ptr< Nodes::Generator::Logic > logic_node)
Coroutine that executes callback on any logic node state change.
Definition Tasks.cpp:185
Vruta::SoundRoutine line(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.
Definition Tasks.cpp:66
std::shared_ptr< Vruta::Routine > sequence(std::vector< std::pair< double, std::function< void()> > > sequence, Vruta::ProcessingToken token)
Creates a temporal sequence that executes callbacks at specified time offsets.
Definition Tasks.cpp:46
Vruta::SoundRoutine Trigger(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.
Definition Tasks.cpp:158
std::shared_ptr< Vruta::Routine > metro(double interval_seconds, std::function< void()> callback, Vruta::ProcessingToken token)
Creates a periodic event generator that executes a callback at regular intervals.
Definition Tasks.cpp:12
@ SAMPLE_ACCURATE
Coroutine is sample-accurate.
Main namespace for the Maya Flux audio engine.
Definition Runtime.cpp:12