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

Author Topic: [Help] Sound attenuation  (Read 2320 times)

0 Members and 1 Guest are viewing this topic.

Charmander

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
[Help] Sound attenuation
« on: September 28, 2024, 08:47:51 pm »
Hello folks,
I am playing with the sound in SFML. And my code does not make any difference with respect to the attention. I would appreciate it if someone corrects me. Or if the example code is correct, can it be my eaphone setting?

Thanks for your time

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <cmath>


 namespace
 {
    const float ListenerZ = 10.f;
    const float Attenuation = 1.f;
    const float MinDistance2D = 0.1f;
    const float MinDistance3D =  
      std::sqrt(MinDistance2D*MinDistance2D + ListenerZ*ListenerZ);
 }

int main() {
    // Load a sound buffer from a file
    sf::SoundBuffer buffer;
    if (!buffer.loadFromFile("Media/sound.wav")) {
        return -1; // Return if the sound file can&#39;t be loaded
    }

    // Create a sound instance and set the buffer
    sf::Sound sound;
    sound.setBuffer(buffer);
    // sound.setLoop(true); // Loop the sound for continuous effect
    // sound.play(); // Play the sound

    // Set the initial position of the sound source (in 3D space);
    sound.setPosition(0.f, 0.f, 0.f); // Position at (0, 0, 0)
    sound.setAttenuation(Attenuation);
    sound.setMinDistance(MinDistance3D);
    // sound.setPosition(0.f, 0.f, 0.f); // Position at (0, 0, 0)
    // sound.setMinDistance(100.f); // Minimum distance before the sound starts to attenuate (reduced for a more noticeable effect)
    // sound.setAttenuation(100.f); // Attenuation factor for the sound fall-off (increased for more dramatic effect)
    sound.setRelativeToListener(true);

    // Set up the listener (our "ears")
    sf::Listener::setPosition(0.f, 0.f, ListenerZ); // Position the listener further from the source
    sf::Listener::setDirection(0.f, 0.f, -1.f); // Facing towards negative Z
    // sf::Listener::setDirection(0.5f, 0.f, 0.5f);

    // Set up a simple window to visualize sound movement
    sf::RenderWindow window(sf::VideoMode(1200, 900), "SFML 3D Sound Attenuation Example");

    // To visualize, create a circle to represent the sound source
    sf::CircleShape sourceCircle(10.f);
    sourceCircle.setFillColor(sf::Color::Red);
    sourceCircle.setPosition(400.f, 300.f); // Initial position in the center of the window

    // Game loop to control the window
    while (window.isOpen()) {
        // printf("%lf %lf\n", sound.getPosition().x, sound.getPosition().y);
        // printf("%lf %lf\n", sf::Listener::getPosition().x, sf::Listener::getPosition().y);
        // printf("----------------------\n");
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // Move the sound source using arrow keys
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
            // Only move if within right boundary
            if (sourceCircle.getPosition().x + sourceCircle.getRadius() * 2 < window.getSize().x) {
                sourceCircle.move(0.1f, 0.f);            
                sound.setPosition(sourceCircle.getPosition().x, sourceCircle.getPosition().y, 0.f);
            }
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
            // Only move if within left boundary
            if (sourceCircle.getPosition().x > 0) {
                sourceCircle.move(-0.1f, 0.f);
                sound.setPosition(sourceCircle.getPosition().x, sourceCircle.getPosition().y, 0.f);
            }
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
            // Only move if within top boundary
            if (sourceCircle.getPosition().y > 0) {
                sourceCircle.move(0.f, -0.1f);
                sound.setPosition(sourceCircle.getPosition().x, sourceCircle.getPosition().y, 0.f);
            }
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
            // Only move if within bottom boundary
            if (sourceCircle.getPosition().y + sourceCircle.getRadius() * 2 < window.getSize().y) {
                sourceCircle.move(0.f, 0.1f);
                sound.setPosition(sourceCircle.getPosition().x, sourceCircle.getPosition().y, 0.f);
            }
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
            sound.play();
        }

        // Clear the window and draw the sound source
        window.clear();
        window.draw(sourceCircle);
        window.display();
    }

    return 0;
}

 
« Last Edit: September 28, 2024, 09:28:14 pm by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11018
    • View Profile
    • development blog
    • Email
Re: [Help] Sound attenuation
« Reply #1 on: October 03, 2024, 08:17:32 pm »
You set the sound relative to the listener, you likely want this to be absolute instead.

Quote
Making a sound relative to the listener will ensure that it will always be played the same way regardless of the position of the listener. This can be useful for non-spatialized sounds, sounds that are produced by the listener, or sounds attached to it. The default value is false (position is absolute).

Also make sure, you're using the openal32.dll that SFML ships with (it needs to be placed next to your executable) and not some other DLL randomly found on your system. Some versions can cause issues similar to this.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/