我无法使用3D声音听到前后位置的区别

时间:2016-01-14 19:43:59

标签: c++ audio sfml

我想制作真正的3D音效。我可以听到左右之间的区别,但不能听到前后之间的区别。

以下是我设置监听器的方法:

sf::Listener::setPosition(0.f, 0.f, 0.f);
sf::Listener::setDirection(0.f, 0.f, 1.f);

这是我设置声音位置的方法:

sound.setPosition(0.f, 0.f, -10.f);

然后在下一秒移动它。

sound.setPosition(0.f, 0.f, 10.f);

如果我改变X坐标,我会听到声音从左向右移动,但它对Z坐标不起作用。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

这段代码对我有用。前面的声音有点怪异。

#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;
}

参考文献:
sf::Music Doc
Audio Spatialization with SFML
Audio Streams