MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
CycleCoordinator.cpp
Go to the documentation of this file.
2
3#include "BufferPipeline.hpp"
4
6
8
9namespace MayaFlux::Kriya {
10
12 : m_scheduler(scheduler)
13{
14}
15
17 std::vector<std::reference_wrapper<BufferPipeline>> pipelines,
18 uint32_t sync_every_n_cycles,
19 uint64_t samples_per_cycle)
20{
21
22 auto& promise = co_await GetAudioPromise {};
23 uint32_t cycle = 0;
24
25 while (true) {
26 if (promise.should_terminate) {
27 break;
28 }
29
30 if (cycle % sync_every_n_cycles == 0) {
31 for (auto& pipeline_ref : pipelines) {
32 auto& pipeline = pipeline_ref.get();
33 if (pipeline.has_pending_data()) {
34 MF_WARN(Journal::Component::Kriya, Journal::Context::CoroutineScheduling, "Sync point: Pipeline has stale data at cycle {}", cycle);
35 }
36 }
37 }
38
39 for (auto& pipeline_ref : pipelines) {
40 pipeline_ref.get().execute_once();
41 }
42
43 ++cycle;
44 co_await SampleDelay { samples_per_cycle };
45 }
46}
47
48std::shared_ptr<Vruta::SoundRoutine> CycleCoordinator::sync_pipelines_at_rate(
49 std::vector<std::reference_wrapper<BufferPipeline>> pipelines,
50 uint32_t sync_every_n_cycles,
51 double seconds_per_cycle)
52{
53 uint64_t samples_per_cycle = m_scheduler.seconds_to_samples(seconds_per_cycle);
54 auto routine = sync_pipelines(pipelines, sync_every_n_cycles, samples_per_cycle);
55 return std::make_shared<Vruta::SoundRoutine>(std::move(routine));
56}
57
59 std::shared_ptr<Buffers::AudioBuffer> buffer,
60 std::function<void(uint32_t)> on_data_ready,
61 std::function<void(uint32_t)> on_data_expired)
62{
63
64 auto& promise = co_await GetAudioPromise {};
65 uint32_t cycle = 0;
66
67 while (true) {
68 if (promise.should_terminate) {
69 break;
70 }
71
72 if (buffer->has_data_for_cycle()) {
73 on_data_ready(cycle);
74 co_await SampleDelay { 1 };
75
76 if (buffer->has_data_for_cycle()) {
77 on_data_expired(cycle + 1);
78 }
79 }
80
81 cycle++;
82 co_await SampleDelay { 1 };
83 }
84}
85}
#define MF_WARN(comp, ctx,...)
Vruta::SoundRoutine sync_pipelines(std::vector< std::reference_wrapper< BufferPipeline > > pipelines, uint32_t sync_every_n_cycles=1, uint64_t samples_per_cycle=1)
Create a synchronization routine for multiple pipelines.
Vruta::SoundRoutine manage_transient_data(std::shared_ptr< Buffers::AudioBuffer > buffer, std::function< void(uint32_t)> on_data_ready, std::function< void(uint32_t)> on_data_expired)
Create a transient data management routine.
CycleCoordinator(Vruta::TaskScheduler &scheduler)
Construct coordinator with task scheduler integration.
std::shared_ptr< Vruta::SoundRoutine > sync_pipelines_at_rate(std::vector< std::reference_wrapper< BufferPipeline > > pipelines, uint32_t sync_every_n_cycles, double seconds_per_cycle)
Create a synchronization routine based on real-time rate.
A C++20 coroutine-based audio processing task with sample-accurate timing.
Definition Routine.hpp:316
uint64_t seconds_to_samples(double seconds) const
Converts a time in seconds to a number of samples.
Token-based multimodal task scheduling system for unified coroutine processing.
Definition Scheduler.hpp:51
@ CoroutineScheduling
Coroutine scheduling and temporal coordination (Vruta::TaskScheduler)
@ Kriya
Automatable tasks and fluent scheduling api for Nodes and Buffers.
Templated awaitable for accessing a coroutine's promise object.
Awaitable object for precise sample-accurate timing delays.