MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Chronie.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace MayaFlux {
7
8namespace Core {
9 class Window;
10 struct NetworkMessage;
11 struct EndpointInfo;
12}
13
14namespace Nodes {
15 class Node;
16}
17
18namespace Vruta {
19 class TaskScheduler;
20 class EventManager;
21 class SoundRoutine;
22 class GraphicsRoutine;
23 class FreeRoutine;
24 class NetworkSource;
25}
26
27namespace Kriya {
28 class BufferPipeline;
29}
30
31/**
32 * @brief Gets the task scheduler from the default engine
33 * @return Shared pointer to the centrally managed TaskScheduler
34 *
35 * Returns the scheduler that's managed by the default engine instance.
36 * All scheduled tasks using the convenience functions will use this scheduler.
37 */
38MAYAFLUX_API std::shared_ptr<Vruta::TaskScheduler> get_scheduler();
39
40/**
41 * @brief Gets the event manager from the default engine
42 * @return Shared pointer to the centrally managed EventManager
43 *
44 * Returns the event manager that's managed by the default engine instance.
45 * Used for handling windowing and input events.
46 */
47MAYAFLUX_API std::shared_ptr<Vruta::EventManager> get_event_manager();
48
49/**
50 * @brief Creates a metronome task and addes it to the default scheduler list for evaluation
51 * @param interval_seconds Time between calls in seconds
52 * @param callback Function to call on each tick
53 * @param name Name of the metronome task (optional but recommended).
54 If not provided, a default name will be generated.
55 * @param token Processing token to determine which scheduler rate to use (default: SAMPLE_ACCURATE)
56 *
57 * Uses the task scheduler from the default engine.
58 */
59MAYAFLUX_API void schedule_metro(double interval_seconds, std::function<void()> callback, std::string name = "", Vruta::ProcessingToken token = Vruta::ProcessingToken::SAMPLE_ACCURATE);
60
61/**
62 * @brief Creates a sequence task that calls functions at specified times
63 * and addes it to the default scheduler list for evaluation
64 * @param sequence Vector of (time, function) pairs
65 * @param name Name of the metronome task (optional but recommended).
66 If not provided, a default name will be generated.
67 * @param token Processing token to determine which scheduler rate to use (default: SAMPLE_ACCURATE)
68 *
69 * Uses the task scheduler from the default engine.
70 */
71MAYAFLUX_API void schedule_sequence(std::vector<std::pair<double, std::function<void()>>> sequence, std::string name = "", Vruta::ProcessingToken token = Vruta::ProcessingToken::SAMPLE_ACCURATE);
72
73/**
74 * @brief Creates a line generator that interpolates between values over time and schedules it for evaluation
75 * @param start_value Starting value
76 * @param end_value Ending value
77 * @param duration_seconds Total duration in seconds
78 * @param step_duration Time between steps in seconds
79 * @param retain Whether the coroutine should stay alive for potential restarts (default: true)
80 * @param name Name of the line task (optional but recommended).
81 If not provided, a default name will be generated.
82 * @return shared_ptr to SoundRoutine object representing the line generator
83 *
84 * Uses the task scheduler from the default engine.
85 */
86MAYAFLUX_API std::shared_ptr<Vruta::SoundRoutine> schedule_line(float start_value, float end_value, float duration_seconds, uint32_t step_duration = 5, bool retain = true, std::string name = "");
87
88/**
89 * @brief Schedules a pattern generator that produces values based on a pattern function
90 * and addes it to the default scheduler list for evaluation
91 * @param pattern_func Function that generates pattern values based on step index
92 * @param callback Function to call with each pattern value
93 * @param interval_seconds Time between pattern steps
94 * @param name Name of the metronome task (optional but recommended).
95 If not provided, a default name will be generated.
96 * @param token Processing token to determine which scheduler rate to use (default: SAMPLE_ACCURATE)
97 *
98 * Uses the task scheduler from the default engine.
99 */
100MAYAFLUX_API void schedule_pattern(std::function<std::any(uint64_t)> pattern_func, std::function<void(std::any)> callback, double interval_seconds, std::string name = "", Vruta::ProcessingToken token = Vruta::ProcessingToken::SAMPLE_ACCURATE);
101
102/**
103 * @brief Gets a pointer to a task's current value
104 * @param name Name of the task
105 * @return Pointer to the float value, or nullptr if not found
106 */
107MAYAFLUX_API float* get_line_value(const std::string& name);
108
109/**
110 * @brief Schedules a new sound routine task
111 * @param name Unique name for the task
112 * @param task The sound routine to schedule
113 * @param initialize Whether to initialize the task immediately
114 */
115MAYAFLUX_API void schedule_task(const std::string& name, Vruta::SoundRoutine&& task, bool initialize = false);
116
117/**
118 * @brief Schedules a new sound routine task
119 * @param name Unique name for the task
120 * @param task The sound routine to schedule
121 * @param initialize Whether to initialize the task immediately
122 */
123MAYAFLUX_API void schedule_task(const std::string& name, Vruta::GraphicsRoutine&& task, bool initialize = false);
124
125/**
126 * @brief Schedules a new free routine conditional task
127 * @param name Unique name for the task
128 * @param task The sound routine to schedule
129 * @param initialize Whether to initialize the task immediately
130 */
131MAYAFLUX_API void schedule_task(const std::string& name, Vruta::FreeRoutine&& task, bool initialize = false);
132
133/**
134 * @brief Cancels a scheduled task
135 * @param name Name of the task to cancel
136 * @return true if task was found and canceled, false otherwise
137 */
138MAYAFLUX_API bool cancel_task(const std::string& name);
139
140/**
141 * @brief Restarts a scheduled task
142 * @param name Name of the task to restart
143 * @return true if task was found and restarted, false otherwise
144 */
145MAYAFLUX_API bool restart_task(const std::string& name);
146
147/**
148 * @brief Updates parameters of a scheduled task
149 * @tparam Args Parameter types
150 * @param name Name of the task to update
151 * @param args New parameter values
152 * @return true if task was found and updated, false otherwise
153 */
154template <typename... Args>
155MAYAFLUX_API bool update_task_params(const std::string& name, Args... args);
156
157/**
158 * @brief Creates a new buffer pipeline instance
159 * @return Shared pointer to the created BufferPipeline
160 *
161 * Uses the task scheduler from the default engine.
162 */
163MAYAFLUX_API std::shared_ptr<Kriya::BufferPipeline> create_buffer_pipeline();
164
165/**
166 * @brief Schedule a key press handler
167 * @param window Window to listen to
168 * @param key Key to wait for
169 * @param callback Function to call on key press
170 * @param name Optional name for the event handler
171 *
172 * Example:
173 * @code
174 * MayaFlux::on_key_pressed(window, MayaFlux::IO::Keys::Escape, []() {
175 * // Handle Escape key press
176 * }, "escape_handler");
177 * @endcode
178 */
179MAYAFLUX_API void on_key_pressed(
180 const std::shared_ptr<Core::Window>& window,
181 IO::Keys key,
182 std::function<void()> callback,
183 std::string name = "");
184
185/**
186 * @brief Schedule a key release handler
187 * @param window Window to listen to
188 * @param key Key to wait for
189 * @param callback Function to call on key release
190 * @param name Optional name for the event handler
191 *
192 * Example:
193 * @code
194 * MayaFlux::on_key_released(window, MayaFlux::IO::Keys::Enter, []() {
195 * // Handle Enter key release
196 * }, "enter_release_handler");
197 * @endcode
198 */
199MAYAFLUX_API void on_key_released(
200 const std::shared_ptr<Core::Window>& window,
201 IO::Keys key,
202 std::function<void()> callback,
203 std::string name = "");
204
205/**
206 * @brief Schedule a handler for any key press
207 * @param window Window to listen to
208 * @param callback Function to call with key code when any key is pressed
209 * @param name Optional name for the event handler
210 *
211 * Example:
212 * @code
213 * MayaFlux::on_any_key(window, [](MayaFlux::IO::Keys key) {
214 * // Handle any key press, key code in 'key'
215 * }, "any_key_handler");
216 * @endcode
217 */
218MAYAFLUX_API void on_any_key(
219 const std::shared_ptr<Core::Window>& window,
220 std::function<void(IO::Keys)> callback,
221 std::string name = "");
222
223/**
224 * @brief Schedule a mouse button press handler
225 * @param window Window to listen to
226 * @param button Mouse button to wait for
227 * @param callback Function to call on button press (x, y)
228 * @param name Optional name for the event handler
229 *
230 * Example:
231 * @code
232 * MayaFlux::on_mouse_pressed(window, MayaFlux::IO::MouseButtons::Left, [](double x, double y) {
233 * // Handle left mouse button press at (x, y)
234 * }, "mouse_left_press_handler");
235 * @endcode
236 */
237MAYAFLUX_API void on_mouse_pressed(
238 const std::shared_ptr<Core::Window>& window,
239 IO::MouseButtons button,
240 std::function<void(double, double)> callback,
241 std::string name = "");
242
243/**
244 * @brief Schedule a mouse button release handler
245 * @param window Window to listen to
246 * @param button Mouse button to wait for
247 * @param callback Function to call on button release (x, y)
248 * @param name Optional name for the event handler
249 *
250 * Example:
251 * @code
252 * MayaFlux::on_mouse_released(window, MayaFlux::IO::MouseButtons::Right, [](double x, double y) {
253 * // Handle right mouse button release at (x, y)
254 * }, "mouse_right_release_handler");
255 * @endcode
256 */
257MAYAFLUX_API void on_mouse_released(
258 const std::shared_ptr<Core::Window>& window,
259 IO::MouseButtons button,
260 std::function<void(double, double)> callback,
261 std::string name = "");
262
263/**
264 * @brief Schedule a mouse movement handler
265 * @param window Window to listen to
266 * @param callback Function to call on mouse move (x, y)
267 * @param name Optional name for the event handler
268 *
269 * Example:
270 * @code
271 * MayaFlux::on_mouse_move(window, [](double x, double y) {
272 * // Handle mouse move at (x, y)
273 * }, "mouse_move_handler");
274 * @endcode
275 */
276MAYAFLUX_API void on_mouse_move(
277 const std::shared_ptr<Core::Window>& window,
278 std::function<void(double, double)> callback,
279 std::string name = "");
280
281/**
282 * @brief Schedule a mouse drag handler
283 * @param window Window to listen to
284 * @param button Mouse button that must be held for dragging
285 * @param callback Function to call on mouse drag (x, y)
286 * @param name Optional name for the event handler
287 *
288 * Example:
289 * @code
290 * MayaFlux::on_mouse_drag(window, MayaFlux::IO::MouseButtons::Left, [](double x, double y) {
291 * // Handle mouse drag with left button at (x, y)
292 * }, "mouse_left_drag_handler");
293 * @endcode
294 */
295MAYAFLUX_API void on_mouse_drag(
296 const std::shared_ptr<Core::Window>& window,
297 IO::MouseButtons button,
298 std::function<void(double, double)> callback,
299 std::string name = "");
300
301/**
302 * @brief Schedule a mouse scroll handler
303 * @param window Window to listen to
304 * @param callback Function to call on scroll (xoffset, yoffset)
305 * @param name Optional name for the event handler
306 *
307 * Example:
308 * @code
309 * MayaFlux::on_scroll(window, [](double xoffset, double yoffset) {
310 * // Handle mouse scroll with offsets
311 * }, "mouse_scroll_handler");
312 * @endcode
313 */
314MAYAFLUX_API void on_scroll(
315 const std::shared_ptr<Core::Window>& window,
316 std::function<void(double, double)> callback,
317 std::string name = "");
318
319/**
320 * @brief Cancel an event handler by name
321 * @param name Event handler name
322 * @return True if cancelled successfully
323 */
324MAYAFLUX_API bool cancel_event_handler(const std::string& name);
325
326/**
327 * @brief Converts a time duration in seconds to the equivalent number of audio samples
328 * @param seconds Time duration in seconds
329 * @return Equivalent number of audio samples based on the current sample rate
330 *
331 * This function uses the sample rate from the default engine to calculate how many
332 * audio samples correspond to the given time duration in seconds. It's useful for
333 * scheduling tasks or processing buffers based on time intervals.
334 */
335MAYAFLUX_API uint64_t seconds_to_samples(double seconds);
336
337/**
338 * @brief Converts a time duration in seconds to the equivalent number of processing blocks
339 * @param seconds Time duration in seconds
340 * @return Equivalent number of blocks based on current sample rate and block size
341 *
342 * Uses the sample rate and block size from the default engine. Falls back to
343 * 48kHz/512 samples if engine is not running.
344 */
345MAYAFLUX_API uint64_t seconds_to_blocks(double seconds);
346
347/**
348 * @brief Converts samples to blocks based on current block size
349 * @param samples Number of samples
350 * @return Number of blocks
351 */
352MAYAFLUX_API uint64_t samples_to_blocks(uint64_t samples);
353
354/**
355 * @brief Converts a number of audio samples to the equivalent time duration in seconds
356 * @param samples Number of audio samples
357 * @return Time duration in seconds based on the current sample rate
358 */
359MAYAFLUX_API double samples_to_seconds(uint64_t samples);
360
361/**
362 * @brief Schedule an on_message handler with an existing NetworkSource
363 * @param source Shared NetworkSource; coroutine frame takes co-ownership
364 * @param callback Invoked with each received message
365 * @param name Optional name for the event handler
366 *
367 * @code
368 * auto src = std::make_shared<Vruta::NetworkSource>(
369 * Core::EndpointInfo{ .transport = Core::NetworkTransport::UDP, .local_port = 8000 });
370 * MayaFlux::on_network_message(src, [](const Core::NetworkMessage& msg) { });
371 * @endcode
372 */
373MAYAFLUX_API void on_network_message(
374 std::shared_ptr<Vruta::NetworkSource> source,
375 std::function<void(const Core::NetworkMessage&)> callback,
376 std::string name = "");
377
378/**
379 * @brief Schedule an on_message handler, constructing the NetworkSource from config
380 * @param info Endpoint configuration used to open the source
381 * @param callback Invoked with each received message
382 * @param name Optional name for the event handler
383 * @return Shared pointer to the opened NetworkSource (caller owns lifetime anchor)
384 *
385 * @code
386 * auto src = MayaFlux::on_network_message(
387 * Core::EndpointInfo{ .transport = Core::NetworkTransport::UDP, .local_port = 8000 },
388 * [](const Core::NetworkMessage& msg) { });
389 * @endcode
390 */
391MAYAFLUX_API std::shared_ptr<Vruta::NetworkSource> on_network_message(
392 const Core::EndpointInfo& info,
393 std::function<void(const Core::NetworkMessage&)> callback,
394 std::string name = "");
395
396/**
397 * @brief Schedule an on_message_from handler with an existing NetworkSource
398 * @param source Shared NetworkSource
399 * @param sender_address Sender IP address to filter on
400 * @param callback Invoked with each matching message
401 * @param name Optional name for the event handler
402 */
403MAYAFLUX_API void on_network_message_from(
404 std::shared_ptr<Vruta::NetworkSource> source,
405 std::string sender_address,
406 std::function<void(const Core::NetworkMessage&)> callback,
407 std::string name = "");
408
409/**
410 * @brief Schedule an on_message_from handler, constructing the NetworkSource from config
411 * @param info Endpoint configuration used to open the source
412 * @param sender_address Sender IP address to filter on
413 * @param callback Invoked with each matching message
414 * @param name Optional name for the event handler
415 * @return Shared pointer to the opened NetworkSource
416 */
417MAYAFLUX_API std::shared_ptr<Vruta::NetworkSource> on_network_message_from(
418 const Core::EndpointInfo& info,
419 std::string sender_address,
420 std::function<void(const Core::NetworkMessage&)> callback,
421 std::string name = "");
422
423/**
424 * @brief Schedule an on_message_matching handler with an existing NetworkSource
425 * @param source Shared NetworkSource
426 * @param predicate Filter; only matching messages invoke callback
427 * @param callback Invoked with each matching message
428 * @param name Optional name for the event handler
429 */
430MAYAFLUX_API void on_network_message_matching(
431 std::shared_ptr<Vruta::NetworkSource> source,
432 std::function<bool(const Core::NetworkMessage&)> predicate,
433 std::function<void(const Core::NetworkMessage&)> callback,
434 std::string name = "");
435
436/**
437 * @brief Schedule an on_message_matching handler, constructing the NetworkSource from config
438 * @param info Endpoint configuration used to open the source
439 * @param predicate Filter; only matching messages invoke callback
440 * @param callback Invoked with each matching message
441 * @param name Optional name for the event handler
442 * @return Shared pointer to the opened NetworkSource
443 */
444MAYAFLUX_API std::shared_ptr<Vruta::NetworkSource> on_network_message_matching(
445 const Core::EndpointInfo& info,
446 std::function<bool(const Core::NetworkMessage&)> predicate,
447 std::function<void(const Core::NetworkMessage&)> callback,
448 std::string name = "");
449
450}
Platform-agnostic window wrapper.
Definition Window.hpp:22
Coroutine-based execution engine for composable, multi-strategy buffer processing.
Base interface for all computational processing nodes.
Definition Node.hpp:126
Coroutine resumed when a caller-supplied condition becomes true.
Definition Routine.hpp:753
A C++20 coroutine-based graphics processing task with frame-accurate timing.
Definition Routine.hpp:496
Awaitable broadcast message stream for a network endpoint.
A C++20 coroutine-based audio processing task with sample-accurate timing.
Definition Routine.hpp:316
Token-based multimodal task scheduling system for unified coroutine processing.
Definition Scheduler.hpp:51
void initialize()
Definition main.cpp:11
MouseButtons
Enumeration for mouse buttons.
Definition Keys.hpp:147
@ SAMPLE_ACCURATE
Coroutine is sample-accurate.
bool cancel_event_handler(const std::string &name)
Cancel an event handler by name.
Definition Chronie.cpp:266
void on_mouse_move(const std::shared_ptr< Core::Window > &window, std::function< void(double, double)> callback, std::string name)
Schedule a mouse movement handler.
Definition Chronie.cpp:217
bool update_task_params(const std::string &name, Args... args)
Updates parameters of a scheduled task.
Definition Chronie.cpp:33
void on_scroll(const std::shared_ptr< Core::Window > &window, std::function< void(double, double)> callback, std::string name)
Schedule a mouse scroll handler.
Definition Chronie.cpp:250
void on_mouse_drag(const std::shared_ptr< Core::Window > &window, IO::MouseButtons button, std::function< void(double, double)> callback, std::string name)
Schedule a mouse drag handler.
Definition Chronie.cpp:233
std::shared_ptr< Vruta::EventManager > get_event_manager()
Gets the event manager from the default engine.
Definition Chronie.cpp:27
bool restart_task(const std::string &name)
Restarts a scheduled task.
Definition Chronie.cpp:123
void schedule_metro(double interval_seconds, std::function< void()> callback, std::string name, Vruta::ProcessingToken token)
Creates a metronome task and addes it to the default scheduler list for evaluation.
Definition Chronie.cpp:38
void schedule_sequence(std::vector< std::pair< double, std::function< void()> > > seq, std::string name, Vruta::ProcessingToken token)
Creates a sequence task that calls functions at specified times and addes it to the default scheduler...
Definition Chronie.cpp:49
uint64_t seconds_to_samples(double seconds)
Converts a time duration in seconds to the equivalent number of audio samples.
Definition Chronie.cpp:271
void on_mouse_pressed(const std::shared_ptr< Core::Window > &window, IO::MouseButtons button, std::function< void(double, double)> callback, std::string name)
Schedule a mouse button press handler.
Definition Chronie.cpp:183
void schedule_pattern(std::function< std::any(uint64_t)> pattern_func, std::function< void(std::any)> callback, double interval_seconds, std::string name, Vruta::ProcessingToken token)
Schedules a pattern generator that produces values based on a pattern function and addes it to the de...
Definition Chronie.cpp:74
void schedule_task(const std::string &name, Vruta::SoundRoutine &&task, bool initialize)
Schedules a new sound routine task.
Definition Chronie.cpp:100
float * get_line_value(const std::string &name)
Gets a pointer to a task's current value.
Definition Chronie.cpp:85
uint64_t seconds_to_blocks(double seconds)
Converts a time duration in seconds to the equivalent number of processing blocks.
Definition Chronie.cpp:280
void on_key_released(const std::shared_ptr< Core::Window > &window, IO::Keys key, std::function< void()> callback, std::string name)
Schedule a key release handler.
Definition Chronie.cpp:150
std::shared_ptr< Kriya::BufferPipeline > create_buffer_pipeline()
Creates a new buffer pipeline instance.
Definition Chronie.cpp:128
std::shared_ptr< Vruta::SoundRoutine > schedule_line(float start_value, float end_value, float duration_seconds, uint32_t step_duration, bool retain, std::string name)
Creates a line generator that interpolates between values over time and schedules it for evaluation.
Definition Chronie.cpp:61
bool cancel_task(const std::string &name)
Cancels a scheduled task.
Definition Chronie.cpp:118
void on_network_message_from(std::shared_ptr< Vruta::NetworkSource > source, std::string sender_address, std::function< void(const Core::NetworkMessage &)> callback, std::string name)
Schedule an on_message_from handler with an existing NetworkSource.
Definition Chronie.cpp:338
double samples_to_seconds(uint64_t samples)
Converts a number of audio samples to the equivalent time duration in seconds.
Definition Chronie.cpp:304
void on_mouse_released(const std::shared_ptr< Core::Window > &window, IO::MouseButtons button, std::function< void(double, double)> callback, std::string name)
Schedule a mouse button release handler.
Definition Chronie.cpp:200
void on_network_message(std::shared_ptr< Vruta::NetworkSource > source, std::function< void(const Core::NetworkMessage &)> callback, std::string name)
Schedule an on_message handler with an existing NetworkSource.
Definition Chronie.cpp:314
void on_network_message_matching(std::shared_ptr< Vruta::NetworkSource > source, std::function< bool(const Core::NetworkMessage &)> predicate, std::function< void(const Core::NetworkMessage &)> callback, std::string name)
Schedule an on_message_matching handler with an existing NetworkSource.
Definition Chronie.cpp:365
uint64_t samples_to_blocks(uint64_t samples)
Converts samples to blocks based on current block size.
Definition Chronie.cpp:293
std::shared_ptr< Vruta::TaskScheduler > get_scheduler()
Gets the task scheduler from the default engine.
Definition Chronie.cpp:22
void on_any_key(const std::shared_ptr< Core::Window > &window, std::function< void(IO::Keys)> callback, std::string name)
Schedule a handler for any key press.
Definition Chronie.cpp:167
void on_key_pressed(const std::shared_ptr< Core::Window > &window, IO::Keys key, std::function< void()> callback, std::string name)
Schedule a key press handler.
Definition Chronie.cpp:133
Main namespace for the Maya Flux audio engine.
Definition Runtime.cpp:12
Describes one logical send/receive endpoint managed by a backend.
A received datagram or framed message with sender metadata.