SFML community forums

Help => Audio => Topic started by: tgnottingham on October 18, 2017, 01:36:12 am

Title: Ogg Vorbis files don't play correctly on big-endian systems
Post by: tgnottingham on October 18, 2017, 01:36:12 am
Ogg Vorbis files sound like static when played on big-endian systems using SFML. This is because ov_read in SoundFileReaderOgg.cpp requires you to specify the endianness that you want the samples in, and it's currently always passed 0, corresponding to little-endian.

I've tested a solution that checks system endianness to pass the right argument to ov_read on both x86-64 (little-endian) and big-endian PowerPC, and it seems to fix the problem.

FWIW, I've read the discussion on endianness in https://github.com/SFML/SFML/pull/357, and I believe this is one of those rare cases where we have to check endianness, because, for whatever reason, ov_read can't be invoked to automatically use the native byte order.

Example Ogg Vorbis File (https://www.dropbox.com/s/0m7ato0itmpwrrr/tone_16.ogg?dl=1)
 Documentation for ov_read (https://xiph.org/vorbis/doc/vorbisfile/ov_read.html)

Example code (invoke on big-endian system with sound file name argument):

Code: [Select]
#include <SFML/Audio.hpp>
#include <SFML/System.hpp>

int main(int argc, char** argv)
{
    sf::SoundBuffer sb;
    sb.loadFromFile(argv[1]);

    sf::Sound s(sb);
    s.play();
    sf::sleep(sf::seconds(2.5));
}
Title: Re: Ogg Vorbis files don't play correctly on big-endian systems
Post by: Laurent on October 18, 2017, 08:24:47 am
Interesting, thanks for taking the time to investigate this issue. How did you check the native byte order?
Title: Re: Ogg Vorbis files don't play correctly on big-endian systems
Post by: tgnottingham on October 18, 2017, 11:23:14 pm
Code: [Select]
bool is_big_endian()
{   
    sf::Uint16 n = 1;
    return *reinterpret_cast<char*>(&n) == 0;
}

It's not 100% portable, but I'm guessing it's portable enough for the systems SFML is likely to run on. I'm also guessing it gets mostly optimized away when you turn up optimization. But these are just guesses. Maybe someone with more expertise on endianness can chime in. Or I'll do some research.