MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
test_AudioBounceBuffer.cpp File Reference
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <vector>
#include <catch2/catch_test_macros.hpp>
#include "AudioBounceBuffer.hpp"
Include dependency graph for test_AudioBounceBuffer.cpp:

Go to the source code of this file.

Functions

 TEST_CASE ("ofi: audio bounce buffer rejects a count longer than the ring", "[ofi][AudioBounceBuffer]")
 TEST_CASE ("ofi: audio bounce buffer unpacks an empty header into nothing", "[ofi][AudioBounceBuffer]")
 TEST_CASE ("ofi: audio bounce buffer unpacks channel-major payload across ring wrap", "[ofi][AudioBounceBuffer]")

Function Documentation

◆ TEST_CASE() [1/3]

TEST_CASE ( "ofi: audio bounce buffer rejects a count longer than the ring" ,
"" [ofi][AudioBounceBuffer] )

Definition at line 106 of file test_AudioBounceBuffer.cpp.

106 : audio bounce buffer rejects a count longer than the ring", "[ofi][AudioBounceBuffer]")
107{
108 constexpr auto LongCount = TargetBufferLength + 1;
109 auto target = std::vector<Sample>(ChannelCount * TargetBufferLength, UntouchedSample);
110 auto const targetLayout = DataLayout::Continuous{.sampleSize = sizeof(Sample), .channelCount = ChannelCount, .bufferLength = TargetBufferLength};
111 auto bounceBuffer = AudioBounceBuffer{1, LongCount * ChannelCount * sizeof(Sample), targetLayout};
112
113 auto* entryData = reinterpret_cast<std::uint8_t*>(bounceBuffer.getRegions().front().base); // NOLINT
114 auto const header = AudioEntryHeader{.headIndex = HeadIndex, .count = LongCount};
115 std::memcpy(entryData, &header, sizeof(header));
116
117 auto const outRegion = Region{regionBaseOf(target.data()), target.size() * sizeof(Sample), nullptr, nullptr, Region::Location::host()};
118 REQUIRE_THROWS(bounceBuffer.unpack(0, outRegion));
119 REQUIRE(target == std::vector<Sample>(ChannelCount * TargetBufferLength, UntouchedSample));
120}
A bounce buffer for audio samples.
std::size_t count(uuids::uuid id) const noexcept
Returns the number of writers registered for flow id 'id'.
uint8_t * buffer
Set a mark at the beginning and the end of the grain payload.

◆ TEST_CASE() [2/3]

TEST_CASE ( "ofi: audio bounce buffer unpacks an empty header into nothing" ,
"" [ofi][AudioBounceBuffer] )

Definition at line 93 of file test_AudioBounceBuffer.cpp.

93 : audio bounce buffer unpacks an empty header into nothing", "[ofi][AudioBounceBuffer]")
94{
95 auto target = std::vector<Sample>(ChannelCount * TargetBufferLength, UntouchedSample);
96 auto const targetLayout = DataLayout::Continuous{.sampleSize = sizeof(Sample), .channelCount = ChannelCount, .bufferLength = TargetBufferLength};
97 auto bounceBuffer = AudioBounceBuffer{1, SampleCount * ChannelCount * sizeof(Sample), targetLayout};
98
99 auto const outRegion = Region{regionBaseOf(target.data()), target.size() * sizeof(Sample), nullptr, nullptr, Region::Location::host()};
100 auto const unpackedHeader = bounceBuffer.unpack(0, outRegion);
101 REQUIRE(unpackedHeader.count == 0U);
102 REQUIRE(target == std::vector<Sample>(ChannelCount * TargetBufferLength, UntouchedSample));
103}
static FabricInfo empty() noexcept
Create and empty FabricInfo.

◆ TEST_CASE() [3/3]

TEST_CASE ( "ofi: audio bounce buffer unpacks channel-major payload across ring wrap" ,
"" [ofi][AudioBounceBuffer] )

Definition at line 44 of file test_AudioBounceBuffer.cpp.

44 : audio bounce buffer unpacks channel-major payload across ring wrap", "[ofi][AudioBounceBuffer]")
45{
46 auto target = std::vector<Sample>(ChannelCount * TargetBufferLength, UntouchedSample);
47 auto targetSlice = mxlMutableWrappedMultiBufferSlice{};
48 AudioBounceBuffer::getMutableMultiBufferSlices(
49 HeadIndex, SampleCount, TargetBufferLength, sizeof(Sample), ChannelCount, reinterpret_cast<std::uint8_t*>(target.data()), targetSlice);
50
51 REQUIRE((targetSlice.base.fragments[0].size / sizeof(Sample)) == 3U);
52 REQUIRE((targetSlice.base.fragments[1].size / sizeof(Sample)) == 2U);
53
54 // channel 0 payload: 103, 104, 105, 106, 107
55 // channel 1 payload: 203, 204, 205, 206, 207
56 auto const payloadSamples = std::vector<Sample>{103U, 104U, 105U, 106U, 107U, 203U, 204U, 205U, 206U, 207U};
57 auto const payloadSize = payloadSamples.size() * sizeof(Sample);
58
59 auto const targetLayout = DataLayout::Continuous{.sampleSize = sizeof(Sample), .channelCount = ChannelCount, .bufferLength = TargetBufferLength};
60 auto bounceBuffer = AudioBounceBuffer{1, payloadSize, targetLayout};
61 auto bounceRegions = bounceBuffer.getRegions();
62 REQUIRE(bounceRegions.size() == 1U);
63 REQUIRE(bounceRegions.front().size == (sizeof(AudioEntryHeader) + payloadSize));
64
65 // Write directly into the bounce-buffer entry:
66 auto* entryData = reinterpret_cast<std::uint8_t*>(bounceRegions.front().base); // NOLINT
67 auto const header = AudioEntryHeader{.headIndex = HeadIndex, .count = SampleCount};
68 std::memcpy(entryData, &header, sizeof(header));
69 std::memcpy(entryData + sizeof(AudioEntryHeader), payloadSamples.data(), payloadSize);
70
71 // Let entry 0 of the bounce buffer copy its payload into the target ring buffer.
72 auto const outRegion = Region{regionBaseOf(target.data()), target.size() * sizeof(Sample), nullptr, nullptr, Region::Location::host()};
73 auto const unpackedHeader = bounceBuffer.unpack(0, outRegion);
74 REQUIRE(unpackedHeader.headIndex == HeadIndex);
75 REQUIRE(unpackedHeader.count == SampleCount);
76
77 // Verify that unpack did correctly consume channel-major payload
78 auto expectedTarget = std::vector<Sample>(ChannelCount * TargetBufferLength, UntouchedSample);
79 auto const targetStartOffset = ringStartOffset(HeadIndex, SampleCount, TargetBufferLength);
80 for (auto channel = std::size_t{0}; channel < ChannelCount; channel++)
81 {
82 for (auto offset = std::size_t{0}; offset < SampleCount; offset++)
83 {
84 auto const targetIndex = (targetStartOffset + offset) % TargetBufferLength;
85 sampleAt(expectedTarget, channel, targetIndex, TargetBufferLength) = payloadSamples[(channel * SampleCount) + offset];
86 }
87 }
88
89 REQUIRE(target == expectedTarget);
90}