MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Creator.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Domain.hpp"
4#include "Registry.hpp"
5
6namespace MayaFlux {
7
9 std::optional<Domain> domain;
10 std::optional<uint32_t> channel;
11 std::optional<std::vector<uint32_t>> channels;
12
13 CreationContext() = default;
15 : domain(d)
16 {
17 }
18 CreationContext(Domain d, uint32_t ch)
19 : domain(d)
20 , channel(ch)
21 {
22 }
23 CreationContext(uint32_t ch)
24 : channel(ch)
25 {
26 }
27 CreationContext(std::vector<uint32_t> ch)
28 : channels(std::move(ch))
29 {
30 }
31};
32
33MAYAFLUX_API std::shared_ptr<Nodes::Node> operator|(const std::shared_ptr<Nodes::Node>& node, Domain d);
34MAYAFLUX_API std::shared_ptr<Buffers::Buffer> operator|(const std::shared_ptr<Buffers::Buffer>& buffer, Domain d);
35MAYAFLUX_API std::shared_ptr<Nodes::NodeNetwork> operator|(const std::shared_ptr<Nodes::NodeNetwork>& network, Domain d);
36
37MAYAFLUX_API void register_node(const std::shared_ptr<Nodes::Node>& node, const CreationContext& ctx);
38MAYAFLUX_API void register_network(const std::shared_ptr<Nodes::NodeNetwork>& network, const CreationContext& ctx);
39MAYAFLUX_API void register_buffer(const std::shared_ptr<Buffers::Buffer>& buffer, const CreationContext& ctx);
40MAYAFLUX_API void register_container(const std::shared_ptr<Kakshya::SoundFileContainer>& container, const Domain& domain);
41
42template <typename T>
43class CreationHandle : public std::shared_ptr<T> {
44public:
45 using std::shared_ptr<T>::shared_ptr;
46
47 CreationHandle(const std::shared_ptr<T>& ptr)
48 : std::shared_ptr<T>(ptr)
49 {
50 }
51
52 CreationHandle(std::shared_ptr<T>&& ptr)
53 : std::shared_ptr<T>(std::move(ptr))
54 {
55 }
56
58 {
59 m_ctx.domain = d;
61 return *this;
62 }
63
64 CreationHandle& channel(uint32_t ch)
65 {
66 m_ctx.channel = ch;
67 m_ctx.channels.reset();
69 return *this;
70 }
71
72 CreationHandle& channels(std::vector<uint32_t> ch)
73 {
74 m_ctx.channels = std::move(ch);
75 m_ctx.channel.reset();
77 return *this;
78 }
79
80 template <typename... Args>
81 CreationHandle& channels(Args... args)
82 {
83 static_assert(sizeof...(args) > 0, "channels() requires at least one argument");
84 static_assert((std::is_convertible_v<Args, uint32_t> && ...),
85 "All arguments must be convertible to uint32_t");
86
87 m_ctx.channels = std::vector<uint32_t> { static_cast<uint32_t>(args)... };
88 m_ctx.channel.reset();
90 return *this;
91 }
92
94 CreationHandle& operator[](uint32_t ch) { return channel(ch); }
95 CreationHandle& operator[](std::initializer_list<uint32_t> ch_list)
96 {
97 m_ctx.channels = std::vector<uint32_t>(ch_list);
98 m_ctx.channel.reset();
100 return *this;
101 }
102
103private:
105 {
106 if constexpr (std::is_base_of_v<Kakshya::SignalSourceContainer, T>) {
107 if (m_ctx.domain) {
110 }
111 } else if (m_ctx.domain && (m_ctx.channel || m_ctx.channels)) {
112 if constexpr (std::is_base_of_v<Nodes::Node, T>) {
113 if (m_ctx.domain) {
115 }
116 } else if constexpr (std::is_base_of_v<Buffers::Buffer, T>) {
117 if (m_ctx.domain) {
119 }
120 } else if constexpr (std::is_base_of_v<Nodes::NodeNetwork, T>) {
121 if (m_ctx.domain) {
123 }
124 }
126 } else if (m_ctx.domain) {
127 if constexpr (std::is_base_of_v<Buffers::VKBuffer, T>) {
129 }
131 if constexpr (std::is_base_of_v<Nodes::Node, T>) {
133 }
134 if constexpr (std::is_base_of_v<Nodes::NodeNetwork, T>) {
136 }
137 }
139 }
140 }
141
143 {
144 if (!*this)
145 return;
146
147 std::shared_ptr<Nodes::Node> node = std::static_pointer_cast<Nodes::Node>(*this);
148 register_node(node, m_ctx);
149 }
150
152 {
153 if (!*this)
154 return;
155
156 std::shared_ptr<Nodes::NodeNetwork> network = std::static_pointer_cast<Nodes::NodeNetwork>(*this);
157 register_network(network, m_ctx);
158 }
159
161 {
162 if (!*this)
163 return;
164
165 std::shared_ptr<Buffers::Buffer> buffer = std::static_pointer_cast<Buffers::Buffer>(*this);
166 register_buffer(buffer, m_ctx);
167 }
168
170 {
171 if (!*this)
172 return;
173
174 std::shared_ptr<Kakshya::SoundFileContainer> container = std::static_pointer_cast<Kakshya::SoundFileContainer>(*this);
175 register_container(container, m_ctx.domain.value());
176 }
177
179};
180
181class MAYAFLUX_API Creator {
182public:
183#define N(method_name, full_type_name) \
184 template <typename... Args> \
185 auto method_name(Args&&... args) -> CreationHandle<full_type_name> \
186 { \
187 auto obj = std::make_shared<full_type_name>(std::forward<Args>(args)...); \
188 return CreationHandle<full_type_name>(obj); \
189 }
191#undef N
192
193#define W(method_name, full_type_name) \
194 template <typename... Args> \
195 auto method_name(Args&&... args) -> CreationHandle<full_type_name> \
196 { \
197 auto obj = std::make_shared<full_type_name>(std::forward<Args>(args)...); \
198 return CreationHandle<full_type_name>(obj); \
199 }
201#undef W
202
203#define B(method_name, full_type_name) \
204 template <typename... Args> \
205 auto method_name(Args&&... args) -> CreationHandle<full_type_name> \
206 { \
207 auto obj = std::make_shared<full_type_name>(std::forward<Args>(args)...); \
208 return CreationHandle<full_type_name>(obj); \
209 }
211#undef B
212
213 auto read_audio(const std::string& filepath) -> CreationHandle<Kakshya::SoundFileContainer>
214 {
215 auto container = load_container(filepath);
217 }
218
219 auto read_image(const std::string& filepath) -> CreationHandle<Buffers::TextureBuffer>
220 {
221 auto buffer = load_buffer(filepath);
223 }
224
225private:
226 std::shared_ptr<Kakshya::SoundFileContainer> load_container(const std::string& filepath);
227 std::shared_ptr<Buffers::TextureBuffer> load_buffer(const std::string& filepath);
228};
229
230template <typename T>
231auto operator|(std::shared_ptr<T> obj, Domain d) -> CreationHandle<T>
232{
233 CreationHandle<T> handle(obj);
234 return handle.domain(d);
235}
236
237/**
238 * @brief Domain constant for Audio domain.
239 *
240 * This domain unwraps to Nodes::ProccingToken::AUDIO_RATE | Buffers::ProcessingToken::AUDIO_BACKEND | Vruta::ProcessingToken::SAMPLE_ACCURATE;
241 */
242static constexpr auto Audio = Domain::AUDIO;
243
244/**
245 * @brief Domain constant for Graphics domain.
246 *
247 * This domain unwraps to Nodes::ProccingToken::FRAME_RATE | Buffers::ProcessingToken::GRAPHICS_BACKEND | Vruta::ProcessingToken::FRAME_ACCURATE;
248 */
249static constexpr auto Graphics = Domain::GRAPHICS;
250
251/**
252 * @brief Global Creator instance for creating nodes, buffers, and containers.
253 *
254 * This instance provides a convenient interface to create various MayaFlux components
255 * such as nodes, buffers, and signal source containers. It supports method chaining
256 * for setting creation context like domain and channels.
257 * Each object can be registered automatically based on the provided context.
258 * The contexts include domain, channel, and metadata.
259 * Domains supported are AUDIO and GRAPHICS.
260 * For example:
261 * ```
262 * auto myNode = ::vega.Sine(440.0f).domain(Audio).channel(0);
263 * auto node_buffer = vega.NodeBuffer(0, 512, myNode)[{0, 1, 2}] | Graphics;
264 * ```
265 */
266extern MAYAFLUX_API Creator vega;
267
268static std::vector<std::shared_ptr<Buffers::ContainerBuffer>> s_last_created_container_buffers;
269
270/**
271 * @brief Retrieves the last created container buffers from the Creator.
272 * @return Vector of shared pointers to the last created ContainerBuffer instances.
273 *
274 * This function returns the container buffers that were most recently created
275 * by the Creator instance when registering a SoundFileContainer in the AUDIO domain.
276 * It allows access to these buffers for further manipulation or inspection.
277 */
278std::vector<std::shared_ptr<Buffers::ContainerBuffer>> MAYAFLUX_API get_last_created_container_buffers();
279
280} // namespace MayaFlux
#define ALL_NODE_NETWORK_REGISTRATIONS
Definition Registry.hpp:69
#define ALL_NODE_REGISTRATIONS
Definition Registry.hpp:54
#define ALL_BUFFER_REGISTRATION
Definition Registry.hpp:73
CreationHandle & domain(Domain d)
Definition Creator.hpp:57
CreationHandle(std::shared_ptr< T > &&ptr)
Definition Creator.hpp:52
CreationContext m_ctx
Definition Creator.hpp:178
CreationHandle & operator|(Domain d)
Definition Creator.hpp:93
CreationHandle & channel(uint32_t ch)
Definition Creator.hpp:64
CreationHandle & operator[](uint32_t ch)
Definition Creator.hpp:94
CreationHandle & channels(Args... args)
Definition Creator.hpp:81
CreationHandle(const std::shared_ptr< T > &ptr)
Definition Creator.hpp:47
CreationHandle & channels(std::vector< uint32_t > ch)
Definition Creator.hpp:72
CreationHandle & operator[](std::initializer_list< uint32_t > ch_list)
Definition Creator.hpp:95
auto read_image(const std::string &filepath) -> CreationHandle< Buffers::TextureBuffer >
Definition Creator.hpp:219
auto read_audio(const std::string &filepath) -> CreationHandle< Kakshya::SoundFileContainer >
Definition Creator.hpp:213
Creator vega
Global Creator instance for creating nodes, buffers, and containers.
Definition Creator.cpp:14
void register_node(const std::shared_ptr< Nodes::Node > &node, const Nodes::ProcessingToken &token, uint32_t channel)
Definition Graph.cpp:72
static constexpr auto Graphics
Domain constant for Graphics domain.
Definition Creator.hpp:249
void register_network(const std::shared_ptr< Nodes::NodeNetwork > &network, const CreationContext &ctx)
Definition Creator.cpp:37
static constexpr auto Audio
Domain constant for Audio domain.
Definition Creator.hpp:242
void register_buffer(const std::shared_ptr< Buffers::Buffer > &buffer, const CreationContext &ctx)
Definition Creator.cpp:65
std::shared_ptr< Nodes::Node > operator|(const std::shared_ptr< Nodes::Node > &node, Domain d)
Definition Creator.cpp:104
static std::vector< std::shared_ptr< Buffers::ContainerBuffer > > s_last_created_container_buffers
Definition Creator.hpp:268
std::vector< std::shared_ptr< Buffers::ContainerBuffer > > get_last_created_container_buffers()
Retrieves the last created container buffers from the Creator.
Definition Creator.cpp:99
void register_container(const std::shared_ptr< Kakshya::SoundFileContainer > &container, const Domain &domain)
Definition Creator.cpp:84
Domain
Unified domain enum combining all three ProcessingToken subsystems.
Definition Domain.hpp:22
@ AUDIO
Standard real-time audio processing domain.
Definition Domain.hpp:33
@ GRAPHICS
Standard real-time graphics processing domain.
Definition Domain.hpp:55
Main namespace for the Maya Flux audio engine.
Definition LiveAid.hpp:6
CreationContext(std::vector< uint32_t > ch)
Definition Creator.hpp:27
std::optional< uint32_t > channel
Definition Creator.hpp:10
CreationContext(uint32_t ch)
Definition Creator.hpp:23
CreationContext(Domain d, uint32_t ch)
Definition Creator.hpp:18
std::optional< std::vector< uint32_t > > channels
Definition Creator.hpp:11
std::optional< Domain > domain
Definition Creator.hpp:9