OpenAL 3D声音

时间:2013-06-26 19:05:29

标签: audio openal

在我的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属性?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您的来源位置未设置。您可能听不到声音,因为它默认位于零位置,而您的听众远离那里。在playSound尝试设置这样的东西:

alSourcefv(Sources.get(currentsource), AL_POSITION, sourcePosition);