MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Timers.cpp
Go to the documentation of this file.
1#include "Timers.hpp"
2
5
9
10namespace MayaFlux::Kriya {
11
13 : m_Scheduler(scheduler)
14 , m_active(false)
15 , m_token(token)
16{
17}
18
19void Timer::schedule(double delay_seconds, std::function<void()> callback)
20{
21 cancel();
22 m_callback = std::move(callback);
23 m_active = true;
24
26 auto routine_func = [](Vruta::TaskScheduler& scheduler, uint64_t delay_frames, Timer* timer_ptr) -> Vruta::GraphicsRoutine {
27 auto& promise = co_await Kriya::GetGraphicsPromise {};
28
29 co_await FrameDelay { .frames_to_wait = delay_frames };
30
31 if (timer_ptr && timer_ptr->is_active()) {
32 timer_ptr->m_callback();
33 timer_ptr->m_active = false;
34 }
35 };
36 m_routine = std::make_shared<Vruta::GraphicsRoutine>(
38 } else {
39 auto routine_func = [](Vruta::TaskScheduler& scheduler, uint64_t delay_samples, Timer* timer_ptr) -> Vruta::SoundRoutine {
40 auto& promise = co_await Kriya::GetAudioPromise {};
41
42 co_await SampleDelay { delay_samples };
43
44 if (timer_ptr && timer_ptr->is_active()) {
45 timer_ptr->m_callback();
46 timer_ptr->m_active = false;
47 }
48 };
49 m_routine = std::make_shared<Vruta::SoundRoutine>(
50 routine_func(m_Scheduler, m_Scheduler.seconds_to_samples(delay_seconds), this));
51 }
52
53 uint64_t current_time = m_Scheduler.current_units(m_token);
54 m_Scheduler.add_task(m_routine, "", false);
55
56 m_routine->initialize_state(current_time);
57}
58
60{
61 if (m_active && m_routine) {
63 m_routine = nullptr;
64 m_active = false;
65 }
66}
67
69 : m_Scheduler(scheduler)
70 , m_timer(scheduler, token)
71 , m_token(token)
72{
73}
74
75void TimedAction::execute(const std::function<void()>& start_func, const std::function<void()>& end_func, double duration_seconds)
76{
77 cancel();
78 start_func();
79 m_timer.schedule(duration_seconds, end_func);
80}
81
83{
85}
86
88{
89 return m_timer.is_active();
90}
91
93 Nodes::NodeGraphManager& node_graph_manager,
95 : m_scheduler(scheduler)
96 , m_node_graph_manager(node_graph_manager)
97 , m_buffer_manager(buffer_manager)
98 , m_timer(scheduler, token)
99 , m_execution_token(token)
100{
101}
102
103void TemporalActivation::activate_node(const std::shared_ptr<Nodes::Node>& node,
104 double duration_seconds,
106 const std::vector<uint32_t>& channels)
107{
108 cancel();
109
110 m_current_node = node;
111 m_node_token = token;
112 m_channels = channels;
114
115 for (auto channel : channels) {
116 m_node_graph_manager.add_to_root(node, token, channel);
117 }
118
119 m_timer.schedule(duration_seconds, [this]() {
121 });
122}
123
124void TemporalActivation::activate_network(const std::shared_ptr<Nodes::Network::NodeNetwork>& network,
125 double duration_seconds, Nodes::ProcessingToken token, const std::vector<uint32_t>& channels)
126{
127 cancel();
128
130 m_node_token = token;
132 m_channels = channels;
133
134 for (const auto& ch : channels) {
135 network->add_channel_usage(ch);
136 }
137
139
140 m_timer.schedule(duration_seconds, [this]() {
142 });
143}
144
145void TemporalActivation::activate_buffer(const std::shared_ptr<Buffers::Buffer>& buffer,
146 double duration_seconds,
148 uint32_t channel)
149{
150 cancel();
151
152 m_current_buffer = buffer;
153 m_buffer_token = token;
154 m_channels = { channel };
156
157 m_buffer_manager.add_buffer(buffer, token, channel);
158
159 m_timer.schedule(duration_seconds, [this]() {
161 });
162}
163
165{
166 switch (m_active_type) {
167 case ActiveType::NODE:
168 if (m_current_node) {
169 if (m_channels.empty()) {
171 }
172 for (auto channel : m_channels) {
173 if (m_current_node->is_used_by_channel(channel)) {
175 }
176 }
177 m_current_node = nullptr;
178 m_channels.clear();
179 }
180 break;
181
183 if (m_current_network) {
184 for (const auto& ch : m_channels) {
185 m_current_network->remove_channel_usage(ch);
186 }
187
189 m_current_network = nullptr;
190 }
191 break;
192
194 if (m_current_buffer) {
195 for (auto channel : m_channels) {
197 }
198 m_current_buffer = nullptr;
199 }
200 break;
201
202 case ActiveType::NONE:
203 break;
204 }
205
207}
208
210{
211 if (m_timer.is_active()) {
213 }
214 m_timer.cancel();
215}
216
217}
Core::GlobalNetworkConfig network
Definition Config.cpp:37
void remove_buffer(const std::shared_ptr< Buffer > &buffer, ProcessingToken token, uint32_t channel=0)
Removes a buffer from a token.
void add_buffer(const std::shared_ptr< Buffer > &buffer, ProcessingToken token, uint32_t channel=0)
Adds a buffer to a token and channel.
Token-based multimodal buffer management system for unified data stream processing.
Buffers::ProcessingToken m_buffer_token
The processing token associated with the currently active buffer.
Definition Timers.hpp:420
std::vector< uint32_t > m_channels
The output channels the current node is connected to.
Definition Timers.hpp:438
TemporalActivation(Vruta::TaskScheduler &scheduler, Nodes::NodeGraphManager &graph_manager, Buffers::BufferManager &buffer_manager, Vruta::ProcessingToken token=Vruta::ProcessingToken::SAMPLE_ACCURATE)
Constructs a TemporalActivation with the specified scheduler and manager.
Definition Timers.cpp:92
void activate_network(const std::shared_ptr< Nodes::Network::NodeNetwork > &network, double duration_seconds, Nodes::ProcessingToken token=Nodes::ProcessingToken::AUDIO_RATE, const std::vector< uint32_t > &channels={})
Activates a node network for a specified duration.
Definition Timers.cpp:124
Nodes::ProcessingToken m_node_token
The processing token associated with the currently active node or buffer.
Definition Timers.hpp:411
void cleanup_current_operation()
Cleans up the current operation, disconnecting the entity and resetting state.
Definition Timers.cpp:164
std::shared_ptr< Buffers::Buffer > m_current_buffer
The currently active buffer being played.
Definition Timers.hpp:402
std::shared_ptr< Nodes::Network::NodeNetwork > m_current_network
The currently active network being played.
Definition Timers.hpp:393
Nodes::NodeGraphManager & m_node_graph_manager
Reference to the graph manager that manages processing nodes.
Definition Timers.hpp:359
void cancel()
Cancels any currently active node.
Definition Timers.cpp:209
void activate_node(const std::shared_ptr< Nodes::Node > &node, double duration_seconds, Nodes::ProcessingToken token=Nodes::ProcessingToken::AUDIO_RATE, const std::vector< uint32_t > &channels={})
Activates a node for a specified duration.
Definition Timers.cpp:103
void activate_buffer(const std::shared_ptr< Buffers::Buffer > &buffer, double duration_seconds, Buffers::ProcessingToken token=Buffers::ProcessingToken::AUDIO_BACKEND, uint32_t channel=0)
Activates a buffer for a specified duration.
Definition Timers.cpp:145
std::shared_ptr< Nodes::Node > m_current_node
The currently active node being played.
Definition Timers.hpp:384
Buffers::BufferManager & m_buffer_manager
Reference to the buffer manager that manages processing buffers.
Definition Timers.hpp:367
Timer m_timer
The timer used to schedule processing duration.
Definition Timers.hpp:375
TimedAction(Vruta::TaskScheduler &scheduler, Vruta::ProcessingToken token=Vruta::ProcessingToken::SAMPLE_ACCURATE)
Constructs a TimedAction with the specified scheduler.
Definition Timers.cpp:68
void cancel()
Cancels any active action.
Definition Timers.cpp:82
bool is_pending() const
Checks if an action is currently in progress.
Definition Timers.cpp:87
Timer m_timer
The timer used to schedule the end function.
Definition Timers.hpp:218
void execute(const std::function< void()> &start_func, const std::function< void()> &end_func, double duration_seconds)
Executes a pair of functions with a time interval between them.
Definition Timers.cpp:75
bool m_active
Flag indicating whether a callback is currently scheduled.
Definition Timers.hpp:116
bool is_active() const
Checks if a callback is currently scheduled.
Definition Timers.hpp:90
std::shared_ptr< Vruta::Routine > m_routine
The underlying computational routine that implements the timer.
Definition Timers.hpp:108
std::function< void()> m_callback
The callback function to execute when the timer fires.
Definition Timers.hpp:124
Vruta::TaskScheduler & m_Scheduler
Reference to the scheduler that manages this timer.
Definition Timers.hpp:99
Vruta::ProcessingToken m_token
The processing token that determines the timing context and thread evaluator for this timer.
Definition Timers.hpp:129
void schedule(double delay_seconds, std::function< void()> callback)
Schedules a callback to execute after a delay.
Definition Timers.cpp:19
Timer(Vruta::TaskScheduler &scheduler, Vruta::ProcessingToken token=Vruta::ProcessingToken::SAMPLE_ACCURATE)
Constructs a Timer with the specified scheduler.
Definition Timers.cpp:12
void cancel()
Cancels any scheduled callback.
Definition Timers.cpp:59
High-level utility for scheduling one-shot timed callbacks.
Definition Timers.hpp:47
void remove_network(const std::shared_ptr< Network::NodeNetwork > &network, ProcessingToken token)
Remove a network from a processing token.
void add_network(const std::shared_ptr< Network::NodeNetwork > &network, ProcessingToken token)
Add a network to a processing token.
void add_to_root(const std::shared_ptr< Node > &node, ProcessingToken token, unsigned int channel=0)
Add node to specific processing token and channel.
RootNode & get_root_node(ProcessingToken token, unsigned int channel)
Gets or creates the root node for a specific token and channel.
Central manager for the computational processing node graph.
void unregister_node(const std::shared_ptr< Node > &node)
Removes a node from this root node.
Definition RootNode.cpp:46
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 seconds_to_units(double seconds, ProcessingToken token=ProcessingToken::SAMPLE_ACCURATE) const
Convert seconds to processing units for a specific domain.
uint64_t seconds_to_samples(double seconds) const
Converts a time in seconds to a number of samples.
uint64_t current_units(ProcessingToken token=ProcessingToken::SAMPLE_ACCURATE) const
Get current processing units for a domain.
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
ProcessingToken
Bitfield enum defining processing characteristics and backend requirements for buffer operations.
ProcessingToken
Enumerates the different processing domains for nodes.
@ FRAME_ACCURATE
Coroutine is frame-accurate.
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.