MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
NetworkAudioBuffer.cpp
Go to the documentation of this file.
2
4
6
7namespace MayaFlux::Buffers {
8
10 std::shared_ptr<Nodes::Network::NodeNetwork> network,
11 float mix,
12 bool clear_before_process)
13 : m_network(std::move(network))
14 , m_mix(mix)
15 , m_clear_before_process(clear_before_process)
16{
17}
18
19void NetworkAudioProcessor::on_attach(const std::shared_ptr<Buffer>& /*buffer*/)
20{
21}
22
23void NetworkAudioProcessor::on_detach(const std::shared_ptr<Buffer>& /*buffer*/)
24{
25}
26
27void NetworkAudioProcessor::processing_function(const std::shared_ptr<Buffer>& buffer)
28{
29 if (!m_network) {
31 "NetworkAudioSourceProcessor has null network");
32 return;
33 }
34
35 auto output_mode = m_network->get_output_mode();
39 "NetworkAudioSourceProcessor: network is not an audio sink (mode={})",
40 static_cast<int>(output_mode));
41 return;
42 }
43
44 if (!m_network->is_enabled()) {
46 "NetworkAudioSourceProcessor: network is disabled");
47 return;
48 }
49
50 auto audio_buffer = std::dynamic_pointer_cast<AudioBuffer>(buffer);
51 if (!audio_buffer) {
52 return;
53 }
54
55 bool should_clear = m_clear_before_process;
56 if (auto net_buf = std::dynamic_pointer_cast<NetworkAudioBuffer>(buffer)) {
57 should_clear = net_buf->get_clear_before_process();
58 }
59
60 if (should_clear) {
61 buffer->clear();
62 }
63
64 if (!m_network->is_processed_this_cycle()) {
65 if (m_network->is_processing()) {
66 return;
67 }
68
69 m_self_processing = true;
70 m_network->add_channel_usage(audio_buffer->get_channel_id());
71 m_network->mark_processing(true);
72 m_network->process_batch(audio_buffer->get_num_samples());
73 m_network->mark_processing(false);
74 m_network->mark_processed(true);
75 }
76
77 const auto net_output = m_network->get_audio_buffer();
78 if (!net_output) {
79 return;
80 }
81
82 auto& buffer_data = audio_buffer->get_data();
83 const auto& network_data = *net_output;
84
85 auto scale = static_cast<double>(m_mix);
86
88 if (m_network->needs_channel_routing()) {
89 uint32_t channel = audio_buffer->get_channel_id();
90 double routing_amount = m_network->get_routing_state().amount[channel];
91 scale *= routing_amount;
92 }
93 }
94
95 if (scale == 0.0) {
96 return;
97 }
98
99 const size_t copy_size = std::min(buffer_data.size(), network_data.size());
100
101 if (should_clear && scale == 1.0) {
102 std::copy_n(network_data.begin(), copy_size, buffer_data.begin());
103 } else {
104 for (size_t i = 0; i < copy_size; ++i) {
105 buffer_data[i] += network_data[i] * scale;
106 }
107 }
108
109 if (m_self_processing) {
110 m_self_processing = false;
111 m_network->request_reset_from_channel(audio_buffer->get_channel_id());
112 }
113}
114
116 uint32_t channel_id,
117 uint32_t num_samples,
118 std::shared_ptr<Nodes::Network::NodeNetwork> network,
119 bool clear_before_process)
120 : AudioBuffer(channel_id, num_samples)
121 , m_network(std::move(network))
122 , m_clear_before_process(clear_before_process)
123{
124 auto mode = m_network->get_output_mode();
127 error<std::invalid_argument>(
130 std::source_location::current(),
131 "NetworkAudioBuffer requires AUDIO_SINK or AUDIO_COMPUTE network, got mode={}",
132 static_cast<int>(mode));
133 }
134}
135
141
143{
145 clear();
146 }
147 m_default_processor->process(shared_from_this());
148}
149
150std::shared_ptr<BufferProcessor> NetworkAudioBuffer::create_default_processor()
151{
152 return std::make_shared<NetworkAudioProcessor>(m_network);
153}
154
155}
#define MF_RT_ERROR(comp, ctx,...)
#define MF_RT_DEBUG(comp, ctx,...)
void clear() override
Resets all audio samples in the buffer to silence.
std::shared_ptr< BufferProcessor > m_default_processor
Default audio transformation processor for this buffer.
Concrete audio implementation of the Buffer interface for double-precision audio data.
void process_default() override
Applies the default audio transformation to the buffer's data.
NetworkAudioBuffer(uint32_t channel_id, uint32_t num_samples, std::shared_ptr< Nodes::Network::NodeNetwork > network, bool clear_before_process=true)
Creates a buffer connected to a NodeNetwork.
std::shared_ptr< BufferProcessor > create_default_processor() override
Creates a default audio transformation processor for this buffer type.
std::shared_ptr< Nodes::Network::NodeNetwork > m_network
void setup_processors(ProcessingToken token) override
Sets up audio processors for the specified processing token.
void processing_function(const std::shared_ptr< Buffer > &buffer) override
Drives or reads network batch output into the buffer.
std::shared_ptr< Nodes::Network::NodeNetwork > m_network
void on_attach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is attached to a buffer.
NetworkAudioProcessor(std::shared_ptr< Nodes::Network::NodeNetwork > network, float mix=1.0F, bool clear_before_process=true)
Creates a processor connected to a NodeNetwork.
void on_detach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is detached from a buffer.
ProcessingToken
Bitfield enum defining processing characteristics and backend requirements for buffer operations.
@ BufferProcessing
Buffer processing (Buffers::BufferManager, processing chains)
@ Init
Engine/subsystem initialization.
@ Buffers
Buffers, Managers, processors and processing chains.
@ AUDIO_COMPUTE
processed each cycle but not sent to output
@ AUDIO_SINK
Aggregated audio samples sent to output.
std::vector< double > mix(const std::vector< std::vector< double > > &streams)
Mix multiple data streams with equal weighting.
Definition Yantra.cpp:1021