MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Capture.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux {
6
7namespace Buffers {
8 class Buffer;
9 class AudioBuffer;
10 class VKBuffer;
11}
12
13namespace Kriya {
14
15 class BufferOperation;
16
17 using OperationFunction = std::function<void(Kakshya::DataVariant&, uint32_t)>;
19
20 /**
21 * @class BufferCapture
22 * @brief Flexible data capture interface for buffer-based processing pipelines.
23 *
24 * BufferCapture provides sophisticated data capture capabilities within the Kriya
25 * processing system, enabling various capture modes and lifecycle management for
26 * buffer data. It supports transient, accumulative, windowed, and circular capture
27 * patterns for different real-time processing scenarios.
28 *
29 * **Capture Modes:**
30 * - **TRANSIENT**: Single-cycle capture with automatic expiration (default)
31 * - **ACCUMULATE**: Multi-cycle accumulation in persistent container
32 * - **TRIGGERED**: Conditional capture based on predicates
33 * - **WINDOWED**: Rolling window capture with configurable overlap
34 * - **CIRCULAR**: Fixed-size circular buffer with overwrite behavior
35 *
36 * **Key Features:**
37 * - Multiple capture strategies for different use cases
38 * - Callback system for data lifecycle events
39 * - Metadata and tagging support for organization
40 * - Integration with BufferPipeline and BufferOperation
41 * - Scheduler timing and synchronization
42 *
43 * **Use Cases:**
44 * - Real-time audio analysis and feature extraction
45 * - Frame capture from graphics buffers
46 * - Streaming data collection for machine learning
47 * - Circular delay lines and feedback systems
48 * - Windowed processing for spectral analysis
49 * - Event-driven data capture and recording
50 *
51 * @see BufferOperation For pipeline integration
52 * @see BufferPipeline For execution context
53 * @see CaptureBuilder For fluent construction
54 */
55 class MAYAFLUX_API BufferCapture {
56 public:
57 /**
58 * @enum CaptureMode
59 * @brief Defines the data capture and retention strategy.
60 */
61 enum class CaptureMode : uint8_t {
62 TRANSIENT, ///< Single cycle capture (default) - data expires after 1 cycle
63 ACCUMULATE, ///< Accumulate over multiple cycles in container
64 TRIGGERED, ///< Capture only when condition met
65 WINDOWED, ///< Rolling window capture with overlap
66 CIRCULAR ///< Circular buffer with overwrite
67 };
68
69 /**
70 * @enum ProcessingControl
71 * @brief Controls how and when data processing occurs.
72 */
73 enum class ProcessingControl : uint8_t {
74 AUTOMATIC, // Let BufferManager handle processing (default)
75 ON_CAPTURE, // Only process when capture reads data
76 MANUAL // User controls processing explicitly
77 };
78
79 BufferCapture() = default;
80 BufferCapture(std::nullptr_t)
82 {
83 }
84
85 /**
86 * @brief Construct a BufferCapture with specified mode and parameters.
87 * @param buffer Source buffer to capture from
88 * @param mode Capture strategy to use (default: TRANSIENT)
89 * @param cycle_count Number of cycles for multi-cycle modes (default: 1)
90 */
91 BufferCapture(std::shared_ptr<Buffers::AudioBuffer> buffer,
92 CaptureMode mode = CaptureMode::TRANSIENT,
93 uint32_t cycle_count = 1);
94
95 BufferCapture(std::shared_ptr<Buffers::VKBuffer> buffer,
96 CaptureMode mode = CaptureMode::TRANSIENT,
97 uint32_t cycle_count = 1);
98
99 /**
100 * @brief Set processing control strategy.
101 * @param control ProcessingControl strategy (default: AUTOMATIC)
102 * @return Reference to this BufferCapture for chaining
103 */
104 BufferCapture& with_processing_control(ProcessingControl control);
105
106 /**
107 * @brief Set the number of times to execute this capture operation.
108 * @param count Number of sequential executions per pipeline cycle
109 * @return Reference to this BufferCapture for chaining
110 *
111 * Controls how many times the capture operation executes within a single
112 * pipeline cycle. Each execution receives an incrementing cycle number
113 * and calls the `on_data_ready` callback. The buffer is re-read for
114 * each iteration, allowing accumulation of multiple data blocks.
115 *
116 * @note This affects only CAPTURE operations. Other operation types
117 * (TRANSFORM, ROUTE, etc.) always execute once per pipeline cycle.
118 */
119 BufferCapture& for_cycles(uint32_t count);
120
121 /**
122 * @brief Set a condition that stops capture when met.
123 * @param predicate Function returning true when capture should stop
124 * @return Reference to this BufferCapture for chaining
125 */
126 BufferCapture& until_condition(std::function<bool()> predicate);
127
128 /**
129 * @brief Configure windowed capture with overlap.
130 * @param window_size Size of each capture window
131 * @param overlap_ratio Overlap between windows (0.0-1.0, default: 0.0)
132 * @return Reference to this BufferCapture for chaining
133 */
134 BufferCapture& with_window(uint32_t window_size, float overlap_ratio = 0.0F);
135
136 /**
137 * @brief Enable circular buffer mode with fixed size.
138 * @param size Fixed size of the circular buffer
139 * @return Reference to this BufferCapture for chaining
140 */
141 BufferCapture& as_circular(uint32_t size);
142
143 /**
144 * @brief Set callback for when captured data is ready.
145 * @param callback Function called with captured data and cycle number
146 * @return Reference to this BufferCapture for chaining
147 */
149
150 /**
151 * @brief Set callback for when a capture cycle completes.
152 * @param callback Function called with cycle number
153 * @return Reference to this BufferCapture for chaining
154 */
155 BufferCapture& on_cycle_complete(std::function<void(uint32_t)> callback);
156
157 /**
158 * @brief Set callback for when captured data expires.
159 * @param callback Function called with expired data and cycle number
160 * @return Reference to this BufferCapture for chaining
161 */
162 BufferCapture& on_data_expired(std::function<void(const Kakshya::DataVariant&, uint32_t)> callback);
163
164 /**
165 * @brief Assign a tag for identification and organization.
166 * @param tag String identifier for this capture
167 * @return Reference to this BufferCapture for chaining
168 */
169 BufferCapture& with_tag(const std::string& tag);
170
171 /**
172 * @brief Add metadata key-value pair.
173 * @param key Metadata key
174 * @param value Metadata value
175 * @return Reference to this BufferCapture for chaining
176 */
177 BufferCapture& with_metadata(const std::string& key, const std::string& value);
178
179 /** @brief Return the configured audio source. */
180 inline std::shared_ptr<Buffers::AudioBuffer> get_audio_buffer() const { return m_audio_buffer; }
181 /** @brief Return the configured graphics source. */
182 inline std::shared_ptr<Buffers::VKBuffer> get_graphics_buffer() const { return m_graphics_buffer; }
183 /** @brief Return the configured source buffer. */
184 std::shared_ptr<Buffers::Buffer> get_source_buffer() const;
185 inline CaptureMode get_mode() const { return m_mode; }
186 inline ProcessingControl get_processing_control() const { return m_processing_control; }
187 inline uint32_t get_cycle_count() const { return m_cycle_count; }
188 inline const std::string& get_tag() const { return m_tag; }
189 inline uint32_t get_circular_size() const { return m_circular_size; }
190 inline uint32_t get_window_size() const { return m_window_size; }
191 inline float get_overlap_ratio() const { return m_overlap_ratio; }
192 inline void set_processing_control(ProcessingControl control) { m_processing_control = control; }
193
194 private:
195 std::shared_ptr<Buffers::AudioBuffer> m_audio_buffer;
196 std::shared_ptr<Buffers::VKBuffer> m_graphics_buffer;
197 CaptureMode m_mode { CaptureMode::TRANSIENT };
198 ProcessingControl m_processing_control = ProcessingControl::AUTOMATIC;
199 uint32_t m_cycle_count { 1 };
200 uint32_t m_window_size {};
201 uint32_t m_circular_size {};
202 float m_overlap_ratio {};
203
204 std::function<bool()> m_stop_condition;
206 std::function<void(uint32_t)> m_cycle_callback;
207 std::function<void(const Kakshya::DataVariant&, uint32_t)> m_data_expired_callback;
208
209 std::string m_tag;
210 std::unordered_map<std::string, std::string> m_metadata;
211
212 friend class BufferOperation;
213 friend class BufferPipeline;
214 };
215
216 /**
217 * @class CaptureBuilder
218 * @brief Fluent builder interface for constructing BufferCapture configurations.
219 *
220 * CaptureBuilder provides a fluent, chainable API for constructing BufferCapture
221 * objects with complex configurations. It follows the builder pattern to make
222 * capture setup more readable and maintainable, especially for advanced capture
223 * scenarios with multiple parameters and callbacks.
224 *
225 * **Design Philosophy:**
226 * The builder pattern is used here to address the complexity of BufferCapture
227 * configuration while maintaining type safety and compile-time validation.
228 * It provides a more expressive API than constructor overloading for complex
229 * capture scenarios.
230 *
231 * **Integration:**
232 * CaptureBuilder seamlessly converts to BufferOperation, allowing it to be
233 * used directly in BufferPipeline construction without explicit conversion.
234 * This maintains the fluent nature of the Kriya pipeline API.
235 *
236 * **Example Usage:**
237 * ```cpp
238 * auto capture_op = CaptureBuilder(audio_buffer)
239 * .for_cycles(10)
240 * .with_window(512, 0.5f)
241 * .on_data_ready([](const auto& data, uint32_t cycle) {
242 * process_windowed_data(data, cycle);
243 * })
244 * .with_tag("spectral_analysis");
245 *
246 * pipeline >> capture_op >> route_to_container(output_stream);
247 * ```
248 */
249 class MAYAFLUX_API CaptureBuilder {
250 public:
251 /**
252 * @brief Construct builder with a buffer source.
253 * @param buffer Buffer to capture from
254 */
255 explicit CaptureBuilder(std::shared_ptr<Buffers::AudioBuffer> buffer);
256 explicit CaptureBuilder(std::shared_ptr<Buffers::VKBuffer> buffer);
257 explicit CaptureBuilder(std::nullptr_t);
258
259 /**
260 * @brief Set number of cycles to capture (enables ACCUMULATE mode).
261 * @param count Number of processing cycles
262 * @return Reference to this builder for chaining
263 */
264 CaptureBuilder& for_cycles(uint32_t count);
265
266 /**
267 * @brief Set processing control to ON_CAPTURE mode.
268 * @return Reference to this builder for chaining
269 */
270 CaptureBuilder& on_capture_processing();
271
272 /**
273 * @brief Set processing control to MANUAL mode.
274 * @return Reference to this builder for chaining
275 */
276 CaptureBuilder& manual_processing();
277
278 /**
279 * @brief Set processing control to AUTOMATIC mode.
280 * @return Reference to this builder for chaining
281 */
282 CaptureBuilder& auto_processing();
283
284 /**
285 * @brief Set stop condition (enables TRIGGERED mode).
286 * @param predicate Function returning true when capture should stop
287 * @return Reference to this builder for chaining
288 */
289 CaptureBuilder& until_condition(std::function<bool()> predicate);
290
291 /**
292 * @brief Enable circular buffer mode with specified size.
293 * @param buffer_size Fixed size of the circular buffer
294 * @return Reference to this builder for chaining
295 */
296 CaptureBuilder& as_circular(uint32_t buffer_size);
297
298 /**
299 * @brief Configure windowed capture (enables WINDOWED mode).
300 * @param window_size Size of each capture window
301 * @param overlap_ratio Overlap between windows (0.0-1.0)
302 * @return Reference to this builder for chaining
303 */
304 CaptureBuilder& with_window(uint32_t window_size, float overlap_ratio = 0.0F);
305
306 /**
307 * @brief Set data ready callback.
308 * @param callback Function called when data is available
309 * @return Reference to this builder for chaining
310 */
312
313 /**
314 * @brief Set cycle completion callback.
315 * @param callback Function called at end of each cycle
316 * @return Reference to this builder for chaining
317 */
318 CaptureBuilder& on_cycle_complete(std::function<void(uint32_t)> callback);
319
320 /**
321 * @brief Set data expiration callback.
322 * @param callback Function called when data expires
323 * @return Reference to this builder for chaining
324 */
325 CaptureBuilder& on_data_expired(std::function<void(const Kakshya::DataVariant&, uint32_t)> callback);
326
327 /**
328 * @brief Assign identification tag.
329 * @param tag String identifier for this capture
330 * @return Reference to this builder for chaining
331 */
332 CaptureBuilder& with_tag(const std::string& tag);
333
334 /**
335 * @brief Add metadata key-value pair.
336 * @param key Metadata key
337 * @param value Metadata value
338 * @return Reference to this builder for chaining
339 */
340 CaptureBuilder& with_metadata(const std::string& key, const std::string& value);
341
342 /**
343 * @brief Get the assigned tag for this capture.
344 * @return Tag string
345 */
346 inline std::string get_tag() { return m_capture.get_tag(); }
347
348 /**
349 * @brief Convert to BufferOperation for pipeline integration.
350 * @return BufferOperation containing this capture configuration
351 */
352 operator BufferOperation();
353
354 private:
355 BufferCapture m_capture; ///< Internal BufferCapture being configured
356 };
357
358}
359}
bool size
std::shared_ptr< NetworkGeometryBuffer > buffer
PickCallback callback
Definition Picker.cpp:22
uint32_t mode
glm::vec3 value
size_t count
OperationFunction m_data_ready_callback
Definition Capture.hpp:205
std::shared_ptr< Buffers::AudioBuffer > get_audio_buffer() const
Return the configured audio source.
Definition Capture.hpp:180
std::function< void(const Kakshya::DataVariant &, uint32_t)> m_data_expired_callback
Definition Capture.hpp:207
std::shared_ptr< Buffers::VKBuffer > m_graphics_buffer
Definition Capture.hpp:196
ProcessingControl get_processing_control() const
Definition Capture.hpp:186
CaptureMode
Defines the data capture and retention strategy.
Definition Capture.hpp:61
std::shared_ptr< Buffers::VKBuffer > get_graphics_buffer() const
Return the configured graphics source.
Definition Capture.hpp:182
std::function< bool()> m_stop_condition
Definition Capture.hpp:204
void set_processing_control(ProcessingControl control)
Definition Capture.hpp:192
ProcessingControl
Controls how and when data processing occurs.
Definition Capture.hpp:73
CaptureMode get_mode() const
Definition Capture.hpp:185
uint32_t get_window_size() const
Definition Capture.hpp:190
std::shared_ptr< Buffers::AudioBuffer > m_audio_buffer
Definition Capture.hpp:195
uint32_t get_cycle_count() const
Definition Capture.hpp:187
BufferCapture(std::nullptr_t)
Definition Capture.hpp:80
std::unordered_map< std::string, std::string > m_metadata
Definition Capture.hpp:210
const std::string & get_tag() const
Definition Capture.hpp:188
std::function< void(uint32_t)> m_cycle_callback
Definition Capture.hpp:206
uint32_t get_circular_size() const
Definition Capture.hpp:189
Flexible data capture interface for buffer-based processing pipelines.
Definition Capture.hpp:55
Fundamental unit of operation in buffer processing pipelines.
Coroutine-based execution engine for composable, multi-strategy buffer processing.
std::string get_tag()
Get the assigned tag for this capture.
Definition Capture.hpp:346
BufferCapture m_capture
Internal BufferCapture being configured.
Definition Capture.hpp:355
Fluent builder interface for constructing BufferCapture configurations.
Definition Capture.hpp:249
@ Buffers
Buffers, Managers, processors and processing chains.
@ Kriya
Automatable tasks and fluent scheduling api for Nodes and Buffers.
std::variant< std::vector< double >, std::vector< float >, std::vector< uint8_t >, std::vector< uint16_t >, std::vector< uint32_t >, std::vector< std::complex< float > >, std::vector< std::complex< double > >, std::vector< glm::vec2 >, std::vector< glm::vec3 >, std::vector< glm::vec4 >, std::vector< glm::mat4 > > DataVariant
Multi-type data storage for different precision needs.
Definition NDData.hpp:70
std::function< Kakshya::DataVariant(Kakshya::DataVariant &, uint32_t)> TransformationFunction
Definition Capture.hpp:18
std::function< void(Kakshya::DataVariant &, uint32_t)> OperationFunction
Definition Capture.hpp:17
Main namespace for the Maya Flux audio engine.
Definition Runtime.cpp:12