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