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 static constexpr auto kChannels = "channels"sv;
110 friend void to_json (nlohmann::json &j, const AudioPort &port);
111 friend void from_json (const nlohmann::json &j, AudioPort &port);
112
113private:
114 BusLayout layout_{};
115 Purpose purpose_{};
116
117 uint8_t num_channels_{};
118
125 bool requires_limiting_{};
126
130 std::unique_ptr<juce::AudioSampleBuffer> buf_;
131
141 std::vector<RingBuffer<float>> audio_ring_;
142
143 BOOST_DESCRIBE_CLASS (
144 AudioPort,
145 (Port),
146 (),
147 (),
148 (layout_, purpose_, requires_limiting_))
149};
150
154class StereoPorts final
155{
156public:
157 static std::pair<utils::Utf8String, utils::Utf8String> get_name_and_symbols (
158 bool left,
160 utils::Utf8String symbol)
161 {
162 return std::make_pair (
164 fmt::format ("{} {}", name, left ? "L" : "R")),
166 fmt::format ("{}_{}", symbol, left ? "l" : "r")));
167 }
168};
169
170} // 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
Convenience factory for L/R audio port pairs.
Definition audio_port.h:155
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