SFML community forums

Help => Audio => Topic started by: Acumen on October 14, 2021, 08:51:45 pm

Title: Audio Artifact on First Play after Volume Change
Post by: Acumen on October 14, 2021, 08:51:45 pm
So it has been a while since I have used SFML, but my build folder does say I am on 2.5.1.

I am trying to have a mute button, but am getting a strange artifact. It seems volumes will only change when the sound is actually playing.

So if volume is 100, set it to 0, it appears the next time the sound is played it will for a split second be at 100 then switch to 0, but it isn't before the sound starts so there is an audible blip. If that sound is played again (with no volume changes) this does not repeat. This happens individually for each effect, so it isn't one blip, but a blip for the first time each effect is played after change.

Sleep(1000);
sf::Listener::setGlobalVolume(100.0f);
effect[2].play();
Sleep(1000);
sf::Listener::setGlobalVolume(0.0f);
effect[2].play();
Sleep(1000);
effect[2].play();

Results in first play fine, second blip, third silent.

Sleep(1000);
sf::Listener::setGlobalVolume(100.0f);
effect[2].play();
Sleep(1000);
sf::Listener::setGlobalVolume(0.0f);
Sleep(1000);
effect[2].play();
Sleep(1000);
effect[2].play();

Results are the same as above, so a pause before playing doesn't help.

The same happens if I set the volume of individual effects rather than the listener.

Am I doing this incorrectly? Something with my system? With openal32? Seems unlikely that this would not already be in the forums that I could find if it wasn't something on my machine.

*EDIT*

I've been playing around a bunch, and gotten more clues. So I have some very short sounds, and so it is clear the volume change is only being applied while playing. Some are short enough where it takes 10 or 20 plays of the sound, progressively softer, for the volume to go to 0.

Doesn't seem like it should work this way.
Title: Re: Audio Artifact on First Play after Volume Change
Post by: Acumen on October 22, 2021, 04:39:02 pm
Anyone able to confirm if this is SFML/openAL, or my build/system?

Possible work arounds?
Title: Re: Audio Artifact on First Play after Volume Change
Post by: Acumen on November 22, 2021, 12:55:24 am
#include <SFML/OpenGL.hpp> // For Sleep()
#include <SFML/Audio.hpp>

int main()
{
        sf::SoundBuffer SFMLSoundBuffer;
        sf::Sound SFMLSound;

        SFMLSoundBuffer.loadFromFile(std::string("audio/") + "blip1.wav");
        SFMLSound.setBuffer(SFMLSoundBuffer);

        Sleep(1000);
        sf::Listener::setGlobalVolume(100.0f);
        SFMLSound.play();
        Sleep(1000);
        sf::Listener::setGlobalVolume(0.0f);
        Sleep(1000);
        SFMLSound.play();
        Sleep(1000);
        SFMLSound.play();
        Sleep(1000);

        return (0);
}

Complete example where blip1.wav is any audio file where the audio starts right away, so no padding of silence at the start.
Title: Re: Audio Artifact on First Play after Volume Change
Post by: eXpl0it3r on December 02, 2021, 12:29:40 am
Sorry for the delay, given that I promised to have a look the next day...

I've tested your code and I can't reproduce it.
For me there's only one sound being played and no blip when starting the next sound after a volume change.

Small modification to use sf::sleep(), but I guess this shouldn't really have an impact on it.

#include <SFML/System.hpp>
#include <SFML/Audio.hpp>

int main()
{
    sf::SoundBuffer SFMLSoundBuffer;
    sf::Sound SFMLSound;

    if (!SFMLSoundBuffer.loadFromFile("blip.wav"))
    {
        return -1;
    }
    SFMLSound.setBuffer(SFMLSoundBuffer);

    sf::sleep(sf::seconds(1.f));
    sf::Listener::setGlobalVolume(100.0f);
    SFMLSound.play();
    sf::sleep(sf::seconds(1.f));
    sf::Listener::setGlobalVolume(0.0f);
    sf::sleep(sf::seconds(1.f));
    SFMLSound.play();
    sf::sleep(sf::seconds(1.f));
    SFMLSound.play();
    sf::sleep(sf::seconds(1.f));
}

Attached is also the audio clip I used.

I ran with on VS 2022 and the latest master (today's commit level).
Are you also running on SFML master?
Title: Re: Audio Artifact on First Play after Volume Change
Post by: Acumen on December 02, 2021, 11:35:52 pm
Hey!

Thanks very much for confirming it was not reproducible. I dug into it and tracked down the culprit to be the openal32.dll I was using.

No idea how or why that would be the case after a bit of googling. One of the first things I did initially was check if it had been updated but seemed to me like 2009 was the last revision so I didn't bother swapping it. Guess that was way off, or I had a weird build of it somehow?

Appreciate it!




Title: Re: Audio Artifact on First Play after Volume Change
Post by: eXpl0it3r on December 03, 2021, 12:54:13 am
Ah, always make sure to use the DLL that's shipped with SFML. But it right next to your executable to ensure, no system DLL is loaded.