就是这样。我有一个功能来设置OpenAL上下文和设备,并将WAV文件加载到缓冲区中,然后将该缓冲区与数据一起从文件传递到alBufferData。然后,我尝试调用alSourcePlay(source),但没有声音发出。整个函数可以编译并运行良好,但是通过调试,我发现在调用alListener3f(AL_ORIENTATION,0,0,-1)之后,alGetError()函数返回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);
}
最佳答案
我前不久找到了答案,我只记得在这里回答:AL_ORIENTATION
枚举用于alListenerfv
方法,而不是alListener3f
。它需要一个 vector 而不是3个整数。我惊讶地发现了多长时间!