我在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);
}
}
答案 0 :(得分:2)
您传递给format
的{{1}}参数错误。您的值始终为“WAVE”,但该函数需要OpenAL格式,例如alBufferData
,AL_FORMAT_MONO16
,...
您应该根据数据(AL_FORMAT_STEREO16
和data->NumberOfChanels
在您的结构中构建正确的格式。
请注意标题结构中的两个错误: