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