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

Author Topic: 3D Audio Spatialization not working (SOLVED)  (Read 1092 times)

0 Members and 1 Guest are viewing this topic.

Swampertor

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
3D Audio Spatialization not working (SOLVED)
« on: September 27, 2024, 04:37:48 pm »
EDIT: I was adding relative to listener

Hi! I'm making a small game engine and I ran into the (small) 3d spatialization audio documentation. After reading through it I tried to make a small test to see how spatialization works. There are some issues that I'm not finding out how to fix them, and I'm not sure if it's because I'm missing something or if I'm completely wrong.
The test contains 4 sound sources surrounding the original Listeners position. Of course if I play left sound, it is heard on the left side, and so on. BUT trying to move the Listener object around in space doesn't change the way I hear the sounds. Attenuation is the same, volume is the same, sound positions feel the same. Is there anything I'm missing?

Attached the code and showing it below as well

Moving audio Listener is possible with Q and E (left and right movement)
Playing the four audio sources is possible with W A S D (for each direction)

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

#include <iostream>
#include <cmath>

int main()
{
  sf::RenderWindow window(sf::VideoMode(200, 200), "SFML 3D Audio Test");
  sf::Sound leftSound;
  sf::Sound rightSound;
  sf::Sound upSound;
  sf::Sound downSound;

  sf::SoundBuffer buffer;

  if (!buffer.loadFromFile("D:\\University\\SFMLSandbox\\x64\\Debug\\pingas.mp3"))
  {
    std::cout << "Error finding file " << std::endl;
    return -1;
  }
  leftSound.setBuffer(buffer);
  leftSound.setRelativeToListener(true);
  rightSound.setBuffer(buffer);
  rightSound.setRelativeToListener(true);
  upSound.setBuffer(buffer);
  upSound.setRelativeToListener(true);
  downSound.setBuffer(buffer);
  downSound.setRelativeToListener(true);

  leftSound.setMinDistance(5.0f);
  rightSound.setMinDistance(5.0f);
  upSound.setMinDistance(5.0f);
  downSound.setMinDistance(5.0f);

  leftSound.setAttenuation(5.0f);
  rightSound.setAttenuation(5.0f);
  upSound.setAttenuation(5.0f);
  downSound.setAttenuation(5.0f);

  sf::Listener::setPosition(0, 0, 0);

  sf::Clock c;
  sf::Time dt;

  leftSound.setPosition(-50, 0, 0);
  rightSound.setPosition(50, 0, 0);
  upSound.setPosition(0, -50, 0);
  downSound.setPosition(0, 50, 0);

  dt = c.restart();
  while (window.isOpen())
  {
    sf::Event event;
    while (window.pollEvent(event))
    {
      if (event.type == sf::Event::Closed)
      {
        window.close();
      }
     
    }

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
    {
      leftSound.play();
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
    {
      rightSound.play();
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
    {
      upSound.play();
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
    {
      downSound.play();
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
    {
      sf::Listener::setPosition(sf::Listener::getPosition().x - 5 * dt.asSeconds(), 0, 0);
      std::cout << sf::Listener::getPosition().x << std::endl;
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::E))
    {
      sf::Listener::setPosition(sf::Listener::getPosition().x + 5 * dt.asSeconds(), 0, 0);
      std::cout << sf::Listener::getPosition().x << std::endl;
    }
    window.clear();
    window.display();
    dt = c.restart();
  }

  return 0;
}

 
« Last Edit: September 27, 2024, 06:30:27 pm by Swampertor »