Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem with sf::Music on loop with the latest SFML Version  (Read 12496 times)

0 Members and 1 Guest are viewing this topic.

IsThisTheRealLife

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Problem with sf::Music on loop with the latest SFML Version
« Reply #15 on: November 23, 2015, 11:59:13 am »
Quote
Oh, also - Sound does not have this problem
This 'sounds' a lot like my problem, which only happens with music. Maybe they are related somehow

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Problem with sf::Music on loop with the latest SFML Version
« Reply #16 on: November 24, 2015, 03:54:38 pm »
They are almost surely not related.
This is a bug that only happens when wav files with additional non-audio stuff at the end are read too far by the wav reader.
Music exhibits that bug because it reads as much as possible so the wav reader reads past the end of actual data in the wav file.
SoundBuffer doesn't because it reads exactly sample count of samples, and the sample count is correct in the wav reader.
It has nothing to do with ogg reader and Music OpenAL code itself. And I couldn't reproduce your bug on my Linux.
Back to C++ gamedev with SFML in May 2023

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Problem with sf::Music on loop with the latest SFML Version
« Reply #17 on: December 08, 2015, 05:09:27 pm »
This would fix this bug.
It adds a check before reading each sample in the loop in read to see if the wav data section ended (using same way to turn sample count to offset like seek function already does).

diff --git a/src/SFML/Audio/SoundFileReaderWav.cpp b/src/SFML/Audio/SoundFileReaderWav.cpp
index 5323f51..4e86f5b 100644
--- a/src/SFML/Audio/SoundFileReaderWav.cpp
+++ b/src/SFML/Audio/SoundFileReaderWav.cpp
@@ -110,7 +110,8 @@ bool SoundFileReaderWav::check(InputStream& stream)
 SoundFileReaderWav::SoundFileReaderWav() :
 m_stream        (NULL),
 m_bytesPerSample(0),
-m_dataStart     (0)
+m_dataStart     (0),
+m_sampleCount   (0)
 {
 }
 
@@ -145,7 +146,7 @@ Uint64 SoundFileReaderWav::read(Int16* samples, Uint64 maxCount)
     assert(m_stream);
 
     Uint64 count = 0;
-    while (count < maxCount)
+    while (count < maxCount && m_stream->tell() < m_dataStart + m_sampleCount * m_bytesPerSample)
     {
         switch (m_bytesPerSample)
         {
@@ -283,7 +284,8 @@ bool SoundFileReaderWav::parseHeader(Info& info)
             // "data" chunk
 
             // Compute the total number of samples
-            info.sampleCount = subChunkSize / m_bytesPerSample;
+            m_sampleCount = subChunkSize / m_bytesPerSample;
+            info.sampleCount = m_sampleCount;
 
             // Store the starting position of samples in the file
             m_dataStart = m_stream->tell();
diff --git a/src/SFML/Audio/SoundFileReaderWav.hpp b/src/SFML/Audio/SoundFileReaderWav.hpp
index a52bc1f..a824cdb 100644
--- a/src/SFML/Audio/SoundFileReaderWav.hpp
+++ b/src/SFML/Audio/SoundFileReaderWav.hpp
@@ -111,6 +111,7 @@ private:
     InputStream* m_stream;         ///< Source stream to read from
     unsigned int m_bytesPerSample; ///< Size of a sample, in bytes
     Uint64       m_dataStart;      ///< Starting position of the audio data in the open file
+    Uint64       m_sampleCount;
 };
 
 } // namespace priv
 
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with sf::Music on loop with the latest SFML Version
« Reply #18 on: December 08, 2015, 08:07:48 pm »
You should submit a PR, otherwise this patch will most likely be forgotten ;)
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with sf::Music on loop with the latest SFML Version
« Reply #20 on: December 09, 2015, 12:14:15 pm »
Thanks :)
Laurent Gomila - SFML developer

 

anything