MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
AudioBounceBuffer.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Contributors to the Media eXchange Layer project.
2// SPDX-License-Identifier: Apache-2.0
3
5#include <cstddef>
6#include <cstdint>
7#include <cstring>
8#include <bit>
9#include "Exception.hpp"
10
12{
13
14 // TODO: could be shared with PosixContinuousFlowReader
15 void AudioBounceBuffer::getMutableMultiBufferSlices(std::uint64_t index, std::size_t count, std::size_t bufferLength, std::size_t sampleWordSize,
16 std::size_t channelCount, std::uint8_t* baseBufferPtr, mxlMutableWrappedMultiBufferSlice& slice) noexcept
17 {
18 auto const startOffset = (index + bufferLength - count) % bufferLength;
19 auto const endOffset = (index % bufferLength);
20
21 auto const firstLength = (startOffset < endOffset) ? count : bufferLength - startOffset;
22 auto const secondLength = count - firstLength;
23
24 slice.base.fragments[0].pointer = baseBufferPtr + (sampleWordSize * startOffset);
25 slice.base.fragments[0].size = sampleWordSize * firstLength;
26
27 slice.base.fragments[1].pointer = baseBufferPtr;
28 slice.base.fragments[1].size = sampleWordSize * secondLength;
29
30 slice.stride = sampleWordSize * bufferLength;
31 slice.count = channelCount;
32 }
33
37
39 {
40 // The header is located at the beginning of the buffer, so we can reinterpret the start of the data as an AudioEntryHeader
41 return std::bit_cast<AudioEntryHeader const*>(_data.data());
42 }
43
44 std::uint8_t const* AudioBounceBufferEntry::samples() const noexcept
45 {
46 return _data.data() + sizeof(AudioEntryHeader); // Offset the pointer by the size of the header to get to the samples
47 }
48
49 std::uint8_t const* AudioBounceBufferEntry::data() const noexcept
50 {
51 return _data.data();
52 }
53
54 std::size_t AudioBounceBufferEntry::size() const noexcept
55 {
56 return _data.size();
57 }
58
60 : _entries{entryCount, AudioBounceBufferEntry{static_cast<std::uint32_t>(entrySize)}}
61 , _layout{layout}
62 {}
63
64 std::vector<Region> AudioBounceBuffer::getRegions() const noexcept
65 {
66 auto out = std::vector<Region>{};
67 out.reserve(_entries.size());
68 for (auto const& entry : _entries)
69 {
70 out.emplace_back(reinterpret_cast<std::uintptr_t>(entry.data()), entry.size(), nullptr, nullptr, Region::Location::host());
71 }
72 return out;
73 }
74
75 std::size_t AudioBounceBuffer::entryCount() const noexcept
76 {
77 return _entries.size();
78 }
79
80 std::size_t AudioBounceBuffer::entrySize() const noexcept
81 {
82 return _entries.empty() ? 0 : _entries.front().size();
83 }
84
85 AudioEntryHeader const& AudioBounceBuffer::unpack(std::size_t entryIndex, Region const& outRegion) const
86 {
87 auto const& entry = _entries.at(entryIndex);
88 auto const* header = entry.header();
89
90 auto const maxCountPerEntry = (entrySize() - sizeof(AudioEntryHeader)) / (_layout.channelCount * _layout.sampleSize);
91
92 if (header->count == 0)
93 {
94 // The header has not arrived yet.
95 return *header;
96 }
97
98 if (header->count > _layout.bufferLength)
99 {
101 "Invalid 'count' {} received in the header. More samples per channel than the ring holds ({}).", header->count, _layout.bufferLength);
102 }
103
104 if (header->count > maxCountPerEntry)
105 {
107 "Invalid 'count' {} received in the header. That number of samples per channel would bust the bounce buffer entry.", header->count);
108 }
109
110 // Using the given audio data layout, head index and number of samples recover the slices
112 getMutableMultiBufferSlices(header->headIndex,
113 header->count,
114 _layout.bufferLength,
115 _layout.sampleSize,
116 _layout.channelCount,
117 reinterpret_cast<std::uint8_t*>(outRegion.base), // NOLINT
118 slices);
119
120 auto srcAddr = entry.samples();
121 // The egress side writes the payload in channel-major order: all fragments for one channel are contiguous before the next channel.
122 // Consume fragments in that same order so wrapped slices do not interleave channel data.
123 for (auto chan = std::size_t{0}; chan < slices.count; chan++)
124 {
125 for (auto const& fragment : slices.base.fragments)
126 {
127 // check if the fragment present
128 if (fragment.size > 0)
129 {
130 auto const dstAddr = reinterpret_cast<std::uint8_t*>(fragment.pointer) + (slices.stride * chan);
131 std::memcpy(dstAddr, srcAddr, fragment.size);
132 srcAddr += fragment.size;
133 }
134 }
135 }
136
137 return *header;
138 }
139}
An audio bounce buffer entry is backed by a collection of samples The layout is: AudioEntryHeader,...
std::uint8_t const * samples() const noexcept
Return a view to the samples data of the bounce buffer entry.
std::size_t size() const noexcept
The total size of the bounce buffer entry in bytes.
AudioEntryHeader const * header() const noexcept
Return a view on the header of the bounce buffer entry.
AudioBounceBufferEntry(std::uint32_t size)
Construct an AudioBounceBufferEntry with the given size in bytes. The size should be large enough to ...
std::uint8_t const * data() const noexcept
Return a view to the beginning of the bounce buffer entry data.
AudioEntryHeader const & unpack(std::size_t entryIndex, Region const &outRegion) const
Copy the samples from the bounce buffer entry to the provided output region, and return the header of...
std::vector< Region > getRegions() const noexcept
Return the regions corresponding to the bounce buffer entries. Each region corresponds to one bounce ...
static void getMutableMultiBufferSlices(std::uint64_t index, std::size_t count, std::size_t bufferLength, std::size_t sampleWordSize, std::size_t channelCount, std::uint8_t *baseBufferPtr, mxlMutableWrappedMultiBufferSlice &slice) noexcept
std::size_t entryCount() const noexcept
Return the number of entries in the bounce buffer.
AudioBounceBuffer(std::size_t entryCount, std::size_t entrySize, DataLayout::Continuous layout)
std::vector< AudioBounceBufferEntry > _entries
std::size_t entrySize() const noexcept
Return the size of a bounce buffer entry in bytes.
static Exception invalidArgument(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INVALID_ARG exception.
Definition Exception.hpp:43
static Location host() noexcept
specify a host memory location.
Definition Region.cpp:23
Represent a memory region (unregistered).
Definition Region.hpp:24
struct mxlMutableWrappedMultiBufferSlice_t mxlMutableWrappedMultiBufferSlice
std::size_t count(uuids::uuid id) const noexcept
Returns the number of writers registered for flow id 'id'.
mxlMutableBufferSlice fragments[2]
Definition flow.h:87
mxlMutableWrappedBufferSlice base
Definition flow.h:117
Header for an audio bounce buffer entry.
Continuous layout variant of DataLayout.
uint64_t index