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 <juce_audio_basics/juce_audio_basics.h>
12#include <nlohmann/json_fwd.hpp>
13
14namespace zrythm::dsp
15{
16
20class AudioPort final
21 : public QObject,
22 public Port,
24 public PortConnectionsCacheMixin<AudioPort>,
26{
27 Q_OBJECT
28 QML_ELEMENT
29 QML_UNCREATABLE ("")
30
31public:
35 enum class BusLayout : uint8_t
36 {
37 // Implies a single channel
38 Mono,
39
40 // Implies 2 channels: L + R
41 Stereo,
42
43 // Unimplemented
44 Surround,
45 Ambisonic,
46 };
47
51 enum class Purpose : uint8_t
52 {
53 Main,
54 Sidechain,
55 };
56
57 AudioPort (
59 PortFlow flow,
60 BusLayout layout,
61 uint8_t num_channels,
62 Purpose purpose = Purpose::Main);
63
64 static constexpr size_t AUDIO_RING_SIZE = 65536;
65
66 [[gnu::hot]] void process_block (
68 const dsp::ITransport &transport,
69 const dsp::TempoMap &tempo_map) 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,
88 float multiplier = 1.f);
89
90 void copy_source_rt (
91 const AudioPort &src,
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 units::sample_u32_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 static constexpr auto kChannels = "channels"sv;
111 friend void to_json (nlohmann::json &j, const AudioPort &port);
112 friend void from_json (const nlohmann::json &j, AudioPort &port);
113
114private:
115 BusLayout layout_{};
116 Purpose purpose_{};
117
118 uint8_t num_channels_{};
119
126 bool requires_limiting_{};
127
131 std::unique_ptr<juce::AudioSampleBuffer> buf_;
132
142 std::vector<RingBuffer<float>> audio_ring_;
143
144 BOOST_DESCRIBE_CLASS (
145 AudioPort,
146 (Port),
147 (),
148 (),
149 (layout_, purpose_, requires_limiting_))
150};
151
155class StereoPorts final
156{
157public:
158 static std::pair<utils::Utf8String, utils::Utf8String> get_name_and_symbols (
159 bool left,
161 utils::Utf8String symbol)
162 {
163 return std::make_pair (
165 fmt::format ("{} {}", name, left ? "L" : "R")),
167 fmt::format ("{}_{}", symbol, left ? "l" : "r")));
168 }
169};
170
171} // namespace zrythm::dsp
Audio port specifics.
Definition audio_port.h:26
Purpose
Purpose of this port.
Definition audio_port.h:52
void prepare_for_processing(const graph::GraphNode *node, units::sample_rate_t sample_rate, units::sample_u32_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:36
void add_source_rt(const AudioPort &src, dsp::graph::EngineProcessTimeInfo time_nfo, float multiplier=1.f)
Adds the contents of src to this port.
void clear_buffer(std::size_t offset, std::size_t nframes) override
Clears the port buffer.
Interface for transport.
Definition itransport.h:16
Convenience factory for L/R audio port pairs.
Definition audio_port.h:156
Represents a node in a DSP graph.
Definition graph_node.h:160
Lightweight UTF-8 string wrapper with safe conversions.
Definition utf8_string.h:37
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:80
Common struct to pass around during processing to avoid repeating the data in function arguments.
Definition graph_node.h:51