MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
RegionProcessors.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <random>
6
7namespace MayaFlux::Kakshya {
8
9static bool is_segment_complete(const OrganizedRegion& region, size_t segment_index);
10
11/**
12 * @class RegionOrganizationProcessor
13 * @brief Data-driven processor for organizing and processing non-linear audio regions.
14 *
15 * RegionOrganizationProcessor enables advanced workflows by treating audio
16 * as structured, navigable regions rather than linear streams. It supports:
17 * - Dynamic grouping of audio into regions and segments with arbitrary metadata.
18 * - Non-linear playback, editing, and analysis based on region structure.
19 * - Region-level transitions, looping, and selection patterns (sequential, random, etc.).
20 * - Efficient caching and navigation for interactive or content-driven applications.
21 *
22 * This processor is foundational for workflows such as:
23 * - Interactive audio editing and arrangement
24 * - Adaptive playback and generative audio
25 * - Feature-driven or content-aware region processing
26 * - Seamless integration with digital-first nodes, routines, and buffer systems
27 *
28 * Regions are organized using the OrganizedRegion abstraction, which supports metadata,
29 * transitions, and flexible segment definitions. All processing is data-driven and
30 * unconstrained by analog metaphors.
31 */
33public:
34 /**
35 * @brief Construct a region organization processor for a given container.
36 * @param container The signal container to process.
37 */
38 RegionOrganizationProcessor(std::shared_ptr<SignalSourceContainer> container);
39
40 /**
41 * @brief Organize the container's data into regions and segments.
42 * Must be implemented to define region logic for the container.
43 * @param container The signal container to organize.
44 */
45 void organize_container_data(std::shared_ptr<SignalSourceContainer> container) override;
46
47 /**
48 * @brief Processes audio data according to the current region organization.
49 * Applies region selection, transitions, and looping as configured.
50 * @param container The signal container to process.
51 */
52 void process(std::shared_ptr<SignalSourceContainer> container) override;
53
54 /**
55 * @brief Creates a new region group for organizing related regions.
56 * @param group_name Name of the group to create.
57 */
58 void add_region_group(const std::string& group_name);
59
60 /**
61 * @brief Adds a segment to an existing region.
62 * @param group_name Name of the group containing the region.
63 * @param region_index Index of the region within the group.
64 * @param start_coords Starting coordinates of the segment.
65 * @param end_coords Ending coordinates of the segment.
66 * @param attributes Arbitrary metadata for the segment.
67 */
68 void add_segment_to_region(
69 const std::string& group_name,
70 size_t region_index,
71 const std::vector<uint64_t>& start_coords,
72 const std::vector<uint64_t>& end_coords,
73 const std::unordered_map<std::string, std::any>& attributes);
74
75 /**
76 * @brief Configures the transition between regions.
77 * @param group_name Name of the group containing the region.
78 * @param region_index Index of the region within the group.
79 * @param type Type of transition to apply (e.g., IMMEDIATE, FADE).
80 * @param duration_ms Duration of the transition in milliseconds.
81 */
82 void set_region_transition(const std::string& group_name,
83 size_t region_index, RegionTransition type, double duration_ms = 0.0);
84
85 /**
86 * @brief Enable or disable looping for a region.
87 * @param group_name Name of the group containing the region.
88 * @param region_index Index of the region within the group.
89 * @param enabled True to enable looping.
90 * @param loop_start Optional start coordinates for the loop.
91 * @param loop_end Optional end coordinates for the loop.
92 */
93 void set_region_looping(const std::string& group_name,
94 size_t region_index,
95 bool enabled,
96 const std::vector<uint64_t>& loop_start = {},
97 const std::vector<uint64_t>& loop_end = {});
98
99 /**
100 * @brief Jump to a specific region for processing or playback.
101 * @param group_name Name of the group.
102 * @param region_index Index of the region within the group.
103 */
104 void jump_to_region(const std::string& group_name, size_t region_index);
105
106 /**
107 * @brief Jump to a specific position in the data.
108 * @param position N-dimensional coordinates to jump to.
109 */
110 void jump_to_position(const std::vector<uint64_t>& position);
111
112 /**
113 * @brief Set the selection pattern for a region (e.g., sequential, random).
114 * @param group_name Name of the group.
115 * @param region_index Index of the region within the group.
116 * @param pattern Selection pattern to use.
117 */
118 void set_selection_pattern(const std::string& group_name, size_t region_index, RegionSelectionPattern pattern);
119
120protected:
121 /**
122 * @brief Process regions according to their selection pattern.
123 * @param container The signal container to process.
124 * @param output_data Output data variant to fill.
125 */
126 virtual void process_organized_regions(const std::shared_ptr<SignalSourceContainer>& container,
127 std::vector<DataVariant>& output_data);
128
129 /**
130 * @brief Process a single region segment.
131 * @param region The organized region.
132 * @param segment The segment within the region.
133 * @param container The signal container.
134 * @param output_data Output data variant to fill.
135 */
136 virtual void process_region_segment(const OrganizedRegion& region,
137 const RegionSegment& segment,
138 const std::shared_ptr<SignalSourceContainer>& container,
139 std::vector<DataVariant>& output_data);
140
141 /**
142 * @brief Apply a transition between two regions.
143 * @param current_region The current region.
144 * @param next_region The next region.
145 * @param container The signal container.
146 * @param output_data Output data variant to fill.
147 */
148 virtual void apply_region_transition(const OrganizedRegion& current_region,
149 const OrganizedRegion& next_region,
150 const std::shared_ptr<SignalSourceContainer>& container,
151 std::vector<DataVariant>& output_data);
152
153 /**
154 * @brief Select the next segment to process according to the region's pattern.
155 * @param region The organized region.
156 * @return Index of the next segment.
157 */
158 virtual size_t select_next_segment(const OrganizedRegion& region) const;
159
160 std::optional<size_t> find_region_for_position(
161 const std::vector<uint64_t>& position,
162 const std::vector<OrganizedRegion>& regions) const;
163
164private:
165 // Random number generation for stochastic selection
166 mutable std::mt19937 m_random_engine { std::random_device {}() };
167 mutable std::vector<double> m_segment_weights;
168
169 /**
170 * @brief Organize a group of regions within the container.
171 * @param container The signal container.
172 * @param group The region group to organize.
173 */
174 void organize_group(const std::shared_ptr<SignalSourceContainer>& container,
175 const RegionGroup& group);
176
177 /**
178 * @brief Refresh the internal organization of regions.
179 */
180 void refresh_organized_data();
181};
182
183/**
184 * @typedef RegionOrganizer
185 * @brief Function type for dynamic region reorganization.
186 *
187 * Used to provide custom logic for reorganizing regions at runtime, enabling
188 * adaptive and content-driven workflows.
189 */
190using RegionOrganizer = std::function<void(std::vector<OrganizedRegion>&, std::shared_ptr<SignalSourceContainer>)>;
191
192/**
193 * @class DynamicRegionProcessor
194 * @brief Extends RegionOrganizationProcessor with dynamic, runtime reorganization capabilities.
195 *
196 * DynamicRegionProcessor enables adaptive, data-driven audio workflows by allowing
197 * regions to be reorganized at runtime based on content analysis, user interaction,
198 * or external signals. This supports:
199 * - Real-time adaptation to audio features or events
200 * - Interactive or generative region arrangement
201 * - Automated reorganization based on custom criteria
202 *
203 * The processor can trigger reorganization on demand or automatically based on
204 * user-defined criteria, making it ideal for advanced digital-first applications.
205 */
207public:
208 /**
209 * @brief Construct a dynamic region processor for a given container.
210 * @param container The signal container to process.
211 */
212 DynamicRegionProcessor(const std::shared_ptr<SignalSourceContainer>& container);
213
214 /**
215 * @brief Sets the callback for region reorganization.
216 * @param callback Function to call for reorganization.
217 */
218 void set_reorganization_callback(RegionOrganizer callback);
219
220 /**
221 * @brief Processes audio, performing reorganization if needed.
222 * @param container The signal container to process.
223 */
224 void process(std::shared_ptr<SignalSourceContainer> container) override;
225
226 /**
227 * @brief Triggers a reorganization on the next processing cycle.
228 */
229 void trigger_reorganization();
230
231 /**
232 * @brief Set automatic reorganization based on custom criteria.
233 * @param criteria Function that returns true if reorganization should occur.
234 */
235 inline void set_auto_reorganization(std::function<bool(const std::vector<OrganizedRegion>&,
236 std::shared_ptr<SignalSourceContainer>)>
237 criteria)
238 {
239 m_auto_reorganization_criteria = std::move(criteria);
240 }
241
242private:
243 std::atomic<bool> m_needs_reorganization { false }; ///< Flag for pending reorganization
244
245 /**
246 * @brief Checks if reorganization should occur for the current container.
247 * @param container The signal container to check.
248 * @return True if reorganization is needed.
249 */
250 bool should_reorganize(const std::shared_ptr<SignalSourceContainer>& container);
251
252 RegionOrganizer m_reorganizer_callback; ///< Callback for reorganization
253
254 /**
255 * @brief Criteria function for automatic reorganization.
256 * Returns true if reorganization should occur based on current state.
257 */
258 std::function<bool(const std::vector<OrganizedRegion>&,
259 std::shared_ptr<SignalSourceContainer>)>
261};
262
263} // namespace MayaFlux::Kakshya
void set_auto_reorganization(std::function< bool(const std::vector< OrganizedRegion > &, std::shared_ptr< SignalSourceContainer >)> criteria)
Set automatic reorganization based on custom criteria.
RegionOrganizer m_reorganizer_callback
Callback for reorganization.
std::function< bool(const std::vector< OrganizedRegion > &, std::shared_ptr< SignalSourceContainer >)> m_auto_reorganization_criteria
Criteria function for automatic reorganization.
Extends RegionOrganizationProcessor with dynamic, runtime reorganization capabilities.
Data-driven processor for organizing and processing non-linear audio regions.
Base class for N-dimensional region processors.
std::function< void(std::vector< OrganizedRegion > &, std::shared_ptr< SignalSourceContainer >)> RegionOrganizer
Function type for dynamic region reorganization.
bool is_segment_complete(const OrganizedRegion &region, size_t segment_index)
void add_region_group(std::unordered_map< std::string, RegionGroup > &groups, const RegionGroup &group)
Add a RegionGroup to a group map.
RegionSelectionPattern
Describes how regions are selected for processing or playback.
Definition Region.hpp:11
RegionTransition
Describes how transitions between regions are handled.
Definition Region.hpp:26
std::optional< size_t > find_region_for_position(const std::vector< uint64_t > &position, const std::vector< Region > &regions)
Find optimal region containing given position.
A structured audio region with metadata and transition information.
Organizes related signal regions into a categorized collection.
Represents a discrete segment of audio data with caching capabilities.