MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ start()

void MayaFlux::Kriya::EventChain::start ( )

Starts executing the event chain.

This method begins the execution of the event chain, scheduling each event to occur at its specified time. The events are executed in the order they were added, with the specified delays between them.

The timing is sample-accurate, ensuring that each event occurs at precisely the right moment in the computational timeline.

Definition at line 55 of file Chain.cpp.

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}
#define MF_RT_ERROR(comp, ctx,...)
Vruta::TaskScheduler & m_Scheduler
Reference to the scheduler that manages timing.
Definition Chain.hpp:188
size_t m_repeat_count
Number of times to repeat the entire chain.
Definition Chain.hpp:231
std::function< void()> m_on_complete
Optional callback to execute when the chain completes.
Definition Chain.hpp:214
std::vector< TimedEvent > m_events
Collection of events in this chain.
Definition Chain.hpp:180
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
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.
@ CoroutineScheduling
Coroutine scheduling and temporal coordination (Vruta::TaskScheduler)
@ Kriya
Automatable tasks and fluent scheduling api for Nodes and Buffers.
GetPromiseBase< Vruta::graphics_promise > GetGraphicsPromise
Graphics domain promise accessor.
GetPromiseBase< Vruta::audio_promise > GetAudioPromise
Audio domain promise accessor.
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.

References MayaFlux::Vruta::TaskScheduler::add_task(), MayaFlux::Journal::CoroutineScheduling, MayaFlux::Vruta::TaskScheduler::get_next_task_id(), MayaFlux::Journal::Kriya, m_default_rate, m_events, m_name, m_on_complete, m_on_complete_fired, m_repeat_count, m_routine, m_Scheduler, m_token, MF_RT_ERROR, repeat_count(), MayaFlux::Vruta::SAMPLE_ACCURATE, MayaFlux::Vruta::seconds_to_frames(), and MayaFlux::Vruta::seconds_to_samples().

+ Here is the call graph for this function: