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