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

Author Topic: Audio Spacialization  (Read 2894 times)

0 Members and 1 Guest are viewing this topic.

jill

  • Newbie
  • *
  • Posts: 19
    • View Profile
Audio Spacialization
« on: December 17, 2016, 09:34:44 pm »
Hello, thanks to SFML, it is simple and very easy to learn. The sound play and record works on my FreeBSD  computer. My computer is crude and has low hardware configure. sf::SoundBufferRecorder::getAvailableDevices has found two andio record devices on my system:
OSS Default
pcm2:record:dsp2

The problem is I can't make spacialization aka 3D Sound work. I have tried to set some different distances between the listener and the sound source, but the sound heard in my ears are the same without any differences. The following is my c++ code and shell test script line. (c++ code was compile to ./run with clang. cut.oga is short audio file. mksh)

#include <SFML/Audio.hpp>
#include <iostream>
#include <string>

int main(int argc, char * argv[]) {
        if (argc != 2) {std::cerr << argv[0] << " [Listener Z Position]." << std::endl; return 1;}
        float z = std::stof(argv[1]);

        sf::SoundBuffer buffer;
        if (!buffer.loadFromFile("cut.oga")) {std::cerr << "Load cut.oga error.\n"; return 1;}
        sf::Sound sound;
        sound.setBuffer(buffer);
        sound.play();
        sound.setLoop(false);

        sf::Listener::setPosition(0.0f, 0.0f, z);
        sf::Listener::setDirection(0.0f, 0.0f, -1.0f);
        sf::Listener::setUpVector(0.0f, 1.0f, 0.0f);
        sf::Listener::setGlobalVolume(50.0f);

        sound.setPosition(0.0f, 0.0f, 0.0f);
        sound.setRelativeToListener(false);
        sound.setMinDistance(2.0f);
        sound.setAttenuation(20.0f);

        while (sound.getStatus() == sf::Sound::Playing) {
                sf::sleep(sf::seconds(0.1f));
        }

        return 0;
}
 

z=4; while [ $z -gt -5 ]; do echo $z; ./run $((z--)); done
 

3D GUI world should be rendered in your brain to simplify the code.  ;)
« Last Edit: December 17, 2016, 09:41:29 pm by jill »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Audio Spacialization
« Reply #1 on: December 17, 2016, 10:01:31 pm »
I turned the code into something I didn't need a bash for and it works fine for me.

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

int main()
{
    for(int z = 4; z > -10; --z)
        {
                std::cout << "Z position: " << z << "\n";

                sf::SoundBuffer buffer;
                if (!buffer.loadFromFile("canary.flac"))
                {
                        std::cerr << "Load canary.flac error.\n";
                        return 1;
                }

                sf::Sound sound;
                sound.setBuffer(buffer);
                sound.play();
                sound.setLoop(false);

                sf::Listener::setPosition(0.0f, 0.0f, static_cast<float>(z));
                sf::Listener::setDirection(0.0f, 0.0f, -1.0f);
                sf::Listener::setUpVector(0.0f, 1.0f, 0.0f);
                sf::Listener::setGlobalVolume(50.0f);

                sound.setPosition(0.0f, 0.0f, 0.0f);
                sound.setRelativeToListener(false);
                sound.setMinDistance(2.0f);
                sound.setAttenuation(20.0f);

                while (sound.getStatus() == sf::Sound::Playing)
                {
                        sf::sleep(sf::seconds(0.1f));
                }
        }
}
 

Sound goes from silence to a good volume and then slowly drifts off into nearly unhearable.

(Not sure what listing your recording devices had anything to do with the issue).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

jayasimha

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Audio Spacialization
« Reply #2 on: February 02, 2017, 08:44:10 am »
Hi

 in this code every iteration your setting Listener Global Volume to 50 then How it goes sound from silent to audible.

 

anything