MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
DelayAwaiters.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "GetPromise.hpp"
4
5namespace MayaFlux::Kriya {
6
7/**
8 * @struct SampleDelay
9 * @brief Awaitable object for precise sample-accurate timing delays
10 *
11 * SampleDelay is the primary timing mechanism in the computational coroutine system.
12 * When a coroutine co_awaits a SampleDelay, it suspends execution until
13 * exactly the specified number of discrete time units (samples) have been processed.
14 *
15 * This provides deterministic timing for computational events, which is essential
16 * for applications where precise temporal relationships are critical. Unlike
17 * system-time-based delays which can drift due to processing load, SampleDelay guarantees
18 * that operations occur at exact positions in the discrete time continuum.
19 *
20 * Example usage:
21 * ```cpp
22 * // Wait for exactly 4410 time units (100ms at 44.1kHz sample rate)
23 * co_await SampleDelay{4410};
24 * ```
25 *
26 * SampleDelay is the foundation for all timing in the computational engine, enabling
27 * precise sequencing, modulation, and synchronization of events across multiple domains
28 * (signal processing, visual rendering, data transformation, physical modeling, etc.).
29 */
30struct MAYAFLUX_API SampleDelay {
31 /**
32 * @brief Type alias for the coroutine promise type
33 *
34 * This alias simplifies references to the Vruta::promise_type
35 * throughout the Kriya namespace.
36 */
38
39 /**
40 * @brief Number of time units to wait before resuming the coroutine
41 *
42 * This value is added to the coroutine's next_sample field when
43 * await_suspend is called, scheduling the coroutine to resume
44 * after exactly this many discrete time units have been processed.
45 */
47
48 SampleDelay(uint64_t samples)
49 : samples_to_wait(samples)
50 {
51 }
52
53 /**
54 * @brief Checks if the delay should be bypassed
55 * @return True if no delay is needed, false otherwise
56 *
57 * If samples_to_wait is 0, the coroutine continues execution
58 * without suspending. This optimization avoids the overhead
59 * of suspension for zero-length delays.
60 */
61 [[nodiscard]] inline bool await_ready() const { return samples_to_wait == 0; }
62
63 /**
64 * @brief Called when the coroutine is resumed after the delay
65 *
66 * This method is called when the scheduler resumes the coroutine
67 * after the specified delay. It doesn't need to do anything since
68 * the delay itself is the only effect needed.
69 */
70 inline void await_resume() { };
71
72 /**
73 * @brief Schedules the coroutine to resume after the delay
74 * @param h Handle to the suspended coroutine
75 *
76 * This method is called when the coroutine suspends. It updates
77 * the coroutine's next_sample field to schedule it for resumption
78 * after the specified number of time units have been processed.
79 */
80 void await_suspend(std::coroutine_handle<promise_handle> h) noexcept;
81};
82
83/**
84 * @struct BufferDelay
85 * @brief Awaiter for suspending until a buffer cycle boundary
86 *
87 * Works identically to SampleDelay but at buffer cycle granularity.
88 * Accumulates cycles in promise.next_buffer_cycle.
89 *
90 * **Usage:**
91 * ```cpp
92 * auto routine = []() -> SoundRoutine {
93 * while (true) {
94 * process_buffer();
95 * co_await BufferDelay{2}; // Resume every 2 buffer cycles
96 * }
97 * };
98 * ```
99 */
100struct MAYAFLUX_API BufferDelay {
101 /**
102 * @brief Type alias for the coroutine promise type
103 *
104 * This alias simplifies references to the Vruta::promise_type
105 * throughout the Kriya namespace.
106 */
108
109 BufferDelay(uint64_t cycles)
110 : num_cycles(cycles)
111 {
112 }
113
114 uint64_t num_cycles;
115
116 [[nodiscard]] constexpr bool await_ready() const noexcept { return num_cycles == 0; }
117
118 void await_suspend(std::coroutine_handle<promise_handle> h) noexcept
119 {
120 auto& promise = h.promise();
121 promise.next_buffer_cycle += num_cycles;
122 promise.delay_amount = num_cycles;
123 promise.active_delay_context = Vruta::DelayContext::BUFFER_BASED;
124 }
125
126 constexpr void await_resume() const noexcept { }
127};
128
129/**
130 * @struct FrameDelay
131 * @brief graphics-domain awaiter for frame-accurate timing delays
132 *
133 * Future awaiter for visual processing routines that operate at frame rates.
134 * This will work with visual_promise types that have next_frame fields.
135 */
136struct MAYAFLUX_API FrameDelay {
137 /**
138 * @brief Type alias for the coroutine promise type
139 *
140 * This alias simplifies references to the Vruta::promise_type
141 * throughout the Kriya namespace.
142 */
144
146
147 [[nodiscard]] constexpr bool await_ready() const noexcept
148 {
149 return frames_to_wait == 0;
150 }
151
152 constexpr void await_resume() const noexcept { }
153
154 void await_suspend(std::coroutine_handle<Vruta::graphics_promise> h) noexcept;
155};
156
157struct MAYAFLUX_API MultiRateDelay {
158
161
162 [[nodiscard]] constexpr bool await_ready() const noexcept
163 {
164 return samples_to_wait == 0 && frames_to_wait == 0;
165 }
166
167 constexpr void await_resume() const noexcept { }
168
169 void await_suspend(std::coroutine_handle<Vruta::complex_promise> h) noexcept;
170};
171
172} // namespace MayaFlux::Kriya
constexpr void await_resume() const noexcept
void await_suspend(std::coroutine_handle< promise_handle > h) noexcept
constexpr bool await_ready() const noexcept
Awaiter for suspending until a buffer cycle boundary.
constexpr bool await_ready() const noexcept
constexpr void await_resume() const noexcept
graphics-domain awaiter for frame-accurate timing delays
constexpr void await_resume() const noexcept
constexpr bool await_ready() const noexcept
uint64_t samples_to_wait
Number of time units to wait before resuming the coroutine.
void await_resume()
Called when the coroutine is resumed after the delay.
bool await_ready() const
Checks if the delay should be bypassed.
Awaitable object for precise sample-accurate timing delays.
Coroutine promise type for audio processing tasks with sample-accurate timing.
Definition Promise.hpp:196
Coroutine promise type for graphics processing tasks with frame-accurate timing.
Definition Promise.hpp:259