MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
NodeUtils.cpp
Go to the documentation of this file.
1#include "NodeUtils.hpp"
2
3#include "Node.hpp"
4
5namespace MayaFlux::Nodes {
6
7bool conditional_callback_exists(const std::vector<std::pair<NodeHook, NodeCondition>>& callbacks, const NodeCondition& callback)
8{
9 return std::ranges::any_of(callbacks,
10 [&callback](const std::pair<NodeHook, NodeCondition>& pair) {
11 return pair.second.target_type() == callback.target_type();
12 });
13}
14
15bool callback_pair_exists(const std::vector<std::pair<NodeHook, NodeCondition>>& callbacks, const NodeHook& callback, const NodeCondition& condition)
16{
17 return std::ranges::any_of(callbacks,
18 [&callback, &condition](const std::pair<NodeHook, NodeCondition>& pair) {
19 return pair.first.target_type() == callback.target_type() && pair.second.target_type() == condition.target_type();
20 });
21}
22
23bool safe_add_conditional_callback(std::vector<std::pair<NodeHook, NodeCondition>>& callbacks, const NodeHook& callback, const NodeCondition& condition)
24{
25 if (!callback_pair_exists(callbacks, callback, condition)) {
26 callbacks.emplace_back(callback, condition);
27 return true;
28 }
29 return false;
30}
31
32bool safe_remove_conditional_callback(std::vector<std::pair<NodeHook, NodeCondition>>& callbacks, const NodeCondition& callback)
33{
34 bool removed = false;
35 auto it = callbacks.begin();
36
37 while (it != callbacks.end()) {
38 if (it->second.target_type() == callback.target_type()) {
39 it = callbacks.erase(it);
40 removed = true;
41 } else {
42 ++it;
43 }
44 }
45
46 return removed;
47}
48
49void atomic_set_strong(std::atomic<NodeState>& flag, NodeState& expected, const NodeState& desired)
50{
51 flag.compare_exchange_strong(expected, desired);
52};
53
54void atomic_set_flag_strong(std::atomic<NodeState>& flag, const NodeState& desired)
55{
56 auto expected = flag.load();
57 flag.compare_exchange_strong(expected, desired);
58};
59
60void atomic_add_flag(std::atomic<NodeState>& state, NodeState flag)
61{
62 auto current = state.load();
63 NodeState desired;
64 do {
65 desired = static_cast<NodeState>(current | flag);
66 } while (!state.compare_exchange_weak(current, desired,
67 std::memory_order_acq_rel,
68 std::memory_order_acquire));
69}
70
71void atomic_remove_flag(std::atomic<NodeState>& state, NodeState flag)
72{
73 auto current = state.load();
74 NodeState desired;
75 do {
76 desired = static_cast<NodeState>(current & ~flag);
77 } while (!state.compare_exchange_weak(current, desired,
78 std::memory_order_acq_rel,
79 std::memory_order_acquire));
80}
81
82void atomic_set_flag_weak(std::atomic<NodeState>& flag, NodeState& expected, const NodeState& desired)
83{
84 flag.compare_exchange_weak(expected, desired);
85};
86
87void atomic_inc_modulator_count(std::atomic<uint32_t>& count, int amount)
88{
89 count.fetch_add(amount, std::memory_order_relaxed);
90}
91
92void atomic_dec_modulator_count(std::atomic<uint32_t>& count, int amount)
93{
94 count.fetch_sub(amount, std::memory_order_relaxed);
95}
96
97void try_reset_processed_state(std::shared_ptr<Node> node)
98{
99 if (node && node->m_modulator_count.load(std::memory_order_relaxed) == 0) {
100 node->reset_processed_state();
101 }
102}
103
104std::vector<uint32_t> get_active_channels(const std::shared_ptr<Nodes::Node>& node, uint32_t fallback_channel)
105{
106 uint32_t channel_mask = node ? node->get_channel_mask().load() : 0;
107 return get_active_channels(channel_mask, fallback_channel);
108}
109
110std::vector<uint32_t> get_active_channels(uint32_t channel_mask, uint32_t fallback_channel)
111{
112 std::vector<uint32_t> channels;
113
114 if (channel_mask == 0) {
115 channels.push_back(fallback_channel);
116 } else {
117 for (uint32_t channel = 0; channel < MAX_CHANNEL_COUNT; ++channel) {
118 if (channel_mask & (1ULL << channel)) {
119 channels.push_back(channel);
120 }
121 }
122 }
123
124 return channels;
125}
126
128{
129 state.cycles_elapsed++;
130
131 double progress = 0.;
132 if (state.fade_cycles > 0) {
133 progress = std::min<double>(1.0, static_cast<double>(state.cycles_elapsed) / state.fade_cycles);
134 } else {
135 for (uint32_t ch = 0; ch < 32; ++ch) {
136 bool will_be_in = state.to_channels & (1 << ch);
137 state.amount[ch] = will_be_in ? 1.0 : 0.0;
138 }
139 state.phase = RoutingState::NONE;
140 return;
141 }
142
143 for (uint32_t ch = 0; ch < 32; ch++) {
144 bool was_in = state.from_channels & (1 << ch);
145 bool will_be_in = state.to_channels & (1 << ch);
146
147 if (was_in && will_be_in) {
148 state.amount[ch] = 1.0;
149 } else if (was_in && !will_be_in) {
150 state.amount[ch] = 1.0 - progress;
151 } else if (!was_in && will_be_in) {
152 state.amount[ch] = progress;
153 } else {
154 state.amount[ch] = 0.0;
155 }
156 }
157
158 if (state.cycles_elapsed >= state.fade_cycles) {
159 state.phase = RoutingState::COMPLETED;
160 }
161}
162
163}
Eigen::Index count
uint32_t channel
TypedHook<> NodeHook
Alias for TypedHook<NodeContext>.
Definition NodeUtils.hpp:38
NodeState
Represents the processing state of a node in the audio graph.
Definition NodeSpec.hpp:43
bool callback_pair_exists(const std::vector< std::pair< NodeHook, NodeCondition > > &callbacks, const NodeHook &callback, const NodeCondition &condition)
Returns true if the exact callback+condition pair is already present.
Definition NodeUtils.cpp:15
void atomic_add_flag(std::atomic< NodeState > &state, NodeState flag)
Atomically adds a flag to a node state.
Definition NodeUtils.cpp:60
bool safe_remove_conditional_callback(std::vector< std::pair< NodeHook, NodeCondition > > &callbacks, const NodeCondition &callback)
Removes all conditional callbacks whose condition target_type() matches.
Definition NodeUtils.cpp:32
constexpr uint32_t MAX_CHANNEL_COUNT
Hard limit imposed by uint32_t channel mask width.
Definition NodeSpec.hpp:5
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 atomic_set_strong(std::atomic< NodeState > &flag, NodeState &expected, const NodeState &desired)
Atomically sets a node state flag with strong memory ordering.
Definition NodeUtils.cpp:49
bool conditional_callback_exists(const std::vector< std::pair< NodeHook, NodeCondition > > &callbacks, const NodeCondition &callback)
Returns true if a condition function is already present in a conditional callback collection.
Definition NodeUtils.cpp:7
void try_reset_processed_state(std::shared_ptr< Node > node)
Attempts to reset the processed state of a node.
Definition NodeUtils.cpp:97
void atomic_inc_modulator_count(std::atomic< uint32_t > &count, int amount)
Atomically increments the modulator count by a specified amount.
Definition NodeUtils.cpp:87
void atomic_remove_flag(std::atomic< NodeState > &state, NodeState flag)
Atomically removes a flag from a node state.
Definition NodeUtils.cpp:71
void atomic_set_flag_strong(std::atomic< NodeState > &flag, const NodeState &desired)
Atomically sets a node state flag to a specific value.
Definition NodeUtils.cpp:54
std::function< bool(NodeContext &)> NodeCondition
Predicate function type for conditional callbacks.
Definition NodeUtils.hpp:54
void atomic_dec_modulator_count(std::atomic< uint32_t > &count, int amount)
Atomically decrements the modulator count by a specified amount.
Definition NodeUtils.cpp:92
bool safe_add_conditional_callback(std::vector< std::pair< NodeHook, NodeCondition > > &callbacks, const NodeHook &callback, const NodeCondition &condition)
Adds a conditional callback if the exact pair is not already present.
Definition NodeUtils.cpp:23
void atomic_set_flag_weak(std::atomic< NodeState > &flag, NodeState &expected, const NodeState &desired)
Atomically sets a node state flag with weak memory ordering.
Definition NodeUtils.cpp:82
void update_routing_state(RoutingState &state)
Updates the routing state for a node based on its current channel usage.
Contains the node-based computational processing system components.
Definition Chronie.hpp:13
enum MayaFlux::Nodes::RoutingState::Phase NONE
@ COMPLETED
Routing transition has completed.
Definition NodeSpec.hpp:82
Represents the state of routing transitions for a node.
Definition NodeSpec.hpp:64