MayaFlux 0.1.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 18 of file Timers.cpp.

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}
static MayaFlux::Nodes::ProcessingToken token
Definition Timers.cpp:8
bool m_active
Flag indicating whether a callback is currently scheduled.
Definition Timers.hpp:106
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 cancel()
Cancels any scheduled callback.
Definition Timers.cpp:45
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.
GetPromiseBase< Vruta::audio_promise > GetAudioPromise
Audio domain promise accessor.
Definition Awaiters.hpp:231

References MayaFlux::Vruta::TaskScheduler::add_task(), cancel(), MayaFlux::Vruta::TaskScheduler::current_units(), m_active, m_callback, m_routine, m_Scheduler, MayaFlux::Vruta::TaskScheduler::seconds_to_samples(), and token.

Referenced by MayaFlux::Kriya::TimedAction::execute(), MayaFlux::Kriya::NodeTimer::play_for(), and MayaFlux::Kriya::NodeTimer::play_with_processing().

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