MayaFlux 0.3.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}
10
11namespace Nodes {
12 class Node;
13}
14
15namespace Vruta {
16 class TaskScheduler;
17 class EventManager;
18 class SoundRoutine;
19}
20
21namespace Kriya {
22 class BufferPipeline;
23}
24
25/**
26 * @brief Gets the task scheduler from the default engine
27 * @return Shared pointer to the centrally managed TaskScheduler
28 *
29 * Returns the scheduler that's managed by the default engine instance.
30 * All scheduled tasks using the convenience functions will use this scheduler.
31 */
32MAYAFLUX_API std::shared_ptr<Vruta::TaskScheduler> get_scheduler();
33
34/**
35 * @brief Gets the event manager from the default engine
36 * @return Shared pointer to the centrally managed EventManager
37 *
38 * Returns the event manager that's managed by the default engine instance.
39 * Used for handling windowing and input events.
40 */
41MAYAFLUX_API std::shared_ptr<Vruta::EventManager> get_event_manager();
42
43/**
44 * @brief Creates a simple task that calls a function at a specified interval
45 * @param interval_seconds Time between calls in seconds
46 * @param callback Function to call on each tick
47 * This is conceptually similar to Metronomes in PureData and MaxMSP
48 */
49MAYAFLUX_API Vruta::SoundRoutine create_metro(double interval_seconds, std::function<void()> callback);
50
51/**
52 * @brief Creates a metronome task and addes it to the default scheduler list for evaluation
53 * @param interval_seconds Time between calls in seconds
54 * @param callback Function to call on each tick
55 * @param name Name of the metronome task (optional but recommended).
56 If not provided, a default name will be generated.
57 *
58 * Uses the task scheduler from the default engine.
59 */
60MAYAFLUX_API void schedule_metro(double interval_seconds, std::function<void()> callback, std::string name = "");
61
62/**
63 * @brief Creates a sequence task that calls functions at specified times
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 * @return SoundRoutine object representing the scheduled task
68 *
69 * Uses the task scheduler from the default engine.
70 */
71MAYAFLUX_API Vruta::SoundRoutine create_sequence(std::vector<std::pair<double, std::function<void()>>> sequence);
72
73/**
74 * @brief Creates a sequence task that calls functions at specified times
75 * and addes it to the default scheduler list for evaluation
76 * @param sequence Vector of (time, function) pairs
77 * @param name Name of the metronome task (optional but recommended).
78 If not provided, a default name will be generated.
79 *
80 * Uses the task scheduler from the default engine.
81 */
82MAYAFLUX_API void schedule_sequence(std::vector<std::pair<double, std::function<void()>>> sequence, std::string name = "");
83
84/**
85 * @brief Creates a line generator that interpolates between values over time
86 * @param start_value Starting value
87 * @param end_value Ending value
88 * @param duration_seconds Total duration in seconds
89 * @param step_duration Time between steps in seconds
90 * @param loop Whether to loop back to start after reaching end
91 * @return SoundRoutine object representing the line generator
92 *
93 * Uses the task scheduler from the default engine.
94 */
95MAYAFLUX_API Vruta::SoundRoutine create_line(float start_value, float end_value, float duration_seconds, uint32_t step_duration, bool loop);
96
97/**
98 * @brief Schedules a pattern generator that produces values based on a pattern function
99 * @param pattern_func Function that generates pattern values based on step index
100 * @param callback Function to call with each pattern value
101 * @param interval_seconds Time between pattern steps
102 * @return SoundRoutine object representing the scheduled task
103 *
104 * Uses the task scheduler from the default engine.
105 */
106MAYAFLUX_API Vruta::SoundRoutine create_pattern(std::function<std::any(uint64_t)> pattern_func, std::function<void(std::any)> callback, double interval_seconds);
107
108/**
109 * @brief Schedules a pattern generator that produces values based on a pattern function
110 * and addes it to the default scheduler list for evaluation
111 * @param pattern_func Function that generates pattern values based on step index
112 * @param callback Function to call with each pattern value
113 * @param interval_seconds Time between pattern steps
114 * @param name Name of the metronome task (optional but recommended).
115 If not provided, a default name will be generated.
116 *
117 * Uses the task scheduler from the default engine.
118 */
119MAYAFLUX_API void schedule_pattern(std::function<std::any(uint64_t)> pattern_func, std::function<void(std::any)> callback, double interval_seconds, std::string name = "");
120
121/**
122 * @brief Gets a pointer to a task's current value
123 * @param name Name of the task
124 * @return Pointer to the float value, or nullptr if not found
125 *
126 * Convenience wrapper for Engine::get_line_value() on the default engine.
127 */
128MAYAFLUX_API float* get_line_value(const std::string& name);
129
130/**
131 * @brief Schedules a new sound routine task
132 * @param name Unique name for the task
133 * @param task The sound routine to schedule
134 * @param initialize Whether to initialize the task immediately
135 *
136 * Convenience wrapper for Engine::schedule_task() on the default engine.
137 */
138MAYAFLUX_API void schedule_task(const std::string& name, Vruta::SoundRoutine&& task, bool initialize = false);
139
140/**
141 * @brief Cancels a scheduled task
142 * @param name Name of the task to cancel
143 * @return true if task was found and canceled, false otherwise
144 *
145 * Convenience wrapper for Engine::cancel_task() on the default engine.
146 */
147MAYAFLUX_API bool cancel_task(const std::string& name);
148
149/**
150 * @brief Restarts a scheduled task
151 * @param name Name of the task to restart
152 * @return true if task was found and restarted, false otherwise
153 *
154 * Convenience wrapper for Engine::restart_task() on the default engine.
155 */
156MAYAFLUX_API bool restart_task(const std::string& name);
157
158/**
159 * @brief Updates parameters of a scheduled task
160 * @tparam Args Parameter types
161 * @param name Name of the task to update
162 * @param args New parameter values
163 * @return true if task was found and updated, false otherwise
164 *
165 * Convenience wrapper for Engine::update_task_params() on the default engine.
166 */
167template <typename... Args>
168MAYAFLUX_API bool update_task_params(const std::string& name, Args... args);
169
170/**
171 * @brief Creates a new buffer pipeline instance
172 * @return Shared pointer to the created BufferPipeline
173 *
174 * Uses the task scheduler from the default engine.
175 */
176MAYAFLUX_API std::shared_ptr<Kriya::BufferPipeline> create_buffer_pipeline();
177
178/**
179 * @brief Schedule a key press handler
180 * @param window Window to listen to
181 * @param key Key to wait for
182 * @param callback Function to call on key press
183 * @param name Optional name for the event handler
184 *
185 * Example:
186 * @code
187 * MayaFlux::on_key_pressed(window, MayaFlux::IO::Keys::Escape, []() {
188 * // Handle Escape key press
189 * }, "escape_handler");
190 * @endcode
191 */
192MAYAFLUX_API void on_key_pressed(
193 const std::shared_ptr<Core::Window>& window,
194 IO::Keys key,
195 std::function<void()> callback,
196 std::string name = "");
197
198/**
199 * @brief Schedule a key release handler
200 * @param window Window to listen to
201 * @param key Key to wait for
202 * @param callback Function to call on key release
203 * @param name Optional name for the event handler
204 *
205 * Example:
206 * @code
207 * MayaFlux::on_key_released(window, MayaFlux::IO::Keys::Enter, []() {
208 * // Handle Enter key release
209 * }, "enter_release_handler");
210 * @endcode
211 */
212MAYAFLUX_API void on_key_released(
213 const std::shared_ptr<Core::Window>& window,
214 IO::Keys key,
215 std::function<void()> callback,
216 std::string name = "");
217
218/**
219 * @brief Schedule a handler for any key press
220 * @param window Window to listen to
221 * @param callback Function to call with key code when any key is pressed
222 * @param name Optional name for the event handler
223 *
224 * Example:
225 * @code
226 * MayaFlux::on_any_key(window, [](MayaFlux::IO::Keys key) {
227 * // Handle any key press, key code in 'key'
228 * }, "any_key_handler");
229 * @endcode
230 */
231MAYAFLUX_API void on_any_key(
232 const std::shared_ptr<Core::Window>& window,
233 std::function<void(IO::Keys)> callback,
234 std::string name = "");
235
236/**
237 * @brief Schedule a mouse button press handler
238 * @param window Window to listen to
239 * @param button Mouse button to wait for
240 * @param callback Function to call on button press (x, y)
241 * @param name Optional name for the event handler
242 *
243 * Example:
244 * @code
245 * MayaFlux::on_mouse_pressed(window, MayaFlux::IO::MouseButtons::Left, [](double x, double y) {
246 * // Handle left mouse button press at (x, y)
247 * }, "mouse_left_press_handler");
248 * @endcode
249 */
250MAYAFLUX_API void on_mouse_pressed(
251 const std::shared_ptr<Core::Window>& window,
252 IO::MouseButtons button,
253 std::function<void(double, double)> callback,
254 std::string name = "");
255
256/**
257 * @brief Schedule a mouse button release handler
258 * @param window Window to listen to
259 * @param button Mouse button to wait for
260 * @param callback Function to call on button release (x, y)
261 * @param name Optional name for the event handler
262 *
263 * Example:
264 * @code
265 * MayaFlux::on_mouse_released(window, MayaFlux::IO::MouseButtons::Right, [](double x, double y) {
266 * // Handle right mouse button release at (x, y)
267 * }, "mouse_right_release_handler");
268 * @endcode
269 */
270MAYAFLUX_API void on_mouse_released(
271 const std::shared_ptr<Core::Window>& window,
272 IO::MouseButtons button,
273 std::function<void(double, double)> callback,
274 std::string name = "");
275
276/**
277 * @brief Schedule a mouse movement handler
278 * @param window Window to listen to
279 * @param callback Function to call on mouse move (x, y)
280 * @param name Optional name for the event handler
281 *
282 * Example:
283 * @code
284 * MayaFlux::on_mouse_move(window, [](double x, double y) {
285 * // Handle mouse move at (x, y)
286 * }, "mouse_move_handler");
287 * @endcode
288 */
289MAYAFLUX_API void on_mouse_move(
290 const std::shared_ptr<Core::Window>& window,
291 std::function<void(double, double)> callback,
292 std::string name = "");
293
294/**
295 * @brief Schedule a mouse scroll handler
296 * @param window Window to listen to
297 * @param callback Function to call on scroll (xoffset, yoffset)
298 * @param name Optional name for the event handler
299 *
300 * Example:
301 * @code
302 * MayaFlux::on_scroll(window, [](double xoffset, double yoffset) {
303 * // Handle mouse scroll with offsets
304 * }, "mouse_scroll_handler");
305 * @endcode
306 */
307MAYAFLUX_API void on_scroll(
308 const std::shared_ptr<Core::Window>& window,
309 std::function<void(double, double)> callback,
310 std::string name = "");
311
312/**
313 * @brief Cancel an event handler by name
314 * @param name Event handler name
315 * @return True if cancelled successfully
316 */
317MAYAFLUX_API bool cancel_event_handler(const std::string& name);
318
319/**
320 * @brief Converts a time duration in seconds to the equivalent number of audio samples
321 * @param seconds Time duration in seconds
322 * @return Equivalent number of audio samples based on the current sample rate
323 *
324 * This function uses the sample rate from the default engine to calculate how many
325 * audio samples correspond to the given time duration in seconds. It's useful for
326 * scheduling tasks or processing buffers based on time intervals.
327 */
328MAYAFLUX_API uint64_t seconds_to_samples(double seconds);
329
330/**
331 * @brief Converts a time duration in seconds to the equivalent number of processing blocks
332 * @param seconds Time duration in seconds
333 * @return Equivalent number of blocks based on current sample rate and block size
334 *
335 * Uses the sample rate and block size from the default engine. Falls back to
336 * 48kHz/512 samples if engine is not running.
337 */
338MAYAFLUX_API uint64_t seconds_to_blocks(double seconds);
339
340/**
341 * @brief Converts samples to blocks based on current block size
342 * @param samples Number of samples
343 * @return Number of blocks
344 */
345MAYAFLUX_API uint64_t samples_to_blocks(uint64_t samples);
346
347}
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:109
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:239
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:207
bool update_task_params(const std::string &name, Args... args)
Updates parameters of a scheduled task.
Definition Chronie.cpp:31
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:223
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:41
std::shared_ptr< Vruta::EventManager > get_event_manager()
Gets the event manager from the default engine.
Definition Chronie.cpp:25
bool restart_task(const std::string &name)
Restarts a scheduled task.
Definition Chronie.cpp:113
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:72
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:57
uint64_t seconds_to_samples(double seconds)
Converts a time duration in seconds to the equivalent number of audio samples.
Definition Chronie.cpp:244
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:173
Vruta::SoundRoutine create_line(float start_value, float end_value, float duration_seconds, uint32_t step_duration, bool loop)
Creates a line generator that interpolates between values over time.
Definition Chronie.cpp:67
void schedule_task(const std::string &name, Vruta::SoundRoutine &&task, bool initialize)
Schedules a new sound routine task.
Definition Chronie.cpp:102
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:77
float * get_line_value(const std::string &name)
Gets a pointer to a task's current value.
Definition Chronie.cpp:87
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:36
uint64_t seconds_to_blocks(double seconds)
Converts a time duration in seconds to the equivalent number of processing blocks.
Definition Chronie.cpp:253
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:140
std::shared_ptr< Kriya::BufferPipeline > create_buffer_pipeline()
Creates a new buffer pipeline instance.
Definition Chronie.cpp:118
bool cancel_task(const std::string &name)
Cancels a scheduled task.
Definition Chronie.cpp:108
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:190
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:52
uint64_t samples_to_blocks(uint64_t samples)
Converts samples to blocks based on current block size.
Definition Chronie.cpp:266
std::shared_ptr< Vruta::TaskScheduler > get_scheduler()
Gets the task scheduler from the default engine.
Definition Chronie.cpp:20
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:157
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:123
Main namespace for the Maya Flux audio engine.
Definition LiveAid.hpp:6