MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ schedule()

void MayaFlux::Kriya::Timer::schedule ( double  delay_seconds,
std::function< void()>  callback 
)

Schedules a callback to execute after a delay.

Parameters
delay_secondsTime to wait before executing the callback (in seconds)
callbackFunction to execute after the delay

This method schedules the provided callback to execute after exactly the specified delay. If a callback is already scheduled, it is cancelled and replaced with the new one.

The timing is sample-accurate, ensuring that the callback executes at precisely the right moment in the processing timeline.

Definition at line 19 of file Timers.cpp.

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}
bool m_active
Flag indicating whether a callback is currently scheduled.
Definition Timers.hpp:116
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
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
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.
GetPromiseBase< Vruta::graphics_promise > GetGraphicsPromise
Graphics domain promise accessor.
GetPromiseBase< Vruta::audio_promise > GetAudioPromise
Audio domain promise accessor.
@ FRAME_ACCURATE
Coroutine is frame-accurate.

References MayaFlux::Vruta::TaskScheduler::add_task(), cancel(), MayaFlux::Vruta::TaskScheduler::current_units(), MayaFlux::Vruta::FRAME_ACCURATE, MayaFlux::Kriya::FrameDelay::frames_to_wait, m_active, m_callback, m_routine, m_Scheduler, m_token, MayaFlux::Vruta::TaskScheduler::seconds_to_samples(), and MayaFlux::Vruta::TaskScheduler::seconds_to_units().

Referenced by MayaFlux::Kriya::TemporalActivation::activate_buffer(), MayaFlux::Kriya::TemporalActivation::activate_network(), MayaFlux::Kriya::TemporalActivation::activate_node(), and MayaFlux::Kriya::TimedAction::execute().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: