MayaFlux 0.4.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 "Creator.hpp"
5
6namespace MayaFlux {
7
8/**
9 * @struct TimeSpec
10 * @brief Duration operand for the entity >> Time(s) | DomainSpec syntax.
11 *
12 * The TimeSpec struct encapsulates the concept of activating a processing entity
13 * for a specific duration. 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 * Carries only the activation duration. Channel binding is expressed at the
23 * domain site via DomainSpec::operator[]:
24 * @code
25 * node >> Time(2.0) | Audio[0];
26 * node >> Time(2.0) | Audio[{0, 1}];
27 * buffer >> Time(2.0) | Audio[0];
28 * network >> Time(2.0) | Graphics;
29 * @endcode
30 * ```
31 *
32 * The TimeSpec is part of a broader pattern of using operator overloading
33 * to create a domain-specific language for computational flow programming within C++.
34 */
35struct MAYAFLUX_API TimeSpec {
36 double seconds;
37
38 /**
39 * @brief Constructs a TimeSpec with the specified duration.
40 * @param s Duration of the activation in seconds.
41 */
42 explicit TimeSpec(double s)
43 : seconds(s)
44 {
45 }
46};
47
48/**
49 * @class TemporalWrapper
50 * @brief Intermediate produced by operator>> binding an entity to a TimeSpec.
51 *
52 * Consumed by operator|(TemporalWrapper, CreationContext) which resolves the
53 * domain and channel binding expressed by the right-hand DomainSpec.
54 */
55template <typename T>
56class TemporalWrapper {
57public:
58 TemporalWrapper(const std::shared_ptr<T>& entity, TimeSpec spec)
59 : m_entity(entity)
60 , m_spec(std::move(spec))
61 {
62 }
63
64 template <typename U>
65 requires std::derived_from<U, T>
66 TemporalWrapper(const TemporalWrapper<U>& other)
67 : m_entity(other.entity())
68 , m_spec(other.spec())
69 {
70 }
71
72 std::shared_ptr<T> entity() const { return m_entity; }
73 [[nodiscard]] const TimeSpec& spec() const { return m_spec; }
74
75private:
76 std::shared_ptr<T> m_entity;
77 TimeSpec m_spec;
78};
79
80/**
81 * @brief Constructs a TimeSpec for the given duration.
82 *
83 * @code
84 * node >> Time(2.0) | Audio[0];
85 * @endcode
86 *
87 * @param seconds Duration in seconds.
88 * @return TimeSpec for use with operator>>.
89 */
90MAYAFLUX_API TimeSpec Time(double seconds);
91
92/**
93 * @brief Wraps an entity and a TimeSpec into a TemporalWrapper.
94 *
95 * @tparam T Entity type (Node, Buffer, or NodeNetwork).
96 * @param entity The entity to activate.
97 * @param spec The duration specification.
98 * @return TemporalWrapper<T> ready for operator|.
99 */
100template <typename T>
101TemporalWrapper<T> operator>>(std::shared_ptr<T> entity, const TimeSpec& spec)
102{
103 return TemporalWrapper<T>(entity, spec);
104}
105
106/**
107 * @brief Activates a node in the domain and channel(s) expressed by ctx.
108 *
109 * @code
110 * node >> Time(2.0) | Audio[0];
111 * node >> Time(2.0) | Audio[{0, 1}];
112 * @endcode
113 *
114 * @param wrapper TemporalWrapper carrying the node and duration.
115 * @param ctx CreationContext produced by DomainSpec or DomainSpec::operator[].
116 * @return Shared pointer to the activated node.
117 */
118MAYAFLUX_API std::shared_ptr<Nodes::Node> operator|(
119 const TemporalWrapper<Nodes::Node>& wrapper,
120 const CreationContext& ctx);
121
122/**
123 * @brief Activates a buffer in the domain and channel expressed by ctx.
124 *
125 * @code
126 * buffer >> Time(2.0) | Audio[0];
127 * @endcode
128 *
129 * @param wrapper TemporalWrapper carrying the buffer and duration.
130 * @param ctx CreationContext produced by DomainSpec or DomainSpec::operator[].
131 * @return Shared pointer to the activated buffer.
132 */
133MAYAFLUX_API std::shared_ptr<Buffers::Buffer> operator|(
134 const TemporalWrapper<Buffers::Buffer>& wrapper,
135 const CreationContext& ctx);
136
137/**
138 * @brief Activates a node network in the domain and channel(s) expressed by ctx.
139 *
140 * @code
141 * network >> Time(2.0) | Audio[{0, 1}];
142 * network >> Time(2.0) | Graphics;
143 * @endcode
144 *
145 * @param wrapper TemporalWrapper carrying the network and duration.
146 * @param ctx CreationContext produced by DomainSpec or DomainSpec::operator[].
147 * @return Shared pointer to the activated network.
148 */
149MAYAFLUX_API std::shared_ptr<Nodes::Network::NodeNetwork> operator|(
150 const TemporalWrapper<Nodes::Network::NodeNetwork>& wrapper,
151 const CreationContext& ctx);
152
153} // namespace MayaFlux
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:342
TimeSpec Time(double seconds)
Definition Temporal.cpp:9
std::shared_ptr< T > operator|(std::shared_ptr< T > obj, const CreationContext &ctx)
Definition Creator.hpp:259
Main namespace for the Maya Flux audio engine.
Definition Runtime.cpp:12