Fix saving FLAC files to non-ASCII paths

This commit is contained in:
Chris Thrasher 2025-02-07 10:24:26 -07:00
parent 41dc238981
commit 2241bee942
4 changed files with 13 additions and 8 deletions

View File

@ -137,14 +137,16 @@ bool SoundFileWriterFlac::open(const std::filesystem::path& filename,
return false;
}
// Open file
m_file = openFile(filename, "w+b");
// Setup the encoder
FLAC__stream_encoder_set_channels(m_encoder.get(), channelCount);
FLAC__stream_encoder_set_bits_per_sample(m_encoder.get(), 16);
FLAC__stream_encoder_set_sample_rate(m_encoder.get(), sampleRate);
// Initialize the output stream
if (FLAC__stream_encoder_init_file(m_encoder.get(), filename.string().c_str(), nullptr, nullptr) !=
FLAC__STREAM_ENCODER_INIT_STATUS_OK)
if (FLAC__stream_encoder_init_FILE(m_encoder.get(), m_file, nullptr, nullptr) != FLAC__STREAM_ENCODER_INIT_STATUS_OK)
{
err() << "Failed to write flac file (failed to open the file)\n" << formatDebugPathInfo(filename) << std::endl;
m_encoder.reset();

View File

@ -36,6 +36,7 @@
#include <vector>
#include <cstdint>
#include <cstdio>
namespace sf::priv
@ -90,6 +91,7 @@ private:
{
void operator()(FLAC__StreamEncoder* encoder) const;
};
std::FILE* m_file{};
std::unique_ptr<FLAC__StreamEncoder, FlacStreamEncoderDeleter> m_encoder; //!< FLAC stream encoder
unsigned int m_channelCount{}; //!< Number of channels
std::array<std::size_t, 8> m_remapTable{}; //!< Table we use to remap source to target channel order

View File

@ -17,7 +17,7 @@ TEST_CASE("[Audio] sf::OutputSoundFile")
}
const std::u32string stem = GENERATE(U"tmp", U"tmp-ń", U"tmp-🐌");
const std::u32string extension = GENERATE(U".wav", U".ogg"); // , U".flac"); FLAC fails to handle Unicode strings
const std::u32string extension = GENERATE(U".wav", U".ogg", U".flac");
const auto filename = std::filesystem::temp_directory_path() / std::filesystem::path(stem + extension);
const std::vector<sf::SoundChannel> channelMap{sf::SoundChannel::FrontLeft, sf::SoundChannel::FrontRight};

View File

@ -183,12 +183,13 @@ TEST_CASE("[Audio] sf::SoundBuffer", runAudioDeviceTests())
SECTION("saveToFile()")
{
const auto filename = std::filesystem::temp_directory_path() / "ding.flac";
const std::u32string stem = GENERATE(U"tmp", U"tmp-ń", U"tmp-🐌");
const std::u32string extension = GENERATE(U".wav", U".ogg", U".flac");
const auto filename = std::filesystem::temp_directory_path() / std::filesystem::path(stem + extension);
{
const sf::SoundBuffer soundBuffer("Audio/ding.flac");
REQUIRE(soundBuffer.saveToFile(filename));
}
INFO("Filename: " << reinterpret_cast<const char*>(filename.u8string().c_str()));
REQUIRE(sf::SoundBuffer("Audio/ding.flac").saveToFile(filename));
const sf::SoundBuffer soundBuffer(filename);
CHECK(soundBuffer.getSamples() != nullptr);