12 uint64_t initial_capacity,
bool circular_mode)
13 : m_sample_rate(sample_rate)
14 , m_num_channels(num_channels)
15 , m_num_frames(initial_capacity)
16 , m_circular_mode(circular_mode)
26 m_data = std::views::iota(0U, num_channels)
27 | std::views::transform([](
auto) {
return DataVariant(std::vector<double> {}); })
28 | std::ranges::to<std::vector>();
37 std::vector<uint64_t> shape;
94 std::span<const double> const_span(spans[0].data(), spans[0].size());
99 auto const_spans = spans | std::views::transform([](
const auto& span) {
100 return std::span<const double>(span.data(), span.size());
103 auto extracted_channels = extract_region_data<double>(
104 std::vector<std::span<const double>>(const_spans.begin(), const_spans.end()),
107 return extracted_channels
108 | std::views::transform([](
auto&& channel) {
109 return DataVariant(std::forward<
decltype(channel)>(channel));
111 | std::ranges::to<std::vector>();
117 if (
m_data.empty() || data.empty())
121 auto dest_span = convert_variant<double>(
m_data[0]);
122 auto src_span = convert_variant<double>(data[0]);
126 size_t channels_to_update = std::min(
m_data.size(), data.size());
129 for (
size_t i = 0; i < channels_to_update; ++i) {
130 auto dest_span = convert_variant<double>(
m_data[i]);
131 auto src_span = convert_variant<double>(data[i]);
147 auto const_spans = spans | std::views::transform([](
const auto& span) {
148 return std::span<const double>(span.data(), span.size());
151 auto extracted_channels = extract_group_data<double>(
152 std::vector<std::span<const double>>(const_spans.begin(), const_spans.end()),
155 return extracted_channels
156 | std::views::transform([](
auto&& channel) {
157 return DataVariant(std::forward<
decltype(channel)>(channel));
159 | std::ranges::to<std::vector>();
166 if (spans.empty() || segments.empty())
169 auto const_spans = spans | std::views::transform([](
const auto& span) {
170 return std::span<const double>(span.data(), span.size());
173 auto extracted_channels = extract_segments_data<double>(
175 std::vector<std::span<const double>>(const_spans.begin(), const_spans.end()),
178 return extracted_channels
179 | std::views::transform([](
auto&& channel) {
180 return DataVariant(std::forward<
decltype(channel)>(channel));
182 | std::ranges::to<std::vector>();
187 if (type ==
typeid(
double)) {
192 "SoundStreamContainer::get_frames_impl: unsupported type requested");
207 auto frame_span = extract_frame<double>(spans[0], frame_index,
m_num_channels);
208 return { frame_span.data(), frame_span.size() };
211 static thread_local std::vector<double> frame_buffer;
212 auto frame_span = extract_frame<double>(spans, frame_index, frame_buffer);
213 return { frame_span.data(), frame_span.size() };
219 std::ranges::fill(
output, 0.0);
223 uint64_t frames_to_copy = std::min<size_t>(num_frames,
m_num_frames - start_frame);
224 uint64_t elements_to_copy = std::min(
226 static_cast<uint64_t
>(
output.size()));
231 if (
offset < interleaved_data.size()) {
232 uint64_t available = std::min<size_t>(elements_to_copy, interleaved_data.size() -
offset);
233 std::copy_n(interleaved_data.begin() +
offset, available,
output.begin());
235 if (available <
output.size()) {
236 std::fill(
output.begin() + available,
output.end(), 0.0);
239 std::ranges::fill(
output, 0.0);
257 std::ranges::for_each(
m_data, [](
auto& vec) {
258 std::visit([](
auto& v) { v.clear(); }, vec);
261 std::visit([](
auto& v) { v.clear(); }, vec);
278 return span.empty() ? nullptr : span.data();
285 result = std::ranges::any_of(
m_data, [](
const auto& variant) {
286 return std::visit([](
const auto& vec) {
return !vec.empty(); }, variant);
302 std::optional<RegionGroup> result;
307 return result.value_or(empty_group);
312 std::optional<std::unordered_map<std::string, RegionGroup>> result;
317 return result.value_or(std::unordered_map<std::string, RegionGroup> {});
349 for (
size_t i = 0; i < wrapped_pos.size(); ++i) {
363 static thread_local std::vector<uint64_t> pos_cache;
385 for (
size_t i = 0; i < new_pos.size() && i <
m_read_position.size(); ++i) {
406 std::vector<uint64_t> start_pos;
415 m_read_position = std::vector<std::atomic<uint64_t>>(start_pos.size());
418 for (
size_t i = 0; i < start_pos.size(); ++i) {
451 bool outside_loop =
false;
481 for (
auto& frame : frames) {
482 frame = std::numeric_limits<uint64_t>::max();
487 for (
size_t i = 0; i < frames.size(); i++) {
499 std::vector<uint64_t> advance_amount(
m_num_channels, frames_to_advance);
508 if (interleaved_span.empty() ||
output.empty())
513 uint64_t elements_to_read = std::min<uint64_t>(
count,
static_cast<uint64_t
>(
output.size()));
517 if (linear_start >= interleaved_span.size()) {
518 std::ranges::fill(
output, 0.0);
521 auto view = interleaved_span
522 | std::views::drop(linear_start)
523 | std::views::take(elements_to_read);
525 auto copied = std::ranges::copy(view,
output.begin());
526 std::ranges::fill(
output.subspan(copied.out -
output.begin()), 0.0);
527 return static_cast<uint64_t
>(copied.out -
output.begin());
531 std::ranges::fill(
output, 0.0);
537 uint64_t loop_length_frames = loop_end_frame - loop_start_frame + 1;
539 std::ranges::for_each(
540 std::views::iota(0UZ, elements_to_read),
546 uint64_t wrapped_frame = ((frame_pos - loop_start_frame) % loop_length_frames) + loop_start_frame;
547 uint64_t wrapped_element = wrapped_frame *
m_num_channels + channel_offset;
549 output[i] = (wrapped_element < interleaved_span.size()) ? interleaved_span[wrapped_element] : 0.0;
552 if (elements_to_read <
output.size()) {
553 std::ranges::fill(
output.subspan(elements_to_read), 0.0);
555 return elements_to_read;
562 if (old_state != new_state) {
597 auto processor = std::make_shared<ContiguousAccessProcessor>();
616 old_processor->on_detach(shared_from_this());
620 processor->on_attach(shared_from_this());
650 auto& [dim,
count] = *it;
674 "Attempted to mark dimension {} as consumed for unknown reader_id {}. "
675 "This may indicate the reader was not registered or has already been unregistered. "
676 "Please ensure readers are properly registered before marking dimensions as consumed.",
677 dimension_index, reader_id);
685 result = std::ranges::all_of(
m_active_readers, [
this](
const auto& dim_reader_pair) {
686 const auto& [dim, expected_count] = dim_reader_pair;
688 [dim](
const auto& reader_dims_pair) {
689 return reader_dims_pair.second.contains(dim);
691 return actual_count >= expected_count;
701 [](
auto& reader_dims_pair) {
702 reader_dims_pair.second.clear();
724 auto current_span = convert_variant<double>(
m_data[0]);
725 std::vector<double> current_data(current_span.begin(), current_span.end());
727 auto channels = deinterleave_channels<double>(
728 std::span<const double>(current_data.data(), current_data.size()),
731 std::vector<double> reorganized_data;
735 reorganized_data.reserve(current_data.size());
736 for (
const auto& channel : channels) {
737 reorganized_data.insert(reorganized_data.end(), channel.begin(), channel.end());
759 std::span<const double> result;
761 auto span = convert_variant<double>(
m_data[0]);
762 result = { span.data(), span.size() };
769 auto channels = spans
770 | std::views::transform([](
const auto& span) {
771 return std::vector<double>(span.begin(), span.end());
773 | std::ranges::to<std::vector>();
783 if (channel >=
m_data.size()) {
784 error<std::out_of_range>(
787 std::source_location::current(),
788 "Channel index {} out of range (max {})",
789 channel,
m_data.size() - 1);
796 std::vector<DataAccess> result;
797 result.reserve(
m_data.size());
815 | std::views::transform([](
auto& variant) {
816 return convert_variant<double>(
const_cast<DataVariant&
>(variant));
818 | std::ranges::to<std::vector>();
833 const std::vector<uint64_t>& coords,
835 const std::type_info& type)
const
837 if (type !=
typeid(
double) || coords.size() != 2 || !
has_data())
840 const uint64_t frame = coords[0];
841 const uint64_t channel = coords[1];
852 if (idx >= spans[0].size())
854 *
static_cast<double*
>(out) = spans[0][idx];
856 if (channel >= spans.size() || frame >= spans[channel].size())
858 *
static_cast<double*
>(out) = spans[channel][frame];
863 const std::vector<uint64_t>& coords,
865 const std::type_info& type)
867 if (type !=
typeid(
double) || coords.size() != 2)
870 const uint64_t frame = coords[0];
871 const uint64_t channel = coords[1];
882 if (idx >= spans[0].size())
884 const_cast<std::span<double>&
>(spans[0])[idx] = *
static_cast<const double*
>(in);
886 if (channel >= spans.size() || frame >= spans[channel].size())
888 const_cast<std::span<double>&
>(spans[channel])[frame] = *
static_cast<const double*
>(in);
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
std::shared_ptr< Core::VKImage > output
Type-erased accessor for NDData with semantic view construction.
std::span< const double > get_frame_typed(uint64_t frame_index) const
void reset_read_position() override
Reset read position to the beginning of the stream.
std::atomic< ProcessingState > m_processing_state
Memory::Seqlock m_reader_lock
std::span< const double > get_data_as_double() const
Get the audio data as a specific type.
void get_frames_typed(std::span< double > output, uint64_t start_frame, uint64_t num_frames) const
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.
std::shared_ptr< DataProcessingChain > m_processing_chain
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
void get_value_impl(const std::vector< uint64_t > &coords, void *out, const std::type_info &type) const override
Type-erased single-element read.
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.
std::vector< DataVariant > m_processed_data
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.
std::vector< DataVariant > m_data
void set_value_impl(const std::vector< uint64_t > &coords, const void *in, const std::type_info &type) override
Type-erased single-element write.
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.
const std::vector< uint64_t > & get_read_position() const override
Get the current read position.
void set_default_processor(const std::shared_ptr< DataProcessor > &processor) override
Set the default data processor for this container.
void load_region(const Region ®ion) override
Load a region into memory.
Memory::Seqlock m_region_lock
Memory::Seqlock m_data_lock
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()
std::shared_ptr< DataProcessingChain > get_processing_chain() override
Get the current processing chain for this container.
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.
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.
std::vector< DataVariant > get_region_group_data(const RegionGroup &group) const override
Get data for multiple regions efficiently.
void get_frames_impl(void *output, size_t count, uint64_t start_frame, uint64_t num_frames, const std::type_info &type) const override
Implementation-specific method to retrieve multiple frames.
void clear() override
Clear all data in the container.
void process_default() override
Process the container's data using the default processor.
Memory::Seqlock m_cb_lock
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
std::shared_ptr< DataProcessor > get_default_processor() const override
Get the current default data processor.
RegionGroup get_region_group(const std::string &name) const override
Get a region group by name.
uint64_t get_frame_size() const override
Get the number of elements that constitute one "frame".
RAII guard that brackets a Seqlock write region.
@ ContainerProcessing
Container operations (Kakshya - file/stream/region processing)
@ Runtime
General runtime operations (default fallback)
@ Kakshya
Containers[Signalsource, Stream, File], Regions, DataProcessors.
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.