MayaFlux 0.3.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 * @return True if the buffer was successfully supplied, false otherwise
51 *
52 * The buffer data is added, mixed, and normalized at the end of the processing
53 * chain of the channel's root buffer, but before final processing. This is
54 * useful when one AudioBuffer needs to be supplied to multiple channels.
55 */
56 bool supply_audio_buffer_to(
57 const std::shared_ptr<AudioBuffer>& buffer,
58 ProcessingToken token,
59 uint32_t channel,
60 double mix = 1.0);
61
62 /**
63 * @brief Removes a previously supplied buffer from a token and channel
64 * @param buffer Buffer to remove
65 * @param token Processing domain
66 * @param channel Channel index
67 * @return True if the buffer was successfully removed, false otherwise
68 *
69 * Cleans up the mixing relationship between the supplied buffer and the target channel.
70 */
71 bool remove_supplied_audio_buffer(
72 const std::shared_ptr<AudioBuffer>& buffer,
73 ProcessingToken token,
74 uint32_t channel);
75
76 // =========================================================================
77 // Interleaved Data I/O
78 // =========================================================================
79
80 /**
81 * @brief Fills audio token channels from interleaved source data
82 * @param interleaved_data Source interleaved data buffer
83 * @param num_frames Number of frames to process
84 * @param token Processing domain
85 * @param num_channels Number of channels in the interleaved data
86 *
87 * Takes interleaved data (like typical audio file format or hardware I/O)
88 * and distributes it to the token's channels.
89 */
90 void fill_audio_from_interleaved(
91 const double* interleaved_data,
92 uint32_t num_frames,
93 ProcessingToken token,
94 uint32_t num_channels);
95
96 /**
97 * @brief Fills interleaved buffer from audio token channels
98 * @param interleaved_data Target interleaved data buffer
99 * @param num_frames Number of frames to process
100 * @param token Processing domain
101 * @param num_channels Number of channels to interleave
102 *
103 * Takes channel-separated data from the token and interleaves it into
104 * a single buffer (like typical audio file format or hardware I/O).
105 */
106 void fill_audio_interleaved(
107 double* interleaved_data,
108 uint32_t num_frames,
109 ProcessingToken token,
110 uint32_t num_channels) const;
111
112 // =========================================================================
113 // Buffer Cloning
114 // =========================================================================
115
116 /**
117 * @brief Clones an audio buffer for each channel in the specified list
118 * @param buffer Buffer to clone
119 * @param channels Vector of channel indices to clone for
120 * @param token Processing domain
121 *
122 * Creates a new buffer for each specified channel, copying the structure
123 * but maintaining independent data. Useful for multi-channel processing
124 * where each channel needs its own processing chain.
125 */
126 std::vector<std::shared_ptr<AudioBuffer>> clone_audio_buffer_for_channels(
127 const std::shared_ptr<AudioBuffer>& buffer,
128 const std::vector<uint32_t>& channels,
129 ProcessingToken token);
130
131 /**
132 * @brief Routes a buffer's processing from one channel to another with fade
133 * @param buffer Buffer to route
134 * @param target_channel Destination channel
135 * @param fade_cycles Number of processing cycles to fade over
136 * @param token Processing domain
137 *
138 * Transitions the buffer's channel_id from its current channel to the target channel
139 * with smooth crossfade. During transition, buffer continues processing in its original
140 * channel while fading in via supply to the target channel.
141 */
142 void route_buffer_to_channel(
143 const std::shared_ptr<AudioBuffer>& buffer,
144 uint32_t target_channel,
145 uint32_t fade_cycles,
146 ProcessingToken token);
147
148 /**
149 * @brief Updates routing states for all buffers undergoing transitions
150 * @param token Processing domain to update
151 *
152 * Should be called once per processing cycle to advance routing fade states.
153 */
154 void update_routing_states_for_cycle(ProcessingToken token);
155
156 /**
157 * @brief Cleans up completed routing transitions
158 * @param token Processing domain to clean up
159 *
160 * Removes buffers from old channels and finalizes registration to new channels
161 * after fade completion.
162 */
163 void cleanup_completed_routing(ProcessingToken token);
164
165private:
166 /// Reference to the token/unit manager
168
169 /// Reference to the buffer access control
171
172 /**
173 * @brief Updates a single buffer's routing state
174 * @param state Routing state to update
175 */
176 void update_routing_state(BufferRoutingState& state);
177};
178
179} // namespace MayaFlux::Buffers
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.