MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ write_frames() [1/2]

uint64_t MayaFlux::Kakshya::DynamicSoundStream::write_frames ( std::span< const double >  data,
uint64_t  start_frame = 0,
uint32_t  channel = 0 
)

Write audio frame data to the container with automatic capacity management.

Parameters
dataSpan of interleaved audio samples to write
start_frameFrame index where writing begins (default: append at end) %
channelChannel index for planar data (default: 0)
Returns
Number of frames actually written

Definition at line 166 of file DynamicSoundStream.cpp.

167{
168 auto num_frames = validate_single_channel(data, start_frame, channel);
169
170 if (!num_frames)
171 return 0;
172
173 if (m_is_circular && start_frame + num_frames > m_circular_capacity) {
174 uint64_t frames_to_end = m_circular_capacity - start_frame;
175 uint64_t frames_from_start = num_frames - frames_to_end;
176
177 if (frames_to_end > 0) {
178 write_frames(data.subspan(0, frames_to_end), start_frame, channel);
179 }
180
181 if (frames_from_start > 0) {
182 write_frames(data.subspan(frames_to_end, frames_from_start), 0, channel);
183 }
184
185 return num_frames;
186 }
187
189 std::unique_lock lock(m_data_mutex);
190
191 if (m_data.empty()) {
192 expand_to(start_frame + num_frames);
193 }
194
195 auto& interleaved_data = std::get<std::vector<double>>(m_data[0]);
196 uint32_t num_channels = get_num_channels();
197
198 for (uint64_t frame = 0; frame < num_frames; ++frame) {
199 uint64_t interleaved_index = (start_frame + frame) * num_channels + channel;
200 if (interleaved_index < interleaved_data.size()) {
201 interleaved_data[interleaved_index] = data[frame];
202 }
203 }
204 } else {
205 std::unique_lock lock(m_data_mutex);
206
207 if (channel >= m_data.size()) {
208 return 0;
209 }
210
211 auto dest_span = convert_variant<double>(m_data[channel]);
212
213 if (start_frame + num_frames > dest_span.size()) {
214 std::vector<double> current_data(dest_span.begin(), dest_span.end());
215 current_data.resize(start_frame + num_frames, 0.0);
216 m_data[channel] = DataVariant(std::move(current_data));
217 dest_span = convert_variant<double>(m_data[channel]);
218 }
219
220 std::copy(data.begin(), data.begin() + num_frames,
221 dest_span.begin() + start_frame);
222 }
223
224 if (m_is_circular) {
226 }
227
229 m_double_extraction_dirty.store(true, std::memory_order_release);
230
231 return num_frames;
232}
uint64_t validate_single_channel(std::span< const double > data, uint64_t start_frame=0, uint32_t channel=0)
bool m_is_circular
True when operating in circular buffer mode.
uint64_t write_frames(std::span< const double > data, uint64_t start_frame=0, uint32_t channel=0)
Write audio frame data to the container with automatic capacity management.
uint64_t m_circular_capacity
Fixed capacity for circular mode.
void invalidate_span_cache()
Invalidate the span cache when data or layout changes.
void lock() override
Acquire a lock for thread-safe access.
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.
Definition NDData.hpp:76
@ INTERLEAVED
Single DataVariant with interleaved data (LRLRLR for stereo)

References expand_to(), MayaFlux::Kakshya::SoundStreamContainer::get_num_channels(), MayaFlux::Kakshya::INTERLEAVED, MayaFlux::Kakshya::SoundStreamContainer::invalidate_span_cache(), MayaFlux::Kakshya::SoundStreamContainer::lock(), m_circular_capacity, MayaFlux::Kakshya::SoundStreamContainer::m_data, MayaFlux::Kakshya::SoundStreamContainer::m_data_mutex, MayaFlux::Kakshya::SoundStreamContainer::m_double_extraction_dirty, m_is_circular, MayaFlux::Kakshya::SoundStreamContainer::m_num_frames, MayaFlux::Kakshya::SoundStreamContainer::m_structure, MayaFlux::Kakshya::ContainerDataStructure::organization, validate_single_channel(), and write_frames().

Referenced by write_frames(), and write_frames().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: