Audacity 3.2.0
ClipSegmentTest.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 WaveClipSegmentTest.cpp
7
8 Matthieu Hodgkinson
9
10**********************************************************************/
11#include "ClipSegment.h"
12#include "AudioContainer.h"
13#include "FloatVectorClip.h"
14
15
16#include <catch2/catch.hpp>
17
18namespace
19{
20constexpr auto sampleRate = 3;
21using FloatVectorVector = std::vector<std::vector<float>>;
22} // namespace
23
24TEST_CASE("ClipSegment")
25{
26 const auto direction =
28
29 SECTION("simple test case")
30 {
31 const auto numChannels = GENERATE(1u, 2u);
32 const auto clip =
33 numChannels == 1u ?
34 std::make_shared<FloatVectorClip>(
35 sampleRate, FloatVectorVector { { 1.f, 2.f, 3.f } }) :
36 std::make_shared<FloatVectorClip>(
38 FloatVectorVector { { 1.f, 2.f, 3.f }, { -1.f, -2.f, -3.f } });
39 ClipSegment sut { *clip, 0., direction };
40 AudioContainer output(sampleRate, numChannels);
41 REQUIRE(sut.GetFloats(output.channelPointers.data(), sampleRate) == sampleRate);
42 if (numChannels == 1u)
43 {
44 const auto expected = direction == PlaybackDirection::forward ?
45 std::vector<float> { 1.f, 2.f, 3.f } :
46 std::vector<float> { 3.f, 2.f, 1.f };
47 const auto asExpected = output.channelVectors[0] == expected;
48 REQUIRE(asExpected);
49 }
50 else
51 {
52 const auto expected =
53 direction == PlaybackDirection::forward ?
54 std::vector<std::vector<float>> { { 1.f, 2.f, 3.f },
55 { -1.f, -2.f, -3.f } } :
56 std::vector<std::vector<float>> { { 3.f, 2.f, 1.f },
57 { -3.f, -2.f, -1.f } };
58 const auto asExpected = output.channelVectors == expected;
59 REQUIRE(asExpected);
60 }
61 }
62
63 SECTION("accounts for playback offset")
64 {
65 const auto clip = std::make_shared<FloatVectorClip>(
66 sampleRate, FloatVectorVector { { 1.f, 2.f, 3.f, 4.f, 5.f } });
67 const auto numSamples = clip->GetVisibleSampleCount().as_size_t(); // 5
68 // Offset of two samples, in seconds.
69 constexpr auto playbackOffset = 2 / static_cast<double>(sampleRate);
70 ClipSegment sut { *clip, playbackOffset, direction };
71 AudioContainer output(numSamples, 1u);
72 REQUIRE(sut.GetFloats(output.channelPointers.data(), numSamples) == 3);
73 const auto expected = direction == PlaybackDirection::forward ?
74 std::vector<float> { 3.f, 4.f, 5.f, 0.f, 0.f } :
75 std::vector<float> { 3.f, 2.f, 1.f, 0.f, 0.f };
76 REQUIRE(output.channelVectors[0] == expected);
77 }
78}
TEST_CASE("ClipSegment")
std::vector< std::vector< float > > FloatVectorVector
std::vector< float * > channelPointers
std::vector< std::vector< float > > channelVectors