|
1 | 1 | #include <SFML/Audio/Music.hpp> |
2 | 2 |
|
| 3 | +// Other 1st party headers |
| 4 | +#include <SFML/System/FileInputStream.hpp> |
| 5 | + |
| 6 | +#include <catch2/catch_test_macros.hpp> |
| 7 | + |
| 8 | +#include <AudioUtil.hpp> |
| 9 | +#include <SystemUtil.hpp> |
| 10 | +#include <array> |
| 11 | +#include <fstream> |
| 12 | +#include <thread> |
3 | 13 | #include <type_traits> |
4 | 14 |
|
5 | | -static_assert(!std::is_copy_constructible_v<sf::Music>); |
6 | | -static_assert(!std::is_copy_assignable_v<sf::Music>); |
7 | | -static_assert(!std::is_nothrow_move_constructible_v<sf::Music>); |
8 | | -static_assert(!std::is_nothrow_move_assignable_v<sf::Music>); |
| 15 | +TEST_CASE("[Audio] sf::Music", runAudioDeviceTests()) |
| 16 | +{ |
| 17 | + SECTION("Type traits") |
| 18 | + { |
| 19 | + STATIC_CHECK(!std::is_copy_constructible_v<sf::Music>); |
| 20 | + STATIC_CHECK(!std::is_copy_assignable_v<sf::Music>); |
| 21 | + STATIC_CHECK(!std::is_nothrow_move_constructible_v<sf::Music>); |
| 22 | + STATIC_CHECK(!std::is_nothrow_move_assignable_v<sf::Music>); |
| 23 | + STATIC_CHECK(std::has_virtual_destructor_v<sf::Music>); |
| 24 | + } |
| 25 | + |
| 26 | + SECTION("Span") |
| 27 | + { |
| 28 | + const sf::Music::Span<float> span; |
| 29 | + CHECK(span.offset == 0); |
| 30 | + CHECK(span.length == 0); |
| 31 | + |
| 32 | + const sf::Music::TimeSpan timeSpan; |
| 33 | + CHECK(timeSpan.offset == sf::Time::Zero); |
| 34 | + CHECK(timeSpan.length == sf::Time::Zero); |
| 35 | + } |
| 36 | + |
| 37 | + SECTION("Construction") |
| 38 | + { |
| 39 | + const sf::Music music; |
| 40 | + CHECK(music.getDuration() == sf::Time::Zero); |
| 41 | + const auto [offset, length] = music.getLoopPoints(); |
| 42 | + CHECK(offset == sf::Time::Zero); |
| 43 | + CHECK(length == sf::Time::Zero); |
| 44 | + CHECK(music.getChannelCount() == 0); |
| 45 | + CHECK(music.getSampleRate() == 0); |
| 46 | + CHECK(music.getStatus() == sf::SoundSource::Status::Stopped); |
| 47 | + CHECK(music.getPlayingOffset() == sf::Time::Zero); |
| 48 | + CHECK(!music.getLoop()); |
| 49 | + } |
| 50 | + |
| 51 | + SECTION("openFromFile()") |
| 52 | + { |
| 53 | + sf::Music music; |
| 54 | + |
| 55 | + SECTION("Invalid file") |
| 56 | + { |
| 57 | + REQUIRE(!music.openFromFile("does/not/exist.wav")); |
| 58 | + CHECK(music.getDuration() == sf::Time::Zero); |
| 59 | + const auto [offset, length] = music.getLoopPoints(); |
| 60 | + CHECK(offset == sf::Time::Zero); |
| 61 | + CHECK(length == sf::Time::Zero); |
| 62 | + CHECK(music.getChannelCount() == 0); |
| 63 | + CHECK(music.getSampleRate() == 0); |
| 64 | + CHECK(music.getStatus() == sf::SoundSource::Status::Stopped); |
| 65 | + CHECK(music.getPlayingOffset() == sf::Time::Zero); |
| 66 | + CHECK(!music.getLoop()); |
| 67 | + } |
| 68 | + |
| 69 | + SECTION("Valid file") |
| 70 | + { |
| 71 | + REQUIRE(music.openFromFile("Audio/ding.mp3")); |
| 72 | + CHECK(music.getDuration() == sf::microseconds(1990884)); |
| 73 | + const auto [offset, length] = music.getLoopPoints(); |
| 74 | + CHECK(offset == sf::Time::Zero); |
| 75 | + CHECK(length == sf::microseconds(1990884)); |
| 76 | + CHECK(music.getChannelCount() == 1); |
| 77 | + CHECK(music.getSampleRate() == 44100); |
| 78 | + CHECK(music.getStatus() == sf::SoundSource::Status::Stopped); |
| 79 | + CHECK(music.getPlayingOffset() == sf::Time::Zero); |
| 80 | + CHECK(!music.getLoop()); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + SECTION("openFromMemory()") |
| 85 | + { |
| 86 | + std::vector<std::byte> memory; |
| 87 | + sf::Music music; |
| 88 | + |
| 89 | + SECTION("Invalid buffer") |
| 90 | + { |
| 91 | + REQUIRE(!music.openFromMemory(memory.data(), memory.size())); |
| 92 | + CHECK(music.getDuration() == sf::Time::Zero); |
| 93 | + const auto [offset, length] = music.getLoopPoints(); |
| 94 | + CHECK(offset == sf::Time::Zero); |
| 95 | + CHECK(length == sf::Time::Zero); |
| 96 | + CHECK(music.getChannelCount() == 0); |
| 97 | + CHECK(music.getSampleRate() == 0); |
| 98 | + CHECK(music.getStatus() == sf::SoundSource::Status::Stopped); |
| 99 | + CHECK(music.getPlayingOffset() == sf::Time::Zero); |
| 100 | + CHECK(!music.getLoop()); |
| 101 | + } |
| 102 | + |
| 103 | + SECTION("Valid buffer") |
| 104 | + { |
| 105 | + memory = loadIntoMemory("Audio/ding.flac"); |
| 106 | + REQUIRE(music.openFromMemory(memory.data(), memory.size())); |
| 107 | + CHECK(music.getDuration() == sf::microseconds(1990884)); |
| 108 | + const auto [offset, length] = music.getLoopPoints(); |
| 109 | + CHECK(offset == sf::Time::Zero); |
| 110 | + CHECK(length == sf::microseconds(1990884)); |
| 111 | + CHECK(music.getChannelCount() == 1); |
| 112 | + CHECK(music.getSampleRate() == 44100); |
| 113 | + CHECK(music.getStatus() == sf::SoundSource::Status::Stopped); |
| 114 | + CHECK(music.getPlayingOffset() == sf::Time::Zero); |
| 115 | + CHECK(!music.getLoop()); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + SECTION("openFromStream()") |
| 120 | + { |
| 121 | + sf::FileInputStream stream; |
| 122 | + sf::Music music; |
| 123 | + |
| 124 | + SECTION("Invalid stream") |
| 125 | + { |
| 126 | + CHECK(!music.openFromStream(stream)); |
| 127 | + CHECK(music.getDuration() == sf::Time::Zero); |
| 128 | + const auto [offset, length] = music.getLoopPoints(); |
| 129 | + CHECK(offset == sf::Time::Zero); |
| 130 | + CHECK(length == sf::Time::Zero); |
| 131 | + CHECK(music.getChannelCount() == 0); |
| 132 | + CHECK(music.getSampleRate() == 0); |
| 133 | + CHECK(music.getStatus() == sf::SoundSource::Status::Stopped); |
| 134 | + CHECK(music.getPlayingOffset() == sf::Time::Zero); |
| 135 | + CHECK(!music.getLoop()); |
| 136 | + } |
| 137 | + |
| 138 | + SECTION("Valid stream") |
| 139 | + { |
| 140 | + REQUIRE(stream.open("Audio/doodle_pop.ogg")); |
| 141 | + REQUIRE(music.openFromStream(stream)); |
| 142 | + CHECK(music.getDuration() == sf::microseconds(24002176)); |
| 143 | + const auto [offset, length] = music.getLoopPoints(); |
| 144 | + CHECK(offset == sf::Time::Zero); |
| 145 | + CHECK(length == sf::microseconds(24002176)); |
| 146 | + CHECK(music.getChannelCount() == 2); |
| 147 | + CHECK(music.getSampleRate() == 44100); |
| 148 | + CHECK(music.getStatus() == sf::SoundSource::Status::Stopped); |
| 149 | + CHECK(music.getPlayingOffset() == sf::Time::Zero); |
| 150 | + CHECK(!music.getLoop()); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + SECTION("play/pause/stop") |
| 155 | + { |
| 156 | + sf::Music music; |
| 157 | + REQUIRE(music.openFromFile("Audio/ding.mp3")); |
| 158 | + |
| 159 | + // Wait for background thread to start |
| 160 | + music.play(); |
| 161 | + while (music.getStatus() == sf::SoundSource::Status::Stopped) |
| 162 | + std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 163 | + CHECK(music.getStatus() == sf::SoundSource::Status::Playing); |
| 164 | + |
| 165 | + // Wait for background thread to pause |
| 166 | + music.pause(); |
| 167 | + while (music.getStatus() == sf::SoundSource::Status::Playing) |
| 168 | + std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 169 | + CHECK(music.getStatus() == sf::SoundSource::Status::Paused); |
| 170 | + |
| 171 | + // Wait for background thread to stop |
| 172 | + music.stop(); |
| 173 | + while (music.getStatus() == sf::SoundSource::Status::Paused) |
| 174 | + std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 175 | + CHECK(music.getStatus() == sf::SoundSource::Status::Stopped); |
| 176 | + } |
| 177 | + |
| 178 | + SECTION("setLoopPoints()") |
| 179 | + { |
| 180 | + sf::Music music; |
| 181 | + |
| 182 | + SECTION("No file") |
| 183 | + { |
| 184 | + music.setLoopPoints({sf::Time::Zero, sf::Time::Zero}); |
| 185 | + const auto [offset, length] = music.getLoopPoints(); |
| 186 | + CHECK(offset == sf::Time::Zero); |
| 187 | + CHECK(length == sf::Time::Zero); |
| 188 | + CHECK(music.getChannelCount() == 0); |
| 189 | + CHECK(music.getSampleRate() == 0); |
| 190 | + CHECK(music.getStatus() == sf::SoundSource::Status::Stopped); |
| 191 | + CHECK(music.getPlayingOffset() == sf::Time::Zero); |
| 192 | + CHECK(!music.getLoop()); |
| 193 | + } |
| 194 | + |
| 195 | + SECTION("Loaded file") |
| 196 | + { |
| 197 | + REQUIRE(music.openFromFile("Audio/killdeer.wav")); |
| 198 | + music.setLoopPoints({sf::seconds(1), sf::seconds(2)}); |
| 199 | + const auto [offset, length] = music.getLoopPoints(); |
| 200 | + CHECK(offset == sf::seconds(1)); |
| 201 | + CHECK(length == sf::seconds(2)); |
| 202 | + CHECK(music.getChannelCount() == 1); |
| 203 | + CHECK(music.getSampleRate() == 22050); |
| 204 | + CHECK(music.getStatus() == sf::SoundSource::Status::Stopped); |
| 205 | + CHECK(music.getPlayingOffset() == sf::Time::Zero); |
| 206 | + CHECK(!music.getLoop()); |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments