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

◆ snapshot()

std::shared_ptr< DynamicSoundStream > MayaFlux::Kakshya::DynamicSoundStream::snapshot ( uint64_t  frames) const

Copy the most recent frames into a new stream, oldest first.

Reads back from channel 0's write head, so it is meant for streams filled with append_frames. A ring is unrolled across its wrap; frames from before the first write are silence. The copy is independent of this stream.

Parameters
framesFrames to keep, capped at the ring capacity, or at the frames written for a linear stream.
Returns
New stream with the same rate and channels, or null if empty.

Definition at line 256 of file DynamicSoundStream.cpp.

257{
258 const uint64_t head = get_write_head(0);
259 const uint64_t available = m_is_circular ? m_circular_capacity : head;
260 const uint64_t count = std::min(frames, available);
261
262 if (count == 0) {
263 return nullptr;
264 }
265
266 const uint64_t first = m_is_circular ? head + m_circular_capacity - count : head - count;
267
268 std::vector<uint64_t> indices(count);
269 for (uint64_t i = 0; i < count; ++i) {
270 indices[i] = m_is_circular ? (first + i) % m_circular_capacity : first + i;
271 }
272
273 const uint64_t channels = get_num_channels();
274 std::vector<double> taps(count * channels);
275 gather_frames(indices, taps);
276
277 auto copy = std::make_shared<DynamicSoundStream>(get_sample_rate(), get_num_channels());
278
279 if (copy->get_structure().organization == OrganizationStrategy::INTERLEAVED) {
280 copy->write_frames(std::vector<std::span<const double>> { taps }, 0);
281 return copy;
282 }
283
284 std::vector<std::vector<double>> planar(channels, std::vector<double>(count));
285 for (uint64_t i = 0; i < count; ++i) {
286 for (uint64_t c = 0; c < channels; ++c) {
287 planar[c][i] = taps[i * channels + c];
288 }
289 }
290
291 std::vector<std::span<const double>> spans(planar.begin(), planar.end());
292 copy->write_frames(std::move(spans), 0);
293 return copy;
294}
std::vector< uint32_t > indices
bool available
Definition VKDevice.cpp:41
size_t count
uint32_t channels
bool m_is_circular
True when operating in circular buffer mode.
uint64_t get_write_head(uint32_t channel=0) const
Index of the next frame append_frames will write for a channel.
uint64_t m_circular_capacity
Fixed capacity for circular mode.
void gather_frames(std::span< const uint64_t > frames, std::span< double > out) const
Copy arbitrary frames into a caller-owned buffer without allocating.
@ INTERLEAVED
Single DataVariant with interleaved data (LRLRLR for stereo)
CopiedText copy(std::string_view text, const LayoutResult &layout, size_t a, size_t b)
Copy a byte range: the text it spans, and the quads that render it.

References available, channels, count, MayaFlux::Kakshya::SoundStreamContainer::gather_frames(), MayaFlux::Kakshya::SoundStreamContainer::get_num_channels(), MayaFlux::Kakshya::SoundStreamContainer::get_sample_rate(), get_write_head(), indices, MayaFlux::Kakshya::INTERLEAVED, m_circular_capacity, and m_is_circular.

+ Here is the call graph for this function: