MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Timers.cpp
Go to the documentation of this file.
1#include "Timers.hpp"
7
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 = 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(std::function<void()> start_func, 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 : m_scheduler(scheduler)
79 , m_node_graph_manager(*MayaFlux::get_node_graph_manager())
80 , m_timer(scheduler)
81{
82}
83
85 : m_scheduler(scheduler)
86 , m_node_graph_manager(graph_manager)
87 , m_timer(scheduler)
88{
89}
90
91void NodeTimer::play_for(std::shared_ptr<Nodes::Node> node, double duration_seconds, uint32_t channel)
92{
93 play_for(node, duration_seconds, std::vector<uint32_t> { channel });
94}
95
96void NodeTimer::play_for(std::shared_ptr<Nodes::Node> node, double duration_seconds, std::vector<uint32_t> channels)
97{
98 cancel();
99
100 m_current_node = node;
101 m_channels = channels;
102
103 for (auto& channel : channels) {
104 m_node_graph_manager.add_to_root(node, token, channel);
105 }
106
107 m_timer.schedule(duration_seconds, [this]() {
109 });
110}
111
112void NodeTimer::play_for(std::shared_ptr<Nodes::Node> node, double duration_seconds)
113{
114 auto source_mask = node->get_channel_mask().load(std::memory_order_relaxed);
115 std::vector<uint32_t> channels;
116
117 if (source_mask == 0) {
118 channels = { 0 };
119 } else {
120 for (auto& channel : Nodes::get_active_channels(source_mask, 0)) {
121 channels.push_back(channel);
122 }
123 }
124
125 play_for(node, duration_seconds, channels);
126}
127
128void NodeTimer::play_with_processing(std::shared_ptr<Nodes::Node> node, std::function<void(std::shared_ptr<Nodes::Node>)> setup_func, std::function<void(std::shared_ptr<Nodes::Node>)> cleanup_func, double duration_seconds, uint32_t channel)
129{
130 play_with_processing(node, setup_func, cleanup_func, duration_seconds, std::vector<uint32_t> { channel });
131}
132
133void NodeTimer::play_with_processing(std::shared_ptr<Nodes::Node> node, std::function<void(std::shared_ptr<Nodes::Node>)> setup_func, std::function<void(std::shared_ptr<Nodes::Node>)> cleanup_func, double duration_seconds, std::vector<uint32_t> channels)
134{
135 cancel();
136
137 m_current_node = node;
138 m_channels = channels;
139
140 setup_func(node);
141
142 for (auto channel : channels) {
143 m_node_graph_manager.add_to_root(node, token, channel);
144 }
145
146 m_timer.schedule(duration_seconds, [this, node, cleanup_func]() {
147 cleanup_func(node);
149 });
150}
151
152void NodeTimer::play_with_processing(std::shared_ptr<Nodes::Node> node,
153 std::function<void(std::shared_ptr<Nodes::Node>)> setup_func,
154 std::function<void(std::shared_ptr<Nodes::Node>)> cleanup_func,
155 double duration_seconds)
156{
157 auto source_mask = node->get_channel_mask().load(std::memory_order_relaxed);
158 std::vector<uint32_t> channels;
159
160 if (source_mask == 0) {
161 channels = { 0 };
162 } else {
163 for (auto& channel : Nodes::get_active_channels(source_mask, 0)) {
164 channels.push_back(channel);
165 }
166 }
167
168 play_with_processing(node, setup_func, cleanup_func, duration_seconds, channels);
169}
170
172{
173 if (m_current_node) {
174 for (auto channel : m_channels) {
175 if (m_current_node->is_used_by_channel(channel)) {
177 }
178 }
179 m_current_node = nullptr;
180 m_channels.clear();
181 }
182}
183
185{
186 if (m_timer.is_active()) {
188 }
189 m_timer.cancel();
190}
191
192}
static MayaFlux::Nodes::ProcessingToken token
Definition Timers.cpp:8
std::shared_ptr< Nodes::Node > m_current_node
The currently active node being played.
Definition Timers.hpp:366
NodeTimer(Vruta::TaskScheduler &scheduler)
Constructs a NodeTimer with the specified scheduler.
Definition Timers.cpp:77
Nodes::NodeGraphManager & m_node_graph_manager
Reference to the graph manager that manages processing nodes.
Definition Timers.hpp:349
void play_with_processing(std::shared_ptr< Nodes::Node > node, std::function< void(std::shared_ptr< Nodes::Node >)> setup_func, std::function< void(std::shared_ptr< Nodes::Node >)> cleanup_func, double duration_seconds, std::vector< uint32_t > channels)
Activates a processing node with custom setup and cleanup functions.
Definition Timers.cpp:133
void cleanup_current_operation()
Cleans up the current operation, disconnecting the node and resetting state.
Definition Timers.cpp:171
void play_for(std::shared_ptr< Nodes::Node > node, double duration_seconds, std::vector< uint32_t > channels)
Activates a processing node for a specific duration.
Definition Timers.cpp:96
void cancel()
Cancels any currently active node.
Definition Timers.cpp:184
std::vector< uint32_t > m_channels
The output channels the current node is connected to.
Definition Timers.hpp:375
Timer m_timer
The timer used to schedule node disconnection.
Definition Timers.hpp:357
void execute(std::function< void()> start_func, std::function< void()> end_func, double duration_seconds)
Executes a pair of functions with a time interval between them.
Definition Timers.cpp:60
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:202
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:106
bool is_active() const
Checks if a callback is currently scheduled.
Definition Timers.hpp:80
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:114
Vruta::TaskScheduler & m_Scheduler
Reference to the scheduler that manages this timer.
Definition Timers.hpp:89
std::shared_ptr< Vruta::SoundRoutine > m_routine
The underlying computational routine that implements the timer.
Definition Timers.hpp:98
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:38
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:52
A C++20 coroutine-based audio processing task with sample-accurate timing.
Definition Routine.hpp:309
bool cancel_task(std::shared_ptr< Routine > task)
Cancels and removes a task from the scheduler.
Definition Scheduler.cpp:60
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.
uint64_t current_units(ProcessingToken token=ProcessingToken::SAMPLE_ACCURATE) const
Get current processing units for a domain.
Token-based multimodal task scheduling system for unified coroutine processing.
Definition Scheduler.hpp:51
ProcessingToken
Enumerates the different processing domains for nodes.
@ AUDIO_RATE
Nodes that process at the audio sample rate.
std::vector< uint32_t > get_active_channels(const std::shared_ptr< Nodes::Node > &node, uint32_t fallback_channel)
Extracts active channel list from a node's channel mask.
std::shared_ptr< Nodes::NodeGraphManager > get_node_graph_manager()
Gets the node graph manager from the default engine.
Definition Graph.cpp:18
Main namespace for the Maya Flux audio engine.
Definition LiveAid.hpp:6
Templated awaitable for accessing a coroutine's promise object.
Definition Awaiters.hpp:188
Awaitable object for precise sample-accurate timing delays.
Definition Awaiters.hpp:35