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
100 void
101 prepare_for_processing (sample_rate_t sample_rate, nframes_t max_block_length)
102 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: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(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
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 sample_rate_t
Sample rate.
Definition types.h:61
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:136