MayaFlux 0.3.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{
16}
17
18void Timer::schedule(double delay_seconds, std::function<void()> callback)
19{
20 cancel();
21 m_callback = std::move(callback);
22 m_active = true;
23
24 auto routine_func = [](Vruta::TaskScheduler& scheduler, uint64_t delay_samples, Timer* timer_ptr) -> Vruta::SoundRoutine {
25 auto& promise = co_await Kriya::GetAudioPromise {};
26 co_await SampleDelay { delay_samples };
27
28 if (timer_ptr && timer_ptr->is_active()) {
29 timer_ptr->m_callback();
30 timer_ptr->m_active = false;
31 }
32 };
33
34 m_routine = std::make_shared<Vruta::SoundRoutine>(
35 routine_func(m_Scheduler, m_Scheduler.seconds_to_samples(delay_seconds), this));
36
37 Vruta::ProcessingToken token = m_routine->get_processing_token();
38 uint64_t current_time = m_Scheduler.current_units(token);
39
40 m_Scheduler.add_task(m_routine, "", false);
41
42 m_routine->initialize_state(current_time);
43}
44
46{
47 if (m_active && m_routine) {
49 m_routine = nullptr;
50 m_active = false;
51 }
52}
53
55 : m_Scheduler(scheduler)
56 , m_timer(scheduler)
57{
58}
59
60void TimedAction::execute(const std::function<void()>& start_func, const std::function<void()>& end_func, double duration_seconds)
61{
62 cancel();
63 start_func();
64 m_timer.schedule(duration_seconds, end_func);
65}
66
68{
70}
71
73{
74 return m_timer.is_active();
75}
76
78 Nodes::NodeGraphManager& node_graph_manager,
79 Buffers::BufferManager& buffer_manager)
80 : m_scheduler(scheduler)
81 , m_node_graph_manager(node_graph_manager)
82 , m_buffer_manager(buffer_manager)
83 , m_timer(scheduler)
84{
85}
86
87void TemporalActivation::activate_node(const std::shared_ptr<Nodes::Node>& node,
88 double duration_seconds,
90 const std::vector<uint32_t>& channels)
91{
92 cancel();
93
94 m_current_node = node;
95 m_node_token = token;
96 m_channels = channels;
98
99 for (auto channel : channels) {
100 m_node_graph_manager.add_to_root(node, token, channel);
101 }
102
103 m_timer.schedule(duration_seconds, [this]() {
105 });
106}
107
108void TemporalActivation::activate_network(const std::shared_ptr<Nodes::Network::NodeNetwork>& network,
109 double duration_seconds, Nodes::ProcessingToken token, const std::vector<uint32_t>& channels)
110{
111 cancel();
112
113 m_current_network = network;
114 m_node_token = token;
116 m_channels = channels;
117
118 for (const auto& ch : channels) {
119 network->add_channel_usage(ch);
120 }
121
122 m_node_graph_manager.add_network(network, token);
123
124 m_timer.schedule(duration_seconds, [this]() {
126 });
127}
128
129void TemporalActivation::activate_buffer(const std::shared_ptr<Buffers::Buffer>& buffer,
130 double duration_seconds,
132 uint32_t channel)
133{
134 cancel();
135
136 m_current_buffer = buffer;
137 m_buffer_token = token;
138 m_channels = { channel };
140
141 m_buffer_manager.add_buffer(buffer, token, channel);
142
143 m_timer.schedule(duration_seconds, [this]() {
145 });
146}
147
149{
150 switch (m_active_type) {
151 case ActiveType::NODE:
152 if (m_current_node) {
153 if (m_channels.empty()) {
155 }
156 for (auto channel : m_channels) {
157 if (m_current_node->is_used_by_channel(channel)) {
159 }
160 }
161 m_current_node = nullptr;
162 m_channels.clear();
163 }
164 break;
165
167 if (m_current_network) {
168 for (const auto& ch : m_channels) {
169 m_current_network->remove_channel_usage(ch);
170 }
171
173 m_current_network = nullptr;
174 }
175 break;
176
178 if (m_current_buffer) {
179 for (auto channel : m_channels) {
181 }
182 m_current_buffer = nullptr;
183 }
184 break;
185
186 case ActiveType::NONE:
187 break;
188 }
189
191}
192
194{
195 if (m_timer.is_active()) {
197 }
198 m_timer.cancel();
199}
200
201}
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:406
std::vector< uint32_t > m_channels
The output channels the current node is connected to.
Definition Timers.hpp:415
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:108
Nodes::ProcessingToken m_node_token
The processing token associated with the currently active node or buffer.
Definition Timers.hpp:397
void cleanup_current_operation()
Cleans up the current operation, disconnecting the entity and resetting state.
Definition Timers.cpp:148
std::shared_ptr< Buffers::Buffer > m_current_buffer
The currently active buffer being played.
Definition Timers.hpp:388
std::shared_ptr< Nodes::Network::NodeNetwork > m_current_network
The currently active network being played.
Definition Timers.hpp:379
Nodes::NodeGraphManager & m_node_graph_manager
Reference to the graph manager that manages processing nodes.
Definition Timers.hpp:345
TemporalActivation(Vruta::TaskScheduler &scheduler, Nodes::NodeGraphManager &graph_manager, Buffers::BufferManager &buffer_manager)
Constructs a TemporalActivation with the specified scheduler and manager.
Definition Timers.cpp:77
void cancel()
Cancels any currently active node.
Definition Timers.cpp:193
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:87
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:129
std::shared_ptr< Nodes::Node > m_current_node
The currently active node being played.
Definition Timers.hpp:370
Buffers::BufferManager & m_buffer_manager
Reference to the buffer manager that manages processing buffers.
Definition Timers.hpp:353
Timer m_timer
The timer used to schedule processing duration.
Definition Timers.hpp:361
void cancel()
Cancels any active action.
Definition Timers.cpp:67
bool is_pending() const
Checks if an action is currently in progress.
Definition Timers.cpp:72
Timer m_timer
The timer used to schedule the end function.
Definition Timers.hpp:211
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:60
TimedAction(Vruta::TaskScheduler &scheduler)
Constructs a TimedAction with the specified scheduler.
Definition Timers.cpp:54
bool m_active
Flag indicating whether a callback is currently scheduled.
Definition Timers.hpp:115
bool is_active() const
Checks if a callback is currently scheduled.
Definition Timers.hpp:89
Timer(Vruta::TaskScheduler &scheduler)
Constructs a Timer with the specified scheduler.
Definition Timers.cpp:12
std::function< void()> m_callback
The callback function to execute when the timer fires.
Definition Timers.hpp:123
Vruta::TaskScheduler & m_Scheduler
Reference to the scheduler that manages this timer.
Definition Timers.hpp:98
std::shared_ptr< Vruta::SoundRoutine > m_routine
The underlying computational routine that implements the timer.
Definition Timers.hpp:107
void schedule(double delay_seconds, std::function< void()> callback)
Schedules a callback to execute after a delay.
Definition Timers.cpp:18
void cancel()
Cancels any scheduled callback.
Definition Timers.cpp:45
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 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 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.
Definition Scheduler.cpp:62
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.
Templated awaitable for accessing a coroutine's promise object.
Awaitable object for precise sample-accurate timing delays.