MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Temporal.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Domain.hpp"
5
6namespace MayaFlux {
7
8/**
9 * @class TimeSpec
10 * @brief Represents a timed activation operation for processing nodes, buffers, or networks.
11 *
12 * The TimeSpec struct encapsulates the concept of activating a processing entity
13 * for a specific duration and (optionally) on specific channels. It's designed to be used
14 * with the stream operator (>>) and TemporalWrapper to create a fluent, expressive syntax
15 * for computational flow programming.
16 *
17 * This approach is inspired by flow-based programming paradigms, which use
18 * operators to express data flow and timing relationships. It allows for a more
19 * intuitive, declarative way of expressing temporal operations compared to traditional
20 * function calls.
21 *
22 * Example usage:
23 * ```cpp
24 * // Activate a processing node for 2 seconds
25 * process_node >> Time(2.0);
26 * ```
27 *
28 * The TimeSpec is part of a broader pattern of using operator overloading
29 * to create a domain-specific language for computational flow programming within C++.
30 */
31struct MAYAFLUX_API TimeSpec {
32 double seconds;
33 std::optional<std::vector<uint32_t>> channels;
34
35 /**
36 * @brief Constructs a TimeSpec with the specified duration.
37 * @param s Duration of the operation in seconds.
38 */
39 TimeSpec(double s)
40 : seconds(s)
41 {
42 }
43 /**
44 * @brief Constructs a TimeSpec with the specified duration and channels.
45 * @param s Duration of the operation in seconds.
46 * @param ch List of channels to activate.
47 */
48 TimeSpec(double s, std::vector<uint32_t> ch)
49 : seconds(s)
50 , channels(ch)
51 {
52 }
53 /**
54 * @brief Constructs a TimeSpec with the specified duration and a single channel.
55 * @param s Duration of the operation in seconds.
56 * @param ch Channel to activate.
57 */
58 TimeSpec(double s, uint32_t ch)
59 : seconds(s)
60 , channels(std::vector<uint32_t> { ch })
61 {
62 }
63};
64
65/**
66 * @class TemporalWrapper
67 * @brief Wraps an entity with a TimeSpec for temporal activation.
68 *
69 * The TemporalWrapper class enables a fluent, expressive syntax for
70 * activating processing entities (nodes, buffers, networks) for a specific duration,
71 * using the stream operator (>>).
72 *
73 * Example usage:
74 * ```cpp
75 * // Activate a processing node for 2 seconds
76 * process_node >> Time(2.0);
77 * ```
78 *
79 * This is part of a broader pattern of using operator overloading to create
80 * a domain-specific language for computational flow programming within C++.
81 */
82template <typename T>
84public:
85 TemporalWrapper(const std::shared_ptr<T>& entity, TimeSpec spec)
87 , m_spec(std::move(spec))
88 {
89 }
90
91 template <typename U>
92 requires std::derived_from<U, T>
94 : m_entity(other.entity())
95 , m_spec(other.spec())
96 {
97 }
98
99 std::shared_ptr<T> entity() const { return m_entity; }
100 [[nodiscard]] const TimeSpec& spec() const { return m_spec; }
101
102private:
103 std::shared_ptr<T> m_entity;
105};
106
107/**
108 * @brief Creates a TimeSpec with the specified duration and a single channel.
109 *
110 * This overload allows specifying a single channel to activate.
111 *
112 * Example usage:
113 * ```cpp
114 * // Activate a processing node for 2 seconds on channel 1
115 * process_node >> Time(2.0, 1);
116 * // Activate a processing node for 2 seconds on default 0 channel
117 * process_node >> Time(2.0);
118 * ```
119 *
120 * @param seconds Duration in seconds.
121 * @param channel Channel to activate.
122 * @return A TimeSpec object representing the specified duration and channel.
123 */
124MAYAFLUX_API TimeSpec Time(double seconds, uint32_t channel = 0);
125
126/**
127 * @brief Creates a TimeSpec with the specified duration and a list of channels.
128 *
129 * This overload allows specifying multiple channels to activate.
130 *
131 * Example usage:
132 * ```cpp
133 * // Activate a processing node for 2 seconds on channels 0 and 1
134 * process_node >> Time(2.0, {0, 1});
135 * ```
136 *
137 * @param seconds Duration in seconds.
138 * @param channels List of channels to activate.
139 * @return A TimeSpec object representing the specified duration and channels.
140 */
141MAYAFLUX_API TimeSpec Time(double seconds, std::vector<uint32_t> channels);
142
143/**
144 * @brief Creates a TemporalWrapper for an entity and a TimeSpec.
145 *
146 * This operator overload implements the entity >> Time(seconds) syntax, which
147 * wraps the entity with a TimeSpec for subsequent temporal activation.
148 *
149 * Example usage:
150 * ```cpp
151 * // Activate a processing node for 2 seconds
152 * process_node >> Time(2.0);
153 * ```
154 *
155 * This is part of a broader pattern of using operator overloading to create
156 * a domain-specific language for computational flow programming within C++.
157 *
158 * @tparam T The type of the entity to wrap.
159 * @param entity The entity to activate.
160 * @param spec The TimeSpec specifying the duration and channels.
161 * @return A TemporalWrapper<T> representing the timed activation.
162 */
163template <typename T>
164TemporalWrapper<T> operator>>(std::shared_ptr<T> entity, const TimeSpec& spec)
165{
166 return TemporalWrapper<T>(entity, spec);
167}
168
169/**
170 * @brief Activates a node in a specific domain for the duration specified by the TemporalWrapper.
171 *
172 * This operator overload implements the syntax:
173 * ```cpp
174 * process_node >> Time(2.0) | domain;
175 * ```
176 * It activates the wrapped node in the given domain for the specified duration and channels.
177 *
178 * @param wrapper The TemporalWrapper containing the node and timing information.
179 * @param domain The domain in which to activate the node.
180 * @return A shared pointer to the activated node.
181 */
182MAYAFLUX_API std::shared_ptr<Nodes::Node> operator|(const TemporalWrapper<Nodes::Node>& wrapper, Domain domain);
183
184/**
185 * @brief Activates a buffer in a specific domain for the duration specified by the TemporalWrapper.
186 *
187 * This operator overload implements the syntax:
188 * ```cpp
189 * buffer >> Time(2.0, 1) | domain;
190 * ```
191 * It activates the wrapped buffer in the given domain for the specified duration and channel(s).
192 *
193 * @param wrapper The TemporalWrapper containing the buffer and timing information.
194 * @param domain The domain in which to activate the buffer.
195 * @return A shared pointer to the activated buffer.
196 */
197MAYAFLUX_API std::shared_ptr<Buffers::Buffer> operator|(const TemporalWrapper<Buffers::Buffer>& wrapper, Domain domain);
198
199/**
200 * @brief Activates a node network in a specific domain for the duration specified by the TemporalWrapper.
201 *
202 * This operator overload implements the syntax:
203 * ```cpp
204 * network >> Time(2.0, {0, 1}) | domain;
205 * ```
206 * It activates the wrapped node network in the given domain for the specified duration and channels.
207 *
208 * @param wrapper The TemporalWrapper containing the node network and timing information.
209 * @param domain The domain in which to activate the network.
210 * @return A shared pointer to the activated node network.
211 */
212MAYAFLUX_API std::shared_ptr<Nodes::Network::NodeNetwork> operator|(const TemporalWrapper<Nodes::Network::NodeNetwork>& wrapper, Domain domain);
213
214}
const TimeSpec & spec() const
Definition Temporal.hpp:100
std::shared_ptr< T > entity() const
Definition Temporal.hpp:99
TemporalWrapper(const TemporalWrapper< U > &other)
Definition Temporal.hpp:93
TemporalWrapper(const std::shared_ptr< T > &entity, TimeSpec spec)
Definition Temporal.hpp:85
std::shared_ptr< T > m_entity
Definition Temporal.hpp:103
Wraps an entity with a TimeSpec for temporal activation.
Definition Temporal.hpp:83
std::shared_ptr< Nodes::Node > operator>>(const std::shared_ptr< Nodes::Node > &lhs, const std::shared_ptr< Nodes::Node > &rhs)
Connects two nodes in series (pipeline operator)
Definition Graph.cpp:328
std::shared_ptr< Nodes::Node > operator|(const std::shared_ptr< Nodes::Node > &node, Domain d)
Definition Creator.cpp:105
TimeSpec Time(double seconds, uint32_t channel)
Creates a TimeSpec with the specified duration and a single channel.
Definition Temporal.cpp:9
Domain
Unified domain enum combining all three ProcessingToken subsystems.
Definition Domain.hpp:22
Main namespace for the Maya Flux audio engine.
Definition LiveAid.hpp:6
std::optional< std::vector< uint32_t > > channels
Definition Temporal.hpp:33
TimeSpec(double s)
Constructs a TimeSpec with the specified duration.
Definition Temporal.hpp:39
TimeSpec(double s, std::vector< uint32_t > ch)
Constructs a TimeSpec with the specified duration and channels.
Definition Temporal.hpp:48
TimeSpec(double s, uint32_t ch)
Constructs a TimeSpec with the specified duration and a single channel.
Definition Temporal.hpp:58
Represents a timed activation operation for processing nodes, buffers, or networks.
Definition Temporal.hpp:31