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 // Implies a single channel
37 Mono,
38
39 // Implies 2 channels: L + R
40 Stereo,
41
42 // Unimplemented
43 Surround,
44 Ambisonic,
45 };
46
50 enum class Purpose : uint8_t
51 {
52 Main,
53 Sidechain,
54 };
55
56 AudioPort (
58 PortFlow flow,
59 BusLayout layout,
60 uint8_t num_channels,
61 Purpose purpose = Purpose::Main);
62
63 static constexpr size_t AUDIO_RING_SIZE = 65536;
64
65 [[gnu::hot]] void process_block (
66 EngineProcessTimeInfo time_nfo,
67 const dsp::ITransport &transport,
68 const dsp::TempoMap &tempo_map) noexcept override;
69
70 void clear_buffer (std::size_t offset, std::size_t nframes) override;
71
72 [[nodiscard]] auto layout () const { return layout_; }
73 [[nodiscard]] auto purpose () const { return purpose_; }
74 [[nodiscard]] auto &buffers () const { return buf_; }
75 [[nodiscard]] auto &audio_ring_buffers () const { return audio_ring_; }
76 auto num_channels () const { return num_channels_; }
77
78 void mark_as_requires_limiting () { requires_limiting_ = true; }
79 auto requires_limiting () const { return requires_limiting_; }
80
85 const AudioPort &src,
86 EngineProcessTimeInfo time_nfo,
87 float multiplier = 1.f);
88
89 void copy_source_rt (
90 const AudioPort &src,
91 EngineProcessTimeInfo time_nfo,
92 float multiplier = 1.f);
93
94 friend void init_from (
95 AudioPort &obj,
96 const AudioPort &other,
97 utils::ObjectCloneType clone_type);
98
100 const graph::GraphNode * node,
101 units::sample_rate_t sample_rate,
102 nframes_t max_block_length) override;
103 void release_resources () override;
104
105private:
106 static constexpr auto kBusLayoutId = "busLayout"sv;
107 static constexpr auto kPurposeId = "purpose"sv;
108 static constexpr auto kRequiresLimitingId = "requiresLimiting"sv;
109 friend void to_json (nlohmann::json &j, const AudioPort &port)
110 {
111 to_json (j, static_cast<const Port &> (port));
112 j[kBusLayoutId] = port.layout_;
113 j[kPurposeId] = port.purpose_;
114 j[kRequiresLimitingId] = port.requires_limiting_;
115 }
116 friend void from_json (const nlohmann::json &j, AudioPort &port)
117 {
118 from_json (j, static_cast<Port &> (port));
119 j.at (kBusLayoutId).get_to (port.layout_);
120 j.at (kPurposeId).get_to (port.purpose_);
121 j.at (kRequiresLimitingId).get_to (port.requires_limiting_);
122 }
123
124private:
125 BusLayout layout_{};
126 Purpose purpose_{};
127
128 uint8_t num_channels_{};
129
136 bool requires_limiting_{};
137
141 std::unique_ptr<juce::AudioSampleBuffer> buf_;
142
152 std::vector<RingBuffer<float>> audio_ring_;
153
154 BOOST_DESCRIBE_CLASS (
155 AudioPort,
156 (Port),
157 (),
158 (),
159 (layout_, purpose_, requires_limiting_))
160};
161
165class StereoPorts final
166{
167public:
168 static std::pair<utils::Utf8String, utils::Utf8String> get_name_and_symbols (
169 bool left,
171 utils::Utf8String symbol)
172 {
173 return std::make_pair (
175 fmt::format ("{} {}", name, left ? "L" : "R")),
177 fmt::format ("{}_{}", symbol, left ? "l" : "r")));
178 }
179};
180
181} // namespace zrythm::dsp
Audio port specifics.
Definition audio_port.h:25
Purpose
Purpose of this port.
Definition audio_port.h:51
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:166
Represents a node in a DSP graph.
Definition graph_node.h:131
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