SFML community forums

Help => Audio => Topic started by: Ironbell on November 20, 2015, 06:01:06 pm

Title: Problem with sf::Music on loop with the latest SFML Version
Post by: Ironbell on November 20, 2015, 06:01:06 pm
Hey all,

We've updated our game on Github to use SFML as a submodule. The version of SFML that we were using before was SFML 2.2 and everything worked fine with the music loop in our level. With the new SFML version however, the music loop didn't run smoothly and when I looked into the logs, the following message appeared after every loop:

(click to show/hide)

The code we are using to instantiate the music looks like this:

(click to show/hide)

Has anyone an idea why the AL check might fail? Any help is greatly appreciated.
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: eXpl0it3r on November 20, 2015, 06:35:27 pm
Which version is "the new SFML version"? master branch or 2.3.2?
What compiler do you use?
And what's the OS you're testing this on?


Also hello fellow Swiss citizen! ;D
Your project looks quite interesting.
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: Ironbell on November 20, 2015, 06:57:16 pm
Our project references the master branch.
The problem occurred on Windows 8 x64 (Visual Studio 2013 x64 in debug mode) and also in Mac OS X 10.11 (compiler: clang) and was never seen before the update.

Also, it's nice to see that there are Swiss people working on SFML :)
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: eXpl0it3r on November 20, 2015, 07:46:07 pm
What audio format are you playing? Can you provide that file?


I'm not the only Swiss on the SFML team. ;)
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: Ironbell on November 20, 2015, 07:55:38 pm
We're only playing .wav files. The particular file lies @ https://github.com/tizian/Cendric2/raw/master/res/level/ratcave/cave_try.wav. Please don't judge, it's only a placeholder track to test the music in the game.
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: Hapax on November 20, 2015, 11:24:28 pm
I tested it using your sound file and it failed to loop correctly for me too.

"Failed to loop correctly" also means - for me - that it seems to stutter at the end (repeats a small part) before starting from the beginning again.

I tested debug, release, debug (static), and release (static).

Windows 7, SFML release version 2.3.2 (32-bit)


