12 : m_registered_sample_rate(sample_rate)
13 , m_registered_block_size(block_size)
14 , m_registered_frame_rate(frame_rate)
28 node->set_gpu_compatible(
true);
32 root.register_node(node);
42 root.unregister_node(node);
46 std::function<
void(std::span<RootNode*>)> processor)
53 static std::unordered_map<unsigned int, std::shared_ptr<RootNode>> audio_roots;
58 for (
const auto& [
channel, root] : it->second) {
69 if (!processing_ptr) {
70 processing_ptr = std::make_unique<std::atomic<bool>>(
false);
73 bool expected =
false;
74 return processing_ptr->compare_exchange_strong(
76 std::memory_order_acquire,
77 std::memory_order_relaxed);
88 it->second(std::span<RootNode*>(roots.data(), roots.size()));
98 for (
auto& network : it->second) {
99 if (!network || !network->is_enabled()) {
103 if (!network->is_processed_this_cycle()) {
104 network->mark_processing(
true);
105 network->process_batch(num_samples);
106 network->mark_processing(
false);
107 network->mark_processed(
true);
115 for (
auto* root : roots) {
116 root->process_batch(num_samples);
119 for (
auto* root : roots) {
120 root->process_batch_frame(num_samples);
131 std::vector<std::vector<double>> all_network_outputs;
135 for (
auto& network : audio_it->second) {
136 if (!network || !network->is_enabled()) {
140 if (!network->is_registered_on_channel(
channel)) {
144 if (!network->is_processed_this_cycle()) {
145 network->mark_processing(
true);
146 network->process_batch(num_samples);
147 network->mark_processing(
false);
148 network->mark_processed(
true);
151 const auto& net_buffer = network->get_audio_buffer();
153 if (network->needs_channel_routing()) {
154 double scale = network->get_routing_state().amount[
channel];
159 all_network_outputs.push_back(*net_buffer);
161 std::vector<double> scaled_buffer = *net_buffer;
162 for (
auto& sample : scaled_buffer)
165 all_network_outputs.push_back(std::move(scaled_buffer));
168 all_network_outputs.push_back(*net_buffer);
175 return all_network_outputs;
181 auto ch =
channel.value_or(0U);
184 for (
auto& network : it->second) {
185 if (network && network->is_enabled()) {
186 network->mark_processed(
false);
192 it->second->store(
false, std::memory_order_release);
200 for (
auto& network : audio_it->second) {
202 if (network->is_registered_on_channel(
channel)) {
203 network->request_reset_from_channel(
channel);
223 unsigned int channel,
unsigned int num_samples)
232 return it->second(&root, num_samples);
235 std::vector<double> samples = root.process_batch(num_samples);
237 uint32_t normalize_coef = root.get_node_size();
238 for (
double& sample : samples) {
252 return it->second(&root,
channel);
255 double sample = root.process_sample();
265 sample /= std::sqrt(
static_cast<double>(num_nodes));
267 const double threshold = 0.95;
268 const double knee = 0.1;
269 const double abs_sample = std::abs(sample);
271 if (abs_sample > threshold) {
272 const double excess = abs_sample - threshold;
273 const double compressed_excess = std::tanh(excess / knee) * knee;
274 const double limited_abs = threshold + compressed_excess;
275 sample = std::copysign(limited_abs, sample);
282 std::unordered_map<unsigned int, std::vector<double>> channel_data;
286 for (
unsigned int channel : channels) {
296 return static_cast<unsigned int>(channels.size());
301 std::vector<RootNode*> roots;
305 for (
auto& [
channel, root] : it->second) {
306 roots.push_back(root.get());
335 for (uint32_t ch = 0; ch < num_channels; ++ch) {
343 std::stringstream ss;
344 ss <<
"node_" << node.get();
345 std::string generated_id = ss.str();
353 node->register_channel_usage(channel_id);
359 if (pair.second == node) {
369 node->unregister_channel_usage(channel_id);
374 std::vector<ProcessingToken> tokens;
376 if (!channels.empty()) {
377 tokens.push_back(token);
385 std::vector<unsigned int> channels;
388 for (
const auto& [
channel, root] : it->second) {
400 for (
const auto& [
channel, root] : it->second) {
401 count += root->get_node_size();
411 "=== NodeGraphManager Summary ===");
419 "Token {}: {} nodes across {} channels",
420 static_cast<int>(token), total_nodes, channels.size());
422 for (
auto channel : channels) {
428 " Channel {}: {} nodes, {} networks",
429 channel, root.get_node_size(), networks.size());
431 for (
const auto& network : networks) {
435 " Network: {} internal nodes, mode={}, enabled={}",
436 network->get_node_count(),
437 static_cast<int>(network->get_output_mode()),
438 network->is_enabled());
458 [&node](
const auto& pair) {
return pair.second == node; });
466 if (source && target) {
467 auto chain = std::make_shared<ChainNode>(source, target, *
this);
485 network->set_enabled(
true);
489 uint32_t channel_mask = network->get_channel_mask();
491 if (channel_mask == 0) {
493 network->add_channel_usage(0);
496 auto channels = network->get_registered_channels();
499 for (
auto ch : channels) {
503 "Added audio network to token {} channel {}: {} nodes",
504 static_cast<int>(token), ch, network->get_node_count());
512 "Added network to token {}: {} nodes, mode={}",
513 static_cast<int>(token),
514 network->get_node_count(),
515 static_cast<int>(network->get_output_mode()));
530 auto& networks = token_it->second;
531 std::erase_if(networks, [&](
const auto& n) {
return n == network; });
536 auto& networks = it->second;
537 std::erase_if(networks, [&](
const auto& n) {
return n == network; });
544std::vector<std::shared_ptr<Network::NodeNetwork>>
549 std::vector<std::shared_ptr<Network::NodeNetwork>> networks_on_channel;
550 for (
const auto& network : token_it->second) {
551 if (network && network->is_registered_on_channel(
channel)) {
552 networks_on_channel.push_back(network);
555 return networks_on_channel;
560std::vector<std::shared_ptr<Network::NodeNetwork>>
563 std::vector<std::shared_ptr<Network::NodeNetwork>> all_networks;
567 all_networks.insert(all_networks.end(),
568 audio_it->second.begin(),
569 audio_it->second.end());
574 all_networks.insert(all_networks.end(),
575 token_it->second.begin(),
576 token_it->second.end());
588 count += audio_it->second.size();
593 count += token_it->second.size();
608 std::stringstream ss;
609 ss <<
"network_" << network.get();
610 std::string generated_id = ss.str();
620 if (pair.second == network) {
630 [&network](
const auto& pair) {
return pair.second == network; });
639 for (
auto& network : networks) {
647 for (
auto& network : networks) {
658 for (
auto* root : roots) {
659 root->terminate_all_nodes();
678 if (!node->needs_channel_routing())
684 if (!network || !network->needs_channel_routing())
691 const std::shared_ptr<Node>& node,
692 const std::vector<uint32_t>& target_channels,
693 uint32_t fade_cycles,
696 uint32_t current_channels = node->get_channel_mask();
698 uint32_t target_bitmask = 0;
699 for (
auto ch : target_channels) {
700 target_bitmask |= (1 << ch);
704 fade_blocks = std::max(1U, fade_blocks);
712 for (uint32_t ch = 0; ch < 32; ch++) {
713 state.
amount[ch] = (current_channels & (1 << ch)) ? 1.0 : 0.0;
716 node->get_routing_state() = state;
718 for (
auto ch : target_channels) {
719 if (!(current_channels & (1 << ch))) {
726 const std::shared_ptr<Network::NodeNetwork>& network,
727 const std::vector<uint32_t>& target_channels,
728 uint32_t fade_cycles,
734 "Attempted to route network that is not an audio sink. Operation ignored.");
739 network->set_enabled(
true);
742 if (std::ranges::find(networks, network) == networks.end()) {
743 networks.push_back(network);
746 uint32_t current_channels = network->get_channel_mask();
748 uint32_t target_bitmask = 0;
749 for (
auto ch : target_channels) {
750 target_bitmask |= (1 << ch);
753 uint32_t combined_mask = current_channels | target_bitmask;
754 network->set_channel_mask(combined_mask);
755 for (
auto ch : target_channels) {
756 network->add_channel_usage(ch);
761 fade_blocks = std::max(1u, fade_blocks);
769 for (uint32_t ch = 0; ch < 32; ch++) {
770 state.
amount[ch] = (current_channels & (1 << ch)) ? 1.0 : 0.0;
773 network->get_routing_state() = state;
778 std::vector<std::pair<std::shared_ptr<Node>, uint32_t>> nodes_to_remove;
781 if (!node->needs_channel_routing())
784 auto& state = node->get_routing_state();
787 for (uint32_t ch = 0; ch < 32; ch++) {
788 if ((state.from_channels & (1 << ch)) && !(state.to_channels & (1 << ch))) {
789 nodes_to_remove.emplace_back(node, ch);
796 for (
auto& [node,
channel] : nodes_to_remove) {
800 std::vector<std::pair<std::shared_ptr<Network::NodeNetwork>, uint32_t>> networks_to_cleanup;
803 if (!network || !network->needs_channel_routing())
806 auto& state = network->get_routing_state();
809 network->set_channel_mask(state.to_channels);
811 for (uint32_t ch = 0; ch < 32; ch++) {
812 if ((state.from_channels & (1 << ch)) && !(state.to_channels & (1 << ch))) {
813 networks_to_cleanup.emplace_back(network, ch);
820 for (
auto& [network,
channel] : networks_to_cleanup) {
821 network->remove_channel_usage(
channel);
823 if (network->get_channel_mask() == 0) {
825 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.
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.
NodeGraphManager(uint32_t sample_rate=48000, uint32_t block_size=512, uint32_t frame_rate=60)
Creates a new NodeGraphManager.
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.
uint32_t m_registered_frame_rate
Frame rate for visual processing, used for timing and normalization.
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.