Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
graph_dispatcher.h
1// SPDX-FileCopyrightText: © 2019-2021, 2024-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "dsp/graph_builder.h"
7#include "dsp/graph_scheduler.h"
8#include "dsp/hardware_audio_interface.h"
9#include "utils/rt_thread_id.h"
10#include "utils/types.h"
11
12namespace zrythm::dsp
13{
14
25class DspGraphDispatcher final
26{
27public:
28 using RunFunctionWithEngineLock = std::function<void (std::function<void ()>)>;
29
30 DspGraphDispatcher (
31 std::unique_ptr<graph::IGraphBuilder> graph_builder,
32 std::vector<graph::IProcessable *> terminal_processables,
33 const IHardwareAudioInterface &hw_interface,
34 RunFunctionWithEngineLock run_function_with_engine_lock,
36
42 void recalc_graph (bool soft);
43
53 const dsp::ITransport &current_transport_state,
54 EngineProcessTimeInfo time_nfo,
55 nframes_t remaining_latency_preroll,
56 bool realtime_context,
57 const dsp::TempoMap &tempo_map) noexcept [[clang::nonblocking]];
58
64
69 [[nodiscard, gnu::hot]] bool is_processing_kickoff_thread () const
70 {
71 return process_kickoff_thread_.has_value ()
72 ? current_thread_id.get () == process_kickoff_thread_.value ()
73 : false;
74 }
75
79 [[nodiscard, gnu::hot]] bool is_processing_thread () const
80 {
81 /* this is called too often so use this optimization */
82 static thread_local bool have_result = false;
83 static thread_local bool is_processing_thread = false;
84
85 if (have_result) [[likely]]
86 {
88 }
89
90 if (!scheduler_) [[unlikely]]
91 {
92 have_result = false;
94 return false;
95 }
96
97 if (scheduler_->contains_thread (current_thread_id.get ()))
98 {
100 have_result = true;
101 return true;
102 }
103
104 have_result = true;
105 is_processing_thread = false;
106 return false;
107 }
108
116 {
117 return scheduler_->get_nodes ().trigger_nodes_;
118 }
119
120private:
136 void
137 preprocess_at_start_of_cycle (const EngineProcessTimeInfo &time_nfo) noexcept
138 [[clang::nonblocking]];
139
140private:
141 std::unique_ptr<graph::IGraphBuilder> graph_builder_;
142 const IHardwareAudioInterface &hw_interface_;
143
148 RunFunctionWithEngineLock run_function_with_engine_lock_;
149
153 std::vector<graph::IProcessable *> terminal_processables_;
154
155 std::unique_ptr<graph::GraphScheduler> scheduler_;
156
158 nframes_t max_route_playback_latency_ = 0;
159
168 nframes_t global_offset_ = 0;
169
171 std::optional<unsigned int> process_kickoff_thread_;
172
174};
175
176}
auto & current_trigger_nodes() const
Accessor for currently active trigger nodes.
bool is_processing_kickoff_thread() const
Returns whether this is the thread that kicks off processing (thread that calls router_start_cycle())...
bool is_processing_thread() const
Returns if the current thread is a processing thread.
void recalc_graph(bool soft)
Recalculates the process acyclic directed graph.
nframes_t get_max_route_playback_latency()
Returns the max playback latency of the trigger nodes.
void start_cycle(const dsp::ITransport &current_transport_state, EngineProcessTimeInfo time_nfo, nframes_t remaining_latency_preroll, bool realtime_context, const dsp::TempoMap &tempo_map) noexcept
Starts a new cycle.
Abstraction for hardware audio interface.
Interface for transport.
Definition itransport.h:17
std::function< void(std::function< void()>)> RunOnMainThreadFunc
Request for a function to run on the main thread (blocking).
uint32_t nframes_t
Frame count.
Definition types.h:58
Common struct to pass around during processing to avoid repeating the data in function arguments.
Definition types.h:133