Zrythm v2.0.0-alpha.1
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 using TerminalProcessablesProvider =
32 std::function<std::span<graph::IProcessable *> ()>;
33
34 DspGraphDispatcher (
35 std::unique_ptr<graph::IGraphBuilder> graph_builder,
36 TerminalProcessablesProvider terminal_processables_provider,
37 const IHardwareAudioInterface &hw_interface,
38 RunFunctionWithEngineLock run_function_with_engine_lock,
40 std::optional<juce::AudioWorkgroup> workgroup = std::nullopt);
41
47 void recalc_graph (bool soft);
48
54 void clear_graph ();
55
65 const dsp::ITransport &current_transport_state,
67 units::sample_u64_t remaining_latency_preroll,
68 bool realtime_context,
69 const dsp::TempoMap &tempo_map) noexcept [[clang::nonblocking]];
70
75 units::sample_u32_t get_max_route_playback_latency ();
76
81 [[nodiscard, gnu::hot]] bool is_processing_kickoff_thread () const
82 {
83 return process_kickoff_thread_.has_value ()
84 ? current_thread_id.get () == process_kickoff_thread_.value ()
85 : false;
86 }
87
91 [[nodiscard, gnu::hot]] bool is_processing_thread () const
92 {
93 /* this is called too often so use this optimization */
94 static thread_local bool have_result = false;
95 static thread_local bool is_processing_thread = false;
96
97 if (have_result) [[likely]]
98 {
100 }
101
102 if (!scheduler_) [[unlikely]]
103 {
104 have_result = false;
105 is_processing_thread = false;
106 return false;
107 }
108
109 if (scheduler_->contains_thread (current_thread_id.get ()))
110 {
112 have_result = true;
113 return true;
114 }
115
116 have_result = true;
117 is_processing_thread = false;
118 return false;
119 }
120
128 {
129 return scheduler_->get_nodes ().trigger_nodes_;
130 }
131
132private:
148 void preprocess_at_start_of_cycle (
149 const dsp::graph::ProcessBlockInfo &time_nfo) noexcept
150 [[clang::nonblocking]];
151
152private:
153 std::unique_ptr<graph::IGraphBuilder> graph_builder_;
154 const IHardwareAudioInterface &hw_interface_;
155 std::optional<juce::AudioWorkgroup> workgroup_;
156
161 RunFunctionWithEngineLock run_function_with_engine_lock_;
162
166 TerminalProcessablesProvider terminal_processables_provider_;
167
168 std::unique_ptr<graph::GraphScheduler> scheduler_;
169
171 units::sample_u32_t max_route_playback_latency_;
172
181 units::sample_u32_t global_offset_;
182
184 std::optional<unsigned int> process_kickoff_thread_;
185
187};
188
189}
void start_cycle(const dsp::ITransport &current_transport_state, dsp::graph::ProcessBlockInfo time_nfo, units::sample_u64_t remaining_latency_preroll, bool realtime_context, const dsp::TempoMap &tempo_map) noexcept
Starts a new cycle.
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 clear_graph()
Releases resources held by the current graph nodes and clears the graph.
Abstraction for hardware audio interface.
Interface for transport.
Definition itransport.h:47
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