11 uint64_t initial_capacity,
bool circular_mode)
12 : m_sample_rate(sample_rate)
13 , m_num_channels(num_channels)
14 , m_num_frames(initial_capacity)
15 , m_circular_mode(circular_mode)
24 m_data = std::views::iota(0U, num_channels)
25 | std::views::transform([](
auto) {
return DataVariant(std::vector<double> {}); })
26 | std::ranges::to<std::vector>();
35 std::vector<uint64_t> shape;
92 std::span<const double> const_span(spans[0].data(), spans[0].size());
97 auto const_spans = spans | std::views::transform([](
const auto& span) {
98 return std::span<const double>(span.data(), span.size());
101 auto extracted_channels = extract_region_data<double>(
102 std::vector<std::span<const double>>(const_spans.begin(), const_spans.end()),
105 return extracted_channels
106 | std::views::transform([](
auto&& channel) {
107 return DataVariant(std::forward<
decltype(channel)>(channel));
109 | std::ranges::to<std::vector>();
115 if (
m_data.empty() || data.empty())
120 auto dest_span = convert_variant<double>(
m_data[0]);
121 auto src_span = convert_variant<double>(data[0]);
123 set_or_update_region_data<double>(
127 size_t channels_to_update = std::min(
m_data.size(), data.size());
130 for (
size_t i = 0; i < channels_to_update; ++i) {
131 auto dest_span = convert_variant<double>(
m_data[i]);
132 auto src_span = convert_variant<double>(data[i]);
134 set_or_update_region_data<double>(
150 auto const_spans = spans | std::views::transform([](
const auto& span) {
151 return std::span<const double>(span.data(), span.size());
154 auto extracted_channels = extract_group_data<double>(
155 std::vector<std::span<const double>>(const_spans.begin(), const_spans.end()),
158 return extracted_channels
159 | std::views::transform([](
auto&& channel) {
160 return DataVariant(std::forward<
decltype(channel)>(channel));
162 | std::ranges::to<std::vector>();
169 if (spans.empty() || segments.empty())
172 auto const_spans = spans | std::views::transform([](
const auto& span) {
173 return std::span<const double>(span.data(), span.size());
176 auto extracted_channels = extract_segments_data<double>(
178 std::vector<std::span<const double>>(const_spans.begin(), const_spans.end()),
181 return extracted_channels
182 | std::views::transform([](
auto&& channel) {
183 return DataVariant(std::forward<
decltype(channel)>(channel));
185 | std::ranges::to<std::vector>();
200 auto frame_span = extract_frame<double>(spans[0], frame_index,
m_num_channels);
201 return { frame_span.data(), frame_span.size() };
204 static thread_local std::vector<double> frame_buffer;
205 auto frame_span = extract_frame<double>(spans, frame_index, frame_buffer);
206 return { frame_span.data(), frame_span.size() };
212 std::ranges::fill(output, 0.0);
216 uint64_t frames_to_copy = std::min<size_t>(num_frames,
m_num_frames - start_frame);
217 uint64_t elements_to_copy = std::min(
219 static_cast<uint64_t
>(output.size()));
224 if (offset < interleaved_data.size()) {
225 uint64_t available = std::min<size_t>(elements_to_copy, interleaved_data.size() - offset);
226 std::copy_n(interleaved_data.begin() + offset, available, output.begin());
228 if (available < output.size()) {
229 std::fill(output.begin() + available, output.end(), 0.0);
232 std::ranges::fill(output, 0.0);
238 if (!
has_data() || coordinates.size() != 2) {
242 uint64_t frame = coordinates[0];
243 uint64_t channel = coordinates[1];
256 return (linear_index < spans[0].size()) ? spans[0][linear_index] : 0.0;
259 if (channel >= spans.size())
262 return (frame < spans[channel].size()) ? spans[channel][frame] : 0.0;
267 if (coordinates.size() != 2)
270 uint64_t frame = coordinates[0];
271 uint64_t channel = coordinates[1];
284 if (linear_index < spans[0].size()) {
285 const_cast<std::span<double>&
>(spans[0])[linear_index] = value;
289 if (channel >= spans.size())
292 if (frame < spans[channel].size()) {
293 const_cast<std::span<double>&
>(spans[channel])[frame] = value;
315 std::ranges::for_each(
m_data, [](
auto& vec) {
316 std::visit([](
auto& v) { v.clear(); }, vec);
320 std::visit([](
auto& v) { v.clear(); }, vec);
338 return span.empty() ? nullptr : span.data();
345 return std::ranges::any_of(
m_data, [](
const auto& variant) {
346 return std::visit([](
const auto& vec) {
return !vec.empty(); }, variant);
400 for (
size_t i = 0; i < wrapped_pos.size(); ++i) {
414 static thread_local std::vector<uint64_t> pos_cache;
436 for (
size_t i = 0; i < new_pos.size() && i <
m_read_position.size(); ++i) {
457 std::vector<uint64_t> start_pos;
466 m_read_position = std::vector<std::atomic<uint64_t>>(start_pos.size());
469 for (
size_t i = 0; i < start_pos.size(); ++i) {
502 bool outside_loop =
false;
532 for (
auto& frame : frames) {
533 frame = std::numeric_limits<uint64_t>::max();
538 for (
size_t i = 0; i < frames.size(); i++) {
550 std::vector<uint64_t> advance_amount(
m_num_channels, frames_to_advance);
559 if (interleaved_span.empty() || output.empty())
563 start_frame += offset;
564 uint64_t elements_to_read = std::min<uint64_t>(count,
static_cast<uint64_t
>(output.size()));
568 if (linear_start >= interleaved_span.size()) {
569 std::ranges::fill(output, 0.0);
572 auto view = interleaved_span
573 | std::views::drop(linear_start)
574 | std::views::take(elements_to_read);
576 auto copied = std::ranges::copy(view, output.begin());
577 std::ranges::fill(output.subspan(copied.out - output.begin()), 0.0);
578 return static_cast<uint64_t
>(copied.out - output.begin());
582 std::ranges::fill(output, 0.0);
588 uint64_t loop_length_frames = loop_end_frame - loop_start_frame + 1;
590 std::ranges::for_each(
591 std::views::iota(0UZ, elements_to_read),
597 uint64_t wrapped_frame = ((frame_pos - loop_start_frame) % loop_length_frames) + loop_start_frame;
598 uint64_t wrapped_element = wrapped_frame *
m_num_channels + channel_offset;
600 output[i] = (wrapped_element < interleaved_span.size()) ? interleaved_span[wrapped_element] : 0.0;
603 if (elements_to_read < output.size()) {
604 std::ranges::fill(output.subspan(elements_to_read), 0.0);
606 return elements_to_read;
613 if (old_state != new_state) {
648 auto processor = std::make_shared<ContiguousAccessProcessor>();
667 old_processor->on_detach(shared_from_this());
671 processor->on_attach(shared_from_this());
696 auto& [dim, count] = *it;
715 std::cerr <<
"WARNING: Attempted to mark dimension " << dimension_index
716 <<
" as consumed for unknown reader_id " << reader_id
717 <<
". This may indicate the reader was not registered or has already been unregistered. "
718 <<
"Please ensure readers are properly registered before marking dimensions as consumed."
727 return std::ranges::all_of(
m_active_readers, [
this](
const auto& dim_reader_pair) {
728 const auto& [dim, expected_count] = dim_reader_pair;
731 [dim](
const auto& reader_dims_pair) {
732 return reader_dims_pair.second.contains(dim);
735 return actual_count >= expected_count;
744 [](
auto& reader_dims_pair) {
745 reader_dims_pair.second.clear();
767 auto current_span = convert_variant<double>(
m_data[0]);
768 std::vector<double> current_data(current_span.begin(), current_span.end());
770 auto channels = deinterleave_channels<double>(
771 std::span<const double>(current_data.data(), current_data.size()),
774 std::vector<double> reorganized_data;
778 reorganized_data.reserve(current_data.size());
779 for (
const auto& channel : channels) {
780 reorganized_data.insert(reorganized_data.end(), channel.begin(), channel.end());
804 auto span = convert_variant<double>(
m_data[0]);
805 return { span.data(), span.size() };
810 auto channels = spans
811 | std::views::transform([](
const auto& span) {
812 return std::vector<double>(span.begin(), span.end());
814 | std::ranges::to<std::vector>();
824 if (channel >=
m_data.size()) {
825 throw std::out_of_range(
"Channel index out of range");
832 std::vector<DataAccess> result;
833 result.reserve(
m_data.size());
854 | std::views::transform([](
auto& variant) {
855 return convert_variant<double>(
const_cast<DataVariant&
>(variant));
857 | std::ranges::to<std::vector>();
Type-erased accessor for NDData with semantic view construction.
void reset_read_position() override
Reset read position to the beginning of the stream.
std::atomic< ProcessingState > m_processing_state
std::span< const double > get_data_as_double() const
Get the audio data as a specific type.
std::optional< std::vector< std::span< double > > > m_span_cache
void set_memory_layout(MemoryLayout layout) override
Set the memory layout for this container.
std::unordered_map< std::string, RegionGroup > m_region_groups
double position_to_time(uint64_t position) const override
Convert from position units (e.g., frame/sample index) to time (seconds).
Region get_loop_region() const override
Get the current loop region.
std::unordered_map< uint32_t, int > m_active_readers
void unload_region(const Region ®ion) override
Unload a region from memory.
std::unordered_set< uint32_t > m_consumed_dimensions
uint32_t register_dimension_reader(uint32_t dimension_index) override
Register a reader for a specific dimension.
bool has_active_readers() const override
Check if any dimensions currently have active readers.
void set_looping(bool enable) override
Enable or disable looping behavior for the stream.
uint64_t get_total_elements() const override
Get the total number of elements in the container.
std::vector< std::atomic< uint64_t > > m_read_position
const std::vector< std::span< double > > & get_span_cache() const
Get the cached spans for each channel, recomputing if dirty.
bool is_region_loaded(const Region ®ion) const override
Check if a region is loaded in memory.
bool all_dimensions_consumed() const override
Check if all active dimensions have been consumed in this cycle.
std::vector< double > m_cached_ext_buffer
uint64_t peek_sequential(std::span< double > output, uint64_t count, uint64_t offset=0) const override
Peek at data without advancing the read position.
std::vector< DataVariant > get_region_data(const Region ®ion) const override
Get data for a specific region.
std::atomic< bool > m_double_extraction_dirty
uint64_t time_to_position(double time) const override
Convert from time (seconds) to position units (e.g., frame/sample index).
const void * get_raw_data() const override
Get a raw pointer to the underlying data storage.
void set_default_processor(std::shared_ptr< DataProcessor > processor) override
Set the default data processor for this container.
std::vector< DataVariant > m_processed_data
std::shared_mutex m_data_mutex
void advance_read_position(const std::vector< uint64_t > &frames) override
Advance the read position by a specified amount.
void unregister_dimension_reader(uint32_t dimension_index) override
Unregister a reader for a specific dimension.
void reorganize_data_layout(MemoryLayout new_layout)
void update_processing_state(ProcessingState new_state) override
Update the processing state of the container.
void mark_ready_for_processing(bool ready) override
Mark the container as ready or not ready for processing.
const RegionGroup & get_region_group(const std::string &name) const override
Get a region group by name.
std::vector< DataVariant > m_data
void set_read_position(const std::vector< uint64_t > &position) override
Set the current read position in the primary temporal dimension per channel.
std::vector< uint64_t > linear_index_to_coordinates(uint64_t linear_index) const override
Convert linear index to coordinates based on current memory layout.
void notify_state_change(ProcessingState new_state)
bool has_data() const override
Check if the container currently holds any data.
std::mutex m_reader_mutex
const std::vector< uint64_t > & get_read_position() const override
Get the current read position.
std::span< const double > get_frame(uint64_t frame_index) const override
Get a single frame of data efficiently.
void load_region(const Region ®ion) override
Load a region into memory.
void add_region_group(const RegionGroup &group) override
Add a named group of regions to the container.
ProcessingState get_processing_state() const override
Get the current processing state of the container.
void create_default_processor() override
Create and configure a default processor for this container.
void invalidate_span_cache()
Invalidate the span cache when data or layout changes.
bool is_ready_for_processing() const override
Check if the container is ready for processing.
uint64_t m_circular_write_position
virtual void clear_all_consumption()
uint64_t read_sequential(std::span< double > output, uint64_t count) override
Read data sequentially from the current position.
DataAccess channel_data(size_t channel) override
Get channel data with semantic interpretation.
void get_frames(std::span< double > output, uint64_t start_frame, uint64_t num_frames) const override
Get multiple frames efficiently.
std::vector< DataDimension > get_dimensions() const override
Get the dimensions describing the structure of the data.
std::unordered_map< std::string, RegionGroup > get_all_region_groups() const override
Get all region groups in the container.
bool is_at_end() const override
Check if read position has reached the end of the stream.
std::vector< DataAccess > all_channel_data() override
Get all channel data as accessors.
std::vector< DataVariant > get_segments_data(const std::vector< RegionSegment > &segment) const override
Get data for multiple region segments efficiently.
uint64_t get_num_frames() const override
Get the number of frames in the primary (temporal) dimension.
std::unordered_map< uint32_t, std::unordered_set< uint32_t > > m_reader_consumed_dimensions
std::shared_ptr< DataProcessor > m_default_processor
void remove_region_group(const std::string &name) override
Remove a region group by name.
void lock() override
Acquire a lock for thread-safe access.
void set_value_at(const std::vector< uint64_t > &coordinates, double value) override
Set a single value at the specified coordinates.
std::vector< DataVariant > get_region_group_data(const RegionGroup &group) const override
Get data for multiple regions efficiently.
void clear() override
Clear all data in the container.
void process_default() override
Process the container's data using the default processor.
void set_region_data(const Region ®ion, const std::vector< DataVariant > &data) override
Set data for a specific region.
std::atomic< bool > m_span_cache_dirty
void update_read_position_for_channel(size_t channel, uint64_t frame) override
Update the read position for a specific channel.
uint64_t coordinates_to_linear_index(const std::vector< uint64_t > &coordinates) const override
Convert coordinates to linear index based on current memory layout.
SoundStreamContainer(uint32_t sample_rate=48000, uint32_t num_channels=2, uint64_t initial_capacity=0, bool circular_mode=false)
Construct a SoundStreamContainer with specified parameters.
void set_loop_region(const Region ®ion) override
Set the loop region using a Region.
std::vector< uint64_t > get_remaining_frames() const override
Get the number of remaining frames from the current position, per channel.
void mark_dimension_consumed(uint32_t dimension_index, uint32_t reader_id) override
Mark a dimension as consumed for the current processing cycle.
std::function< void(std::shared_ptr< SignalSourceContainer >, ProcessingState)> m_state_callback
ContainerDataStructure m_structure
bool is_ready() const override
Check if the stream is ready for reading.
std::unordered_map< uint32_t, uint32_t > m_dimension_to_next_reader_id
double get_value_at(const std::vector< uint64_t > &coordinates) const override
Get a single value at the specified coordinates.
std::shared_ptr< DataProcessor > get_default_processor() const override
Get the current default data processor.
uint64_t get_frame_size() const override
Get the number of elements that constitute one "frame".
ProcessingState
Represents the current processing lifecycle state of a container.
@ READY
Container has data loaded and is ready for processing.
@ IDLE
Container is inactive with no data or not ready for processing.
@ PROCESSING
Container is actively being processed.
@ PROCESSED
Container has completed processing and results are available.
uint64_t coordinates_to_linear(const std::vector< uint64_t > &coords, const std::vector< DataDimension > &dimensions)
Convert N-dimensional coordinates to a linear index for interleaved data.
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.
DataModality
Data modality types for cross-modal analysis.
@ AUDIO_MULTICHANNEL
Multi-channel audio.
@ AUDIO_1D
1D audio signal
std::vector< uint64_t > advance_position(const std::vector< uint64_t > ¤t_positions, uint64_t frames_to_advance, const ContainerDataStructure &structure, bool looping_enabled, const Region &loop_region)
Advance current positions by a number of frames, with optional looping.
std::vector< uint64_t > linear_to_coordinates(uint64_t index, const std::vector< DataDimension > &dimensions)
Convert a linear index to N-dimensional coordinates for interleaved data.
MemoryLayout
Memory layout for multi-dimensional data.
@ ROW_MAJOR
C/C++ style (last dimension varies fastest)
double position_to_time(uint64_t position, double sample_rate)
Convert position (samples/frames) to time (seconds) given a sample rate.
OrganizationStrategy
Data organization strategy for multi-channel/multi-frame data.
@ PLANAR
Separate DataVariant per logical unit (LLL...RRR for stereo)
@ INTERLEAVED
Single DataVariant with interleaved data (LRLRLR for stereo)
std::vector< T > interleave_channels(const std::vector< std::vector< T > > &channels)
Interleave multiple channels of data into a single vector.
uint64_t time_to_position(double time, double sample_rate)
Convert time (seconds) to position (samples/frames) given a sample rate.
std::vector< uint64_t > wrap_position_with_loop(const std::vector< uint64_t > &positions, const Region &loop_region, bool looping_enabled)
Wrap a position within loop boundaries if looping is enabled.
std::vector< DataDimension > dimensions
std::optional< size_t > channel_dims
OrganizationStrategy organization
std::optional< size_t > time_dims
static uint64_t get_total_elements(const std::vector< DataDimension > &dimensions)
Get total elements across all dimensions.
MemoryLayout memory_layout
Container structure for consistent dimension ordering.
static std::vector< DataDimension > create_dimensions(DataModality modality, const std::vector< uint64_t > &shape, MemoryLayout layout=MemoryLayout::ROW_MAJOR)
Create dimension descriptors for a data modality.
std::string name
Descriptive name of the group.
Organizes related signal regions into a categorized collection.
static Region time_span(uint64_t start_frame, uint64_t end_frame, const std::string &label="", const std::any &extra_data={})
Create a Region representing a time span (e.g., a segment of frames).
std::vector< uint64_t > end_coordinates
Ending frame index (inclusive)
std::vector< uint64_t > start_coordinates
Starting frame index (inclusive)
Represents a point or span in N-dimensional space.