12 : m_registered_sample_rate(sample_rate)
13 , m_registered_block_size(block_size)
26 node->set_gpu_compatible(
true);
30 root.register_node(node);
40 root.unregister_node(node);
44 std::function<
void(std::span<RootNode*>)> processor)
51 static std::unordered_map<unsigned int, std::shared_ptr<RootNode>> audio_roots;
56 for (
const auto& [channel, root] : it->second) {
57 audio_roots[channel] = root;
67 if (!processing_ptr) {
68 processing_ptr = std::make_unique<std::atomic<bool>>(
false);
71 bool expected =
false;
72 return processing_ptr->compare_exchange_strong(
74 std::memory_order_acquire,
75 std::memory_order_relaxed);
86 it->second(std::span<RootNode*>(roots.data(), roots.size()));
96 for (
auto& network : it->second) {
97 if (!network || !network->is_enabled()) {
101 if (!network->is_processed_this_cycle()) {
102 network->mark_processing(
true);
103 network->process_batch(num_samples);
104 network->mark_processing(
false);
105 network->mark_processed(
true);
113 for (
auto* root : roots) {
114 root->process_batch(num_samples);
117 for (
auto* root : roots) {
118 root->process_batch_frame(num_samples);
129 std::vector<std::vector<double>> all_network_outputs;
133 for (
auto& network : audio_it->second) {
134 if (!network || !network->is_enabled()) {
138 if (!network->is_registered_on_channel(channel)) {
142 if (!network->is_processed_this_cycle()) {
143 network->mark_processing(
true);
144 network->process_batch(num_samples);
145 network->mark_processing(
false);
146 network->mark_processed(
true);
149 const auto& net_buffer = network->get_audio_buffer();
151 if (network->needs_channel_routing()) {
152 double scale = network->get_routing_state().amount[channel];
157 all_network_outputs.push_back(*net_buffer);
159 std::vector<double> scaled_buffer = *net_buffer;
160 for (
auto& sample : scaled_buffer)
163 all_network_outputs.push_back(std::move(scaled_buffer));
166 all_network_outputs.push_back(*net_buffer);
173 return all_network_outputs;
179 auto ch = channel.value_or(0U);
182 for (
auto& network : it->second) {
183 if (network && network->is_enabled()) {
184 network->mark_processed(
false);
190 it->second->store(
false, std::memory_order_release);
198 for (
auto& network : audio_it->second) {
200 if (network->is_registered_on_channel(channel)) {
201 network->request_reset_from_channel(channel);
221 unsigned int channel,
unsigned int num_samples)
230 return it->second(&root, num_samples);
233 std::vector<double> samples = root.process_batch(num_samples);
235 uint32_t normalize_coef = root.get_node_size();
236 for (
double& sample : samples) {
250 return it->second(&root, channel);
253 double sample = root.process_sample();
263 sample /= std::sqrt(
static_cast<double>(num_nodes));
265 const double threshold = 0.95;
266 const double knee = 0.1;
267 const double abs_sample = std::abs(sample);
269 if (abs_sample > threshold) {
270 const double excess = abs_sample - threshold;
271 const double compressed_excess = std::tanh(excess / knee) * knee;
272 const double limited_abs = threshold + compressed_excess;
273 sample = std::copysign(limited_abs, sample);
280 std::unordered_map<unsigned int, std::vector<double>> channel_data;
284 for (
unsigned int channel : channels) {
294 return static_cast<unsigned int>(channels.size());
299 std::vector<RootNode*> roots;
303 for (
auto& [channel, root] : it->second) {
304 roots.push_back(root.get());
327 m_token_roots[token][channel] = std::make_shared<RootNode>(token, channel);
333 for (uint32_t ch = 0; ch < num_channels; ++ch) {
341 std::stringstream ss;
342 ss <<
"node_" << node.get();
343 std::string generated_id = ss.str();
351 node->register_channel_usage(channel_id);
357 if (pair.second == node) {
367 node->unregister_channel_usage(channel_id);
372 std::vector<ProcessingToken> tokens;
374 if (!channels.empty()) {
375 tokens.push_back(token);
383 std::vector<unsigned int> channels;
386 for (
const auto& [channel, root] : it->second) {
387 channels.push_back(channel);
398 for (
const auto& [channel, root] : it->second) {
399 count += root->get_node_size();
409 "=== NodeGraphManager Summary ===");
417 "Token {}: {} nodes across {} channels",
418 static_cast<int>(token), total_nodes, channels.size());
420 for (
auto channel : channels) {
426 " Channel {}: {} nodes, {} networks",
427 channel, root.get_node_size(), networks.size());
429 for (
const auto& network : networks) {
433 " Network: {} internal nodes, mode={}, enabled={}",
434 network->get_node_count(),
435 static_cast<int>(network->get_output_mode()),
436 network->is_enabled());
456 [&node](
const auto& pair) {
return pair.second == node; });
464 if (source && target) {
465 auto chain = std::make_shared<ChainNode>(source, target, *
this);
483 network->set_enabled(
true);
487 uint32_t channel_mask = network->get_channel_mask();
489 if (channel_mask == 0) {
491 network->add_channel_usage(0);
494 auto channels = network->get_registered_channels();
497 for (
auto ch : channels) {
501 "Added audio network to token {} channel {}: {} nodes",
502 static_cast<int>(token), ch, network->get_node_count());
510 "Added network to token {}: {} nodes, mode={}",
511 static_cast<int>(token),
512 network->get_node_count(),
513 static_cast<int>(network->get_output_mode()));
528 auto& networks = token_it->second;
529 std::erase_if(networks, [&](
const auto& n) {
return n == network; });
534 auto& networks = it->second;
535 std::erase_if(networks, [&](
const auto& n) {
return n == network; });
542std::vector<std::shared_ptr<Network::NodeNetwork>>
547 std::vector<std::shared_ptr<Network::NodeNetwork>> networks_on_channel;
548 for (
const auto& network : token_it->second) {
549 if (network && network->is_registered_on_channel(channel)) {
550 networks_on_channel.push_back(network);
553 return networks_on_channel;
558std::vector<std::shared_ptr<Network::NodeNetwork>>
561 std::vector<std::shared_ptr<Network::NodeNetwork>> all_networks;
565 all_networks.insert(all_networks.end(),
566 audio_it->second.begin(),
567 audio_it->second.end());
572 all_networks.insert(all_networks.end(),
573 token_it->second.begin(),
574 token_it->second.end());
586 count += audio_it->second.size();
591 count += token_it->second.size();
606 std::stringstream ss;
607 ss <<
"network_" << network.get();
608 std::string generated_id = ss.str();
618 if (pair.second == network) {
628 [&network](
const auto& pair) {
return pair.second == network; });
637 for (
auto& network : networks) {
645 for (
auto& network : networks) {
656 for (
auto* root : roots) {
657 root->terminate_all_nodes();
676 if (!node->needs_channel_routing())
682 if (!network || !network->needs_channel_routing())
689 const std::shared_ptr<Node>& node,
690 const std::vector<uint32_t>& target_channels,
691 uint32_t fade_cycles,
694 uint32_t current_channels = node->get_channel_mask();
696 uint32_t target_bitmask = 0;
697 for (
auto ch : target_channels) {
698 target_bitmask |= (1 << ch);
702 fade_blocks = std::max(1U, fade_blocks);
710 for (uint32_t ch = 0; ch < 32; ch++) {
711 state.
amount[ch] = (current_channels & (1 << ch)) ? 1.0 : 0.0;
714 node->get_routing_state() = state;
716 for (
auto ch : target_channels) {
717 if (!(current_channels & (1 << ch))) {
724 const std::shared_ptr<Network::NodeNetwork>& network,
725 const std::vector<uint32_t>& target_channels,
726 uint32_t fade_cycles,
732 "Attempted to route network that is not an audio sink. Operation ignored.");
737 network->set_enabled(
true);
740 if (std::ranges::find(networks, network) == networks.end()) {
741 networks.push_back(network);
744 uint32_t current_channels = network->get_channel_mask();
746 uint32_t target_bitmask = 0;
747 for (
auto ch : target_channels) {
748 target_bitmask |= (1 << ch);
751 uint32_t combined_mask = current_channels | target_bitmask;
752 network->set_channel_mask(combined_mask);
753 for (
auto ch : target_channels) {
754 network->add_channel_usage(ch);
759 fade_blocks = std::max(1u, fade_blocks);
767 for (uint32_t ch = 0; ch < 32; ch++) {
768 state.
amount[ch] = (current_channels & (1 << ch)) ? 1.0 : 0.0;
771 network->get_routing_state() = state;
776 std::vector<std::pair<std::shared_ptr<Node>, uint32_t>> nodes_to_remove;
779 if (!node->needs_channel_routing())
782 auto& state = node->get_routing_state();
785 for (uint32_t ch = 0; ch < 32; ch++) {
786 if ((state.from_channels & (1 << ch)) && !(state.to_channels & (1 << ch))) {
787 nodes_to_remove.emplace_back(node, ch);
794 for (
auto& [node, channel] : nodes_to_remove) {
798 std::vector<std::pair<std::shared_ptr<Network::NodeNetwork>, uint32_t>> networks_to_cleanup;
801 if (!network || !network->needs_channel_routing())
804 auto& state = network->get_routing_state();
807 network->set_channel_mask(state.to_channels);
809 for (uint32_t ch = 0; ch < 32; ch++) {
810 if ((state.from_channels & (1 << ch)) && !(state.to_channels & (1 << ch))) {
811 networks_to_cleanup.emplace_back(network, ch);
818 for (
auto& [network, channel] : networks_to_cleanup) {
819 network->remove_channel_usage(channel);
821 if (network->get_channel_mask() == 0) {
823 std::erase_if(networks, [&](
const auto& n) {
return n == network; });
#define MF_INFO(comp, ctx,...)
#define MF_PRINT(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
void register_token_processor(ProcessingToken token, std::function< void(std::span< RootNode * >)> processor)
Register subsystem processor for a specific token.
void update_routing_states_for_cycle(ProcessingToken token)
Updates routing states for all nodes and networks for a given token.
void remove_network(const std::shared_ptr< Network::NodeNetwork > &network, ProcessingToken token)
Remove a network from a processing token.
void register_token_sample_processor(ProcessingToken token, TokenSampleProcessor processor)
Register per-sample processor for a specific token.
void terminate_active_processing()
Terminates all active processing across all tokens and channels.
void clear_networks(ProcessingToken token)
Clear all networks from a token.
void print_summary() const
Prints a summary of all tokens, channels, and node counts.
const std::unordered_map< unsigned int, std::shared_ptr< RootNode > > & get_all_channel_root_nodes(ProcessingToken token=ProcessingToken::AUDIO_RATE) const
Gets all channel root nodes for the AUDIO_RATE domain.
NodeGraphManager(uint32_t sample_rate=48000, uint32_t block_size=512)
Creates a new NodeGraphManager.
std::vector< std::vector< double > > process_audio_networks(ProcessingToken token, uint32_t num_samples, uint32_t channel=0)
Process audio networks for a specific channel.
void route_node_to_channels(const std::shared_ptr< Node > &node, const std::vector< uint32_t > &target_channels, uint32_t fade_cycles, ProcessingToken token)
Routes a node's output to specific channels within a token domain.
std::unordered_map< ProcessingToken, std::vector< std::shared_ptr< Network::NodeNetwork > > > m_token_networks
Non-audio networks (token-level processing) For NONE, GRAPHICS_BIND, CUSTOM output modes.
std::atomic< bool > m_terminate_requested
Global termination flag.
std::unordered_map< ProcessingToken, std::function< void(std::span< RootNode * >)> > m_token_processors
Registered custom processors for each processing token.
void add_network(const std::shared_ptr< Network::NodeNetwork > &network, ProcessingToken token)
Add a network to a processing token.
size_t get_network_count(ProcessingToken token) const
Get count of networks for a token.
bool preprocess_networks(ProcessingToken token)
Preprocess networks for a specific token.
void normalize_sample(double &sample, uint32_t num_nodes)
Normalizes a sample to the range [-1, 1] based on the number of nodes.
size_t get_node_count(ProcessingToken token) const
Gets the total number of nodes registered under a given token.
void unregister_global(const std::shared_ptr< Node > &node)
Unregisters a node globally.
unsigned int get_channel_count(ProcessingToken token) const
Get the number of active channels for a specific token.
void unset_channel_mask(const std::shared_ptr< Node > &node, uint32_t channel_id)
Unsets the specified channel mask from a node's global registration.
std::unordered_map< std::string, std::shared_ptr< Network::NodeNetwork > > m_network_registry
Global network registry (like m_Node_registry)
void reset_audio_network_state(ProcessingToken token, uint32_t channel=0)
Resets the processing state of audio networks for a token and channel.
uint32_t m_registered_block_size
Block size for audio processing, used for normalizationbuffer.
void process_all_tokens(unsigned int num_samples=1)
Process all active tokens sequentially.
void register_global(const std::shared_ptr< Node > &node)
Registers a node globally if not already registered.
uint32_t m_registered_sample_rate
Sample rate for audio processing, used for normalization.
std::unordered_map< ProcessingToken, TokenSampleProcessor > m_token_sample_processors
Per-sample processors for each processing token.
std::unordered_map< std::string, std::shared_ptr< Node > > m_Node_registry
Registry of all nodes by their string identifiers.
std::vector< double > process_channel(ProcessingToken token, unsigned int channel, unsigned int num_samples)
Process a specific channel within a token domain.
void register_network_global(const std::shared_ptr< Network::NodeNetwork > &network)
Register network globally (like nodes)
void postprocess_networks(ProcessingToken token, std::optional< uint32_t > channel)
Postprocess networks for a specific token and channel.
void ensure_root_exists(ProcessingToken token, unsigned int channel)
Ensures a root node exists for the given token and channel.
double process_sample(ProcessingToken token, uint32_t channel)
Process a single sample for a specific channel.
void connect(const std::string &source_id, const std::string &target_id)
Connects two nodes by their string identifiers.
void set_channel_mask(const std::shared_ptr< Node > &node, uint32_t channel_id)
Adds the specified channel mask to a node's global registration.
void add_to_root(const std::shared_ptr< Node > &node, ProcessingToken token, unsigned int channel=0)
Add node to specific processing token and channel.
RootNode & get_root_node(ProcessingToken token, unsigned int channel)
Gets or creates the root node for a specific token and channel.
bool is_node_registered(const std::shared_ptr< Node > &node)
Checks if a node is registered with this manager.
void route_network_to_channels(const std::shared_ptr< Network::NodeNetwork > &network, const std::vector< uint32_t > &target_channels, uint32_t fade_cycles, ProcessingToken token)
Routes a network's output to specific channels within a token domain.
std::unordered_map< ProcessingToken, std::unordered_map< unsigned int, std::shared_ptr< RootNode > > > m_token_roots
Multi-modal map of processing tokens to their channel root nodes.
void cleanup_completed_routing(ProcessingToken token)
Cleans up completed routing transitions for a given token.
std::vector< std::shared_ptr< Network::NodeNetwork > > get_all_networks(ProcessingToken token) const
Get all networks for a specific token across all channels.
~NodeGraphManager()
Destroys the NodeGraphManager.
void remove_from_root(const std::shared_ptr< Node > &node, ProcessingToken token, unsigned int channel=0)
Remove node from a specific processing token and channel.
std::unordered_map< unsigned int, std::vector< double > > process_token_with_channel_data(ProcessingToken token, unsigned int num_samples)
Process all channels for a token and return channel-separated data.
std::unordered_map< ProcessingToken, std::vector< std::shared_ptr< Network::NodeNetwork > > > m_audio_networks
Audio-sink networks Only populated for networks with OutputMode::AUDIO_SINK.
std::shared_ptr< Node > get_node(const std::string &id)
Looks up a node by its string identifier.
void unregister_network_global(const std::shared_ptr< Network::NodeNetwork > &network)
Unregister network globally.
std::vector< ProcessingToken > get_active_tokens() const
Gets all currently active processing tokens (domains)
std::unordered_map< ProcessingToken, TokenChannelProcessor > m_token_channel_processors
Per-channel processors for each processing token.
void ensure_token_exists(ProcessingToken token, uint32_t num_channels=1)
Ensures that a processing token entry exists.
bool is_network_registered(const std::shared_ptr< Network::NodeNetwork > &network)
Check if network is registered globally.
std::unordered_map< ProcessingToken, std::unique_ptr< std::atomic< bool > > > m_token_network_processing
Processing flags for each token's networks.
std::vector< std::shared_ptr< Network::NodeNetwork > > get_networks(ProcessingToken token, uint32_t channel=0) const
Get all networks for a specific token.
std::vector< RootNode * > get_all_root_nodes(ProcessingToken token)
Get spans of root nodes for a token (for custom processing)
std::vector< unsigned int > get_all_channels(ProcessingToken token) const
Gets all channel indices for a given processing token.
void process_token(ProcessingToken token, unsigned int num_samples=1)
Process all nodes in a specific token domain Calls registered processor if available,...
void register_token_channel_processor(ProcessingToken token, TokenChannelProcessor processor)
Register per-channel processor for a specific token.
Central manager for the computational processing node graph.
Container for top-level nodes in a processing channel with multi-modal support.
@ NodeProcessing
Node graph processing (Nodes::NodeGraphManager)
@ Nodes
DSP Generator and Filter Nodes, graph pipeline, node management.
@ AUDIO_COMPUTE
processed each cycle but not sent to output
@ AUDIO_SINK
Aggregated audio samples sent to output.
ProcessingToken
Enumerates the different processing domains for nodes.
@ AUDIO_RATE
Nodes that process at the audio sample rate.
@ VISUAL_RATE
Nodes that process at the visual frame rate.
std::function< double(RootNode *, uint32_t)> TokenSampleProcessor
void update_routing_state(RoutingState &state)
Updates the routing state for a node based on its current channel usage.
std::function< std::vector< double >(RootNode *, uint32_t)> TokenChannelProcessor
Contains the node-based computational processing system components.
@ COMPLETED
Routing transition has completed.
@ ACTIVE
Currently in the fade-out phase of a routing transition.
Represents the state of routing transitions for a node.