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 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
51 void clear_graph ();
52
62 const dsp::ITransport &current_transport_state,
64 units::sample_u64_t remaining_latency_preroll,
65 bool realtime_context,
66 const dsp::TempoMap &tempo_map) noexcept [[clang::nonblocking]];
67
72 units::sample_u32_t get_max_route_playback_latency ();
73
78 [[nodiscard, gnu::hot]] bool is_processing_kickoff_thread () const
79 {
80 return process_kickoff_thread_.has_value ()
81 ? current_thread_id.get () == process_kickoff_thread_.value ()
82 : false;
83 }
84
88 [[nodiscard, gnu::hot]] bool is_processing_thread () const
89 {
90 /* this is called too often so use this optimization */
91 static thread_local bool have_result = false;
92 static thread_local bool is_processing_thread = false;
93
94 if (have_result) [[likely]]
95 {
97 }
98
99 if (!scheduler_) [[unlikely]]
100 {
101 have_result = false;
102 is_processing_thread = false;
103 return false;
104 }
105
106 if (scheduler_->contains_thread (current_thread_id.get ()))
107 {
109 have_result = true;
110 return true;
111 }
112
113 have_result = true;
114 is_processing_thread = false;
115 return false;
116 }
117
125 {
126 return scheduler_->get_nodes ().trigger_nodes_;
127 }
128
129private:
145 void preprocess_at_start_of_cycle (
146 const dsp::graph::ProcessBlockInfo &time_nfo) noexcept
147 [[clang::nonblocking]];
148
149private:
150 std::unique_ptr<graph::IGraphBuilder> graph_builder_;
151 const IHardwareAudioInterface &hw_interface_;
152 std::optional<juce::AudioWorkgroup> workgroup_;
153
158 RunFunctionWithEngineLock run_function_with_engine_lock_;
159
163 std::vector<graph::IProcessable *> terminal_processables_;
164
165 std::unique_ptr<graph::GraphScheduler> scheduler_;
166
168 units::sample_u32_t max_route_playback_latency_;
169
178 units::sample_u32_t global_offset_;
179
181 std::optional<unsigned int> process_kickoff_thread_;
182
184};
185
186}
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: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