MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SoundContainerBuffer.cpp
Go to the documentation of this file.
2
4
6
7namespace MayaFlux::Buffers {
8
9SoundStreamReader::SoundStreamReader(const std::shared_ptr<Kakshya::StreamContainer>& container)
10 : m_container(container)
11{
12 if (container) {
13 auto structure = container->get_structure();
14 m_num_channels = structure.get_channel_count();
15
16 container->register_state_change_callback(
17 [this](auto c, auto s) {
18 this->on_container_state_change(c, s);
19 });
20 }
21}
22
23void SoundStreamReader::processing_function(const std::shared_ptr<Buffer>& buffer)
24{
25 if (!m_container || !buffer) {
26 return;
27 }
28
29 if (m_container->is_at_end()) {
30 buffer->mark_for_removal();
31 return;
32 }
33
34 try {
35 auto state = m_container->get_processing_state();
36
38 if (m_update_flags) {
39 buffer->mark_for_removal();
40 }
41 return;
42 }
43
46 if (m_container->try_acquire_processing_token(m_source_channel)) {
47 m_container->process_default();
48 }
49 }
50 }
51
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();
55
56 auto read_positions = m_container->get_read_position();
57 uint64_t current_pos = (m_source_channel < read_positions.size())
58 ? read_positions[m_source_channel]
59 : 0;
60
61 if (buffer_data.size() != buffer_size) {
62 buffer_data.resize(buffer_size);
63 }
64
65 extract_channel_data(buffer_data);
66
67 if (m_auto_advance) {
68 m_container->update_read_position_for_channel(m_source_channel, current_pos + buffer_size);
69 }
70
71 if (m_update_flags) {
72 buffer->mark_for_processing(true);
73 }
74
75 m_container->mark_dimension_consumed(m_source_channel, m_reader_id);
76
77 if (m_container->all_dimensions_consumed()) {
78 m_container->update_processing_state(Kakshya::ProcessingState::READY);
79 std::dynamic_pointer_cast<Kakshya::SoundFileContainer>(m_container)->clear_all_consumption();
80 m_container->reset_processing_token();
81 }
82
83 } catch (const std::exception& e) {
85 "SoundStreamReader: Error during processing: {}", e.what());
86 }
87}
88
89void SoundStreamReader::extract_channel_data(std::span<double> output)
90{
91 if (!m_container) {
92 std::ranges::fill(output, 0.0);
93 return;
94 }
95
96 auto sound_container = std::dynamic_pointer_cast<Kakshya::SoundStreamContainer>(m_container);
97 if (!sound_container) {
98 std::ranges::fill(output, 0.0);
99 return;
100 }
101
102 auto& processed_data = sound_container->get_processed_data();
103 if (processed_data.empty()) {
104 std::ranges::fill(output, 0.0);
105 return;
106 }
107
108 auto structure = sound_container->get_structure();
109
110 if (structure.organization == Kakshya::OrganizationStrategy::INTERLEAVED) {
111 thread_local std::vector<double> temp_storage;
112 auto data_span = Kakshya::extract_from_variant<double>(processed_data[0], temp_storage);
113
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));
117
118 for (auto i : std::views::iota(0UZ, samples_to_copy)) {
119 auto interleaved_idx = i * num_channels + m_source_channel;
120 output[i] = (interleaved_idx < data_span.size()) ? data_span[interleaved_idx] : 0.0;
121 }
122
123 if (samples_to_copy < output.size()) {
124 std::ranges::fill(output | std::views::drop(samples_to_copy), 0.0);
125 }
126
127 } else {
128 if (m_source_channel >= processed_data.size()) {
129 std::ranges::fill(output, 0.0);
130 return;
131 }
132
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);
135
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());
138
139 if (samples_to_copy < output.size()) {
140 std::ranges::fill(output | std::views::drop(samples_to_copy), 0.0);
141 }
142 }
143}
144
145void SoundStreamReader::on_attach(const std::shared_ptr<Buffer>& buffer)
146{
147 if (!m_container || !buffer) {
148 return;
149 }
150
151 m_reader_id = m_container->register_dimension_reader(m_source_channel);
152
153 if (!m_container->is_ready_for_processing()) {
155 std::source_location::current(),
156 "SoundStreamReader: Container not ready for processing");
157 }
158
159 try {
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();
162
163 extract_channel_data(buffer_data);
164
165 if (m_update_flags) {
166 buffer->mark_for_processing(true);
167 }
168
169 } catch (const std::exception& e) {
171 "SoundStreamReader: Error pre-filling buffer: {}", e.what());
172 }
173}
174
175void SoundStreamReader::on_detach(const std::shared_ptr<Buffer>& /*buffer*/)
176{
177 if (m_container) {
178 m_container->unregister_state_change_callback();
179 m_container->unregister_dimension_reader(m_source_channel);
180 }
181}
182
183void SoundStreamReader::set_source_channel(uint32_t channel_index)
184{
185 if (channel_index >= m_num_channels) {
187 std::source_location::current(),
188 "SoundStreamReader: Channel index {} exceeds container channel count {}",
189 channel_index, m_num_channels);
190 }
191 m_source_channel = channel_index;
192}
193
194void SoundStreamReader::set_container(const std::shared_ptr<Kakshya::StreamContainer>& container)
195{
196 if (m_container) {
197 m_container->unregister_state_change_callback();
198 }
199
200 m_container = container;
201
202 if (container) {
203 auto structure = container->get_structure();
204 m_num_channels = structure.get_channel_count();
205
206 container->register_state_change_callback(
207 [this](std::shared_ptr<Kakshya::SignalSourceContainer> c, Kakshya::ProcessingState s) {
208 this->on_container_state_change(c, s);
209 });
210 }
211}
212
214 const std::shared_ptr<Kakshya::SignalSourceContainer>& /*container*/,
216{
217 switch (state) {
219 break;
220
223 "SoundStreamReader: Container entered ERROR state");
224 break;
225
226 default:
227 break;
228 }
229}
230
231SoundContainerBuffer::SoundContainerBuffer(uint32_t channel_id, uint32_t num_samples,
232 const std::shared_ptr<Kakshya::StreamContainer>& container,
233 uint32_t source_channel)
234 : AudioBuffer(channel_id, num_samples)
235 , m_container(container)
236 , m_source_channel(source_channel)
237{
238 if (!m_container) {
239 error<std::invalid_argument>(Journal::Component::Buffers, Journal::Context::Init,
240 std::source_location::current(),
241 "SoundContainerBuffer: container must not be null");
242 }
243
244 m_pending_adapter = std::make_shared<SoundStreamReader>(m_container);
245 std::dynamic_pointer_cast<SoundStreamReader>(m_pending_adapter)->set_source_channel(m_source_channel);
246
248}
249
258
259void SoundContainerBuffer::set_container(const std::shared_ptr<Kakshya::StreamContainer>& container)
260{
261 m_container = container;
262
263 if (auto adapter = std::dynamic_pointer_cast<SoundStreamReader>(m_default_processor)) {
264 adapter->set_container(container);
265 }
266
268}
269
271{
272 // Check if we can use zero-copy mode
273 // This would be possible if:
274 // 1. Container data is contiguous doubles
275 // 2. Channel is deinterleaved (column-major for audio)
276 // 3. Buffer size matches container frame size
277
278 if (!m_container) {
279 m_zero_copy_mode = false;
280 return;
281 }
282
283 auto dimensions = m_container->get_dimensions();
284 auto layout = m_container->get_memory_layout();
285
286 m_zero_copy_mode = false;
287
288 // TODO: Implement zero-copy when container provides direct memory access
289}
290
291std::shared_ptr<BufferProcessor> SoundContainerBuffer::create_default_processor()
292{
293 if (m_pending_adapter) {
294 return m_pending_adapter;
295 }
296
297 auto adapter = std::make_shared<SoundStreamReader>(m_container);
298 adapter->set_source_channel(m_source_channel);
299 return adapter;
300}
301
302} // namespace MayaFlux::Buffers
#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.
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
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)