Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
router.h
1// SPDX-FileCopyrightText: © 2019-2021, 2024 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3/*
4 * This file incorporates work covered by the following copyright and
5 * permission notice:
6 *
7 * ---
8 *
9 * Copyright (C) 2017, 2019 Robin Gareus <robin@gareus.org>
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 *
24 * ---
25 */
26
27#pragma once
28
29#include "zrythm-config.h"
30
31#include "dsp/graph_scheduler.h"
32#include "engine/device_io/engine.h"
33#include "utils/rt_thread_id.h"
34#include "utils/types.h"
35
36#define ROUTER (AUDIO_ENGINE->router_)
37
38namespace zrythm::engine::session
39{
40
49class Router final
50{
51public:
52 Router (device_io::AudioEngine * engine = nullptr);
53
59 void recalc_graph (bool soft);
60
65
71
76 [[nodiscard, gnu::hot]] bool is_processing_kickoff_thread () const
77 {
78 return current_thread_id.get () == process_kickoff_thread_;
79 }
80
84 [[nodiscard, gnu::hot]] bool is_processing_thread () const
85 {
86 /* this is called too often so use this optimization */
87 static thread_local bool have_result = false;
88 static thread_local bool is_processing_thread = false;
89
90 if (have_result) [[likely]]
91 {
93 }
94
95 if (!scheduler_) [[unlikely]]
96 {
97 have_result = false;
99 return false;
100 }
101
102 if (scheduler_->contains_thread (current_thread_id.get ()))
103 {
105 have_result = true;
106 return true;
107 }
108
109 have_result = true;
110 is_processing_thread = false;
111 return false;
112 }
113
114public:
115 std::unique_ptr<dsp::graph::GraphScheduler> scheduler_;
116
119 std::atomic<bool> graph_setup_in_progress_ = false;
120
123
126
131
133 std::binary_semaphore graph_access_sem_{ 1 };
134
135 bool callback_in_progress_ = false;
136
138 unsigned int process_kickoff_thread_ = 0;
139
140 device_io::AudioEngine * audio_engine_{};
141};
142
143}
unsigned int process_kickoff_thread_
ID of the thread that calls kicks off the cycle.
Definition router.h:138
nframes_t global_offset_
Current global latency offset (max latency of all routes - remaining latency from engine).
Definition router.h:130
nframes_t get_max_route_playback_latency()
Returns the max playback latency of the trigger nodes.
nframes_t max_route_playback_latency_
Stored for the currently processing cycle.
Definition router.h:125
void recalc_graph(bool soft)
Recalculates the process acyclic directed graph.
bool is_processing_thread() const
Returns if the current thread is a processing thread.
Definition router.h:84
std::binary_semaphore graph_access_sem_
Used when recalculating the graph.
Definition router.h:133
void start_cycle(EngineProcessTimeInfo time_nfo)
Starts a new cycle.
bool is_processing_kickoff_thread() const
Returns whether this is the thread that kicks off processing (thread that calls router_start_cycle())...
Definition router.h:76
EngineProcessTimeInfo time_nfo_
Time info for this processing cycle.
Definition router.h:122
std::atomic< bool > graph_setup_in_progress_
An atomic variable to check if the graph is currently being setup (so that we can avoid accessing buf...
Definition router.h:119
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:172