MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
NodeChain.hpp
Go to the documentation of this file.
1#pragma once
2
4
6
7namespace MayaFlux::Nodes {
8
9class NodeGraphManager;
10
11/**
12 * @class ChainNode
13 * @brief Connects an ordered sequence of nodes in series
14 *
15 * ChainNode represents a sequential processing chain of arbitrary length.
16 * The output of each node becomes the input to the next.
17 *
18 * When a NodeGraphManager is provided at construction, initialize() handles
19 * registration and semantics application through the manager directly.
20 * Without a manager, initialize() only sets internal state, suitable for
21 * manual processing or Lila contexts that manage their own graph.
22 *
23 * The >> operator (defined in API/Graph.hpp) detects existing ChainNodes
24 * and appends to the sequence rather than creating nested chains.
25 */
26class MAYAFLUX_API ChainNode : public Node, public std::enable_shared_from_this<ChainNode> {
27public:
28 /**
29 * @brief Creates a chain from an ordered sequence of nodes
30 * @param nodes Ordered sequence of nodes to process in series
31 * @param manager Graph manager for registration (nullptr for unmanaged)
32 * @param token Processing domain for registration (default AUDIO_RATE)
33 */
34 explicit ChainNode(std::vector<std::shared_ptr<Node>> nodes);
35
36 /**
37 * @brief Creates a chain from an ordered sequence of nodes (managed)
38 * @param nodes Ordered sequence of nodes to process in series
39 * @param manager Graph manager for registration
40 * @param token Processing domain for registration (default AUDIO_RATE)
41 */
43 std::vector<std::shared_ptr<Node>> nodes,
44 NodeGraphManager& manager,
45 ProcessingToken token = ProcessingToken::AUDIO_RATE);
46
47 /**
48 * @brief Creates a chain from two nodes (unmanaged)
49 * @param source The upstream node
50 * @param target The downstream node
51 */
53 const std::shared_ptr<Node>& source,
54 const std::shared_ptr<Node>& target);
55
56 /**
57 * @brief Creates a chain from two nodes (managed)
58 * @param source The upstream node
59 * @param target The downstream node
60 * @param manager Graph manager for registration
61 * @param token Processing domain for registration (default AUDIO_RATE)
62 */
64 const std::shared_ptr<Node>& source,
65 const std::shared_ptr<Node>& target,
66 NodeGraphManager& manager,
67 ProcessingToken token = ProcessingToken::AUDIO_RATE);
68
69 /**
70 * @brief Appends a node to the end of the chain
71 * @param node Node to append
72 */
73 void append(const std::shared_ptr<Node>& node);
74
75 /**
76 * @brief Appends all nodes from another chain
77 * @param other Chain whose nodes will be moved into this one
78 */
79 void append_chain(const std::shared_ptr<ChainNode>& other);
80
81 /**
82 * @brief Returns the number of nodes in the chain
83 */
84 [[nodiscard]] size_t size() const { return m_nodes.size(); }
85
86 /**
87 * @brief Returns a const reference to the internal node sequence
88 */
89 [[nodiscard]] const std::vector<std::shared_ptr<Node>>& nodes() const { return m_nodes; }
90
91 /**
92 * @brief Initializes the chain node
93 *
94 * If a manager was provided at construction, registers this chain and
95 * applies chain semantics (REPLACE_TARGET, ONLY_CHAIN, PRESERVE_BOTH).
96 * Without a manager, only sets internal initialization flag.
97 */
98 void initialize();
99
100 double process_sample(double input = 0.) override;
101 std::vector<double> process_batch(unsigned int num_samples) override;
102
103 inline void on_tick(const NodeHook& callback) override
104 {
105 if (!m_nodes.empty())
106 m_nodes.back()->on_tick(callback);
107 }
108
109 inline void on_tick_if(const NodeCondition& condition, const NodeHook& callback) override
110 {
111 if (!m_nodes.empty())
112 m_nodes.back()->on_tick_if(condition, callback);
113 }
114
115 inline void remove_all_hooks() override
116 {
117 if (!m_nodes.empty())
118 m_nodes.back()->remove_all_hooks();
119 }
120
121 void reset_processed_state() override;
122 NodeContext& get_last_context() override;
123 void save_state() override;
124 void restore_state() override;
125
126protected:
127 inline void notify_tick(double) override { }
128 inline void update_context(double) override { }
129
130private:
131 std::vector<std::shared_ptr<Node>> m_nodes;
132 NodeGraphManager* m_manager {};
133 ProcessingToken m_token { ProcessingToken::AUDIO_RATE };
134 bool m_is_initialized {};
135 bool m_state_saved {};
136
137public:
138 bool is_initialized() const;
139};
140
141}
std::vector< std::shared_ptr< Node > > m_nodes
void update_context(double) override
Updates the context object with the current node state.
void remove_all_hooks() override
Removes all registered callbacks.
void on_tick(const NodeHook &callback) override
Registers a callback to be called on each tick.
void on_tick_if(const NodeCondition &condition, const NodeHook &callback) override
Registers a conditional callback.
void notify_tick(double) override
Notifies all registered callbacks with the current context.
const std::vector< std::shared_ptr< Node > > & nodes() const
Returns a const reference to the internal node sequence.
Definition NodeChain.hpp:89
size_t size() const
Returns the number of nodes in the chain.
Definition NodeChain.hpp:84
Connects an ordered sequence of nodes in series.
Definition NodeChain.hpp:26
Base context class for node callbacks.
Definition Node.hpp:30
Central manager for the computational processing node graph.
Base interface for all computational processing nodes.
Definition Node.hpp:109
void initialize()
Definition main.cpp:11
std::function< void(NodeContext &)> NodeHook
Callback function type for node processing events.
Definition NodeUtils.hpp:25
ProcessingToken
Enumerates the different processing domains for nodes.
std::function< bool(NodeContext &)> NodeCondition
Predicate function type for conditional callbacks.
Definition NodeUtils.hpp:43
Contains the node-based computational processing system components.
Definition Chronie.hpp:11
bool is_initialized()
Checks if the default engine has been initialized.
Definition Core.cpp:50