10 : m_container(container)
13 auto structure = container->get_structure();
16 container->register_state_change_callback(
17 [
this](
auto c,
auto s) {
30 buffer->mark_for_removal();
39 buffer->mark_for_removal();
52 auto audio_buffer = std::dynamic_pointer_cast<AudioBuffer>(buffer);
53 auto& buffer_data = audio_buffer->get_data();
54 uint32_t buffer_size = audio_buffer->get_num_samples();
56 auto read_positions =
m_container->get_read_position();
61 if (buffer_data.size() != buffer_size) {
62 buffer_data.resize(buffer_size);
72 buffer->mark_for_processing(
true);
79 std::dynamic_pointer_cast<Kakshya::SoundFileContainer>(
m_container)->clear_all_consumption();
83 }
catch (
const std::exception& e) {
85 "SoundStreamReader: Error during processing: {}", e.what());
92 std::ranges::fill(output, 0.0);
96 auto sound_container = std::dynamic_pointer_cast<Kakshya::SoundStreamContainer>(
m_container);
97 if (!sound_container) {
98 std::ranges::fill(output, 0.0);
102 auto& processed_data = sound_container->get_processed_data();
103 if (processed_data.empty()) {
104 std::ranges::fill(output, 0.0);
108 auto structure = sound_container->get_structure();
111 thread_local std::vector<double> temp_storage;
112 auto data_span = Kakshya::extract_from_variant<double>(processed_data[0], temp_storage);
114 auto num_channels = structure.get_channel_count();
115 auto samples_to_copy = std::min(
static_cast<size_t>(output.size()),
116 static_cast<size_t>(data_span.size() / num_channels));
118 for (
auto i : std::views::iota(0UZ, samples_to_copy)) {
120 output[i] = (interleaved_idx < data_span.size()) ? data_span[interleaved_idx] : 0.0;
123 if (samples_to_copy < output.size()) {
124 std::ranges::fill(output | std::views::drop(samples_to_copy), 0.0);
129 std::ranges::fill(output, 0.0);
133 thread_local std::vector<double> temp_storage;
134 auto channel_data_span = Kakshya::extract_from_variant<double>(processed_data[
m_source_channel], temp_storage);
136 auto samples_to_copy = std::min(output.size(), channel_data_span.size());
137 std::ranges::copy_n(channel_data_span.begin(), samples_to_copy, output.begin());
139 if (samples_to_copy < output.size()) {
140 std::ranges::fill(output | std::views::drop(samples_to_copy), 0.0);
155 std::source_location::current(),
156 "SoundStreamReader: Container not ready for processing");
160 auto& buffer_data = std::dynamic_pointer_cast<AudioBuffer>(buffer)->get_data();
161 uint32_t num_samples = std::dynamic_pointer_cast<AudioBuffer>(buffer)->get_num_samples();
166 buffer->mark_for_processing(
true);
169 }
catch (
const std::exception& e) {
171 "SoundStreamReader: Error pre-filling buffer: {}", e.what());
187 std::source_location::current(),
188 "SoundStreamReader: Channel index {} exceeds container channel count {}",
203 auto structure = container->get_structure();
206 container->register_state_change_callback(
214 const std::shared_ptr<Kakshya::SignalSourceContainer>& ,
223 "SoundStreamReader: Container entered ERROR state");
232 const std::shared_ptr<Kakshya::StreamContainer>& container,
233 uint32_t source_channel)
235 , m_container(container)
236 , m_source_channel(source_channel)
240 std::source_location::current(),
241 "SoundContainerBuffer: container must not be null");
264 adapter->set_container(container);
297 auto adapter = std::make_shared<SoundStreamReader>(
m_container);
#define MF_ERROR(comp, ctx,...)
void enforce_default_processing(bool should_process) override
Controls whether the audio buffer should use default processing.
std::shared_ptr< BufferProcessor > m_default_processor
Default audio transformation processor for this buffer.
void set_default_processor(const std::shared_ptr< BufferProcessor > &processor) override
Sets the default audio transformation processor for this buffer.
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)