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
11#include <juce_audio_basics/juce_audio_basics.h>
12
13namespace zrythm::dsp
14{
15
26class DspGraphDispatcher final
27{
28public:
29 using RunFunctionWithEngineLock = std::function<void (std::function<void ()>)>;
30
31 DspGraphDispatcher (
32 std::unique_ptr<graph::IGraphBuilder> graph_builder,
33 std::vector<graph::IProcessable *> terminal_processables,
34 const IHardwareAudioInterface &hw_interface,
35 RunFunctionWithEngineLock run_function_with_engine_lock,
37 std::optional<juce::AudioWorkgroup> workgroup = std::nullopt);
38
44 void recalc_graph (bool soft);
45
55 const dsp::ITransport &current_transport_state,
57 units::sample_u64_t remaining_latency_preroll,
58 bool realtime_context,
59 const dsp::TempoMap &tempo_map) noexcept [[clang::nonblocking]];
60
65 units::sample_u32_t get_max_route_playback_latency ();
66
71 [[nodiscard, gnu::hot]] bool is_processing_kickoff_thread () const
72 {
73 return process_kickoff_thread_.has_value ()
74 ? current_thread_id.get () == process_kickoff_thread_.value ()
75 : false;
76 }
77
81 [[nodiscard, gnu::hot]] bool is_processing_thread () const
82 {
83 /* this is called too often so use this optimization */
84 static thread_local bool have_result = false;
85 static thread_local bool is_processing_thread = false;
86
87 if (have_result) [[likely]]
88 {
90 }
91
92 if (!scheduler_) [[unlikely]]
93 {
94 have_result = false;
96 return false;
97 }
98
99 if (scheduler_->contains_thread (current_thread_id.get ()))
100 {
102 have_result = true;
103 return true;
104 }
105
106 have_result = true;
107 is_processing_thread = false;
108 return false;
109 }
110
118 {
119 return scheduler_->get_nodes ().trigger_nodes_;
120 }
121
122private:
138 void preprocess_at_start_of_cycle (
139 const dsp::graph::EngineProcessTimeInfo &time_nfo) noexcept
140 [[clang::nonblocking]];
141
142private:
143 std::unique_ptr<graph::IGraphBuilder> graph_builder_;
144 const IHardwareAudioInterface &hw_interface_;
145 std::optional<juce::AudioWorkgroup> workgroup_;
146
151 RunFunctionWithEngineLock run_function_with_engine_lock_;
152
156 std::vector<graph::IProcessable *> terminal_processables_;
157
158 std::unique_ptr<graph::GraphScheduler> scheduler_;
159
161 units::sample_u32_t max_route_playback_latency_;
162
171 units::sample_u32_t global_offset_;
172
174 std::optional<unsigned int> process_kickoff_thread_;
175
177};
178
179}
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.
units::sample_u32_t get_max_route_playback_latency()
Returns the max playback latency of the trigger nodes.
void recalc_graph(bool soft)
Recalculates the process acyclic directed graph.
void start_cycle(const dsp::ITransport &current_transport_state, dsp::graph::EngineProcessTimeInfo time_nfo, units::sample_u64_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:16
std::function< void(std::function< void()>)> RunOnMainThreadFunc
Request for a function to run on the main thread (blocking).
Common struct to pass around during processing to avoid repeating the data in function arguments.
Definition graph_node.h:51