gpt4 book ai didi

java - OpenAL 仅渲染一个 "bump",但没有错误。到底是怎么回事?

转载 作者:太空宇宙 更新时间:2023-11-04 09:31:57 26 4
gpt4 key购买 nike

我正在尝试在 OpenAL 中制作一个简单的声音播放器作为学习练习。我浏览了几个示例和教程,总是得到相同的结果:没有声音,也没有错误。我读过OpenAL Documentation而且它从未提到过类似的事情。

我最终认为我可能没有安装 OpenAL,这很有趣,但我没有安装。所以安装后我也没有声音,也没有错误。

然后我认为我的缓冲区填充不正确,所以我尝试仅使用 Java 库来播放它。事实证明它运行得很好!

[经过大量测试,已编辑]

现在我有了音频,但在调用 alSourcePlay 后以及有时在应用程序结束时,它只是一个“颠簸”。如果我使用 AL_FORMAT_STEREO8 或 AL_FORMAT_MONO8 而不是 16 位格式,就会出现这种情况。我认为这是格式的问题,但如果格式正确,则不会发生任何情况;甚至没有“碰撞”。

我只是不知道出了什么问题。我必须承认,这是我第一次与 OpenAL 合作。关于 OpenAL 故障排除的信息很少,似乎重点是 OpenGL 和 Vulkan。

这是我到目前为止编写的代码。我正在使用LWJGL访问 OpenAL 和 Java 8。


import static org.lwjgl.openal.AL10.AL_BUFFER;
import static org.lwjgl.openal.AL10.AL_FORMAT_MONO16;
import static org.lwjgl.openal.AL10.AL_FORMAT_MONO8;
import static org.lwjgl.openal.AL10.AL_FORMAT_STEREO16;
import static org.lwjgl.openal.AL10.AL_FORMAT_STEREO8;
import static org.lwjgl.openal.AL10.AL_GAIN;
import static org.lwjgl.openal.AL10.AL_INVALID_ENUM;
import static org.lwjgl.openal.AL10.AL_INVALID_VALUE;
import static org.lwjgl.openal.AL10.AL_NO_ERROR;
import static org.lwjgl.openal.AL10.AL_OUT_OF_MEMORY;
import static org.lwjgl.openal.AL10.AL_PITCH;
import static org.lwjgl.openal.AL10.AL_POSITION;
import static org.lwjgl.openal.AL10.AL_VELOCITY;
import static org.lwjgl.openal.AL10.alBufferData;
import static org.lwjgl.openal.AL10.alDeleteBuffers;
import static org.lwjgl.openal.AL10.alDeleteSources;
import static org.lwjgl.openal.AL10.alGenBuffers;
import static org.lwjgl.openal.AL10.alGenSources;
import static org.lwjgl.openal.AL10.alGetError;
import static org.lwjgl.openal.AL10.alListener3f;
import static org.lwjgl.openal.AL10.alSource3f;
import static org.lwjgl.openal.AL10.alSourcePlay;
import static org.lwjgl.openal.AL10.alSourcef;
import static org.lwjgl.openal.AL10.alSourcei;
import static org.lwjgl.openal.ALC10.alcCloseDevice;
import static org.lwjgl.openal.ALC10.alcCreateContext;
import static org.lwjgl.openal.ALC10.alcMakeContextCurrent;
import static org.lwjgl.openal.ALC10.alcOpenDevice;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

import org.lwjgl.openal.AL;
import org.lwjgl.openal.ALC;
import org.lwjgl.openal.ALC10;
import org.lwjgl.openal.ALCCapabilities;

import dev.ckitty.engine.kc.io.AutoByteBuffer;
import dev.ckitty.engine.kc.io.FileLoader;
import dev.ckitty.engine.kc.main.Engine;
import dev.ckitty.engine.kc.utils.Util;

