Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
audio_port.h
1// SPDX-FileCopyrightText: © 2024-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "dsp/port.h"
7#include "utils/icloneable.h"
8#include "utils/monotonic_time_provider.h"
9#include "utils/ring_buffer.h"
10
11#include <nlohmann/json_fwd.hpp>
12
13namespace zrythm::dsp
14{
15
19class AudioPort final
20 : public QObject,
21 public Port,
23 public PortConnectionsCacheMixin<AudioPort>,
25{
26 Q_OBJECT
27 QML_ELEMENT
28 QML_UNCREATABLE ("")
29
30public:
34 enum class BusLayout : uint8_t
35 {
36 Unknown,
37
38 // Implies a single channel
39 Mono,
40
41 // Implies 2 channels: L + R
42 Stereo,
43
44 // Unimplemented
45 Surround,
46 Ambisonic,
47 };
48
52 enum class Purpose : uint8_t
53 {
54 Main,
55 Sidechain,
56 };
57
58 AudioPort (
60 PortFlow flow,
61 BusLayout layout,
62 uint8_t num_channels,
63 Purpose purpose = Purpose::Main);
64
65 static constexpr size_t AUDIO_RING_SIZE = 65536;
66
67 [[gnu::hot]] void process_block (
68 EngineProcessTimeInfo time_nfo,
69 const dsp::ITransport &transport) noexcept override;
70
71 void clear_buffer (std::size_t offset, std::size_t nframes) override;
72
73 [[nodiscard]] auto layout () const { return layout_; }
74 [[nodiscard]] auto purpose () const { return purpose_; }
75 [[nodiscard]] auto &buffers () const { return buf_; }
76 [[nodiscard]] auto &audio_ring_buffers () const { return audio_ring_; }
77 auto num_channels () const { return num_channels_; }
78
79 void mark_as_requires_limiting () { requires_limiting_ = true; }
80 auto requires_limiting () const { return requires_limiting_; }
81
86 const AudioPort &src,
87 EngineProcessTimeInfo time_nfo,
88 float multiplier = 1.f);
89
90 void copy_source_rt (
91 const AudioPort &src,
92 EngineProcessTimeInfo time_nfo,
93 float multiplier = 1.f);
94
95 friend void init_from (
96 AudioPort &obj,
97 const AudioPort &other,
98 utils::ObjectCloneType clone_type);
99
101 const graph::GraphNode * node,
102 units::sample_rate_t sample_rate,
103 nframes_t max_block_length) override;
104 void release_resources () override;
105
106private:
107 static constexpr auto kBusLayoutId = "busLayout"sv;
108 static constexpr auto kPurposeId = "purpose"sv;
109 static constexpr auto kRequiresLimitingId = "requiresLimiting"sv;
110 friend void to_json (nlohmann::json &j, const AudioPort &port)
111 {
112 to_json (j, static_cast<const Port &> (port));
113 j[kBusLayoutId] = port.layout_;
114 j[kPurposeId] = port.purpose_;
115 j[kRequiresLimitingId] = port.requires_limiting_;
116 }
117 friend void from_json (const nlohmann::json &j, AudioPort &port)
118 {
119 from_json (j, static_cast<Port &> (port));
120 j.at (kBusLayoutId).get_to (port.layout_);
121 j.at (kPurposeId).get_to (port.purpose_);
122 j.at (kRequiresLimitingId).get_to (port.requires_limiting_);
123 }
124
125private:
126 BusLayout layout_{};
127 Purpose purpose_{};
128
129 uint8_t num_channels_{};
130
137 bool requires_limiting_{};
138
142 std::unique_ptr<juce::AudioSampleBuffer> buf_;
143
153 std::vector<RingBuffer<float>> audio_ring_;
154
155 BOOST_DESCRIBE_CLASS (
156 AudioPort,
157 (Port),
158 (),
159 (),
160 (layout_, purpose_, requires_limiting_))
161};
162
166class StereoPorts final
167{
168public:
169 static std::pair<utils::Utf8String, utils::Utf8String> get_name_and_symbols (
170 bool left,
172 utils::Utf8String symbol)
173 {
174 return std::make_pair (
176 fmt::format ("{} {}", name, left ? "L" : "R")),
178 fmt::format ("{}_{}", symbol, left ? "l" : "r")));
179 }
180};
181
182} // namespace zrythm::dsp
Audio port specifics.
Definition audio_port.h:25
Purpose
Purpose of this port.
Definition audio_port.h:53
void add_source_rt(const AudioPort &src, EngineProcessTimeInfo time_nfo, float multiplier=1.f)
Adds the contents of src to this port.
void prepare_for_processing(const graph::GraphNode *node, units::sample_rate_t sample_rate, nframes_t max_block_length) override
Called to allocate resources required for processing.
void release_resources() override
Called to release resources allocated by prepare_for_processing().
BusLayout
Description of the channel layout of this port.
Definition audio_port.h:35
void clear_buffer(std::size_t offset, std::size_t nframes) override
Clears the port buffer.
Interface for transport.
Definition itransport.h:17
A base class for ports used for connecting processors in the DSP graph.
Definition port.h:30
Convenience factory for L/R audio port pairs.
Definition audio_port.h:167
Represents a node in a DSP graph.
Definition graph_node.h:129
Lightweight UTF-8 string wrapper with safe conversions.
Definition utf8_string.h:38
static constexpr Utf8String from_utf8_encoded_string(std::string_view str)
Construct from a std::string_view that we are 100% sure is UTF8-encoded.
Definition utf8_string.h:81
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