MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
mxl::lib::fabrics::ofi::AudioBounceBuffer Class Reference

A bounce buffer for audio samples. More...

#include <AudioBounceBuffer.hpp>

Collaboration diagram for mxl::lib::fabrics::ofi::AudioBounceBuffer:

Public Member Functions

 AudioBounceBuffer (std::size_t entryCount, std::size_t entrySize, DataLayout::Continuous layout)
std::size_t entryCount () const noexcept
 Return the number of entries in the bounce buffer.
std::size_t entrySize () const noexcept
 Return the size of a bounce buffer entry in bytes.
std::vector< Region > getRegions () const noexcept
 Return the regions corresponding to the bounce buffer entries. Each region corresponds to one bounce buffer entry. The regions are returned in the same order as the bounce buffer entries.
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 the entry.

Static Public Member Functions

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

Private Attributes

std::vector< AudioBounceBufferEntry > _entries
DataLayout::Continuous _layout

Detailed Description

A bounce buffer for audio samples.

For audio samples, a bounce buffer is used to receive the samples from the network before copying them to the final destination region. This is necessary because the samples to transfer on the Initiator are scattered at multiple locations (one per channel), but only a single memory address can be provided for the destination.

Definition at line 63 of file AudioBounceBuffer.hpp.

Constructor & Destructor Documentation

◆ AudioBounceBuffer()

mxl::lib::fabrics::ofi::AudioBounceBuffer::AudioBounceBuffer ( std::size_t entryCount,
std::size_t entrySize,
DataLayout::Continuous layout )

Definition at line 59 of file AudioBounceBuffer.cpp.

60 : _entries{entryCount, AudioBounceBufferEntry{static_cast<std::uint32_t>(entrySize)}}
61 , _layout{layout}
62 {}
std::size_t entryCount() const noexcept
Return the number of entries in the bounce buffer.
std::vector< AudioBounceBufferEntry > _entries
std::size_t entrySize() const noexcept
Return the size of a bounce buffer entry in bytes.

Member Function Documentation

◆ entryCount()

std::size_t mxl::lib::fabrics::ofi::AudioBounceBuffer::entryCount ( ) const
nodiscardnoexcept

Return the number of entries in the bounce buffer.

Definition at line 75 of file AudioBounceBuffer.cpp.

76 {
77 return _entries.size();
78 }

◆ entrySize()

std::size_t mxl::lib::fabrics::ofi::AudioBounceBuffer::entrySize ( ) const
nodiscardnoexcept

Return the size of a bounce buffer entry in bytes.

Definition at line 80 of file AudioBounceBuffer.cpp.

81 {
82 return _entries.empty() ? 0 : _entries.front().size();
83 }

◆ getMutableMultiBufferSlices()

void mxl::lib::fabrics::ofi::AudioBounceBuffer::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 )
staticnoexcept

Definition at line 15 of file AudioBounceBuffer.cpp.

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 }
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
uint64_t index

◆ getRegions()

std::vector< Region > mxl::lib::fabrics::ofi::AudioBounceBuffer::getRegions ( ) const
nodiscardnoexcept

Return the regions corresponding to the bounce buffer entries. Each region corresponds to one bounce buffer entry. The regions are returned in the same order as the bounce buffer entries.

Definition at line 64 of file AudioBounceBuffer.cpp.

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 }
static Location host() noexcept
specify a host memory location.
Definition Region.cpp:23

◆ unpack()

AudioEntryHeader const & mxl::lib::fabrics::ofi::AudioBounceBuffer::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 the entry.

Internally the header will be used to correctly map the bounce buffer entry slices to the output region slices.

Definition at line 85 of file AudioBounceBuffer.cpp.

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 }
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
static Exception invalidArgument(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INVALID_ARG exception.
Definition Exception.hpp:43
struct mxlMutableWrappedMultiBufferSlice_t mxlMutableWrappedMultiBufferSlice

Member Data Documentation

◆ _entries

std::vector<AudioBounceBufferEntry> mxl::lib::fabrics::ofi::AudioBounceBuffer::_entries
private

Definition at line 92 of file AudioBounceBuffer.hpp.

◆ _layout

DataLayout::Continuous mxl::lib::fabrics::ofi::AudioBounceBuffer::_layout
private

Definition at line 93 of file AudioBounceBuffer.hpp.


The documentation for this class was generated from the following files: