SFML community forums

Help => Audio => Topic started by: jill on December 17, 2016, 09:34:44 pm

Title: Audio Spacialization
Post by: jill 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.  ;)
Title: Re: Audio Spacialization
Post by: eXpl0it3r 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).
Title: Re: Audio Spacialization
Post by: jayasimha 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.