Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sf::Sound does not play mono sf::SoundBuffer when previously played stereo sf::SoundBuffer #2319

Open
varnholt opened this issue Dec 27, 2022 · 6 comments

Comments

@varnholt
Copy link

Subject of the issue

sf::Sound does not play mono sf::SoundBuffer when previously played stereo sf::SoundBuffer.

Your environment

  • Windows 11
  • SFML 2.5.1
  • MSVC 16.11

Steps to reproduce

I load an sf::SoundBuffer (a stereo sample) into my sf::Sound, play it, all good.
Later I decide to recycle the sf::Sound in order to play another sf::SoundBuffer (a mono sample this time) and play that.

Expected behavior

I can hear the 2nd sample.

Actual behavior

I can't hear the 2nd sample.

Workaround

Just converting all my samples to stereo resolves the problem.

@eXpl0it3r
Copy link
Member

Can't reproduce this with SFML 2.5.1, the 2.6.x branch and latest master (thanks GPT Chat for the example code):

#include <SFML/Audio.hpp>

int main()
{
    // Load the stereo sound buffer
    sf::SoundBuffer stereoBuffer;
    if (!stereoBuffer.loadFromFile("stereo.wav"))
    {
        // Error loading the sound buffer
        return 1;
    }

    // Load the mono sound buffer
    sf::SoundBuffer monoBuffer;
    if (!monoBuffer.loadFromFile("mono.wav"))
    {
        // Error loading the sound buffer
        return 1;
    }

    // Create a sound instance
    sf::Sound sound;

    // Play the stereo sound
    sound.setBuffer(stereoBuffer);
    sound.play();

    // Wait for the sound to finish playing
    while (sound.getStatus() == sf::Sound::Playing)
    {
        // Do nothing
    }

    // Play the mono sound
    sound.setBuffer(monoBuffer);
    sound.play();

    // Wait for the sound to finish playing
    while (sound.getStatus() == sf::Sound::Playing)
    {
        // Do nothing
    }
}

Sound files used: mono-stereo.zip

Does the posted example also cause the issue you're describing?

@varnholt
Copy link
Author

Thanks to you and ChatGPT for trying this out :)
This example works perfectly fine on my end.
While I'll try to come up with a working repro, feel free to close the issue for now.

@varnholt
Copy link
Author

Here's a link to my code:
https://github.com/varnholt/deceptus_engine/blob/master/src/game/audio.cpp
It doesn't do much more than the example above.

@eXpl0it3r
Copy link
Member

Some things one could try:

  • I see your code sets a sound position without checking the sample. You can only position mono sounds.
  • Maybe there's some issue with setting the looping?
  • Or some issue with setting the volume?

@varnholt
Copy link
Author

I have mono samples in my project which have not been played at all by my Audio implementation.
Just converting them to stereo resolved the issue for now.

The only difference in my implementation when it comes to playing sound is that an sf::Sound has been used a couple of times before it plays a particular sample for the first time. And mono sounds are particularly problematic.

Also:

  • The position is an optional which currently never has a value assigned; so that position is never set.
  • I've output the volume; for the affected samples 100.0f was passed to SFML.
  • None of the non-audible sounds is looped; but I can review that code a bit. I doubt it causes any issue, though.

@Hapaxia
Copy link
Contributor

Hapaxia commented Jan 31, 2024

If the position is not set, it will be at default (0, 0, 0). This is a problematic value as it's not particularly clear which direction from the listener (also defaults to 0, 0, 0) the mono sound should be (it does, in fact, have no direction since it's at the same location).

Basically, mono sounds that originate from the same position as the listener can cause issues. The solution is to actively and purposefully move it away. Just a tiny amount can be enough; just place it in front of the listener (to get mono in both sides).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants