MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Chronie.cpp
Go to the documentation of this file.
1#include "Chronie.hpp"
2
3#include "Core.hpp"
4
11
13
17
19
20namespace MayaFlux {
21
22std::shared_ptr<Vruta::TaskScheduler> get_scheduler()
23{
24 return get_context().get_scheduler();
25}
26
27std::shared_ptr<Vruta::EventManager> get_event_manager()
28{
30}
31
32template <typename... Args>
33bool update_task_params(const std::string& name, Args... args)
34{
35 return get_scheduler()->update_task_params(name, args...);
36}
37
38void schedule_metro(double interval_seconds, std::function<void()> callback, std::string name, Vruta::ProcessingToken token)
39{
40 auto scheduler = get_scheduler();
41 if (name.empty()) {
42 name = "metro_" + std::to_string(scheduler->get_next_task_id());
43 }
44 auto metronome = Kriya::metro(interval_seconds, std::move(callback), token);
45
46 get_scheduler()->add_task(metronome, name, false);
47}
48
49void schedule_sequence(std::vector<std::pair<double, std::function<void()>>> seq, std::string name, Vruta::ProcessingToken token)
50{
51 auto scheduler = get_scheduler();
52 if (name.empty()) {
53 name = "seq_" + std::to_string(scheduler->get_next_task_id());
54 }
55
56 auto tseq = Kriya::sequence(std::move(seq), token);
57
58 get_scheduler()->add_task(tseq, name, false);
59}
60
61std::shared_ptr<Vruta::SoundRoutine> schedule_line(float start_value, float end_value, float duration_seconds, uint32_t step_duration, bool retain, std::string name)
62{
63 auto scheduler = get_scheduler();
64 if (name.empty()) {
65 name = "seq_" + std::to_string(scheduler->get_next_task_id());
66 }
67
68 auto line = std::make_shared<Vruta::SoundRoutine>(Kriya::line(start_value, end_value, duration_seconds, step_duration, retain));
69
70 get_scheduler()->add_task(line, name, true);
71 return line;
72}
73
74void 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)
75{
76 auto scheduler = get_scheduler();
77 if (name.empty()) {
78 name = "pattern_" + std::to_string(scheduler->get_next_task_id());
79 }
80
81 auto pattern = Kriya::pattern(std::move(pattern_func), std::move(callback), interval_seconds, token);
82 get_scheduler()->add_task(pattern, name, false);
83}
84
85float* get_line_value(const std::string& name)
86{
87 if (auto task = get_scheduler()->get_task(name)) {
88 auto cur_val = task->get_state<float>("current_value");
89 if (cur_val) {
90 return cur_val;
91 }
92
93 MF_ERROR(Journal::Component::API, Journal::Context::CoroutineScheduling, "line value not returned from task. Verify that tasks has not returned");
94 return nullptr;
95 }
96 MF_ERROR(Journal::Component::API, Journal::Context::CoroutineScheduling, "Task: {} not found. Verify task validity or if its been scheduled, or its set to retain after completion", name);
97 return nullptr;
98}
99
100void schedule_task(const std::string& name, Vruta::SoundRoutine&& task, bool initialize)
101{
102 auto task_ptr = std::make_shared<Vruta::SoundRoutine>(std::move(task));
103 get_scheduler()->add_task(std::move(task_ptr), name, initialize);
104}
105
106void schedule_task(const std::string& name, Vruta::GraphicsRoutine&& task, bool initialize)
107{
108 auto task_ptr = std::make_shared<Vruta::GraphicsRoutine>(std::move(task));
109 get_scheduler()->add_task(std::move(task_ptr), name, initialize);
110}
111
112void schedule_task(const std::string& name, Vruta::FreeRoutine&& task, bool initialize)
113{
114 auto task_ptr = std::make_shared<Vruta::FreeRoutine>(std::move(task));
115 get_scheduler()->add_task(std::move(task_ptr), name, initialize);
116}
117
118bool cancel_task(const std::string& name)
119{
120 return get_scheduler()->cancel_task(name);
121}
122
123bool restart_task(const std::string& name)
124{
125 return get_scheduler()->restart_task(name);
126}
127
128std::shared_ptr<Kriya::BufferPipeline> create_buffer_pipeline()
129{
131}
132
134 const std::shared_ptr<Core::Window>& window,
135 IO::Keys key,
136 std::function<void()> callback,
137 std::string name)
138{
139 auto event_manager = get_event_manager();
140 if (name.empty()) {
141 name = "key_press_" + std::to_string(event_manager->get_next_event_id());
142 }
143
144 auto event = std::make_shared<Vruta::Event>(
145 Kriya::key_pressed(window, key, std::move(callback)));
146
147 event_manager->add_event(event, name);
148}
149
151 const std::shared_ptr<Core::Window>& window,
152 IO::Keys key,
153 std::function<void()> callback,
154 std::string name)
155{
156 auto event_manager = get_event_manager();
157 if (name.empty()) {
158 name = "key_release_" + std::to_string(event_manager->get_next_event_id());
159 }
160
161 auto event = std::make_shared<Vruta::Event>(
162 Kriya::key_released(window, key, std::move(callback)));
163
164 event_manager->add_event(event, name);
165}
166
168 const std::shared_ptr<Core::Window>& window,
169 std::function<void(IO::Keys)> callback,
170 std::string name)
171{
172 auto event_manager = get_event_manager();
173 if (name.empty()) {
174 name = "any_key_" + std::to_string(event_manager->get_next_event_id());
175 }
176
177 auto event = std::make_shared<Vruta::Event>(
178 Kriya::any_key(window, std::move(callback)));
179
180 event_manager->add_event(event, name);
181}
182
184 const std::shared_ptr<Core::Window>& window,
185 IO::MouseButtons button,
186 std::function<void(double, double)> callback,
187 std::string name)
188{
189 auto event_manager = get_event_manager();
190 if (name.empty()) {
191 name = "mouse_press_" + std::to_string(event_manager->get_next_event_id());
192 }
193
194 auto event = std::make_shared<Vruta::Event>(
195 Kriya::mouse_pressed(window, button, std::move(callback)));
196
197 event_manager->add_event(event, name);
198}
199
201 const std::shared_ptr<Core::Window>& window,
202 IO::MouseButtons button,
203 std::function<void(double, double)> callback,
204 std::string name)
205{
206 auto event_manager = get_event_manager();
207 if (name.empty()) {
208 name = "mouse_release_" + std::to_string(event_manager->get_next_event_id());
209 }
210
211 auto event = std::make_shared<Vruta::Event>(
212 Kriya::mouse_released(window, button, std::move(callback)));
213
214 event_manager->add_event(event, name);
215}
216
218 const std::shared_ptr<Core::Window>& window,
219 std::function<void(double, double)> callback,
220 std::string name)
221{
222 auto event_manager = get_event_manager();
223 if (name.empty()) {
224 name = "mouse_move_" + std::to_string(event_manager->get_next_event_id());
225 }
226
227 auto event = std::make_shared<Vruta::Event>(
228 Kriya::mouse_moved(window, std::move(callback)));
229
230 event_manager->add_event(event, name);
231}
232
234 const std::shared_ptr<Core::Window>& window,
235 IO::MouseButtons button,
236 std::function<void(double, double)> callback,
237 std::string name)
238{
239 auto event_manager = get_event_manager();
240 if (name.empty()) {
241 name = "mouse_drag_" + std::to_string(event_manager->get_next_event_id());
242 }
243
244 auto event = std::make_shared<Vruta::Event>(
245 Kriya::mouse_dragged(window, button, std::move(callback)));
246
247 event_manager->add_event(event, name);
248}
249
251 const std::shared_ptr<Core::Window>& window,
252 std::function<void(double, double)> callback,
253 std::string name)
254{
255 auto event_manager = get_event_manager();
256 if (name.empty()) {
257 name = "scroll_" + std::to_string(event_manager->get_next_event_id());
258 }
259
260 auto event = std::make_shared<Vruta::Event>(
261 Kriya::mouse_scrolled(window, std::move(callback)));
262
263 event_manager->add_event(event, name);
264}
265
266bool cancel_event_handler(const std::string& name)
267{
268 return get_event_manager()->cancel_event(name);
269}
270
271uint64_t seconds_to_samples(double seconds)
272{
273 uint64_t sample_rate = 48000;
274 if (get_context().is_running()) {
275 sample_rate = get_context().get_stream_info().sample_rate;
276 }
277 return static_cast<uint64_t>(seconds * (double)sample_rate);
278}
279
280uint64_t seconds_to_blocks(double seconds)
281{
282 uint32_t sample_rate = 48000;
283 uint32_t block_size = 512;
284
285 if (get_context().is_running()) {
286 sample_rate = get_context().get_stream_info().sample_rate;
287 block_size = get_context().get_stream_info().buffer_size;
288 }
289
290 return Vruta::seconds_to_blocks(seconds, sample_rate, block_size);
291}
292
293uint64_t samples_to_blocks(uint64_t samples)
294{
295 uint32_t block_size = 512;
296
297 if (get_context().is_running()) {
298 block_size = get_context().get_stream_info().buffer_size;
299 }
300
301 return Vruta::samples_to_blocks(samples, block_size);
302}
303
304double samples_to_seconds(uint64_t samples)
305{
306 uint64_t sample_rate = 48000;
307 if (get_context().is_running()) {
308 sample_rate = get_context().get_stream_info().sample_rate;
309 }
310
311 return static_cast<double>(samples) / (double)sample_rate;
312}
313
315 std::shared_ptr<Vruta::NetworkSource> source,
316 std::function<void(const Core::NetworkMessage&)> callback,
317 std::string name)
318{
319 auto event_manager = get_event_manager();
320 if (name.empty()) {
321 name = "net_msg_" + std::to_string(event_manager->get_next_event_id());
322 }
323 event_manager->add_event(
324 std::make_shared<Vruta::Event>(Kriya::on_message(std::move(source), std::move(callback))),
325 name);
326}
327
328std::shared_ptr<Vruta::NetworkSource> on_network_message(
329 const Core::EndpointInfo& info,
330 std::function<void(const Core::NetworkMessage&)> callback,
331 std::string name)
332{
333 auto source = std::make_shared<Vruta::NetworkSource>(info);
334 on_network_message(source, std::move(callback), std::move(name));
335 return source;
336}
337
339 std::shared_ptr<Vruta::NetworkSource> source,
340 std::string sender_address,
341 std::function<void(const Core::NetworkMessage&)> callback,
342 std::string name)
343{
344 auto event_manager = get_event_manager();
345 if (name.empty()) {
346 name = "net_msg_from_" + std::to_string(event_manager->get_next_event_id());
347 }
348 event_manager->add_event(
349 std::make_shared<Vruta::Event>(
350 Kriya::on_message_from(std::move(source), std::move(sender_address), std::move(callback))),
351 name);
352}
353
354std::shared_ptr<Vruta::NetworkSource> on_network_message_from(
355 const Core::EndpointInfo& info,
356 std::string sender_address,
357 std::function<void(const Core::NetworkMessage&)> callback,
358 std::string name)
359{
360 auto source = std::make_shared<Vruta::NetworkSource>(info);
361 on_network_message_from(source, std::move(sender_address), std::move(callback), std::move(name));
362 return source;
363}
364
366 std::shared_ptr<Vruta::NetworkSource> source,
367 std::function<bool(const Core::NetworkMessage&)> predicate,
368 std::function<void(const Core::NetworkMessage&)> callback,
369 std::string name)
370{
371 auto event_manager = get_event_manager();
372 if (name.empty()) {
373 name = "net_msg_match_" + std::to_string(event_manager->get_next_event_id());
374 }
375 event_manager->add_event(
376 std::make_shared<Vruta::Event>(
377 Kriya::on_message_matching(std::move(source), std::move(predicate), std::move(callback))),
378 name);
379}
380
381std::shared_ptr<Vruta::NetworkSource> on_network_message_matching(
382 const Core::EndpointInfo& info,
383 std::function<bool(const Core::NetworkMessage&)> predicate,
384 std::function<void(const Core::NetworkMessage&)> callback,
385 std::string name)
386{
387 auto source = std::make_shared<Vruta::NetworkSource>(info);
388 on_network_message_matching(source, std::move(predicate), std::move(callback), std::move(name));
389 return source;
390}
391
392}
#define MF_ERROR(comp, ctx,...)
Core engine lifecycle and configuration API.
GlobalStreamInfo & get_stream_info()
Gets the current stream configuration.
Definition Engine.hpp:224
std::shared_ptr< Vruta::EventManager > get_event_manager()
Gets the event manager.
Definition Engine.hpp:303
std::shared_ptr< Vruta::TaskScheduler > get_scheduler()
Gets the task scheduler.
Definition Engine.hpp:276
static std::shared_ptr< BufferPipeline > create(Vruta::TaskScheduler &scheduler, std::shared_ptr< Buffers::BufferManager > buffer_manager=nullptr)
Coroutine resumed when a caller-supplied condition becomes true.
Definition Routine.hpp:784
A C++20 coroutine-based graphics processing task with frame-accurate timing.
Definition Routine.hpp:513
A C++20 coroutine-based audio processing task with sample-accurate timing.
Definition Routine.hpp:316
void initialize()
Definition main.cpp:11
MouseButtons
Enumeration for mouse buttons.
Definition Keys.hpp:147
@ CoroutineScheduling
Coroutine scheduling and temporal coordination (Vruta::TaskScheduler)
@ API
MayaFlux/API Wrapper and convenience functions.
Vruta::Event key_released(std::shared_ptr< Core::Window > window, IO::Keys key, std::function< void()> callback)
Creates an Event coroutine that triggers on specific key release.
Vruta::Event any_key(std::shared_ptr< Core::Window > window, std::function< void(IO::Keys)> callback)
Creates an Event coroutine that triggers on any key press.
Vruta::Event key_pressed(std::shared_ptr< Core::Window > window, IO::Keys key, std::function< void()> callback)
Creates an Event coroutine that triggers on specific key press.
Vruta::Event mouse_moved(std::shared_ptr< Core::Window > window, std::function< void(double, double)> callback)
Creates an Event coroutine that triggers on mouse movement.
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
Vruta::Event mouse_scrolled(std::shared_ptr< Core::Window > window, std::function< void(double, double)> callback)
Creates an Event coroutine that triggers on mouse scroll.
Vruta::Event mouse_released(std::shared_ptr< Core::Window > window, IO::MouseButtons button, std::function< void(double, double)> callback)
Creates an Event coroutine that triggers on specific mouse button release.
Vruta::Event on_message_matching(std::shared_ptr< Vruta::NetworkSource > source, std::function< bool(const Core::NetworkMessage &)> predicate, std::function< void(const Core::NetworkMessage &)> callback)
Creates an Event coroutine that fires only when a predicate matches.
Vruta::Event mouse_pressed(std::shared_ptr< Core::Window > window, IO::MouseButtons button, std::function< void(double, double)> callback)
Creates an Event coroutine that triggers on specific mouse button press.
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::Event mouse_dragged(std::shared_ptr< Core::Window > window, IO::MouseButtons button, std::function< void(double, double)> callback)
Creates an Event coroutine that triggers on mouse drag with specific button.
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
Vruta::Event on_message_from(std::shared_ptr< Vruta::NetworkSource > source, std::string sender_address, std::function< void(const Core::NetworkMessage &)> callback)
Creates an Event coroutine that fires only for messages from a specific sender.
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
Vruta::Event on_message(std::shared_ptr< Vruta::NetworkSource > source, std::function< void(const Core::NetworkMessage &)> callback)
Creates an Event coroutine that fires on every message received by a source.
uint64_t samples_to_blocks(uint64_t samples, uint32_t block_size)
Convert samples to number of processing blocks.
uint64_t seconds_to_blocks(double seconds, uint32_t sample_rate, uint32_t block_size)
Convert seconds to number of processing blocks.
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
std::shared_ptr< Buffers::BufferManager > get_buffer_manager()
Gets the buffer manager from the default engine.
Definition Graph.cpp:132
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
Core::Engine & get_context()
Gets the default engine instance.
Definition Core.cpp:68
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.
uint32_t buffer_size
Number of samples per processing block.
uint32_t sample_rate
Number of samples processed per second (Hz)
A received datagram or framed message with sender metadata.