Zrythm v2.0.0-alpha.1
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
10#include <juce_audio_basics/juce_audio_basics.h>
11#include <nlohmann/json_fwd.hpp>
12
13namespace zrythm::dsp
14{
15
16class AudioPort final
17 : public Port,
18 public PortConnectionsCacheMixin<AudioPort>,
20{
21 Q_OBJECT
22 QML_ELEMENT
23 QML_UNCREATABLE ("")
24
25public:
29 enum class BusLayout : uint8_t
30 {
31 // Implies a single channel
32 Mono,
33
34 // Implies 2 channels: L + R
35 Stereo,
36
37 // Unimplemented
38 Surround,
39 Ambisonic,
40 };
41
45 enum class Purpose : uint8_t
46 {
47 Main,
48 Sidechain,
49 };
50
51 AudioPort (
53 PortFlow flow,
54 BusLayout layout,
55 uint8_t num_channels,
56 Purpose purpose = Purpose::Main);
57
58 [[gnu::hot]] void process_block (
60 const dsp::ITransport &transport,
61 const dsp::TempoMap &tempo_map) noexcept override;
62
63 void clear_buffer (std::size_t offset, std::size_t nframes) override;
64
65 [[nodiscard]] auto layout () const { return layout_; }
66 [[nodiscard]] auto purpose () const { return purpose_; }
67 [[nodiscard]] auto &buffers () const { return buf_; }
68 auto num_channels () const { return num_channels_; }
69
70 void mark_as_requires_limiting () { requires_limiting_ = true; }
71 auto requires_limiting () const { return requires_limiting_; }
72
77 const AudioPort &src,
79 float multiplier = 1.f);
80
81 void copy_source_rt (
82 const AudioPort &src,
84 float multiplier = 1.f);
85
86 friend void init_from (
87 AudioPort &obj,
88 const AudioPort &other,
89 utils::ObjectCloneType clone_type);
90
91 void prepare_for_processing_impl (
92 const graph::GraphNode * node,
93 units::sample_rate_t sample_rate,
94 units::sample_u32_t max_block_length) override;
95 void release_resources () override;
96
97private:
98 static constexpr auto kBusLayoutId = "busLayout"sv;
99 static constexpr auto kPurposeId = "purpose"sv;
100 static constexpr auto kRequiresLimitingId = "requiresLimiting"sv;
101 static constexpr auto kChannels = "channels"sv;
102 friend void to_json (nlohmann::json &j, const AudioPort &port);
103 friend void from_json (const nlohmann::json &j, AudioPort &port);
104
105private:
106 BusLayout layout_{};
107 Purpose purpose_{};
108
109 uint8_t num_channels_{};
110
117 bool requires_limiting_{};
118
122 std::unique_ptr<juce::AudioSampleBuffer> buf_;
123
124 BOOST_DESCRIBE_CLASS (
125 AudioPort,
126 (Port),
127 (),
128 (),
129 (layout_, purpose_, requires_limiting_))
130};
131
135class StereoPorts final
136{
137public:
138 static std::pair<utils::Utf8String, utils::Utf8String> get_name_and_symbols (
139 bool left,
141 utils::Utf8String symbol)
142 {
143 return std::make_pair (
145 fmt::format ("{} {}", name, left ? "L" : "R")),
147 fmt::format ("{}_{}", symbol, left ? "l" : "r")));
148 }
149};
150
151} // namespace zrythm::dsp
Purpose
Purpose of this port.
Definition audio_port.h:46
void add_source_rt(const AudioPort &src, dsp::graph::ProcessBlockInfo time_nfo, float multiplier=1.f)
Adds the contents of src to this port.
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:30
void clear_buffer(std::size_t offset, std::size_t nframes) override
Clears the port buffer.
Interface for transport.
Definition itransport.h:47
Convenience factory for L/R audio port pairs.
Definition audio_port.h:136
Represents a node in a DSP graph.
Definition graph_node.h:180
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