MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Chain.cpp
Go to the documentation of this file.
1#include "Chain.hpp"
7
9
10namespace MayaFlux::Kriya {
11
13 : m_Scheduler(*MayaFlux::get_scheduler())
14{
15}
16
18 : m_Scheduler(scheduler)
19{
20}
21
22EventChain& EventChain::then(std::function<void()> action, double delay_seconds)
23{
24 m_events.push_back({ std::move(action), delay_seconds });
25 return *this;
26}
27
29{
30 if (m_events.empty())
31 return;
32
33 auto shared_this = std::make_shared<EventChain>(*this);
34
35 auto coroutine_func = [](Vruta::TaskScheduler& scheduler, std::shared_ptr<EventChain> chain) -> MayaFlux::Vruta::SoundRoutine {
36 for (const auto& event : chain->m_events) {
37 co_await SampleDelay { scheduler.seconds_to_samples(event.delay_seconds) };
38 try {
39 if (event.action) {
40 event.action();
41 }
42 } catch (const std::exception& e) {
43 std::cerr << "Exception in EventChain action: " << e.what() << '\n';
44 } catch (...) {
45 std::cerr << "Unknown exception in EventChain action" << '\n';
46 }
47 }
48 };
49
50 m_routine = std::make_shared<Vruta::SoundRoutine>(coroutine_func(m_Scheduler, shared_this));
52}
53
54ActionToken::ActionToken(std::shared_ptr<Nodes::Node> _node)
55 : type(Utils::ActionType::NODE)
56 , node(std::move(_node))
57{
58}
59
61 : type(Utils::ActionType::TIME)
62 , seconds(_seconds)
63{
64}
65
66ActionToken::ActionToken(std::function<void()> _func)
67 : type(Utils::ActionType::FUNCTION)
68 , func(std::move(_func))
69{
70}
71
73{
74 tokens.push_back(token);
75 return *this;
76}
77
82
83void Sequence::execute(const std::shared_ptr<Nodes::NodeGraphManager>& node_manager, const std::shared_ptr<Vruta::TaskScheduler>& scheduler)
84{
85 EventChain chain(*scheduler);
86 double accumulated_time = 0.F;
87
88 for (const auto& token : tokens) {
89 if (token.type == Utils::ActionType::NODE) {
90 chain.then([node = token.node, node_manager]() {
91 auto& root = node_manager->get_root_node(node_token, 0);
92 root.register_node(node);
93 },
94 accumulated_time);
95 accumulated_time = 0.F;
96 } else if (token.type == Utils::ActionType::TIME) {
97 accumulated_time += token.seconds;
98 } else if (token.type == Utils::ActionType::FUNCTION) {
99 chain.then(token.func, accumulated_time);
100 accumulated_time = 0.F;
101 }
102 }
103 chain.start();
104}
105}
static const auto node_token
Definition Chain.cpp:8
static MayaFlux::Nodes::ProcessingToken token
Definition Timers.cpp:8
ActionToken(std::shared_ptr< Nodes::Node > _node)
Constructs an ActionToken representing a node connection.
Definition Chain.cpp:54
A token representing an action in a computational sequence.
Definition Chain.hpp:140
Vruta::TaskScheduler & m_Scheduler
Reference to the scheduler that manages timing.
Definition Chain.hpp:113
EventChain & then(std::function< void()> action, double delay_seconds=0.F)
Adds an event to the chain with a specified delay.
Definition Chain.cpp:22
std::vector< TimedEvent > m_events
Collection of events in this chain.
Definition Chain.hpp:105
void start()
Starts executing the event chain.
Definition Chain.cpp:28
EventChain()
Constructs an EventChain using the global scheduler.
Definition Chain.cpp:12
std::shared_ptr< Vruta::SoundRoutine > m_routine
The underlying computational routine that implements the chain.
Definition Chain.hpp:121
A sequential chain of timed events with precise temporal control.
Definition Chain.hpp:39
std::vector< ActionToken > tokens
Collection of actions in this sequence.
Definition Chain.hpp:289
void execute()
Executes the sequence using the global managers.
Definition Chain.cpp:78
Sequence & operator>>(const ActionToken &token)
Adds an action to the sequence.
Definition Chain.cpp:72
A sequence of computational operations with a fluent, declarative API.
Definition Chain.hpp:240
A C++20 coroutine-based audio processing task with sample-accurate timing.
Definition Routine.hpp:309
void add_task(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:17
uint64_t seconds_to_samples(double seconds) const
Converts a time in seconds to a number of samples.
Token-based multimodal task scheduling system for unified coroutine processing.
Definition Scheduler.hpp:51
@ AUDIO_RATE
Nodes that process at the audio sample rate.
std::shared_ptr< Nodes::NodeGraphManager > get_node_graph_manager()
Gets the node graph manager from the default engine.
Definition Graph.cpp:18
std::shared_ptr< Vruta::TaskScheduler > get_scheduler()
Gets the task scheduler from the default engine.
Definition Chronie.cpp:14
Main namespace for the Maya Flux audio engine.
Definition LiveAid.hpp:6
Awaitable object for precise sample-accurate timing delays.
Definition Awaiters.hpp:35