2023-11-27 06:31:42 +08:00
|
|
|
#include <SFML/Audio/SoundFileFactory.hpp>
|
|
|
|
|
|
|
|
// Other 1st party headers
|
2023-04-16 22:12:31 +08:00
|
|
|
#include <SFML/Audio/SoundChannel.hpp>
|
2023-11-27 06:31:42 +08:00
|
|
|
#include <SFML/Audio/SoundFileReader.hpp>
|
|
|
|
#include <SFML/Audio/SoundFileWriter.hpp>
|
|
|
|
|
|
|
|
#include <SFML/System/FileInputStream.hpp>
|
2024-02-05 22:27:39 +08:00
|
|
|
#include <SFML/System/InputStream.hpp>
|
2023-11-27 06:31:42 +08:00
|
|
|
|
|
|
|
#include <catch2/catch_test_macros.hpp>
|
|
|
|
|
2024-02-05 22:27:39 +08:00
|
|
|
#include <filesystem>
|
2023-11-27 06:31:42 +08:00
|
|
|
#include <type_traits>
|
|
|
|
|
2024-02-05 22:27:39 +08:00
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
struct NoopSoundFileReader : sf::SoundFileReader
|
|
|
|
{
|
|
|
|
static bool check(sf::InputStream&)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<Info> open(sf::InputStream&) override
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void seek(std::uint64_t) override
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::uint64_t read(std::int16_t*, std::uint64_t) override
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NoopSoundFileWriter : sf::SoundFileWriter
|
|
|
|
{
|
|
|
|
static bool check(const std::filesystem::path&)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-16 22:12:31 +08:00
|
|
|
bool open(const std::filesystem::path&, unsigned int, unsigned int, const std::vector<sf::SoundChannel>&) override
|
2024-02-05 22:27:39 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write(const std::int16_t*, std::uint64_t) override
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2023-11-27 06:31:42 +08:00
|
|
|
TEST_CASE("[Audio] sf::SoundFileFactory")
|
|
|
|
{
|
|
|
|
SECTION("Type traits")
|
|
|
|
{
|
|
|
|
STATIC_CHECK(std::is_copy_constructible_v<sf::SoundFileFactory>);
|
|
|
|
STATIC_CHECK(std::is_copy_assignable_v<sf::SoundFileFactory>);
|
|
|
|
STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::SoundFileFactory>);
|
|
|
|
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::SoundFileFactory>);
|
|
|
|
}
|
|
|
|
|
2024-02-05 22:27:39 +08:00
|
|
|
SECTION("isReaderRegistered()")
|
|
|
|
{
|
|
|
|
CHECK(!sf::SoundFileFactory::isReaderRegistered<NoopSoundFileReader>());
|
|
|
|
|
|
|
|
sf::SoundFileFactory::registerReader<NoopSoundFileReader>();
|
|
|
|
CHECK(sf::SoundFileFactory::isReaderRegistered<NoopSoundFileReader>());
|
|
|
|
|
|
|
|
sf::SoundFileFactory::unregisterReader<NoopSoundFileReader>();
|
|
|
|
CHECK(!sf::SoundFileFactory::isReaderRegistered<NoopSoundFileReader>());
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("isWriterRegistered()")
|
|
|
|
{
|
|
|
|
CHECK(!sf::SoundFileFactory::isWriterRegistered<NoopSoundFileWriter>());
|
|
|
|
|
|
|
|
sf::SoundFileFactory::registerWriter<NoopSoundFileWriter>();
|
|
|
|
CHECK(sf::SoundFileFactory::isWriterRegistered<NoopSoundFileWriter>());
|
|
|
|
|
|
|
|
sf::SoundFileFactory::unregisterWriter<NoopSoundFileWriter>();
|
|
|
|
CHECK(!sf::SoundFileFactory::isWriterRegistered<NoopSoundFileWriter>());
|
|
|
|
}
|
|
|
|
|
2023-11-27 06:31:42 +08:00
|
|
|
SECTION("createReaderFromFilename()")
|
|
|
|
{
|
|
|
|
SECTION("Missing file")
|
|
|
|
{
|
|
|
|
CHECK(!sf::SoundFileFactory::createReaderFromFilename("does/not/exist.wav"));
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Valid file")
|
|
|
|
{
|
|
|
|
CHECK(sf::SoundFileFactory::createReaderFromFilename("Audio/ding.flac"));
|
|
|
|
CHECK(sf::SoundFileFactory::createReaderFromFilename("Audio/ding.mp3"));
|
|
|
|
CHECK(sf::SoundFileFactory::createReaderFromFilename("Audio/doodle_pop.ogg"));
|
|
|
|
CHECK(sf::SoundFileFactory::createReaderFromFilename("Audio/killdeer.wav"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("createReaderFromStream()")
|
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
sf::FileInputStream stream;
|
2023-11-27 06:31:42 +08:00
|
|
|
|
2024-06-10 11:02:54 +08:00
|
|
|
SECTION("flac")
|
2023-11-27 06:31:42 +08:00
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
REQUIRE(stream.open("Audio/ding.flac"));
|
2023-11-27 06:31:42 +08:00
|
|
|
}
|
|
|
|
|
2024-06-10 11:02:54 +08:00
|
|
|
SECTION("mp3")
|
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
REQUIRE(stream.open("Audio/ding.mp3"));
|
2024-06-10 11:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("ogg")
|
2023-11-27 06:31:42 +08:00
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
REQUIRE(stream.open("Audio/doodle_pop.ogg"));
|
2023-11-27 06:31:42 +08:00
|
|
|
}
|
2024-06-10 11:02:54 +08:00
|
|
|
|
|
|
|
SECTION("wav")
|
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
REQUIRE(stream.open("Audio/killdeer.wav"));
|
2024-06-10 11:02:54 +08:00
|
|
|
}
|
|
|
|
|
2024-07-06 10:24:00 +08:00
|
|
|
CHECK(sf::SoundFileFactory::createReaderFromStream(stream));
|
2023-11-27 06:31:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("createWriterFromFilename()")
|
|
|
|
{
|
|
|
|
SECTION("Invalid extension")
|
|
|
|
{
|
|
|
|
CHECK(!sf::SoundFileFactory::createWriterFromFilename("cannot/write/to.txt"));
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Valid extension")
|
|
|
|
{
|
|
|
|
CHECK(sf::SoundFileFactory::createWriterFromFilename("file.flac"));
|
|
|
|
CHECK(!sf::SoundFileFactory::createWriterFromFilename("file.mp3")); // Mp3 writing not yet implemented
|
|
|
|
CHECK(sf::SoundFileFactory::createWriterFromFilename("file.ogg"));
|
|
|
|
CHECK(sf::SoundFileFactory::createWriterFromFilename("file.wav"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|