MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Chain.cpp
Go to the documentation of this file.
1#include "Chain.hpp"
2
6
8
9namespace MayaFlux::Kriya {
10
12 : m_Scheduler(scheduler)
13 , m_name(std::move(name))
14 , m_token(token)
15 , m_default_rate(scheduler.get_rate(token))
16{
17}
18
19EventChain& EventChain::then(std::function<void()> action, double delay_seconds)
20{
21 m_events.push_back({ .action = std::move(action), .delay_seconds = delay_seconds });
22 return *this;
23}
24
26{
27 if (m_events.empty() || count == 0)
28 return *this;
29
30 auto last_event = m_events.back();
31
32 for (size_t i = 0; i < count; ++i) {
33 m_events.push_back(last_event);
34 }
35
36 return *this;
37}
38
40{
42 return *this;
43}
44
45EventChain& EventChain::wait(double delay_seconds)
46{
47 return then([]() { }, delay_seconds);
48}
49
50EventChain& EventChain::every(double interval_seconds, std::function<void()> action)
51{
52 return then(std::move(action), interval_seconds);
53}
54
56{
57 if (m_events.empty())
58 return;
59
60 m_on_complete_fired = false;
61
62 auto s_coro_func = [](std::vector<TimedEvent> events,
63 uint64_t rate,
64 std::function<void()> on_complete_callback,
66 auto& promise = co_await Kriya::GetAudioPromise {};
67
68 for (size_t iteration = 0; iteration < repeat_count; ++iteration) {
69 for (const auto& event : events) {
70 if (promise.should_terminate) {
71 break;
72 }
73
74 co_await SampleDelay { Vruta::seconds_to_samples(event.delay_seconds, rate) };
75 try {
76 if (event.action) {
77 event.action();
78 }
79 } catch (const std::exception& e) {
83 "Exception in EventChain action: {}", e.what());
84 } catch (...) {
88 "Unknown exception in EventChain action");
89 }
90 }
91
92 if (promise.should_terminate) {
93 break;
94 }
95 }
96
97 if (on_complete_callback) {
98 try {
99 on_complete_callback();
100 } catch (const std::exception& e) {
104 "Exception in EventChain on_complete: {}", e.what());
105 } catch (...) {
109 "Unknown exception in EventChain on_complete");
110 }
111 }
112 };
113
114 auto g_coro_func = [](std::vector<TimedEvent> events,
115 uint64_t rate,
116 std::function<void()> on_complete_callback,
118 auto& promise = co_await Kriya::GetGraphicsPromise {};
119
120 for (size_t iteration = 0; iteration < repeat_count; ++iteration) {
121 for (const auto& event : events) {
122 if (promise.should_terminate) {
123 break;
124 }
125
126 co_await FrameDelay { Vruta::seconds_to_frames(event.delay_seconds, rate) };
127 try {
128 if (event.action) {
129 event.action();
130 }
131 } catch (const std::exception& e) {
135 "Exception in EventChain action: {}", e.what());
136 } catch (...) {
140 "Unknown exception in EventChain action");
141 }
142 }
143
144 if (promise.should_terminate) {
145 break;
146 }
147 }
148
149 if (on_complete_callback) {
150 try {
151 on_complete_callback();
152 } catch (const std::exception& e) {
156 "Exception in EventChain on_complete: {}", e.what());
157 } catch (...) {
161 "Unknown exception in EventChain on_complete");
162 }
163 }
164 };
165
166 std::string task_name = m_name.empty() ? "EventChain_" + std::to_string(m_Scheduler.get_next_task_id()) : m_name;
167
169 m_routine = std::make_shared<Vruta::SoundRoutine>(
171 } else {
172 m_routine = std::make_shared<Vruta::GraphicsRoutine>(
174 }
175
176 m_Scheduler.add_task(m_routine, task_name, true);
177}
178
180{
181 if (is_active()) {
183 m_routine = nullptr;
184
186 }
187}
188
190{
191 return m_routine && m_routine->is_active();
192}
193
194EventChain& EventChain::on_complete(std::function<void()> callback)
195{
196 m_on_complete = std::move(callback);
197 return *this;
198}
199
201{
203 m_on_complete_fired = true;
204 try {
206 } catch (const std::exception& e) {
210 "Exception in EventChain on_complete: {}", e.what());
211 } catch (...) {
215 "Unknown exception in EventChain on_complete");
216 }
217 }
218}
219
220}
#define MF_RT_ERROR(comp, ctx,...)
size_t count
Vruta::TaskScheduler & m_Scheduler
Reference to the scheduler that manages timing.
Definition Chain.hpp:188
EventChain & every(double interval_seconds, std::function< void()> action)
Convenience method for repeating an action at regular intervals.
Definition Chain.cpp:50
size_t m_repeat_count
Number of times to repeat the entire chain.
Definition Chain.hpp:231
void cancel()
Cancels the event chain if it's currently executing.
Definition Chain.cpp:179
EventChain & then(std::function< void()> action, double delay_seconds=0.F)
Adds an event to the chain with a specified delay.
Definition Chain.cpp:19
EventChain & on_complete(std::function< void()> callback)
Sets a callback to execute when the chain stops executing.
Definition Chain.cpp:194
EventChain & repeat(size_t count)
Repeat the last event N times.
Definition Chain.cpp:25
std::function< void()> m_on_complete
Optional callback to execute when the chain completes.
Definition Chain.hpp:214
EventChain(Vruta::TaskScheduler &scheduler, std::string name="", Vruta::ProcessingToken token=Vruta::ProcessingToken::SAMPLE_ACCURATE)
Constructs an EventChain with an explicit scheduler.
Definition Chain.cpp:11
EventChain & times(size_t count)
Repeat entire chain N times.
Definition Chain.cpp:39
void fire_on_complete()
Internal method to safely fire the on_complete callback.
Definition Chain.cpp:200
std::vector< TimedEvent > m_events
Collection of events in this chain.
Definition Chain.hpp:180
EventChain & wait(double delay_seconds)
Add a wait/delay without an action.
Definition Chain.cpp:45
Vruta::ProcessingToken m_token
Processing token to determine which scheduler rate to use.
Definition Chain.hpp:197
bool m_on_complete_fired
Flag to ensure on_complete is only fired once.
Definition Chain.hpp:224
std::shared_ptr< Vruta::Routine > m_routine
The underlying computational routine that implements the chain.
Definition Chain.hpp:205
size_t repeat_count() const
Gets the repeat count for the entire chain.
Definition Chain.hpp:160
std::string m_name
Optional name for the event chain.
Definition Chain.hpp:222
void start()
Starts executing the event chain.
Definition Chain.cpp:55
bool is_active() const
Checks if the event chain is currently active.
Definition Chain.cpp:189
A sequential chain of timed events with precise temporal control.
Definition Chain.hpp:43
A C++20 coroutine-based graphics processing task with frame-accurate timing.
Definition Routine.hpp:496
A C++20 coroutine-based audio processing task with sample-accurate timing.
Definition Routine.hpp:316
void add_task(const std::shared_ptr< Routine > &routine, const std::string &name="", bool initialize=false)
Add a routine to the scheduler based on its processing token.
Definition Scheduler.cpp:23
uint64_t get_next_task_id() const
Generates a unique task ID for new tasks.
bool cancel_task(const std::shared_ptr< Routine > &routine)
Cancels and removes a task from the scheduler.
Token-based multimodal task scheduling system for unified coroutine processing.
Definition Scheduler.hpp:51
@ CoroutineScheduling
Coroutine scheduling and temporal coordination (Vruta::TaskScheduler)
@ Kriya
Automatable tasks and fluent scheduling api for Nodes and Buffers.
uint64_t seconds_to_samples(double seconds, uint32_t sample_rate=s_registered_sample_rate)
Convert seconds to samples at a given sample rate.
@ SAMPLE_ACCURATE
Coroutine is sample-accurate.
uint64_t seconds_to_frames(double seconds, uint32_t frame_rate=s_registered_frame_rate)
Convert seconds to frames at a given frame rate.
graphics-domain awaiter for frame-accurate timing delays
Templated awaitable for accessing a coroutine's promise object.
Awaitable object for precise sample-accurate timing delays.