MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
NetworkAudioBuffer.hpp
Go to the documentation of this file.
1#pragma once
2
5
7class NodeNetwork;
8}
9
10namespace MayaFlux::Buffers {
11
12/**
13 * @class NetworkAudioProcessor
14 * @brief Processor that bridges NodeNetwork batch output into an AudioBuffer
15 *
16 * NetworkAudioProcessor drives or reads the batch output from a NodeNetwork
17 * into an AudioBuffer. If the network has already been processed this cycle
18 * (e.g. by NodeGraphManager during AUDIO_RATE processing), the processor
19 * reads the cached result. If not, the processor drives process_batch()
20 * itself, making the buffer work regardless of whether the network is
21 * registered with NodeGraphManager.
22 *
23 * This mirrors the guard pattern used by NodeGraphManager:
24 * check is_processed_this_cycle(), then mark_processing/process_batch/
25 * mark_processed. If another context is currently processing, the processor
26 * returns immediately rather than blocking.
27 *
28 * The processor respects the network's routing state, applying per-channel
29 * scaling when the network is undergoing a routing transition. The mix
30 * parameter controls interpolation between existing buffer content and
31 * incoming network output.
32 *
33 * Processing sequence per cycle:
34 * 1. Validate network has AUDIO_SINK or AUDIO_COMPUTE output mode
35 * 2. Optionally clear buffer (clear_before_process)
36 * 3. Ensure network is initialized
37 * 4. Drive process_batch() if not yet processed this cycle
38 * 5. Read cached batch output via get_audio_buffer()
39 * 6. Apply routing scale for the buffer's channel if active
40 * 7. Mix into buffer data with configured mix level
41 */
42class MAYAFLUX_API NetworkAudioProcessor : public BufferProcessor {
43public:
44 /**
45 * @brief Creates a processor connected to a NodeNetwork
46 * @param network Source network whose batch output fills this buffer (must be AUDIO_SINK or AUDIO_COMPUTE)
47 * @param mix Interpolation coefficient between existing and incoming data (0.0-1.0)
48 * @param clear_before_process Whether to zero the buffer before writing network output
49 */
51 std::shared_ptr<Nodes::Network::NodeNetwork> network,
52 float mix = 1.0F,
53 bool clear_before_process = true);
54
55 /**
56 * @brief Drives or reads network batch output into the buffer
57 * @param buffer Target buffer to fill with network batch data
58 *
59 * If the network has not been processed this cycle, drives process_batch()
60 * with the buffer's sample count. If another context is currently processing,
61 * returns immediately without writing data.
62 */
63 void processing_function(const std::shared_ptr<Buffer>& buffer) override;
64
65 void set_mix(float mix) { m_mix = mix; }
66 [[nodiscard]] float get_mix() const { return m_mix; }
67
68 void on_attach(const std::shared_ptr<Buffer>& buffer) override;
69 void on_detach(const std::shared_ptr<Buffer>& buffer) override;
70
71private:
72 std::shared_ptr<Nodes::Network::NodeNetwork> m_network;
73 float m_mix;
75
76 bool m_self_processing {};
77};
78
79/**
80 * @class NetworkAudioBuffer
81 * @brief AudioBuffer that captures batch output from a NodeNetwork each cycle
82 *
83 * NetworkAudioBuffer provides a bridge between NodeNetwork (which produces batch
84 * audio via process_batch()) and the AudioBuffer system (which participates
85 * in buffer processing chains, pipelines, and capture operations).
86 *
87 * This is the network-level analogue of NodeBuffer. Where NodeBuffer drives
88 * a single Node sample-by-sample through save_state/process_sample/restore_state,
89 * NetworkAudioBuffer drives or reads the network's batch result. The network may
90 * or may not be managed by NodeGraphManager; NetworkAudioBuffer handles both cases.
91 *
92 * The network must have OutputMode::AUDIO_SINK or AUDIO_COMPUTE. Construction
93 * with a non-audio network is a fatal error.
94 *
95 * The buffer does NOT register itself on any audio output channel. It exists
96 * as an independent capture point. The network's normal output path through
97 * RootAudioBuffer/ChannelProcessor is unaffected.
98 */
99class MAYAFLUX_API NetworkAudioBuffer : public AudioBuffer {
100public:
101 /**
102 * @brief Creates a buffer connected to a NodeNetwork
103 * @param channel_id Channel identifier for this buffer
104 * @param num_samples Buffer size in samples (should match system buffer size)
105 * @param network Source network whose batch output populates this buffer (must be AUDIO_SINK or AUDIO_COMPUTE)
106 * @param clear_before_process Whether to zero buffer before each cycle
107 */
109 uint32_t channel_id,
110 uint32_t num_samples,
111 std::shared_ptr<Nodes::Network::NodeNetwork> network,
112 bool clear_before_process = true);
113
114 void setup_processors(ProcessingToken token) override;
115
116 void set_clear_before_process(bool value) { m_clear_before_process = value; }
117 [[nodiscard]] bool get_clear_before_process() const { return m_clear_before_process; }
118
119 void process_default() override;
120
121 [[nodiscard]] std::shared_ptr<Nodes::Network::NodeNetwork> get_network() const { return m_network; }
122
123protected:
124 std::shared_ptr<BufferProcessor> create_default_processor() override;
125
126private:
127 std::shared_ptr<Nodes::Network::NodeNetwork> m_network;
129};
130
131}
Concrete audio implementation of the Buffer interface for double-precision audio data.
Central computational transformation interface for continuous buffer processing.
std::shared_ptr< Nodes::Network::NodeNetwork > get_network() const
std::shared_ptr< Nodes::Network::NodeNetwork > m_network
AudioBuffer that captures batch output from a NodeNetwork each cycle.
std::shared_ptr< Nodes::Network::NodeNetwork > m_network
Processor that bridges NodeNetwork batch output into an AudioBuffer.
ProcessingToken
Bitfield enum defining processing characteristics and backend requirements for buffer operations.
std::vector< double > mix(const std::vector< std::vector< double > > &streams)
Mix multiple data streams with equal weighting.
Definition Yantra.cpp:1021