MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
RelationNetwork.cpp
Go to the documentation of this file.
1#include "RelationNetwork.hpp"
2
4
10
12{
13 constexpr uint32_t k_default_block_frames = 512;
14
15 slot.index = static_cast<uint32_t>(m_slots.size());
16 if (slot.node) {
17 slot.node->m_fire_events_during_snapshot = true;
18 }
19 slot.reserve_block(k_default_block_frames);
20
21 const uint32_t index = slot.index;
22 m_slots.push_back(std::move(slot));
23
24 return index;
25}
26
27std::vector<uint32_t> RelationNetwork::add_slots(std::initializer_list<RelationSlot> slots)
28{
29 std::vector<uint32_t> indices;
30 indices.reserve(slots.size());
31 for (const auto& slot : slots) {
32 indices.push_back(add_slot(slot));
33 }
34 return indices;
35}
36
41
43{
44 return m_slots[index];
45}
46
47std::optional<std::reference_wrapper<RelationSlot>> RelationNetwork::find_slot(std::string_view name)
48{
49 auto it = std::ranges::find_if(m_slots, [name](const RelationSlot& slot) { return slot.name == name; });
50 if (it == m_slots.end()) {
51 return std::nullopt;
52 }
53 return std::ref(*it);
54}
55
56std::optional<std::reference_wrapper<const RelationSlot>> RelationNetwork::find_slot(std::string_view name) const
57{
58 auto it = std::ranges::find_if(m_slots, [name](const RelationSlot& slot) { return slot.name == name; });
59 if (it == m_slots.end()) {
60 return std::nullopt;
61 }
62 return std::cref(*it);
63}
64
65std::optional<uint32_t> RelationNetwork::find_slot_index(std::string_view name) const
66{
67 for (uint32_t i = 0; i < static_cast<uint32_t>(m_slots.size()); ++i) {
68 if (m_slots[i].name == name) {
69 return i;
70 }
71 }
72 return std::nullopt;
73}
74
75void RelationNetwork::process_batch(unsigned int num_samples)
76{
78
79 for (const auto& op : chain->operators()) {
80 if (auto* relation_op = dynamic_cast<RelationOperator*>(op.get())) {
81 relation_op->set_slots(m_slots);
82 relation_op->begin_block();
83 }
84 }
85
86 chain->process(static_cast<float>(num_samples));
87
88 while (m_audio_buffer_lock.test_and_set(std::memory_order_acquire))
89 std::this_thread::yield();
90
91 if (!m_publishers.empty() && m_publishers.front()->get_output_count() > 0) {
92 auto block = m_publishers.front()->get_output_block(0);
93 m_last_audio_buffer.assign(block.begin(), block.end());
95 } else {
96 m_last_audio_buffer.clear();
97 }
98
99 m_audio_buffer_lock.clear(std::memory_order_release);
100}
101
102std::optional<std::vector<double>> RelationNetwork::get_audio_buffer() const
103{
105}
106
107std::optional<double> RelationNetwork::get_node_output(size_t index) const
108{
109 if (index >= m_slots.size()) {
110 return std::nullopt;
111 }
112 return m_slots[index].level;
113}
114
115std::optional<std::span<const double>> RelationNetwork::get_node_audio_buffer(size_t index) const
116{
117 if (index >= m_slots.size()) {
118 return std::nullopt;
119 }
120 return m_slots[index].block;
121}
122
124{
125 size_t total = 0;
126 for (const auto& publisher : m_publishers) {
127 total += publisher->get_output_count();
128 }
129 return total;
130}
131
132std::optional<std::span<const double>> RelationNetwork::get_output_block(size_t index) const
133{
134 for (const auto& publisher : m_publishers) {
135 const size_t count = publisher->get_output_count();
136 if (index < count) {
137 return publisher->get_output_block(index);
138 }
139 index -= count;
140 }
141 return std::nullopt;
142}
143
144std::unordered_map<std::string, std::string> RelationNetwork::get_metadata() const
145{
146 auto meta = NodeNetwork::get_metadata();
147 meta["slot_count"] = std::to_string(m_slots.size());
148 meta["output_count"] = std::to_string(get_output_count());
149 meta["operator_count"] = std::to_string(get_operator_chain()->size());
150 return meta;
151}
152
153} // namespace MayaFlux::Nodes::Network
bool size
std::vector< uint32_t > indices
BufferProcessingChain * chain
uint32_t total
std::string name
Definition VKDevice.cpp:123
uint32_t index
Definition VKDevice.cpp:122
size_t count
void apply_output_scale()
Apply m_output_scale to m_last_audio_buffer.
virtual std::optional< std::vector< double > > get_audio_buffer() const
Get cached audio buffer from last process_batch()
std::shared_ptr< OperatorChain > m_operator_chain
std::shared_ptr< OperatorChain > get_operator_chain() const
Access the secondary operator chain.
std::atomic_flag m_audio_buffer_lock
Spinlock guarding m_last_audio_buffer.
virtual std::unordered_map< std::string, std::string > get_metadata() const
Get network metadata for debugging/visualization.
std::vector< double > m_last_audio_buffer
void set_output_mode(OutputMode mode)
Set the network's output routing mode.
std::optional< std::span< const double > > get_node_audio_buffer(size_t index) const override
Block of one slot from the last process_batch()
void process_batch(unsigned int num_samples) override
Process the network for the given number of samples.
std::optional< std::span< const double > > get_output_block(size_t index) const
Block of one published output from the last process_batch()
std::unordered_map< std::string, std::string > get_metadata() const override
Get network metadata for debugging/visualization.
std::vector< RelationSlot > & slots() noexcept
std::optional< std::reference_wrapper< RelationSlot > > find_slot(std::string_view name)
Find a slot by name.
std::vector< std::shared_ptr< RelationOperator > > m_publishers
std::vector< uint32_t > add_slots(std::initializer_list< RelationSlot > slots)
Add several slots at once.
std::optional< std::vector< double > > get_audio_buffer() const override
Block of the first published output, or nullopt when none is published.
RelationSlot & get_slot(uint32_t index)
std::optional< uint32_t > find_slot_index(std::string_view name) const
Find the index of a slot by name.
uint32_t add_slot(RelationSlot slot={})
Add a slot to the network.
size_t get_output_count() const
Total published outputs across all publishing operators, in chain order.
std::optional< double > get_node_output(size_t index) const override
Level of one slot after the last processed block.
Operator that acts on a network's slots over one block at a time.
@ AUDIO_SINK
Aggregated audio samples sent to output.
void reserve_block(uint32_t block_frames)
Preallocate this slot's block storage without changing its size.
One addressable participant of a RelationNetwork.