1
Audio / [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
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'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;
}
#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'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;
}