【发布时间】:2013-12-24 10:54:01
【问题描述】:
我在 OpenAL 中加载 WAV 文件时遇到问题。我有一个打开文件数据并将其转换为标头结构的函数。因为我知道数据的对齐方式,所以我只是将数据指针转换为我的数据头对齐结构。
问题是我无法弄清楚为什么它会抛出 40963。如果文件中的标头数据正确,那么我一定是 alBufferData 有问题。直到现在我还没有使用过 OpenAL,所以我可能会做一些明显错误的事情。
这是我的代码:
WAV_HEADER
#pragma pack(1)
typedef struct
{
uint32_t Chunk_ID;
uint32_t ChunkSize;
uint32_t Format;
uint32_t SubChunk1ID;
uint32_t SubChunk1Size;
uint16_t AudioFormat;
uint16_t NumberOfChanels;
uint32_t SampleRate;
uint32_t ByteRate;
uint16_t BlockAlignment;
uint16_t BitsPerSecond;
uint32_t SubChunk2ID;
uint32_t SubChunk2Size;
//Everything else is data. We note it's offset
char data[];
} WAV_HEADER;
#pragma pack()
WAV文件加载器加载器
WAV_HEADER* loadWav(const char* filePath)
{
long size;
WAV_HEADER* header;
void* buffer;
FILE* file = fopen(filePath, "r");
assert(file);
fseek (file , 0 , SEEK_END);
size = ftell (file);
rewind (file);
buffer = malloc(sizeof(char) * size);
fread(buffer, 1, size, file);
header = (WAV_HEADER*)buffer;
//Assert that data is in correct memory location
assert((header->data - (char*)header) == sizeof(WAV_HEADER));
//Extra assert to make sure that the size of our header is actually 44 bytes
//as in the specification https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
assert((header->data - (char*)header) == 44);
fclose(file);
return header;
}
以及负责其余设置的函数:
void AudioController::OpenFile(const char* filePath)
{
WAV_HEADER* data = loadWav(filePath);
ALuint buffer;
alGenBuffers(1, &buffer);
alBufferData(buffer, data->Format, data, data->SubChunk2Size, data->ByteRate);
ALint error;
if ((error = alGetError()) != ALC_NO_ERROR)
{
printf("OpenAL OPEN FILE ERROR: %d \n", error);
}
//Delete it for now to avoid leaks. Whether I need to delete the data here
//I'll figure out later
delete data;
}
我是否在函数中传递了错误的内容,或者我是否错误地设置了标头?
非常感谢任何帮助!
PS。这可能很重要,这里是设置 OpenAL 环境的构造函数:
AudioController::AudioController()
{
//Open preffered audio device
mDevice = alcOpenDevice(0);
//Ensure that there is a device. If not something went wrong
assert(mDevice);
mContext = alcCreateContext(mDevice, 0);
alcMakeContextCurrent(mContext);
alcProcessContext(mContext);
ALint error;
if ((error = alGetError()) != ALC_NO_ERROR)
{
printf("OpenAL CONTEXT CREATION: %d \n", error);
}
}
【问题讨论】:
-
WAV 文件中的音频是什么类型的?什么编解码器?