MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
BufferSupplyMixing.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Buffers {
6
7class AudioBuffer;
8class TokenUnitManager;
9class BufferAccessControl;
10struct BufferRoutingState;
11
12/**
13 * @class BufferSupplyMixing
14 * @brief External buffer supply, mixing, and interleaved data I/O
15 *
16 * Manages operations for supplying external buffers to channels, mixing buffers,
17 * and converting between interleaved and channel-separated formats. This enables
18 * advanced routing patterns where the same buffer feeds multiple channels,
19 * or data is imported/exported in different formats.
20 *
21 * Design Principles:
22 * - Token-aware: Routes operations to appropriate tokens
23 * - Format-agnostic: Handles both channel-separated and interleaved data
24 * - Mixing coordination: Uses MixProcessor for proper signal combining
25 * - Single responsibility: Only handles supply/mixing/format operations
26 *
27 * This class encapsulates all buffer supply and data I/O logic.
28 */
29class MAYAFLUX_API BufferSupplyMixing {
30public:
31 /**
32 * @brief Creates a new supply/mixing control handler
33 * @param unit_manager Reference to the TokenUnitManager
34 * @param access_control Reference to the BufferAccessControl
35 */
36 BufferSupplyMixing(TokenUnitManager& unit_manager, BufferAccessControl& access_control);
37
39
40 // =========================================================================
41 // Buffer Supply and Mixing
42 // =========================================================================
43
44 /**
45 * @brief Supplies an external audio buffer to a specific token and channel
46 * @param buffer Buffer to supply
47 * @param token Processing domain
48 * @param channel Channel index
49 * @param mix Mix level (default: 1.0)
50 * @param force If true, forces supply even if buffer is already registered (default: false)
51 The guard exists to prevent accidental multiple registrations of the same buffer to the same channel,
52 which can cause unintended mixing behavior. Setting force to true allows bypassing this guard when intentional.
53 * @return True if the buffer was successfully supplied, false otherwise
54 *
55 * The buffer data is added, mixed, and normalized at the end of the processing
56 * chain of the channel's root buffer, but before final processing. This is
57 * useful when one AudioBuffer needs to be supplied to multiple channels.
58 */
59 bool supply_audio_buffer_to(
60 const std::shared_ptr<AudioBuffer>& buffer,
61 ProcessingToken token,
62 uint32_t channel,
63 double mix = 1.0, bool force = false);
64
65 /**
66 * @brief Removes a previously supplied buffer from a token and channel
67 * @param buffer Buffer to remove
68 * @param token Processing domain
69 * @param channel Channel index
70 * @return True if the buffer was successfully removed, false otherwise
71 *
72 * Cleans up the mixing relationship between the supplied buffer and the target channel.
73 */
74 bool remove_supplied_audio_buffer(
75 const std::shared_ptr<AudioBuffer>& buffer,
76 ProcessingToken token,
77 uint32_t channel);
78
79 // =========================================================================
80 // Interleaved Data I/O
81 // =========================================================================
82
83 /**
84 * @brief Fills audio token channels from interleaved source data
85 * @param interleaved_data Source interleaved data buffer
86 * @param num_frames Number of frames to process
87 * @param token Processing domain
88 * @param num_channels Number of channels in the interleaved data
89 *
90 * Takes interleaved data (like typical audio file format or hardware I/O)
91 * and distributes it to the token's channels.
92 */
93 void fill_audio_from_interleaved(
94 const double* interleaved_data,
95 uint32_t num_frames,
96 ProcessingToken token,
97 uint32_t num_channels);
98
99 /**
100 * @brief Fills interleaved buffer from audio token channels
101 * @param interleaved_data Target interleaved data buffer
102 * @param num_frames Number of frames to process
103 * @param token Processing domain
104 * @param num_channels Number of channels to interleave
105 *
106 * Takes channel-separated data from the token and interleaves it into
107 * a single buffer (like typical audio file format or hardware I/O).
108 */
109 void fill_audio_interleaved(
110 double* interleaved_data,
111 uint32_t num_frames,
112 ProcessingToken token,
113 uint32_t num_channels) const;
114
115 // =========================================================================
116 // Buffer Cloning
117 // =========================================================================
118
119 /**
120 * @brief Clones an audio buffer for each channel in the specified list
121 * @param buffer Buffer to clone
122 * @param channels Vector of channel indices to clone for
123 * @param token Processing domain
124 *
125 * Creates a new buffer for each specified channel, copying the structure
126 * but maintaining independent data. Useful for multi-channel processing
127 * where each channel needs its own processing chain.
128 */
129 std::vector<std::shared_ptr<AudioBuffer>> clone_audio_buffer_for_channels(
130 const std::shared_ptr<AudioBuffer>& buffer,
131 const std::vector<uint32_t>& channels,
132 ProcessingToken token);
133
134 /**
135 * @brief Routes a buffer's processing from one channel to another with fade
136 * @param buffer Buffer to route
137 * @param target_channel Destination channel
138 * @param fade_cycles Number of processing cycles to fade over
139 * @param token Processing domain
140 *
141 * Transitions the buffer's channel_id from its current channel to the target channel
142 * with smooth crossfade. During transition, buffer continues processing in its original
143 * channel while fading in via supply to the target channel.
144 */
145 void route_buffer_to_channel(
146 const std::shared_ptr<AudioBuffer>& buffer,
147 uint32_t target_channel,
148 uint32_t fade_cycles,
149 ProcessingToken token);
150
151 /**
152 * @brief Updates routing states for all buffers undergoing transitions
153 * @param token Processing domain to update
154 *
155 * Should be called once per processing cycle to advance routing fade states.
156 */
157 void update_routing_states_for_cycle(ProcessingToken token);
158
159 /**
160 * @brief Cleans up completed routing transitions
161 * @param token Processing domain to clean up
162 *
163 * Removes buffers from old channels and finalizes registration to new channels
164 * after fade completion.
165 */
166 void cleanup_completed_routing(ProcessingToken token);
167
168private:
169 /// Reference to the token/unit manager
171
172 /// Reference to the buffer access control
174
175 /**
176 * @brief Updates a single buffer's routing state
177 * @param state Routing state to update
178 */
179 void update_routing_state(BufferRoutingState& state);
180};
181
182} // namespace MayaFlux::Buffers
uint32_t channel
Token-aware buffer and unit access patterns.
TokenUnitManager & m_unit_manager
Reference to the token/unit manager.
BufferAccessControl & m_access_control
Reference to the buffer access control.
External buffer supply, mixing, and interleaved data I/O.
Token-scoped unit storage and lifecycle management.
ProcessingToken
Bitfield enum defining processing characteristics and backend requirements for buffer operations.
Represents the state of routing transitions for a buffer.