MayaFlux 0.3.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
10
11namespace MayaFlux::Kriya {
12
13EventChain::EventChain(Vruta::TaskScheduler& scheduler, std::string name)
14 : m_Scheduler(scheduler)
15 , m_name(std::move(name))
16 , m_default_rate(scheduler.get_rate())
17{
18}
19
20EventChain& EventChain::then(std::function<void()> action, double delay_seconds)
21{
22 m_events.push_back({ std::move(action), delay_seconds });
23 return *this;
24}
25
27{
28 if (m_events.empty() || count == 0)
29 return *this;
30
31 auto last_event = m_events.back();
32
33 for (size_t i = 0; i < count; ++i) {
34 m_events.push_back(last_event);
35 }
36
37 return *this;
38}
39
41{
43 return *this;
44}
45
46EventChain& EventChain::wait(double delay_seconds)
47{
48 return then([]() { }, delay_seconds);
49}
50
51EventChain& EventChain::every(double interval_seconds, std::function<void()> action)
52{
53 return then(std::move(action), interval_seconds);
54}
55
57{
58 if (m_events.empty())
59 return;
60
61 m_on_complete_fired = false;
62
63 auto coroutine_func = [](std::vector<TimedEvent> events,
64 uint64_t rate,
65 std::function<void()> on_complete_callback,
67 auto& promise = co_await Kriya::GetAudioPromise {};
68
69 for (size_t iteration = 0; iteration < repeat_count; ++iteration) {
70 for (const auto& event : events) {
71 if (promise.should_terminate) {
72 break;
73 }
74
75 co_await SampleDelay { Vruta::seconds_to_samples(event.delay_seconds, rate) };
76 try {
77 if (event.action) {
78 event.action();
79 }
80 } catch (const std::exception& e) {
84 "Exception in EventChain action: {}", e.what());
85 } catch (...) {
89 "Unknown exception in EventChain action");
90 }
91 }
92
93 if (promise.should_terminate) {
94 break;
95 }
96 }
97
98 if (on_complete_callback) {
99 try {
100 on_complete_callback();
101 } catch (const std::exception& e) {
105 "Exception in EventChain on_complete: {}", e.what());
106 } catch (...) {
110 "Unknown exception in EventChain on_complete");
111 }
112 }
113 };
114
115 std::string task_name = m_name.empty() ? "EventChain_" + std::to_string(m_Scheduler.get_next_task_id()) : m_name;
116 m_routine = std::make_shared<Vruta::SoundRoutine>(
118 m_Scheduler.add_task(m_routine, task_name, true);
119}
120
122{
123 if (is_active()) {
125 m_routine = nullptr;
126
128 }
129}
130
132{
133 return m_routine && m_routine->is_active();
134}
135
136EventChain& EventChain::on_complete(std::function<void()> callback)
137{
138 m_on_complete = std::move(callback);
139 return *this;
140}
141
143{
145 m_on_complete_fired = true;
146 try {
148 } catch (const std::exception& e) {
152 "Exception in EventChain on_complete: {}", e.what());
153 } catch (...) {
157 "Unknown exception in EventChain on_complete");
158 }
159 }
160}
161
162}
#define MF_RT_ERROR(comp, ctx,...)
static const auto node_token
Definition Chain.cpp:9
Eigen::Index count
Vruta::TaskScheduler & m_Scheduler
Reference to the scheduler that manages timing.
Definition Chain.hpp:185
EventChain & every(double interval_seconds, std::function< void()> action)
Convenience method for repeating an action at regular intervals.
Definition Chain.cpp:51
size_t m_repeat_count
Number of times to repeat the entire chain.
Definition Chain.hpp:219
void cancel()
Cancels the event chain if it's currently executing.
Definition Chain.cpp:121
EventChain & then(std::function< void()> action, double delay_seconds=0.F)
Adds an event to the chain with a specified delay.
Definition Chain.cpp:20
EventChain & on_complete(std::function< void()> callback)
Sets a callback to execute when the chain stops executing.
Definition Chain.cpp:136
EventChain & repeat(size_t count)
Repeat the last event N times.
Definition Chain.cpp:26
std::function< void()> m_on_complete
Optional callback to execute when the chain completes.
Definition Chain.hpp:202
EventChain(Vruta::TaskScheduler &scheduler, std::string name="")
Constructs an EventChain with an explicit scheduler.
Definition Chain.cpp:13
EventChain & times(size_t count)
Repeat entire chain N times.
Definition Chain.cpp:40
void fire_on_complete()
Internal method to safely fire the on_complete callback.
Definition Chain.cpp:142
std::vector< TimedEvent > m_events
Collection of events in this chain.
Definition Chain.hpp:177
EventChain & wait(double delay_seconds)
Add a wait/delay without an action.
Definition Chain.cpp:46
bool m_on_complete_fired
Flag to ensure on_complete is only fired once.
Definition Chain.hpp:212
size_t repeat_count() const
Gets the repeat count for the entire chain.
Definition Chain.hpp:157
std::string m_name
Optional name for the event chain.
Definition Chain.hpp:210
void start()
Starts executing the event chain.
Definition Chain.cpp:56
std::shared_ptr< Vruta::SoundRoutine > m_routine
The underlying computational routine that implements the chain.
Definition Chain.hpp:193
bool is_active() const
Checks if the event chain is currently active.
Definition Chain.cpp:131
A sequential chain of timed events with precise temporal control.
Definition Chain.hpp:41
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:19
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.
Definition Scheduler.cpp:62
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.
@ AUDIO_RATE
Nodes that process at the audio sample rate.
uint64_t seconds_to_samples(double seconds, uint32_t sample_rate)
Convert seconds to samples at a given sample rate.
Templated awaitable for accessing a coroutine's promise object.
Awaitable object for precise sample-accurate timing delays.