2

在我的 3D 游戏中,我目前有通过工厂类“声音”工作的声音。我正在通过我的相机类初始化 OpenAL,加载时它将存储其位置、方向和速度的全局浮动缓冲区

private static FloatBuffer listenerPosition = BufferUtils.createFloatBuffer( 3 ).put( new float[] { X(), Y(), Z() } );
private static FloatBuffer listenerOrientation = BufferUtils.createFloatBuffer( 6 ).put (new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f } );
private static FloatBuffer listenerVelocity = BufferUtils.createFloatBuffer( 3 ).put (new float[] { Velocity.x, Velocity.y, Velocity.z } );

然后在相机移动或旋转时的每个刻度上,它会使用代码更新这些

listenerVelocity.put(0, Velocity.x);
listenerVelocity.put(1, Velocity.y);
listenerVelocity.put(2, Velocity.z);

alListener( AL_POSITION, listenerPosition );
alListener( AL_ORIENTATION, listenerOrientation );
alListener( AL_VELOCITY, listenerVelocity );

这是我认为不让 OpenAL 知道我想要的声音的类,尽管我正在向它提供它所需要的所有信息,据我所知。

private int ID;

public Sound(String name) {
    try {
        ID = alGenBuffers();
        WaveData data = WaveData.create(new BufferedInputStream(new FileInputStream("res/Sound/"+name+".wav")));
        alBufferData(ID, data.format, data.data, data.samplerate);
        data.dispose();
    } catch (FileNotFoundException e) {
        JOptionPane.showMessageDialog(null, "Could not find \"" + name + "\"", "IO Exception", JOptionPane.ERROR_MESSAGE);
        Display.destroy();
        System.exit(1);
    }
}

public void play(float x, float y, float z) {
    playSound(ID, new Vector3f(x,y,z));
}

private static void playSound(int buffer, Vector3f pos) {
    while(alGetSourcei(Sources.get(currentsource), AL10.AL_SOURCE_STATE) == AL_PLAYING) {
        currentsource++;
        currentsource %= 10; //there are only 10 sources
    }
    alSourcei(Sources.get(currentsource), AL_BUFFER,   buffer );
    alSourcef(Sources.get(currentsource), AL_PITCH,    1.0f   );
    alSourcef(Sources.get(currentsource), AL_GAIN,     1.0f   );
    alSourcei(Sources.get(currentsource), AL_LOOPING,  AL_FALSE);
    alSourcef(Sources.get(currentsource), AL_REFERENCE_DISTANCE, 0);
    alSourcef(Sources.get(currentsource), AL_MAX_DISTANCE, 100);

    alSourcePlay(Sources.get(currentsource));
}

public static boolean hasLoaded(){return loaded;}

我怀疑是 playSound 方法,有没有更好的方法来找到非播放源?是否有任何我没有给出的属性会导致声音没有任何 3D 属性?

4

2 回答 2

1

您应该查看音频文件通道,因为 openAL 仅对单声道声音应用衰减。

于 2013-08-27T14:27:43.070 回答
0

您的源位置未设置。您可能听不到声音,因为默认情况下它处于零位置,而您的听众远离那里。在 playSound 中尝试设置如下内容:

alSourcefv(Sources.get(currentsource), AL_POSITION, sourcePosition);
于 2013-06-28T12:13:09.767 回答