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

◆ metro()

MAYAFLUX_API std::shared_ptr< Vruta::Routine > MayaFlux::Kriya::metro ( double  interval_seconds,
std::function< void()>  callback,
Vruta::ProcessingToken  token = Vruta::ProcessingToken::SAMPLE_ACCURATE 
)

Creates a periodic event generator that executes a callback at regular intervals.

Parameters
interval_secondsTime between callback executions in seconds
callbackFunction to execute on each interval
tokenProcessing token to determine which scheduler rate to use (default: SAMPLE_ACCURATE)
Returns
A Routine shared_ptr of type determined by the processing token of the scheduler (SoundRoutine, GraphicsRoutine, etc.)

The metro task provides a fundamental temporal mechanism for creating time-based structures in computational systems. It executes the provided callback function at precise, regular intervals with sample-accurate timing, enabling the creation of rhythmic patterns, event sequences, and temporal frameworks that can synchronize across different domains.

Unlike system timers which can drift due to processing load, this implementation maintains precise timing by synchronizing with the sample-rate clock, making it suitable for both audio and cross-domain applications where timing accuracy is critical.

Example usage:

// Create a periodic event generator (2Hz)
auto periodic_task = Kriya::metro(0.5, []() {
trigger_event(); // Could affect audio, visuals, data, etc.
});
scheduler->add_task(periodic_task);
std::shared_ptr< Vruta::Routine > metro(double interval_seconds, std::function< void()> callback, Vruta::ProcessingToken token)
Creates a periodic event generator that executes a callback at regular intervals.
Definition Tasks.cpp:12

The metro task continues indefinitely until explicitly cancelled, creating a persistent temporal structure within the computational system.

Definition at line 12 of file Tasks.cpp.

13{
14 if (token == Vruta::ProcessingToken::FRAME_ACCURATE) {
15 auto coro = [](double interval, std::function<void()> cb) -> Vruta::GraphicsRoutine {
16 uint64_t units = Vruta::seconds_to_frames(interval);
17 auto& p = co_await GetGraphicsPromise {};
18 while (!p.should_terminate) {
19 cb();
20 co_await FrameDelay { .frames_to_wait = units };
21 }
22 };
23 return std::make_shared<Vruta::GraphicsRoutine>(coro(interval_seconds, std::move(callback)));
24 }
25 auto coro = [](double interval, std::function<void()> cb) -> Vruta::SoundRoutine {
26 uint64_t units = Vruta::seconds_to_samples(interval);
27 auto& p = co_await GetAudioPromise {};
28 while (!p.should_terminate) {
29 cb();
30 co_await SampleDelay { units };
31 }
32 };
33 return std::make_shared<Vruta::SoundRoutine>(coro(interval_seconds, std::move(callback)));
34}
A C++20 coroutine-based graphics processing task with frame-accurate timing.
Definition Routine.hpp:496
GetPromiseBase< Vruta::audio_promise > GetAudioPromise
Audio domain promise accessor.
Templated awaitable for accessing a coroutine's promise object.

References MayaFlux::Vruta::FRAME_ACCURATE, MayaFlux::Kriya::FrameDelay::frames_to_wait, MayaFlux::Vruta::seconds_to_frames(), and MayaFlux::Vruta::seconds_to_samples().

Referenced by MayaFlux::Nexus::Wiring::finalise(), and MayaFlux::schedule_metro().

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