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