MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
RegionProcessorBase.cpp
Go to the documentation of this file.
3
5
6namespace MayaFlux::Kakshya {
7
8void RegionProcessorBase::on_attach(const std::shared_ptr<SignalSourceContainer>& container)
9{
10 if (!container) {
11 throw std::invalid_argument("Container cannot be null");
12 }
13
14 m_container_weak = container;
15 m_structure = container->get_structure();
16
17 m_cache_manager = std::make_unique<RegionCacheManager>(m_max_cache_size);
18 m_cache_manager->initialize();
19
21
22 organize_container_data(container);
23
24 container->mark_ready_for_processing(true);
25}
26
27void RegionProcessorBase::on_detach(const std::shared_ptr<SignalSourceContainer>& /*container*/)
28{
29 m_container_weak.reset();
30 m_cache_manager.reset();
31 m_organized_regions.clear();
32 m_current_position.clear();
33}
34
35void RegionProcessorBase::cache_region_if_needed(const RegionSegment& segment, const std::shared_ptr<SignalSourceContainer>& container)
36{
38 return;
39
40 if (m_cache_manager->get_cached_segment(segment)) {
41 return;
42 }
43
44 uint64_t segment_size = segment.get_total_elements();
45 if (segment_size <= m_max_cache_size / 10) { // Use max 10% of cache per segment
46 try {
47 RegionCache cache;
48 cache.data = container->get_region_data(segment.source_region);
49 cache.source_region = segment.source_region;
50 cache.load_time = std::chrono::steady_clock::now();
51
52 m_cache_manager->cache_region(cache);
53 } catch (const std::exception& e) {
55 "Failed to cache region segment: {}", e.what());
56 }
57 }
58}
59
60bool RegionProcessorBase::advance_position(std::vector<uint64_t>& position, uint64_t steps, const OrganizedRegion* region)
61{
62 if (position.empty())
63 return false;
64
65 for (auto& pos : position) {
66 pos += steps;
67 }
68
69 if ((region != nullptr) && region->looping_enabled && !region->loop_start.empty() && !region->loop_end.empty()) {
70 for (size_t dim = 0; dim < std::min(position.size(), region->loop_start.size()); ++dim) {
71 if (position[dim] >= region->loop_end[dim]) {
72 position[dim] = region->loop_start[dim] + (position[dim] - region->loop_end[dim]);
73 }
74 }
75 }
76
77 auto frame_count = m_structure.get_frame_count();
78 for (auto& pos : position) {
79 if (pos >= frame_count) {
80 pos = frame_count - 1;
81 return false;
82 }
83 }
84
85 return true;
86}
87
88void RegionProcessorBase::ensure_output_dimensioning(std::vector<DataVariant>& output_data, const std::vector<uint64_t>& required_shape)
89{
90 if (output_data.size() < required_shape[1]) {
91 output_data.resize(required_shape[1], DataVariant {});
92 }
93
94 for (auto& data : output_data) {
95 std::visit([required_shape](auto& dt) {
96 if (dt.size() < required_shape[0]) {
97 dt.resize(required_shape[0]);
98 }
99 },
100 data);
101 }
102}
103
104}
#define MF_WARN(comp, ctx,...)
virtual void organize_container_data(const std::shared_ptr< SignalSourceContainer > &container)=0
Organize container data into structured regions.
virtual void cache_region_if_needed(const RegionSegment &segment, const std::shared_ptr< SignalSourceContainer > &container)
Cache a region's data if beneficial and not already cached.
std::unique_ptr< RegionCacheManager > m_cache_manager
virtual bool advance_position(std::vector< uint64_t > &position, uint64_t steps=1, const OrganizedRegion *region=nullptr)
Advance position according to memory layout and looping.
virtual void ensure_output_dimensioning(std::vector< DataVariant > &output_data, const std::vector< uint64_t > &required_shape)
Ensure output data is properly dimensioned for region extraction.
std::vector< OrganizedRegion > m_organized_regions
void on_detach(const std::shared_ptr< SignalSourceContainer > &container) override
Detach this processor from its container.
void on_attach(const std::shared_ptr< SignalSourceContainer > &container) override
Attach this processor to a signal source container.
std::weak_ptr< SignalSourceContainer > m_container_weak
@ ContainerProcessing
Container operations (Kakshya - file/stream/region processing)
@ Kakshya
Containers[Signalsource, Stream, File], Regions, DataProcessors.
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
static size_t get_frame_size(const std::vector< DataDimension > &dimensions)
Extract the size of non time dimensions (channel, spatial, frequency)
static size_t get_frame_count(const std::vector< DataDimension > &dimensions)
Extract frame count from video dimensions.
std::vector< uint64_t > loop_start
Loop start coordinates.
std::vector< uint64_t > loop_end
Loop end coordinates.
A structured audio region with metadata and transition information.
Region source_region
Region this cache corresponds to.
std::vector< DataVariant > data
Cached data.
std::chrono::steady_clock::time_point load_time
When cache was loaded.
Stores cached data for a region, with metadata for cache management.
Region source_region
Associated region.
uint64_t get_total_elements() const
Get the total number of elements in the segment.
Represents a discrete segment of audio data with caching capabilities.