MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
NodeNetwork.cpp
Go to the documentation of this file.
1#include "NodeNetwork.hpp"
2
4
6
8{
9 if (!m_initialized) {
10 initialize();
11 m_initialized = true;
12 }
13}
14
15std::optional<std::vector<double>> NodeNetwork::get_audio_buffer() const
16{
17 if (
20 && !m_last_audio_buffer.empty()) {
22 }
23 return std::nullopt;
24}
25
27{
28 if (m_output_scale == 1.0)
29 return;
30
31 for (auto& s : m_last_audio_buffer)
32 s *= m_output_scale;
33}
34
35[[nodiscard]] std::unordered_map<std::string, std::string>
37{
38 return { { "topology", topology_to_string(m_topology) },
39 { "output_mode", output_mode_to_string(m_output_mode) },
40 { "node_count", std::to_string(get_node_count()) },
41 { "enabled", m_enabled ? "true" : "false" } };
42}
43
44void NodeNetwork::map_parameter(const std::string& param_name,
45 const std::shared_ptr<Node>& source,
46 MappingMode mode)
47{
48 m_parameter_mappings.push_back({ param_name, mode, source, nullptr });
49}
50
51void NodeNetwork::map_parameter(const std::string& param_name,
52 const std::shared_ptr<NodeNetwork>& source_network)
53{
54 m_parameter_mappings.push_back(
55 { param_name, MappingMode::ONE_TO_ONE, nullptr, source_network });
56}
57
58void NodeNetwork::unmap_parameter(const std::string& param_name)
59{
60 std::erase_if(m_parameter_mappings,
61 [&](const auto& m) { return m.param_name == param_name; });
62}
63
65{
66 return m_processing_state.load(std::memory_order_acquire);
67}
68
69void NodeNetwork::add_channel_usage(uint32_t channel_id)
70{
71 if (channel_id < 32) {
72 m_channel_mask |= (1U << channel_id);
73 }
74}
75
76void NodeNetwork::remove_channel_usage(uint32_t channel_id)
77{
78 if (channel_id < 32) {
79 m_channel_mask &= ~(1U << channel_id);
80 }
81}
82
83bool NodeNetwork::is_registered_on_channel(uint32_t channel_id) const
84{
85 if (channel_id < 32) {
86 return (m_channel_mask & (1U << channel_id)) != 0;
87 }
88 return false;
89}
90
91std::vector<uint32_t> NodeNetwork::get_registered_channels() const
92{
93 std::vector<uint32_t> channels;
94 for (uint32_t i = 0; i < 32; ++i) {
95 if (m_channel_mask & (1U << i)) {
96 channels.push_back(i);
97 }
98 }
99 return channels;
100}
101
102void NodeNetwork::mark_processing(bool processing)
103{
104 m_processing_state.store(processing, std::memory_order_release);
105}
106
108{
109 return m_processed_this_cycle.load(std::memory_order_acquire);
110}
111
112void NodeNetwork::mark_processed(bool processed)
113{
114 m_processed_this_cycle.store(processed, std::memory_order_release);
115}
116
118{
119 if (channel_id >= 32)
120 return;
121
122 auto channel_bit = static_cast<uint32_t>(1ULL << channel_id);
123 uint32_t old_pending = m_pending_reset_mask.fetch_or(channel_bit,
124 std::memory_order_acq_rel);
125 uint32_t new_pending = old_pending | channel_bit;
126 uint32_t active_channels = m_channel_mask.load(std::memory_order_acquire);
127
128 if ((new_pending & active_channels) == active_channels && active_channels != 0) {
129 uint32_t expected = new_pending;
130 if (m_pending_reset_mask.compare_exchange_strong(expected, 0,
131 std::memory_order_acq_rel)) {
132 mark_processed(false);
133 }
134 }
135}
136
138{
139 return std::string(MayaFlux::Reflect::enum_to_string(topo));
140}
141
143{
144 return std::string(MayaFlux::Reflect::enum_to_string(mode));
145}
146
147} // namespace MayaFlux::Nodes::Network
std::vector< ParameterMapping > m_parameter_mappings
virtual void unmap_parameter(const std::string &param_name)
Remove parameter mapping.
bool is_processed_this_cycle() const
Check if network has been processed this cycle (lock-free)
std::atomic< bool > m_processing_state
Per-channel processing state (lock-free atomic flags)
virtual void map_parameter(const std::string &param_name, const std::shared_ptr< Node > &source, MappingMode mode=MappingMode::BROADCAST)
Map external node output to network parameter.
void apply_output_scale()
Apply m_output_scale to m_last_audio_buffer.
std::vector< uint32_t > get_registered_channels() const
Get all channels this network is registered on.
virtual void initialize()
Called once before first process_batch()
void mark_processing(bool processing)
Mark network as processing or not (lock-free)
virtual std::optional< std::vector< double > > get_audio_buffer() const
Get cached audio buffer from last process_batch()
static std::string topology_to_string(Topology topo)
void add_channel_usage(uint32_t channel_id)
Register network usage on a specific channel.
void remove_channel_usage(uint32_t channel_id)
Unregister network from a specific channel.
void mark_processed(bool processed)
Mark network as processed this cycle (lock-free)
double m_output_scale
Post-processing scalar applied to m_last_audio_buffer each batch.
bool is_registered_on_channel(uint32_t channel_id) const
Check if network is registered on a channel.
bool is_processing() const
Check if network is currently processing (lock-free)
std::atomic< uint32_t > m_channel_mask
Bitfield of channels this network is registered on.
void ensure_initialized()
Ensure initialize() is called exactly once.
void request_reset_from_channel(uint32_t channel_id)
Request a reset from a specific channel.
static std::string output_mode_to_string(OutputMode mode)
virtual std::unordered_map< std::string, std::string > get_metadata() const
Get network metadata for debugging/visualization.
std::vector< double > m_last_audio_buffer
std::atomic< uint32_t > m_pending_reset_mask
virtual size_t get_node_count() const =0
Get the number of nodes in the network.
Topology
Defines the structural relationships between nodes in the network.
MappingMode
Defines how nodes map to external entities (e.g., audio channels, graphics objects)
@ ONE_TO_ONE
Node array/network → network nodes (must match count)
OutputMode
Defines how the network's computational results are exposed.
@ AUDIO_COMPUTE
processed each cycle but not sent to output
@ AUDIO_SINK
Aggregated audio samples sent to output.
constexpr std::string_view enum_to_string(EnumType value) noexcept
Universal enum to string converter using magic_enum (original case)