alListener3f(AL_ORIENTATION, 0, 0, -1) 之后打开AL AL_ILLEGAL_ENUM

发布于 2024-11-26 21:11:31 字数 2962 浏览 1 评论 0原文

差不多就这样了。我有一个函数来设置 OpenAL 上下文和设备,并将 WAV 文件加载到缓冲区中,并将该缓冲区与文件中的数据一起传递到 alBufferData。然后我尝试调用 alSourcePlay(source) 但没有声音发出。整个函数编译并运行良好,但通过调试我发现 alGetError() 函数在调用 alListener3f(AL_ORIENTATION, 0, 0, -1) 后返回 AL_ILLEGAL_ENUM。 AL_ORIENTATION 确实是一个有效的 OpenAL 枚举吗?我一直在关注这里的教程: http://enigma-dev.org/ forums/index.php?topic=730.0

这是我的代码(请原谅混乱,疯狂的调试......):

void initOpenAL() {

ALenum errorNum = alGetError();
// initialise OpenAL
ALCdevice* device = alcOpenDevice(NULL);
ALCcontext* context = alcCreateContext(device, NULL);
alcMakeContextCurrent(context);

errorNum = alGetError();

alListener3f(AL_POSITION, 0, 0, 0);

errorNum = alGetError();

alListener3f(AL_VELOCITY, 0, 0, 0);

errorNum = alGetError();

alListener3f(AL_ORIENTATION, 0, 0, -1);

errorNum = alGetError(); // this is where alGetError == AL_INVALID_ENUM

alGenSources(1, &source);

errorNum = alGetError();

alSourcef(source, AL_PITCH, 1);
alSourcef(source, AL_GAIN, 2);
alSource3f(source, AL_POSITION, 0, 0, 0);
alSource3f(source, AL_VELOCITY, 0, 0, 0);
alSourcei(source, AL_LOOPING, AL_FALSE);

errorNum = alGetError();

ALuint buffer;
alGenBuffers(1, &buffer);
alSourcei(source, AL_BUFFER, buffer);

errorNum = alGetError();

FILE* file;
file = fopen("../../arcade.wav", "r");
if (file == NULL) {
    printf("File access error\n");
    exit(1);
}

char xbuffer[5];
if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "RIFF") != 0)
    throw "Not a WAV file";

file_read_int32_le(xbuffer, file);
if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "WAVE") != 0)
    throw "Not a WAV file!";

if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "fmt ") != 0)
    throw "Invalid WAV file!";

printf("char size: %i", sizeof(char));

errorNum = alGetError();

file_read_int32_le(xbuffer, file);
short audioFormat = file_read_int16_le(xbuffer, file);
short channels = file_read_int16_le(xbuffer, file);
int sampleRate = file_read_int32_le(xbuffer, file);
int byteRate = file_read_int32_le(xbuffer, file);
file_read_int16_le(xbuffer, file);
short bitsPerSample = file_read_int16_le(xbuffer, file);

if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "data") != 0)
    throw "Invalid WAV file";

int dataChunkSize = file_read_int32_le(xbuffer, file)+4;
unsigned char* bufferData = file_allocate_and_read_bytes(file, (size_t)dataChunkSize);

printf("Error before alBufferData: %x\n", alGetError());

float duration = float(dataChunkSize)/byteRate;
alBufferData(buffer, GetFormatFromInfo(channels, bitsPerSample), bufferData, dataChunkSize, sampleRate);
free(bufferData);
fclose(file);

errorNum = alGetError();

printf("Error after alBufferData, before alSourcePlay: %x\n");

alSourcePlay(source);
errorNum = alGetError();
if (errorNum != AL_NO_ERROR) {
    printf("Error starting playback: %x", errorNum);
}
fgetc(stdin);
}

That's pretty much it. I've got a function to set up an OpenAL context and device, and load a WAV file into a buffer and pass that buffer along with data from the file to alBufferData. I then try to call alSourcePlay(source) and no sound comes out. The whole function compiles and runs fine, but through debugging I found that the alGetError() function returned AL_ILLEGAL_ENUM after my call to alListener3f(AL_ORIENTATION, 0, 0, -1). Surely AL_ORIENTATION is a valid OpenAL enum? I've been following the tutorial here: http://enigma-dev.org/forums/index.php?topic=730.0

Here's my code (excuse the mess, frantic debugging...):

void initOpenAL() {

ALenum errorNum = alGetError();
// initialise OpenAL
ALCdevice* device = alcOpenDevice(NULL);
ALCcontext* context = alcCreateContext(device, NULL);
alcMakeContextCurrent(context);

errorNum = alGetError();

alListener3f(AL_POSITION, 0, 0, 0);

errorNum = alGetError();

alListener3f(AL_VELOCITY, 0, 0, 0);

errorNum = alGetError();

alListener3f(AL_ORIENTATION, 0, 0, -1);

errorNum = alGetError(); // this is where alGetError == AL_INVALID_ENUM

alGenSources(1, &source);

errorNum = alGetError();

alSourcef(source, AL_PITCH, 1);
alSourcef(source, AL_GAIN, 2);
alSource3f(source, AL_POSITION, 0, 0, 0);
alSource3f(source, AL_VELOCITY, 0, 0, 0);
alSourcei(source, AL_LOOPING, AL_FALSE);

errorNum = alGetError();

ALuint buffer;
alGenBuffers(1, &buffer);
alSourcei(source, AL_BUFFER, buffer);

errorNum = alGetError();

FILE* file;
file = fopen("../../arcade.wav", "r");
if (file == NULL) {
    printf("File access error\n");
    exit(1);
}

char xbuffer[5];
if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "RIFF") != 0)
    throw "Not a WAV file";

file_read_int32_le(xbuffer, file);
if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "WAVE") != 0)
    throw "Not a WAV file!";

if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "fmt ") != 0)
    throw "Invalid WAV file!";

printf("char size: %i", sizeof(char));

errorNum = alGetError();

file_read_int32_le(xbuffer, file);
short audioFormat = file_read_int16_le(xbuffer, file);
short channels = file_read_int16_le(xbuffer, file);
int sampleRate = file_read_int32_le(xbuffer, file);
int byteRate = file_read_int32_le(xbuffer, file);
file_read_int16_le(xbuffer, file);
short bitsPerSample = file_read_int16_le(xbuffer, file);

if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "data") != 0)
    throw "Invalid WAV file";

int dataChunkSize = file_read_int32_le(xbuffer, file)+4;
unsigned char* bufferData = file_allocate_and_read_bytes(file, (size_t)dataChunkSize);

printf("Error before alBufferData: %x\n", alGetError());

float duration = float(dataChunkSize)/byteRate;
alBufferData(buffer, GetFormatFromInfo(channels, bitsPerSample), bufferData, dataChunkSize, sampleRate);
free(bufferData);
fclose(file);

errorNum = alGetError();

printf("Error after alBufferData, before alSourcePlay: %x\n");

alSourcePlay(source);
errorNum = alGetError();
if (errorNum != AL_NO_ERROR) {
    printf("Error starting playback: %x", errorNum);
}
fgetc(stdin);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

給妳壹絲溫柔 2024-12-03 21:11:31

我不久前找到了这个问题的答案,我只记得在这里回答它:AL_ORIENTATION枚举用于alListenerfv方法,而不是alListener3f。它需要一个向量而不是 3 个整数。我很惊讶花了多长时间才发现这一点!

I found the answer to this a while ago and I've only just remembered to answer it on here: the AL_ORIENTATION enum is used in the alListenerfv method, not alListener3f. And it takes a vector instead of 3 ints. I'm amazed how long it took to find that out!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文