2022-07-25 14:36:05 +08:00
|
|
|
#include <SFML/Audio/SoundBuffer.hpp>
|
|
|
|
|
2023-04-16 10:50:24 +08:00
|
|
|
// Other 1st party headers
|
|
|
|
#include <SFML/System/FileInputStream.hpp>
|
|
|
|
|
|
|
|
#include <catch2/catch_test_macros.hpp>
|
|
|
|
|
|
|
|
#include <AudioUtil.hpp>
|
|
|
|
#include <SystemUtil.hpp>
|
|
|
|
#include <array>
|
2022-07-25 14:36:05 +08:00
|
|
|
#include <type_traits>
|
|
|
|
|
2023-04-16 10:50:24 +08:00
|
|
|
TEST_CASE("[Audio] sf::SoundBuffer", runAudioDeviceTests())
|
|
|
|
{
|
|
|
|
SECTION("Type traits")
|
|
|
|
{
|
|
|
|
STATIC_CHECK(std::is_copy_constructible_v<sf::SoundBuffer>);
|
|
|
|
STATIC_CHECK(std::is_copy_assignable_v<sf::SoundBuffer>);
|
|
|
|
STATIC_CHECK(std::is_move_constructible_v<sf::SoundBuffer>);
|
|
|
|
STATIC_CHECK(!std::is_nothrow_move_constructible_v<sf::SoundBuffer>);
|
|
|
|
STATIC_CHECK(std::is_move_assignable_v<sf::SoundBuffer>);
|
|
|
|
STATIC_CHECK(!std::is_nothrow_move_assignable_v<sf::SoundBuffer>);
|
|
|
|
}
|
|
|
|
|
2024-07-06 09:38:32 +08:00
|
|
|
SECTION("Construction")
|
|
|
|
{
|
|
|
|
const sf::SoundBuffer soundBuffer;
|
|
|
|
CHECK(soundBuffer.getSamples() == nullptr);
|
|
|
|
CHECK(soundBuffer.getSampleCount() == 0);
|
|
|
|
CHECK(soundBuffer.getSampleRate() == 44100);
|
|
|
|
CHECK(soundBuffer.getChannelCount() == 1);
|
|
|
|
CHECK(soundBuffer.getDuration() == sf::Time::Zero);
|
|
|
|
}
|
|
|
|
|
2023-04-16 10:50:24 +08:00
|
|
|
SECTION("Copy semantics")
|
|
|
|
{
|
2024-07-06 09:38:32 +08:00
|
|
|
const auto soundBuffer = sf::SoundBuffer::createFromFile("Audio/ding.flac").value();
|
2023-04-16 10:50:24 +08:00
|
|
|
|
|
|
|
SECTION("Construction")
|
|
|
|
{
|
|
|
|
const sf::SoundBuffer soundBufferCopy(soundBuffer); // NOLINT(performance-unnecessary-copy-initialization)
|
|
|
|
CHECK(soundBufferCopy.getSamples() != nullptr);
|
|
|
|
CHECK(soundBufferCopy.getSampleCount() == 87798);
|
|
|
|
CHECK(soundBufferCopy.getSampleRate() == 44100);
|
|
|
|
CHECK(soundBufferCopy.getChannelCount() == 1);
|
|
|
|
CHECK(soundBufferCopy.getDuration() == sf::microseconds(1990884));
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Assignment")
|
|
|
|
{
|
2024-07-06 09:38:32 +08:00
|
|
|
sf::SoundBuffer soundBufferCopy = sf::SoundBuffer::createFromFile("Audio/doodle_pop.ogg").value();
|
2024-05-19 05:37:06 +08:00
|
|
|
soundBufferCopy = soundBuffer;
|
2023-04-16 10:50:24 +08:00
|
|
|
CHECK(soundBufferCopy.getSamples() != nullptr);
|
|
|
|
CHECK(soundBufferCopy.getSampleCount() == 87798);
|
|
|
|
CHECK(soundBufferCopy.getSampleRate() == 44100);
|
|
|
|
CHECK(soundBufferCopy.getChannelCount() == 1);
|
|
|
|
CHECK(soundBufferCopy.getDuration() == sf::microseconds(1990884));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("loadFromFile()")
|
|
|
|
{
|
2024-07-06 09:38:32 +08:00
|
|
|
sf::SoundBuffer soundBuffer;
|
|
|
|
|
2023-04-16 10:50:24 +08:00
|
|
|
SECTION("Invalid filename")
|
|
|
|
{
|
2024-07-06 09:38:32 +08:00
|
|
|
CHECK(!soundBuffer.loadFromFile("does/not/exist.wav"));
|
2023-04-16 10:50:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Valid file")
|
|
|
|
{
|
2024-07-06 09:38:32 +08:00
|
|
|
REQUIRE(soundBuffer.loadFromFile("Audio/ding.flac"));
|
2023-04-16 10:50:24 +08:00
|
|
|
CHECK(soundBuffer.getSamples() != nullptr);
|
|
|
|
CHECK(soundBuffer.getSampleCount() == 87798);
|
|
|
|
CHECK(soundBuffer.getSampleRate() == 44100);
|
|
|
|
CHECK(soundBuffer.getChannelCount() == 1);
|
|
|
|
CHECK(soundBuffer.getDuration() == sf::microseconds(1990884));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("loadFromMemory()")
|
|
|
|
{
|
2024-07-06 09:38:32 +08:00
|
|
|
sf::SoundBuffer soundBuffer;
|
|
|
|
|
2023-04-16 10:50:24 +08:00
|
|
|
SECTION("Invalid memory")
|
|
|
|
{
|
2024-07-06 09:38:32 +08:00
|
|
|
CHECK(!soundBuffer.loadFromMemory(nullptr, 0));
|
2023-04-16 10:50:24 +08:00
|
|
|
constexpr std::array<std::byte, 5> memory{};
|
2024-07-06 09:38:32 +08:00
|
|
|
CHECK(!soundBuffer.loadFromMemory(memory.data(), memory.size()));
|
2023-04-16 10:50:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Valid memory")
|
|
|
|
{
|
2024-07-06 09:38:32 +08:00
|
|
|
const auto memory = loadIntoMemory("Audio/ding.flac");
|
|
|
|
REQUIRE(soundBuffer.loadFromMemory(memory.data(), memory.size()));
|
2023-04-16 10:50:24 +08:00
|
|
|
CHECK(soundBuffer.getSamples() != nullptr);
|
|
|
|
CHECK(soundBuffer.getSampleCount() == 87798);
|
|
|
|
CHECK(soundBuffer.getSampleRate() == 44100);
|
|
|
|
CHECK(soundBuffer.getChannelCount() == 1);
|
|
|
|
CHECK(soundBuffer.getDuration() == sf::microseconds(1990884));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("loadFromStream()")
|
|
|
|
{
|
2024-07-06 09:38:32 +08:00
|
|
|
sf::FileInputStream stream;
|
|
|
|
sf::SoundBuffer soundBuffer;
|
|
|
|
|
|
|
|
SECTION("Invalid stream")
|
|
|
|
{
|
|
|
|
CHECK(!soundBuffer.loadFromStream(stream));
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Valid stream")
|
|
|
|
{
|
|
|
|
REQUIRE(stream.open("Audio/ding.flac"));
|
|
|
|
REQUIRE(soundBuffer.loadFromStream(stream));
|
|
|
|
CHECK(soundBuffer.getSamples() != nullptr);
|
|
|
|
CHECK(soundBuffer.getSampleCount() == 87798);
|
|
|
|
CHECK(soundBuffer.getSampleRate() == 44100);
|
|
|
|
CHECK(soundBuffer.getChannelCount() == 1);
|
|
|
|
CHECK(soundBuffer.getDuration() == sf::microseconds(1990884));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("createFromFile()")
|
|
|
|
{
|
|
|
|
SECTION("Invalid filename")
|
|
|
|
{
|
|
|
|
CHECK(!sf::SoundBuffer::createFromFile("does/not/exist.wav"));
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Valid file")
|
|
|
|
{
|
|
|
|
const auto soundBuffer = sf::SoundBuffer::createFromFile("Audio/ding.flac").value();
|
|
|
|
CHECK(soundBuffer.getSamples() != nullptr);
|
|
|
|
CHECK(soundBuffer.getSampleCount() == 87798);
|
|
|
|
CHECK(soundBuffer.getSampleRate() == 44100);
|
|
|
|
CHECK(soundBuffer.getChannelCount() == 1);
|
|
|
|
CHECK(soundBuffer.getDuration() == sf::microseconds(1990884));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("createFromMemory()")
|
|
|
|
{
|
|
|
|
SECTION("Invalid memory")
|
|
|
|
{
|
|
|
|
constexpr std::array<std::byte, 5> memory{};
|
|
|
|
CHECK(!sf::SoundBuffer::createFromMemory(memory.data(), memory.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Valid memory")
|
|
|
|
{
|
|
|
|
const auto memory = loadIntoMemory("Audio/ding.flac");
|
|
|
|
const auto soundBuffer = sf::SoundBuffer::createFromMemory(memory.data(), memory.size()).value();
|
|
|
|
CHECK(soundBuffer.getSamples() != nullptr);
|
|
|
|
CHECK(soundBuffer.getSampleCount() == 87798);
|
|
|
|
CHECK(soundBuffer.getSampleRate() == 44100);
|
|
|
|
CHECK(soundBuffer.getChannelCount() == 1);
|
|
|
|
CHECK(soundBuffer.getDuration() == sf::microseconds(1990884));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("createFromStream()")
|
|
|
|
{
|
|
|
|
auto stream = sf::FileInputStream::create("Audio/ding.flac").value();
|
|
|
|
const auto soundBuffer = sf::SoundBuffer::createFromStream(stream).value();
|
2024-06-10 11:02:54 +08:00
|
|
|
CHECK(soundBuffer.getSamples() != nullptr);
|
|
|
|
CHECK(soundBuffer.getSampleCount() == 87798);
|
|
|
|
CHECK(soundBuffer.getSampleRate() == 44100);
|
|
|
|
CHECK(soundBuffer.getChannelCount() == 1);
|
|
|
|
CHECK(soundBuffer.getDuration() == sf::microseconds(1990884));
|
2023-04-16 10:50:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("saveToFile()")
|
|
|
|
{
|
|
|
|
const auto filename = std::filesystem::temp_directory_path() / "ding.flac";
|
|
|
|
|
|
|
|
{
|
2024-07-06 09:38:32 +08:00
|
|
|
const auto soundBuffer = sf::SoundBuffer::createFromFile("Audio/ding.flac").value();
|
2023-04-16 10:50:24 +08:00
|
|
|
REQUIRE(soundBuffer.saveToFile(filename));
|
|
|
|
}
|
|
|
|
|
2024-07-06 09:38:32 +08:00
|
|
|
const auto soundBuffer = sf::SoundBuffer::createFromFile(filename).value();
|
2023-04-16 10:50:24 +08:00
|
|
|
CHECK(soundBuffer.getSamples() != nullptr);
|
|
|
|
CHECK(soundBuffer.getSampleCount() == 87798);
|
|
|
|
CHECK(soundBuffer.getSampleRate() == 44100);
|
|
|
|
CHECK(soundBuffer.getChannelCount() == 1);
|
|
|
|
CHECK(soundBuffer.getDuration() == sf::microseconds(1990884));
|
|
|
|
|
|
|
|
CHECK(std::filesystem::remove(filename));
|
|
|
|
}
|
|
|
|
}
|