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