public class AudioMaster {

private long device, context;
private int buffer, source;

public AudioMaster() {
init();
load();
source();

Engine.log("Playing!");
play();
Util.waitSec(10);

deinit();
}

public void init() {
device = alcOpenDevice((ByteBuffer) null);
if (device == 0)
throw new IllegalStateException("Failed to open the default device.");

ALCCapabilities deviceCaps = ALC.createCapabilities(device);

context = alcCreateContext(device, (IntBuffer) null);
if (context == 0)
throw new IllegalStateException("Failed to create an OpenAL context.");

alcMakeContextCurrent(context);
Engine.log(erroralc());

AL.createCapabilities(deviceCaps);
Engine.log(error());
}

public void load() {
AutoByteBuffer abb = new AutoByteBuffer();
AudioFormat format = null;

try {
InputStream is = FileLoader.Internal.getInputStream("/kc/audios/LFZ - Popsicle.wav");
BufferedInputStream bis = new BufferedInputStream(is);
AudioInputStream ais = AudioSystem.getAudioInputStream(bis);
abb.put(ais);
format = ais.getFormat();

ais.close();
bis.close();
} catch (Exception e) {
e.printStackTrace();
}

buffer = alGenBuffers();
int samplerate = (int) format.getSampleRate();
alBufferData(buffer, AL_FORMAT_STEREO8, abb.flip(), samplerate);
}

public void source() {
source = alGenSources();
Engine.log(error());
alSourcef(source, AL_GAIN, 1f);
alSourcef(source, AL_PITCH, 1f);
alSource3f(source, AL_POSITION, 0f, 0f, 0f);
alSourcei(source, AL_BUFFER, buffer);
}

public void play() {
alListener3f(AL_POSITION, 0, 0, 0);
alListener3f(AL_VELOCITY, 0, 0, 0);



Engine.log("PLAYING");
alSourcePlay(source);
}

public String erroralc() {
switch (ALC10.alcGetError(device)) {
case AL_NO_ERROR: return "ALC_NO_ERROR";
case AL_INVALID_ENUM: return "ALC_INVALID_ENUM";
case AL_INVALID_VALUE: return "ALC_INVALID_VALUE";
case AL_OUT_OF_MEMORY: return "ALC_OUT_OF_MEMORY";
/* ... */
default:
return "Unknown error code";
}
}

public String error() {
switch (alGetError()) {
case AL_NO_ERROR: return "AL_NO_ERROR";
case AL_INVALID_ENUM: return "AL_INVALID_ENUM";
case AL_INVALID_VALUE: return "AL_INVALID_VALUE";
case AL_OUT_OF_MEMORY: return "AL_OUT_OF_MEMORY";
/* ... */
default:
return "Unknown error code";
}
}

public void deinit() {
alDeleteSources(source);
alDeleteBuffers(buffer);
alcCloseDevice(device);
}

}

自动字节缓冲区


import java.io.InputStream;
import java.nio.ByteBuffer;

public class AutoByteBuffer {

private byte[] array = new byte[0];
private int index;

public void put(InputStream is) {
int n;
byte[] data = new byte[1024];
try {
while ((n = is.read(data)) != -1)
put(data, 0, n);
} catch (Exception e) {
e.printStackTrace();
}
}

public void put(byte[] src) {
put(src, 0, src.length);
}

public void put(byte[] src, int off, int length) {
resize(src.length);
for (int i = 0; i < length; i++) {
array[index++] = src[off + i];
}
}

protected void resize(int bytes) {
Math.addExact(index, bytes);
if (index + bytes >= array.length) {
int newsize = (int) ((index + bytes) * 1.5 + 0.5);
if (newsize < index + bytes)
throw new RuntimeException("Could not store enough memory!");

byte[] copy = new byte[newsize];
System.arraycopy(array, 0, copy, 0, index);
array = copy;
}
}

public ByteBuffer flip() {
ByteBuffer buffer = ByteBuffer.allocate(index);
buffer.put(array, 0, index);
buffer.flip();
return buffer;
}

}

控制台输出

 > Starting AudioMaster!
AL lib: (EE) UpdateDeviceParams: Failed to set 44100hz, got 48000hz instead
> ALC_NO_ERROR
> AL_NO_ERROR
> AL_NO_ERROR
> Playing!
> PLAYING

最佳答案

我仍然不知道发生了什么,但我是如何关注的 this教程。这家伙告诉我们使用 this加载 wav 文件的代码,它似乎工作得很好!我的理论是效果很好,因为它使用 LWJGL 缓冲区而不是 java 缓冲区。查看 LWJGL 的源代码,它似乎确实很重要,因为它试图获取缓冲区的内存地址。当然,如果 LWJGL 是创建缓冲区的人,它就会确切地知道它在哪里。

关于java - OpenAL 仅渲染一个 "bump",但没有错误。到底是怎么回事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57007563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com