Skip to content

Commit 34ec279

Browse files
jcowgillChrisThrasher
authored andcommitted
Fix Ogg ov_read call on big-endian systems
In the `ov_read` API, the fouth parameter says what endianness the samples should be returned in - `0` for little-endian, and `1` for big-endian. SFML wants samples in the host endian, so we need to set this parameter to 1 on big-endian systems. Fixes a unit test failure on big-endian systems.
1 parent f930cbc commit 34ec279

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ For a closer look at breaking changes and how to migrate from SFML 2, check out
135135

136136
- Fixed `sf::SoundStream::play` bug (#2037)
137137
- Fixed poor `sf::SoundStream::setPlayingOffset` precision (#3101)
138+
- Fixed a bug when reading Ogg files on big endian systems (#3340)
138139

139140
### Network
140141

src/SFML/Audio/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ target_compile_definitions(sfml-audio PRIVATE MA_NO_MP3 MA_NO_FLAC MA_NO_ENCODIN
169169
# use standard fixed-width integer types
170170
target_compile_definitions(sfml-audio PRIVATE MA_USE_STDINT)
171171

172+
# detect the endianness as required by Ogg
173+
target_compile_definitions(sfml-audio PRIVATE SFML_IS_BIG_ENDIAN=$<STREQUAL:${CMAKE_CXX_BYTE_ORDER},BIG_ENDIAN>)
174+
172175
# setup dependencies
173176
target_link_libraries(sfml-audio
174177
PUBLIC SFML::System

src/SFML/Audio/SoundFileReaderOgg.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ std::uint64_t SoundFileReaderOgg::read(std::int16_t* samples, std::uint64_t maxC
196196
std::uint64_t count = 0;
197197
while (count < maxCount)
198198
{
199-
const int bytesToRead = static_cast<int>(maxCount - count) * static_cast<int>(sizeof(std::int16_t));
200-
const long bytesRead = ov_read(&m_vorbis, reinterpret_cast<char*>(samples), bytesToRead, 0, 2, 1, nullptr);
199+
const int bytesToRead = static_cast<int>(maxCount - count) * static_cast<int>(sizeof(std::int16_t));
200+
const long bytesRead = ov_read(&m_vorbis, reinterpret_cast<char*>(samples), bytesToRead, SFML_IS_BIG_ENDIAN, 2, 1, nullptr);
201201
if (bytesRead > 0)
202202
{
203203
const long samplesRead = bytesRead / static_cast<long>(sizeof(std::int16_t));

0 commit comments

Comments
 (0)