MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ processing_function()

void MayaFlux::Buffers::NetworkAudioProcessor::processing_function ( const std::shared_ptr< Buffer > &  buffer)
overridevirtual

Drives or reads network batch output into the buffer.

Parameters
bufferTarget buffer to fill with network batch data

If the network has not been processed this cycle, drives process_batch() with the buffer's sample count. If another context is currently processing, returns immediately without writing data.

Implements MayaFlux::Buffers::BufferProcessor.

Definition at line 27 of file NetworkAudioBuffer.cpp.

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}
#define MF_RT_ERROR(comp, ctx,...)
#define MF_RT_DEBUG(comp, ctx,...)
std::shared_ptr< Nodes::Network::NodeNetwork > m_network
@ BufferProcessing
Buffer processing (Buffers::BufferManager, processing chains)
@ 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.

References MayaFlux::Nodes::Network::AUDIO_COMPUTE, MayaFlux::Nodes::Network::AUDIO_SINK, MayaFlux::Journal::BufferProcessing, MayaFlux::Journal::Buffers, m_clear_before_process, m_mix, m_network, m_self_processing, MF_RT_DEBUG, and MF_RT_ERROR.