UPDATE: I opened your file in Audacity (http://audacityteam.org/) and resaved it as a .WAV file and it no longer has OpenAL errors. It does, however, click, but that's just imperfect looping. I also resaved it as an .OGG file and it works perfectly.
Here they are:
cave_try 2.wav (http://www.mediafire.com/listen/dyrb5frcr8dqybk/cave_try_2.wav)
cave_try 2.ogg (http://www.mediafire.com/listen/s638a4f2rmt0c6d/cave_try_2.ogg)

Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: Ironbell on November 21, 2015, 01:01:39 am
I've tried the two files you provided and they work for me just like they do for you. It still seems pretty strange to me - I'm going to save them from now on in the .ogg format. But anyway, thank you very much for the kind responses and your efforts!  :)
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: eXpl0it3r on November 21, 2015, 01:16:33 am
I've tried the same thing (with Audacity) and have gotten the same result. While trying to figure out why, I could only find that when the loop starts filling the buffer, the size varies between the two files. The original file contains 13824 samples, while the modified file contains 13839 samples.
From SFML 2.2 to 2.3 we switched from libsndfile to vorbis, ogg and flac. The question probably is why libsndfile accepts it without problems.

For music OGG is anyways recommended, since it's compressed.
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: FRex on November 21, 2015, 03:52:11 pm
Quote
Also, it's nice to see that there are Swiss people working on SFML :)
The conspiracy runs much deeper than that.
Here's the list of Simple Swiss and Fast Multimedia Library devs from GitHub to which I added their nationalities:
Quote
Laurent Gomila - French
Marco Antognini - Swiss
Jonathan De Wachter - Belgian (speaks French)
Jan Haller - Swiss
Stefan Schindler - German
Lukas Dürrenberger - Swiss
binary1248 - Swiss
Artur Moreira - Portuguese
Mario Liebisch - German??
As you can see, 8 out of 9 people in it speak one of Switzerland's official languages* and half of that group is Swiss.

Yes, yes, yes, I know there are dialects and varieties and so on but still!! Swiss conspiracy!! :P
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: zsbzsb on November 22, 2015, 11:25:26 am
I tested it using your sound file and it failed to loop correctly for me too.

"Failed to loop correctly" also means - for me - that it seems to stutter at the end (repeats a small part) before starting from the beginning again.

Hapax, when you say 'stutter' do you mean it sounds like the audio file from this post (http://en.sfml-dev.org/forums/index.php?topic=19320.msg139649#msg139649)?
Title: Problem with sf::Music on loop with the latest SFML Version
Post by: eXpl0it3r on November 22, 2015, 12:57:58 pm
I can hear a "stutter" as well. It sounds more like a short interrupt. Since one function call on a buffer fails it just might fill it wrongly.
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: Ironbell on November 22, 2015, 02:30:22 pm
Quote
Also, it's nice to see that there are Swiss people working on SFML :)
The conspiracy runs much deeper than that.
Here's the list of Simple Swiss and Fast Multimedia Library devs from GitHub to which I added their nationalities:
Quote
Laurent Gomila - French
Marco Antognini - Swiss
Jonathan De Wachter - Belgian (speaks French)
Jan Haller - Swiss
Stefan Schindler - German
Lukas Dürrenberger - Swiss
binary1248 - Swiss
Artur Moreira - Portuguese
Mario Liebisch - German??
As you can see, 8 out of 9 people in it speak one of Switzerland's official languages* and half of that group is Swiss.

Yes, yes, yes, I know there are dialects and varieties and so on but still!! Swiss conspiracy!! :P

Swiss Fast Multimedia Library made my day. That makes working with it even more fun and it really fits for our project, as the game will be available in English, German aaaand yes, Swiss German.  ;)

I can hear a "stutter" as well. It sounds more like a short interrupt. Since one function call on a buffer fails it just might fill it wrongly.

Do you think it's an OpenAL bug or that the .wav file is really corrupted? The latter would bug me; it's created with Fruity Loops and I'm planning to use that program also for other sound in the game.
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: eXpl0it3r on November 22, 2015, 03:33:13 pm
I'm not exactly sure. Fact is libsndfile can open it fine (otherwise Audacity would have an issue), so it might well be SFML's implementation. The issue itself seems to be that SFML passes a wrong size to the OpenAL function, which would end up in an overflow.

The only thing I've noticed when quickly debugging it, is that data.samples is slightly different between the original one and the one from Audacity: 27648 vs 27678

Maybe Laurent, who wrote the WAV thingy implementation, would be faster at figuring it out. But if I got some more time next week, I'll try to look at it more closely.
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: Laurent on November 22, 2015, 05:46:15 pm
I'll try to have a look.
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: FRex on November 22, 2015, 10:25:10 pm
I dug into it a bit.
I've got a Python script online ( http://blog.theroyweb.com/extracting-wav-file-header-information-using-a-python-script ) and checked out the wav file:
Quote
python wav.py cave_try.wav
Subchunks Found:
fmt ,  data,  smpl,  acid,  LIST, 

Data Chunk located at offset [36] of data length [9376744] bytes
ByteRate:  176400
BitsPerSample:  16
NumChannels:  2
Subchunk1Size:  16
ChunkSize:  9376914
Format:  WAVE
AudioFormat:  1
BlockAlign:  4
SampleRate:  44100
Filename:  cave_try.wav
The data length is 9376744 bytes.
But I checked and SFML wav file reader reads 9376878 bytes from the wav in total.
I also checked and ogg file has exactly 13772 samples in last array it passes to OpenAL, while wav has 13839.
This checks out: SFML reads 9376878 - 9376744 = 134 extra bytes and since samples are 16 bit it's exactly 13839 - 13772 = 67 samples, 2 bytes per sample, 134 bytes.
If I hardcode it to change size of last array to 13772 when count is 13839 (cutting off the extra 67 samples) then it works perfectly for wav too.
So SFML assumes that last chunk is data till end of file and reads it blindly, which results in reading some chunks that are other stuff as samples, which produces the extra samples that cause stutter and OpenAL error.
I have no idea why it casues OpenAL error though.
This error has nothing to do with looping, BTW. If you play the wav just once it'll stutter at the end and cause the OpenAL error too.

And this is what last 134 bytes are in hex and ascii:
[frex@localhost audiobug]$ tail --bytes=134 cave_try.wav | xxd -g 1
00000000: 73 6d 70 6c 3c 00 00 00 00 00 00 00 00 00 00 00  smpl<...........
00000010: 94 58 00 00 3c 00 00 00 00 00 00 00 00 00 00 00  .X..<...........
00000020: 00 00 00 00 01 00 00 00 00 00 00 00 00 00 02 00  ................
00000030: 00 04 00 00 00 00 00 00 f9 c4 23 00 00 00 00 00  ..........#.....
00000040: 00 00 00 00 61 63 69 64 18 00 00 00 00 00 00 00  ....acid........
00000050: 3c 00 80 00 00 00 00 00 50 00 00 00 04 00 04 00  <.......P.......
00000060: 9a 99 b4 42 4c 49 53 54 1a 00 00 00 49 4e 46 4f  ...BLIST....INFO
00000070: 49 53 46 54 0d 00 00 00 46 4c 20 53 74 75 64 69  ISFT....FL Studi
00000080: 6f 20 31 31 00 00                                o 11..
Looks too good as ascii (FL Studio 11 :P) to be samples so it's probably metadata and the like.

Edit: Oh, also - Sound does not have this problem, because SoundBuffer uses InputSoundFile::getSampleCount which returns correct value. Music has the problem because it keeps calling the reader, which in case of wav reader goes YOLO till end of file, reading in some additional data that it shouldn't. That's why the bug sat there undetected for so long, wav for sounds works fine, and for music most people use ogg because of file size.

Quote
The only thing I've noticed when quickly debugging it, is that data.samples is slightly different between the original one and the one from Audacity: 27648 vs 27678
data.samples as in the value of samples itself? Or did you mean count (that number is way too little to be sample count for such a long file BTW)? Anyway - Hapax's file does not cause OpenAL error but it also has extra non data bytes at the end (112 of them) that SFML treats as samples. And Hapax's and original ogg are same, exactly same, byte for byte. And all four files (wav and ogg, both Hapax's and OP's) correctly return 4688372 as sample count from InputSoundFile::getSampleCount.
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: IsThisTheRealLife on November 23, 2015, 11:59:13 am
Quote
Oh, also - Sound does not have this problem
This 'sounds' a lot like my problem (http://en.sfml-dev.org/forums/index.php?topic=19320.0), which only happens with music. Maybe they are related somehow
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: FRex 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.
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: FRex 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
 
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: Laurent on December 08, 2015, 08:07:48 pm
You should submit a PR, otherwise this patch will most likely be forgotten ;)
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: FRex on December 09, 2015, 11:21:33 am
https://github.com/SFML/SFML/pull/1018
Title: Re: Problem with sf::Music on loop with the latest SFML Version
Post by: Laurent on December 09, 2015, 12:14:15 pm
Thanks :)