12 : m_container(container)
15 auto structure = container->get_structure();
18 container->register_state_change_callback(
19 [
this](
auto c,
auto s) {
32 buffer->mark_for_removal();
41 buffer->mark_for_removal();
54 auto audio_buffer = std::dynamic_pointer_cast<AudioBuffer>(buffer);
55 auto& buffer_data = audio_buffer->get_data();
56 uint32_t buffer_size = audio_buffer->get_num_samples();
58 auto read_positions =
m_container->get_read_position();
63 if (buffer_data.size() != buffer_size) {
64 buffer_data.resize(buffer_size);
74 buffer->mark_for_processing(
true);
81 std::dynamic_pointer_cast<Kakshya::SoundFileContainer>(
m_container)->clear_all_consumption();
85 }
catch (
const std::exception& e) {
87 "SoundStreamReader: Error during processing: {}", e.what());
94 std::ranges::fill(output, 0.0);
98 auto sound_container = std::dynamic_pointer_cast<Kakshya::SoundStreamContainer>(
m_container);
99 if (!sound_container) {
100 std::ranges::fill(output, 0.0);
104 auto& processed_data = sound_container->get_processed_data();
105 if (processed_data.empty()) {
106 std::ranges::fill(output, 0.0);
110 auto structure = sound_container->get_structure();
113 thread_local std::vector<double> temp_storage;
114 auto data_span = Kakshya::extract_from_variant<double>(processed_data[0], temp_storage);
116 auto num_channels = structure.get_channel_count();
117 auto samples_to_copy = std::min(
static_cast<size_t>(output.size()),
118 static_cast<size_t>(data_span.size() / num_channels));
120 for (
auto i : std::views::iota(0UZ, samples_to_copy)) {
122 output[i] = (interleaved_idx < data_span.size()) ? data_span[interleaved_idx] : 0.0;
125 if (samples_to_copy < output.size()) {
126 std::ranges::fill(output | std::views::drop(samples_to_copy), 0.0);
131 std::ranges::fill(output, 0.0);
135 thread_local std::vector<double> temp_storage;
136 auto channel_data_span = Kakshya::extract_from_variant<double>(processed_data[
m_source_channel], temp_storage);
138 auto samples_to_copy = std::min(output.size(), channel_data_span.size());
139 std::ranges::copy_n(channel_data_span.begin(), samples_to_copy, output.begin());
141 if (samples_to_copy < output.size()) {
142 std::ranges::fill(output | std::views::drop(samples_to_copy), 0.0);
157 std::source_location::current(),
158 "SoundStreamReader: Container not ready for processing");
162 auto& buffer_data = std::dynamic_pointer_cast<AudioBuffer>(buffer)->get_data();
163 uint32_t num_samples = std::dynamic_pointer_cast<AudioBuffer>(buffer)->get_num_samples();
168 buffer->mark_for_processing(
true);
171 }
catch (
const std::exception& e) {
173 "SoundStreamReader: Error pre-filling buffer: {}", e.what());
189 std::source_location::current(),
190 "SoundStreamReader: Channel index {} exceeds container channel count {}",
205 auto structure = container->get_structure();
208 container->register_state_change_callback(
216 const std::shared_ptr<Kakshya::SignalSourceContainer>& ,
225 "SoundStreamReader: Container entered ERROR state");
234 const std::shared_ptr<Kakshya::StreamContainer>& container,
235 uint32_t source_channel)
237 , m_container(container)
238 , m_source_channel(source_channel)
242 std::source_location::current(),
243 "SoundContainerBuffer: container must not be null");
266 adapter->set_container(container);
299 auto adapter = std::make_shared<SoundStreamReader>(
m_container);
#define MF_ERROR(comp, ctx,...)
std::shared_ptr< BufferProcessor > m_default_processor
Default audio transformation processor for this buffer.
virtual void set_default_processor(const std::shared_ptr< BufferProcessor > &processor) override
Sets the default audio transformation processor for this buffer.
virtual void enforce_default_processing(bool should_process) override
Controls whether the audio buffer should use default processing.
Concrete audio implementation of the Buffer interface for double-precision audio data.
void initialize()
Initialize the buffer after construction.
void setup_zero_copy_if_possible()
Attempt to enable zero-copy operation if container layout allows.
std::shared_ptr< Kakshya::StreamContainer > m_container
void set_container(const std::shared_ptr< Kakshya::StreamContainer > &container)
Update the container reference.
SoundContainerBuffer(uint32_t channel_id, uint32_t num_samples, const std::shared_ptr< Kakshya::StreamContainer > &container, uint32_t source_channel=0)
Construct a SoundContainerBuffer for a specific channel and container.
uint32_t m_source_channel
std::shared_ptr< BufferProcessor > m_pending_adapter
std::shared_ptr< BufferProcessor > create_default_processor() override
Create the default processor (SoundStreamReader) for this buffer.
void set_container(const std::shared_ptr< Kakshya::StreamContainer > &container)
Set the container to adapt.
void on_detach(const std::shared_ptr< Buffer > &buffer) override
Detach the adapter from its AudioBuffer.
std::shared_ptr< Kakshya::StreamContainer > m_container
uint32_t m_source_channel
void extract_channel_data(std::span< double > output)
Extract channel data from the container into the output buffer.
void on_attach(const std::shared_ptr< Buffer > &buffer) override
Attach the adapter to an AudioBuffer.
void processing_function(const std::shared_ptr< Buffer > &buffer) override
Extracts and processes data from the container into the target AudioBuffer.
void on_container_state_change(const std::shared_ptr< Kakshya::SignalSourceContainer > &container, Kakshya::ProcessingState state)
Respond to container state changes (e.g., READY, PROCESSED, NEEDS_REMOVAL).
void set_source_channel(uint32_t channel_index)
Set which channel dimension to extract from the container.
SoundStreamReader(const std::shared_ptr< Kakshya::StreamContainer > &container)
@ BufferProcessing
Buffer processing (Buffers::BufferManager, processing chains)
@ Init
Engine/subsystem initialization.
@ Buffers
Buffers, Managers, processors and processing chains.
ProcessingState
Represents the current processing lifecycle state of a container.
@ READY
Container has data loaded and is ready for processing.
@ NEEDS_REMOVAL
Container is marked for removal from the system.
@ ERROR
Container is in an error state and cannot proceed.
@ PROCESSED
Container has completed processing and results are available.
@ INTERLEAVED
Single DataVariant with interleaved data (LRLRLR for stereo)