MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
NodeStructure.cpp
Go to the documentation of this file.
1#include "NodeStructure.hpp"
4
5namespace MayaFlux::Nodes {
6
7ChainNode::ChainNode(const std::shared_ptr<Node>& source, const std::shared_ptr<Node>& target)
8 : m_Source(source)
9 , m_Target(target)
10 , m_is_initialized(false)
11{
12}
13
15{
16 if (!m_is_initialized) {
17 auto self = shared_from_this();
18
19 if (m_Target) {
20 for (auto& channel : get_active_channels(m_Target, 0)) {
21 MayaFlux::register_audio_node(self, channel);
22 }
23 } else {
25 }
26 m_is_initialized = true;
27 }
28
30 switch (semantics) {
32 if (m_Target) {
33 for (auto& channel : get_active_channels(m_Target, 0)) {
35 }
36 }
37 break;
39 if (m_Source) {
40 for (auto& channel : get_active_channels(m_Source, 0)) {
42 }
43 }
44 if (m_Target) {
45 for (auto& channel : get_active_channels(m_Target, 0)) {
47 }
48 }
49 break;
51 default:
52 break;
53 }
54}
55
56double ChainNode::process_sample(double input)
57{
58 if (!m_Source || !m_Target) {
59 return input;
60 }
61 if (!is_initialized())
62 initialize();
63
64 atomic_inc_modulator_count(m_Source->m_modulator_count, 1);
65 atomic_inc_modulator_count(m_Target->m_modulator_count, 1);
66
67 m_last_output = input;
68
69 uint32_t sstate = m_Source->m_state.load();
70 if (sstate & Utils::NodeState::PROCESSED) {
71 m_last_output = input + m_Source->get_last_output();
72 } else {
73 m_last_output = m_Source->process_sample(input);
75 }
76
77 uint32_t tstate = m_Target->m_state.load();
78 if (tstate & Utils::NodeState::PROCESSED) {
79 m_last_output += m_Target->get_last_output();
80 } else {
81 m_last_output = m_Target->process_sample(m_last_output);
82 tstate = tstate | Utils::NodeState::PROCESSED;
84 }
85
86 atomic_dec_modulator_count(m_Source->m_modulator_count, 1);
87 atomic_dec_modulator_count(m_Target->m_modulator_count, 1);
88
91
92 return m_last_output;
93}
94
95std::vector<double> ChainNode::process_batch(unsigned int num_samples)
96{
97 std::vector<double> output(num_samples);
98 for (size_t i = 0; i < num_samples; i++) {
99 output[i] = process_sample(0.F);
100 }
101 return output;
102}
103
105{
107 if (m_Source)
108 m_Source->reset_processed_state();
109 if (m_Target)
110 m_Target->reset_processed_state();
111}
112
114{
115 if (m_Source)
116 m_Source->save_state();
117 if (m_Target)
118 m_Target->save_state();
119
120 m_state_saved = true;
121}
122
124{
125 if (m_Source)
126 m_Source->restore_state();
127 if (m_Target)
128 m_Target->restore_state();
129
130 m_state_saved = false;
131}
132
134{
135 auto sState = m_Source->m_state.load();
136 auto tState = m_Target->m_state.load();
137
138 bool is_source_registered = m_Source ? (sState & Utils::NodeState::ACTIVE) : false;
139 bool is_target_registered = m_Target ? (tState & Utils::NodeState::ACTIVE) : false;
140 return !is_source_registered && !is_target_registered && (m_state.load() & Utils::NodeState::ACTIVE);
141}
142
143BinaryOpContext::BinaryOpContext(double value, double lhs_value, double rhs_value)
144 : NodeContext(value, typeid(BinaryOpContext).name())
145 , lhs_value(lhs_value)
146 , rhs_value(rhs_value)
147{
148}
149
150BinaryOpContextGpu::BinaryOpContextGpu(double value, double lhs_value, double rhs_value, std::span<const float> gpu_data)
151 : BinaryOpContext(value, lhs_value, rhs_value)
152 , GpuVectorData(gpu_data)
153{
154 type_id = typeid(BinaryOpContextGpu).name();
155}
156
157BinaryOpNode::BinaryOpNode(const std::shared_ptr<Node>& lhs, const std::shared_ptr<Node>& rhs, CombineFunc func)
158 : m_lhs(lhs)
159 , m_rhs(rhs)
160 , m_func(std::move(func))
161{
162}
163
165{
166 if (!m_is_initialized) {
167 auto self = shared_from_this();
168 uint32_t lhs_mask = m_lhs ? m_lhs->get_channel_mask().load() : 0;
169 uint32_t rhs_mask = m_rhs ? m_rhs->get_channel_mask().load() : 0;
170 uint32_t combined_mask = lhs_mask | rhs_mask;
171
172 if (combined_mask != 0) {
173 for (auto& channel : get_active_channels(combined_mask, 0)) {
174 MayaFlux::register_audio_node(self, channel);
175 }
176 } else {
178 }
179 m_is_initialized = true;
180 }
181
183 switch (semantics) {
185 if (m_lhs) {
186 for (auto& channel : get_active_channels(m_lhs, 0)) {
188 }
189 }
190 if (m_rhs) {
191 for (auto& channel : get_active_channels(m_rhs, 0)) {
193 }
194 }
195 break;
197 default:
198 break;
199 }
200}
201
203{
204 if (!m_lhs || !m_rhs) {
205 return input;
206 }
207
208 if (!is_initialized())
209 initialize();
210
211 atomic_inc_modulator_count(m_lhs->m_modulator_count, 1);
212 atomic_inc_modulator_count(m_rhs->m_modulator_count, 1);
213
214 uint32_t lstate = m_lhs->m_state.load();
215 if (lstate & Utils::NodeState::PROCESSED) {
216 m_last_lhs_value = input + m_lhs->get_last_output();
217 } else {
218 m_last_lhs_value = m_lhs->process_sample(input);
220 }
221
222 uint32_t rstate = m_rhs->m_state.load();
223 if (rstate & Utils::NodeState::PROCESSED) {
224 m_last_rhs_value = input + m_rhs->get_last_output();
225 } else {
226 m_last_rhs_value = m_rhs->process_sample(input);
228 }
229
231
234
235 atomic_dec_modulator_count(m_lhs->m_modulator_count, 1);
236 atomic_dec_modulator_count(m_rhs->m_modulator_count, 1);
237
240
241 return m_last_output;
242}
243
244std::vector<double> BinaryOpNode::process_batch(unsigned int num_samples)
245{
246 std::vector<double> output(num_samples);
247 for (unsigned int i = 0; i < num_samples; ++i) {
248 output[i] = process_sample(0.0);
249 }
250 return output;
251}
252
254{
256 for (const auto& callback : m_callbacks) {
257 callback(*m_last_context);
258 }
259
260 for (const auto& [callback, condition] : m_conditional_callbacks) {
261 if (condition(*m_last_context)) {
262 callback(*m_last_context);
263 }
264 }
265}
266
268{
270 if (m_lhs)
271 m_lhs->reset_processed_state();
272 if (m_rhs)
273 m_rhs->reset_processed_state();
274}
275
277{
280
281 if (m_lhs)
282 m_lhs->save_state();
283 if (m_rhs)
284 m_rhs->save_state();
285
286 m_state_saved = true;
287}
288
290{
293
294 if (m_lhs)
295 m_lhs->restore_state();
296 if (m_rhs)
297 m_rhs->restore_state();
298
299 m_state_saved = false;
300}
301
302std::unique_ptr<NodeContext> BinaryOpNode::create_context(double value)
303{
304 if (m_gpu_compatible) {
305 return std::make_unique<BinaryOpContextGpu>(
306 value,
310 }
311
312 return std::make_unique<BinaryOpContext>(value, m_last_lhs_value, m_last_rhs_value);
313}
314
316{
317 auto lstate = m_lhs->m_state.load();
318 auto rstate = m_rhs->m_state.load();
319 bool is_lhs_registered = m_lhs ? (lstate & Utils::NodeState::ACTIVE) : false;
320 bool is_rhs_registered = m_rhs ? (rstate & Utils::NodeState::ACTIVE) : false;
321 return !is_lhs_registered && !is_rhs_registered;
322}
323}
BinaryOpContextGpu(double value, double lhs_value, double rhs_value, std::span< const float > gpu_data)
GPU-compatible context for binary operation callbacks.
BinaryOpContext(double value, double lhs_value, double rhs_value)
Constructs a BinaryOpContext with the current operation state.
Specialized context for binary operation callbacks.
bool m_is_initialized
Flag indicating whether the binary operator has been properly initialized.
std::unique_ptr< NodeContext > create_context(double value) override
Creates a context object for callbacks.
void restore_state() override
Restores the node's state from the last save Recursively cascades through all connected modulator nod...
void initialize()
Initializes the binary operation node.
void notify_tick(double value) override
Notifies all registered callbacks about a new output value.
std::shared_ptr< Node > m_lhs
The left-hand side node.
std::function< double(double, double)> CombineFunc
Function type for combining two node outputs.
void reset_processed_state() override
Resets the processed state of the node and any attached input nodes.
CombineFunc m_func
The function used to combine the outputs of both nodes.
double process_sample(double input=0.) override
Processes a single sample through both nodes and combines the results.
void save_state() override
Saves the node's current state for later restoration Recursively cascades through all connected modul...
std::vector< double > process_batch(unsigned int num_samples) override
Processes multiple samples through both nodes and combines the results.
double m_last_rhs_value
The last output value from the right-hand side node.
double m_last_lhs_value
The last output value from the left-hand side node.
BinaryOpNode(const std::shared_ptr< Node > &lhs, const std::shared_ptr< Node > &rhs, CombineFunc func)
Creates a new binary operation node.
std::shared_ptr< Node > m_rhs
The right-hand side node.
std::shared_ptr< Node > m_Source
The upstream node that processes input first.
std::shared_ptr< Node > m_Target
The downstream node that processes the source's output.
void initialize()
Initializes the chain node.
std::vector< double > process_batch(unsigned int num_samples) override
Processes multiple samples through the chain.
bool m_is_initialized
Flag indicating whether the chain has been properly initialized.
double process_sample(double input=0.) override
Processes a single sample through the chain.
void restore_state() override
Restores the node's state from the last save Recursively cascades through all connected modulator nod...
void save_state() override
Saves the node's current state for later restoration Recursively cascades through all connected modul...
void reset_processed_state() override
Resets the processed state of the node and any attached input nodes.
ChainNode(const std::shared_ptr< Node > &source, const std::shared_ptr< Node > &target)
Creates a new chain connecting source to target.
GPU-uploadable 1D array data interface.
std::string type_id
Type identifier for runtime type checking.
Definition Node.hpp:48
Base context class for node callbacks.
Definition Node.hpp:30
std::vector< NodeHook > m_callbacks
Collection of standard callback functions.
Definition Node.hpp:416
double m_last_output
The most recent sample value generated by this oscillator.
Definition Node.hpp:378
bool m_fire_events_during_snapshot
Internal flag controlling whether notify_tick fires during state snapshots Default: false (events don...
Definition Node.hpp:448
std::vector< std::pair< NodeHook, NodeCondition > > m_conditional_callbacks
Collection of conditional callback functions with their predicates.
Definition Node.hpp:426
std::atomic< Utils::NodeState > m_state
Atomic state flag tracking the node's processing status.
Definition Node.hpp:463
bool m_gpu_compatible
Flag indicating if the node supports GPU processing This flag is set by derived classes to indicate w...
Definition Node.hpp:387
std::unique_ptr< NodeContext > m_last_context
The last context object created for callbacks.
Definition Node.hpp:396
std::span< const float > get_gpu_data_buffer() const
Provides access to the GPU data buffer.
Definition Node.cpp:78
GraphConfig & get_graph_config()
Definition Config.cpp:42
void atomic_add_flag(std::atomic< Utils::NodeState > &state, Utils::NodeState flag)
Atomically adds a flag to a node state.
Definition NodeUtils.cpp:96
std::vector< uint32_t > get_active_channels(const std::shared_ptr< Nodes::Node > &node, uint32_t fallback_channel)
Extracts active channel list from a node's channel mask.
void try_reset_processed_state(std::shared_ptr< Node > node)
Attempts to reset the processed state of a node.
void atomic_inc_modulator_count(std::atomic< uint32_t > &count, int amount)
Atomically increments the modulator count by a specified amount.
void atomic_dec_modulator_count(std::atomic< uint32_t > &count, int amount)
Atomically decrements the modulator count by a specified amount.
void atomic_remove_flag(std::atomic< Utils::NodeState > &state, Utils::NodeState flag)
Atomically removes a flag from a node state.
Contains the node-based computational processing system components.
Definition Chronie.hpp:5
@ PRESERVE_BOTH
Preserve both nodes in the chain, add new chain node to root, i.e doubling the target signal.
Definition Utils.hpp:47
@ REPLACE_TARGET
Unregister the target and register with the new chain node.
Definition Utils.hpp:46
@ ONLY_CHAIN
Only keep the new chain node, unregistering the source and target.
Definition Utils.hpp:48
@ KEEP
Preserve both nodes in the binary op, add new binary op node to root, i.e doubling the signal.
Definition Utils.hpp:57
@ REPLACE
Unregister both nodes and register with the new binary op node.
Definition Utils.hpp:56
@ ACTIVE
Engine is processing this node.
Definition Utils.hpp:30
@ PROCESSED
Node has been processed this cycle.
Definition Utils.hpp:34
void register_audio_node(const std::shared_ptr< Nodes::Node > &node, uint32_t channel)
Adds a node to the root node of a specific channel.
Definition Graph.cpp:23
void unregister_audio_node(const std::shared_ptr< Nodes::Node > &node, uint32_t channel)
Removes a node from the root node of a specific channel.
Definition Graph.cpp:40
Utils::NodeBinaryOpSemantics binary_op_semantics
Definition Config.hpp:31
Utils::NodeChainSemantics chain_semantics
Definition Config.hpp:30