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