MayaFlux 1.0.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 if (units < 1) {
18 MF_RT_WARN(Journal::Component::Kriya, Journal::Context::CoroutineScheduling,
19 "metro interval {}s is shorter than one frame, clamping to 1 frame", interval);
20 }
21 units = std::max<uint64_t>(1, units);
22 auto& p = co_await GetGraphicsPromise {};
23 while (!p.should_terminate) {
24 cb();
25 co_await FrameDelay { .frames_to_wait = units };
26 }
27 };
28 return std::make_shared<Vruta::GraphicsRoutine>(coro(interval_seconds, std::move(callback)));
29 }
30 auto coro = [](double interval, std::function<void()> cb) -> Vruta::SoundRoutine {
31 uint64_t units = Vruta::seconds_to_samples(interval);
32 if (units < 1) {
33 MF_RT_WARN(Journal::Component::Kriya, Journal::Context::CoroutineScheduling,
34 "metro interval {}s is shorter than one sample, clamping to 1 sample", interval);
35 }
36 units = std::max<uint64_t>(1, units);
37 auto& p = co_await GetAudioPromise {};
38 while (!p.should_terminate) {
39 cb();
40 co_await SampleDelay { units };
41 }
42 };
43 return std::make_shared<Vruta::SoundRoutine>(coro(interval_seconds, std::move(callback)));
44}
#define MF_RT_WARN(comp, ctx,...)
PickCallback callback
Definition Picker.cpp:22
A C++20 coroutine-based graphics processing task with frame-accurate timing.
Definition Routine.hpp:512
GetPromiseBase< Vruta::audio_promise > GetAudioPromise
Audio domain promise accessor.

References callback, MayaFlux::Journal::CoroutineScheduling, MayaFlux::Vruta::FRAME_ACCURATE, MayaFlux::Kriya::FrameDelay::frames_to_wait, MayaFlux::Journal::Kriya, MF_RT_WARN, 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: