MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SoundStreamContainer.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Kakshya {
6
7/**
8 * @class SoundStreamContainer
9 * @brief Concrete base implementation for streaming audio containers.
10 *
11 * SoundStreamContainer provides a complete, concrete implementation of all StreamContainer
12 * functionality for audio data. It serves as:
13 * 1. A standalone streaming container for real-time audio processing
14 * 2. A base class for specialized containers like SoundFileContainer
15 *
16 * The container implements all common audio streaming operations including:
17 * - Region management and processing state tracking
18 * - Sequential reading with looping support
19 * - Multi-dimensional data access and coordinate mapping
20 * - Processing chain integration and reader tracking
21 * - Memory layout optimization and data reorganization
22 *
23 * Uses virtual inheritance to support diamond inheritance when used as a base
24 * for FileContainer-derived classes.
25 */
26class MAYAFLUX_API SoundStreamContainer : public virtual StreamContainer {
27public:
28 /**
29 * @brief Construct a SoundStreamContainer with specified parameters.
30 * @param sample_rate Sample rate for temporal calculations
31 * @param num_channels Number of audio channels
32 * @param initial_capacity Initial capacity in frames (0 = minimal allocation)
33 * @param circular_mode If true, acts as circular buffer with fixed capacity
34 */
35 SoundStreamContainer(uint32_t sample_rate = 48000,
36 uint32_t num_channels = 2,
37 uint64_t initial_capacity = 0,
38 bool circular_mode = false);
39
40 ~SoundStreamContainer() override = default;
41
42 std::vector<DataDimension> get_dimensions() const override;
43 uint64_t get_total_elements() const override;
44 MemoryLayout get_memory_layout() const override { return m_structure.memory_layout; }
45 void set_memory_layout(MemoryLayout layout) override;
46
47 uint64_t get_frame_size() const override;
48 uint64_t get_num_frames() const override;
49
50 std::vector<DataVariant> get_region_data(const Region& region) const override;
51 void set_region_data(const Region& region, const std::vector<DataVariant>& data) override;
52
53 std::vector<DataVariant> get_region_group_data(const RegionGroup& group) const override;
54 std::vector<DataVariant> get_segments_data(const std::vector<RegionSegment>& segment) const override;
55
56 std::span<const double> get_frame(uint64_t frame_index) const override;
57 void get_frames(std::span<double> output, uint64_t start_frame, uint64_t num_frames) const override;
58
59 double get_value_at(const std::vector<uint64_t>& coordinates) const override;
60 void set_value_at(const std::vector<uint64_t>& coordinates, double value) override;
61
62 uint64_t coordinates_to_linear_index(const std::vector<uint64_t>& coordinates) const override;
63 std::vector<uint64_t> linear_index_to_coordinates(uint64_t linear_index) const override;
64
65 void clear() override;
66 void lock() override { m_data_mutex.lock(); }
67 void unlock() override { m_data_mutex.unlock(); }
68 bool try_lock() override { return m_data_mutex.try_lock(); }
69
70 const void* get_raw_data() const override;
71 bool has_data() const override;
72
73 inline ContainerDataStructure& get_structure() override { return m_structure; }
74 inline const ContainerDataStructure& get_structure() const override { return m_structure; }
75
76 inline void set_structure(ContainerDataStructure structure) override { m_structure = structure; }
77
78 void add_region_group(const RegionGroup& group) override;
79 const RegionGroup& get_region_group(const std::string& name) const override;
80 std::unordered_map<std::string, RegionGroup> get_all_region_groups() const override;
81 void remove_region_group(const std::string& name) override;
82
83 bool is_region_loaded(const Region& region) const override;
84 void load_region(const Region& region) override;
85 void unload_region(const Region& region) override;
86
87 void set_read_position(const std::vector<uint64_t>& position) override;
88 void update_read_position_for_channel(size_t channel, uint64_t frame) override;
89 const std::vector<uint64_t>& get_read_position() const override;
90 void advance_read_position(const std::vector<uint64_t>& frames) override;
91 bool is_at_end() const override;
92 void reset_read_position() override;
93
94 uint64_t get_temporal_rate() const override { return m_sample_rate; }
95 uint64_t time_to_position(double time) const override;
96 double position_to_time(uint64_t position) const override;
97
98 void set_looping(bool enable) override;
99 bool is_looping() const override { return m_looping_enabled; }
100 void set_loop_region(const Region& region) override;
101 Region get_loop_region() const override;
102
103 bool is_ready() const override;
104 std::vector<uint64_t> get_remaining_frames() const override;
105 uint64_t read_sequential(std::span<double> output, uint64_t count) override;
106 uint64_t peek_sequential(std::span<double> output, uint64_t count, uint64_t offset = 0) const override;
107
108 ProcessingState get_processing_state() const override { return m_processing_state.load(); }
109 void update_processing_state(ProcessingState new_state) override;
110
112 std::function<void(const std::shared_ptr<SignalSourceContainer>&, ProcessingState)> callback) override
113 {
114 std::lock_guard<std::mutex> lock(m_state_mutex);
115 m_state_callback = callback;
116 }
117
119 {
120 std::lock_guard<std::mutex> lock(m_state_mutex);
121 m_state_callback = nullptr;
122 }
123
124 bool is_ready_for_processing() const override;
125 void mark_ready_for_processing(bool ready) override;
126
127 void create_default_processor() override;
128 void process_default() override;
129
130 void set_default_processor(const std::shared_ptr<DataProcessor>& processor) override;
131 std::shared_ptr<DataProcessor> get_default_processor() const override;
132
133 std::shared_ptr<DataProcessingChain> get_processing_chain() override { return m_processing_chain; }
134 void set_processing_chain(const std::shared_ptr<DataProcessingChain>& chain) override { m_processing_chain = chain; }
135
136 uint32_t register_dimension_reader(uint32_t dimension_index) override;
137 void unregister_dimension_reader(uint32_t dimension_index) override;
138 bool has_active_readers() const override;
139 void mark_dimension_consumed(uint32_t dimension_index, uint32_t reader_id) override;
140 bool all_dimensions_consumed() const override;
141
142 virtual void clear_all_consumption();
143
144 inline std::vector<DataVariant>& get_processed_data() override
145 {
146 return m_processed_data;
147 }
148
149 inline const std::vector<DataVariant>& get_processed_data() const override
150 {
151 return m_processed_data;
152 }
153
154 void mark_buffers_for_processing(bool) override { /* Delegate to buffer integration */ }
155 void mark_buffers_for_removal() override { /* Delegate to buffer integration */ }
156
157 // void ensure_capacity(uint64_t required_frames);
158 // uint64_t get_capacity() const;
159 // void set_circular_mode(bool enable, std::optional<uint64_t> fixed_capacity = std::nullopt);
160 // bool is_circular_mode() const { return m_circular_mode; }
161
162 // void setup(uint64_t num_frames, uint32_t sample_rate, uint32_t num_channels);
163 // void set_raw_data(const DataVariant& data);
164 // void set_all_raw_data(const DataVariant& data);
165
166 virtual uint32_t get_sample_rate() const { return m_sample_rate; }
167
168 inline virtual uint32_t get_num_channels() const
169 {
170 return static_cast<uint32_t>(m_structure.get_channel_count());
171 }
172 // double get_duration_seconds() const;
173
174 inline void reset_processing_token() override { m_processing_token_channel.store(-1); }
175
176 inline bool try_acquire_processing_token(int channel) override
177 {
178 int expected = -1;
179 return m_processing_token_channel.compare_exchange_strong(expected, channel);
180 }
181
182 inline bool has_processing_token(int channel) const override
183 {
184 return m_processing_token_channel.load() == channel;
185 }
186
187 /**
188 * @brief Get the audio data as a specific type
189 * @return Span of double data for direct access
190 */
191 std::span<const double> get_data_as_double() const;
192
193 inline const std::vector<DataVariant>& get_data() override { return m_data; }
194
195 /**
196 * @brief Get channel data with semantic interpretation
197 * @param channel Channel index
198 * @return Type-erased data accessor
199 */
200 DataAccess channel_data(size_t channel) override;
201
202 /**
203 * @brief Get all channel data as accessors
204 */
205 std::vector<DataAccess> all_channel_data() override;
206
207protected:
208 void setup_dimensions();
209 void notify_state_change(ProcessingState new_state);
210 void reorganize_data_layout(MemoryLayout new_layout);
211
212 /** @brief Get the cached spans for each channel, recomputing if dirty */
213 const std::vector<std::span<double>>& get_span_cache() const;
214
215 /** @brief Invalidate the span cache when data or layout changes */
216 void invalidate_span_cache();
217
218 std::vector<DataVariant> m_data;
219 std::vector<DataVariant> m_processed_data;
220
221 std::atomic<int> m_processing_token_channel { -1 };
222
223 uint32_t m_sample_rate = 48000;
224 uint32_t m_num_channels {};
225 uint64_t m_num_frames {};
226
227 std::vector<std::atomic<uint64_t>> m_read_position;
228 bool m_looping_enabled = false;
230
231 bool m_circular_mode {};
232 uint64_t m_circular_write_position {};
233
234 std::atomic<ProcessingState> m_processing_state { ProcessingState::IDLE };
235 std::shared_ptr<DataProcessor> m_default_processor;
236 std::shared_ptr<DataProcessingChain> m_processing_chain;
237
238 std::unordered_map<std::string, RegionGroup> m_region_groups;
239
240 std::unordered_map<uint32_t, int> m_active_readers;
241 std::unordered_set<uint32_t> m_consumed_dimensions;
242
243 std::unordered_map<uint32_t, std::unordered_set<uint32_t>> m_reader_consumed_dimensions;
244 std::unordered_map<uint32_t, uint32_t> m_dimension_to_next_reader_id;
245
246 std::function<void(std::shared_ptr<SignalSourceContainer>, ProcessingState)> m_state_callback;
247
248 mutable std::shared_mutex m_data_mutex;
249 mutable std::mutex m_state_mutex;
250 mutable std::mutex m_reader_mutex;
251
252 mutable std::vector<double> m_cached_ext_buffer;
253
254 mutable std::atomic<bool> m_double_extraction_dirty { true };
255 mutable std::mutex m_extraction_mutex;
256
258
259private:
260 mutable std::optional<std::vector<std::span<double>>> m_span_cache;
261 mutable std::atomic<bool> m_span_cache_dirty { true };
262};
263
264} // namespace MayaFlux::Kakshya
size_t count
Type-erased accessor for NDData with semantic view construction.
std::optional< std::vector< std::span< double > > > m_span_cache
std::unordered_map< std::string, RegionGroup > m_region_groups
std::unordered_map< uint32_t, int > m_active_readers
std::unordered_set< uint32_t > m_consumed_dimensions
const std::vector< DataVariant > & get_data() override
Get a reference to the raw data stored in the container.
const std::vector< DataVariant > & get_processed_data() const override
Get a const reference to the processed data buffer.
std::vector< DataVariant > & get_processed_data() override
Get a mutable reference to the processed data buffer.
std::vector< std::atomic< uint64_t > > m_read_position
void unregister_state_change_callback() override
Unregister the state change callback, if any.
std::shared_ptr< DataProcessingChain > m_processing_chain
void set_structure(ContainerDataStructure structure) override
Set the data structure for this container.
void unlock() override
Release a previously acquired lock.
bool is_looping() const override
Check if looping is enabled for the stream.
void set_processing_chain(const std::shared_ptr< DataProcessingChain > &chain) override
Set the processing chain for this container.
const ContainerDataStructure & get_structure() const override
void mark_buffers_for_processing(bool) override
Mark associated buffers for processing in the next cycle.
MemoryLayout get_memory_layout() const override
Get the memory layout used by this container.
void register_state_change_callback(std::function< void(const std::shared_ptr< SignalSourceContainer > &, ProcessingState)> callback) override
Register a callback to be invoked on processing state changes.
ProcessingState get_processing_state() const override
Get the current processing state of the container.
bool try_acquire_processing_token(int channel) override
std::shared_ptr< DataProcessingChain > get_processing_chain() override
Get the current processing chain for this container.
std::unordered_map< uint32_t, std::unordered_set< uint32_t > > m_reader_consumed_dimensions
std::shared_ptr< DataProcessor > m_default_processor
uint64_t get_temporal_rate() const override
Get the temporal rate (e.g., sample rate, frame rate) of the stream.
void lock() override
Acquire a lock for thread-safe access.
bool has_processing_token(int channel) const override
ContainerDataStructure & get_structure() override
Get the data structure defining this container's layout.
void mark_buffers_for_removal() override
Mark associated buffers for removal from the system.
bool try_lock() override
Attempt to acquire a lock without blocking.
std::function< void(std::shared_ptr< SignalSourceContainer >, ProcessingState)> m_state_callback
std::unordered_map< uint32_t, uint32_t > m_dimension_to_next_reader_id
Concrete base implementation for streaming audio containers.
Data-driven interface for temporal stream containers with navigable read position.
ProcessingState
Represents the current processing lifecycle state of a container.
std::optional< RegionGroup > get_region_group(const std::unordered_map< std::string, RegionGroup > &groups, const std::string &name)
Get a RegionGroup by name from a group map.
void add_region_group(std::unordered_map< std::string, RegionGroup > &groups, const RegionGroup &group)
Add a RegionGroup to a group map.
MemoryLayout
Memory layout for multi-dimensional data.
Definition NDData.hpp:39
double position_to_time(uint64_t position, double sample_rate)
Convert position (samples/frames) to time (seconds) given a sample rate.
void remove_region_group(std::unordered_map< std::string, RegionGroup > &groups, const std::string &name)
Remove a RegionGroup by name from a group map.
uint64_t time_to_position(double time, double sample_rate)
Convert time (seconds) to position (samples/frames) given a sample rate.
Container structure for consistent dimension ordering.
Organizes related signal regions into a categorized collection.
Represents a point or span in N-dimensional space.
Definition Region.hpp:73