MayaFlux 1.0.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 auto s_coro_func = [](std::vector<TimedEvent> events,
61 uint64_t rate,
63 auto& promise = co_await Kriya::GetAudioPromise {};
64
65 for (size_t iteration = 0; iteration < repeat_count; ++iteration) {
66 for (const auto& event : events) {
67 if (promise.should_terminate) {
68 break;
69 }
70
71 co_await SampleDelay { Vruta::seconds_to_samples(event.delay_seconds, rate) };
72 try {
73 if (event.action) {
74 event.action();
75 }
76 } catch (const std::exception& e) {
80 "Exception in EventChain action: {}", e.what());
81 } catch (...) {
85 "Unknown exception in EventChain action");
86 }
87 }
88
89 if (promise.should_terminate) {
90 break;
91 }
92 }
93 };
94
95 auto g_coro_func = [](std::vector<TimedEvent> events,
96 uint64_t rate,
98 auto& promise = co_await Kriya::GetGraphicsPromise {};
99
100 for (size_t iteration = 0; iteration < repeat_count; ++iteration) {
101 for (const auto& event : events) {
102 if (promise.should_terminate) {
103 break;
104 }
105
106 co_await FrameDelay { Vruta::seconds_to_frames(event.delay_seconds, rate) };
107 try {
108 if (event.action) {
109 event.action();
110 }
111 } catch (const std::exception& e) {
115 "Exception in EventChain action: {}", e.what());
116 } catch (...) {
120 "Unknown exception in EventChain action");
121 }
122 }
123
124 if (promise.should_terminate) {
125 break;
126 }
127 }
128 };
129
130 std::string task_name = m_name.empty() ? "EventChain_" + std::to_string(m_Scheduler.get_next_task_id()) : m_name;
131
133 m_routine = std::make_shared<Vruta::SoundRoutine>(
134 s_coro_func(m_events, m_default_rate, m_repeat_count));
135 } else {
136 m_routine = std::make_shared<Vruta::GraphicsRoutine>(
137 g_coro_func(m_events, m_default_rate, m_repeat_count));
138 }
139
140 if (m_on_complete) {
141 m_routine->on_complete(m_on_complete);
142 }
143
144 m_Scheduler.add_task(m_routine, task_name, true);
145}
146
148{
149 if (!is_active()) {
150 return;
151 }
152
153 auto routine = std::exchange(m_routine, nullptr);
154 routine->set_should_terminate(true);
155 m_Scheduler.cancel_task(routine);
156}
157
159{
160 return m_routine && m_routine->is_active();
161}
162
164{
165 m_on_complete = std::move(callback);
166 return *this;
167}
168
169}
#define MF_RT_ERROR(comp, ctx,...)
float rate
PickCallback callback
Definition Picker.cpp:22
std::string name
Definition VKDevice.cpp:123
size_t count
Vruta::TaskScheduler & m_Scheduler
Reference to the scheduler that manages timing.
Definition Chain.hpp:189
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:225
void cancel()
Cancels the event chain if it's currently executing.
Definition Chain.cpp:147
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:163
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:215
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
std::vector< TimedEvent > m_events
Collection of events in this chain.
Definition Chain.hpp:181
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:198
std::shared_ptr< Vruta::Routine > m_routine
The underlying computational routine that implements the chain.
Definition Chain.hpp:206
size_t repeat_count() const
Gets the repeat count for the entire chain.
Definition Chain.hpp:161
std::string m_name
Optional name for the event chain.
Definition Chain.hpp:223
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:158
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:512
A C++20 coroutine-based audio processing task with sample-accurate timing.
Definition Routine.hpp:327
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:94
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.