I want to make a real 3D sound effect. I can hear the difference between right and left, but not between in front and behind.
Here is how I set the listener:
sf::Listener::setPosition(0.f, 0.f, 0.f);
sf::Listener::setDirection(0.f, 0.f, 1.f);
This is how I set the position of sound:
sound.setPosition(0.f, 0.f, -10.f);
And then move it the next second.
sound.setPosition(0.f, 0.f, 10.f);
If I change the X coordinate, I hear the sound shift from left to right, but it doesn't work for Z coordinate.
What am I doing wrong?
This code worked for me. The sound is a bit weird on the front.
#include <SFML/Audio.hpp>
#include <iostream>
#include <math.h>
int main()
{
// initialize and play our custom stream
sf::Music stream;
stream.openFromFile("music.ogg");
#define MAX_R 10
#define PI 3.141592653589793238
float x = MAX_R, y = 0, z = MAX_R;
stream.setPosition(x, y, z);
stream.setVolume(100);
stream.setMinDistance(5.f);
stream.setAttenuation(10.f);
stream.setLoop(true);
stream.play();
float angle = 0.f;
// let it play until it is finished
while (stream.getStatus() == sf::SoundSource::Playing) {
sf::sleep(sf::seconds(0.1f));
x = cos(angle * PI / 180) * MAX_R;
z = sin(angle * PI / 180) * MAX_R;
std::cout << x << " " << z << " ";
if (angle < 90.f)
std::cout << "1\n";
else if (angle < 180.f)
std::cout << "2\n";
else if (angle < 270.f)
std::cout << "3\n";
else
std::cout << "4\n";
stream.setPosition(x, y, z);
angle += 5.f;
if (angle > 360)
angle = 0.f;
}
return 0;
}
References:
sf::Music Doc
Audio Spatialization with SFML
Audio Streams
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With