MayaFlux 0.1.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 163 of file DynamicSoundStream.cpp.

164{
165 auto num_frames = validate_single_channel(data, start_frame, channel);
166
167 if (!num_frames)
168 return 0;
169
170 if (m_is_circular && start_frame + num_frames > m_circular_capacity) {
171 uint64_t frames_to_end = m_circular_capacity - start_frame;
172 uint64_t frames_from_start = num_frames - frames_to_end;
173
174 if (frames_to_end > 0) {
175 write_frames(data.subspan(0, frames_to_end), start_frame, channel);
176 }
177
178 if (frames_from_start > 0) {
179 write_frames(data.subspan(frames_to_end, frames_from_start), 0, channel);
180 }
181
182 return num_frames;
183 }
184
186 std::unique_lock lock(m_data_mutex);
187
188 if (m_data.empty()) {
189 expand_to(start_frame + num_frames);
190 }
191
192 auto& interleaved_data = std::get<std::vector<double>>(m_data[0]);
193 uint32_t num_channels = get_num_channels();
194
195 for (uint64_t frame = 0; frame < num_frames; ++frame) {
196 uint64_t interleaved_index = (start_frame + frame) * num_channels + channel;
197 if (interleaved_index < interleaved_data.size()) {
198 interleaved_data[interleaved_index] = data[frame];
199 }
200 }
201 } else {
202 std::unique_lock lock(m_data_mutex);
203
204 if (channel >= m_data.size()) {
205 return 0;
206 }
207
208 auto dest_span = convert_variant<double>(m_data[channel]);
209
210 if (start_frame + num_frames > dest_span.size()) {
211 std::vector<double> current_data(dest_span.begin(), dest_span.end());
212 current_data.resize(start_frame + num_frames, 0.0);
213 m_data[channel] = DataVariant(std::move(current_data));
214 dest_span = convert_variant<double>(m_data[channel]);
215 }
216
217 std::copy(data.begin(), data.begin() + num_frames,
218 dest_span.begin() + start_frame);
219 }
220
221 if (m_is_circular) {
223 }
224
226 m_double_extraction_dirty.store(true, std::memory_order_release);
227
228 return num_frames;
229}
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:73
@ 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: