我正在尝试使用OpenAL播放WAVE文件。我将文件内容加载到动态分配的const unsigned char*
数组中。但是当我尝试使用alBufferData()
上传所述数组时,出现AL_INVALID_VALUE
错误。
alBufferData(
this->buffer_id_, //id :218892368
this->format_, //format :AL_FORMAT_STEREO16
this->data_buffer_ptr_, //data :0x0d756040""
this->data_buffer_size_,//data size :17992800
this->frequency_ //frequency :44100
);
CheckForALError(); //Prints error: AL_INVALID_VALUE
根据文档,当“ size参数对于指定的格式无效,正在使用缓冲区或数据为NULL指针。”时,会发生此错误。我已经确认大小符合预期(大约17.1 MB),并控制了指针不是NULL。但是,指针似乎确实指向持有一堆\0
:s的位置,这有点奇怪。我期望会有更多不同的值,因为我相信自己已经在其中加载了.wav
文件。
我的问题是我不知道问题出在我如何处理OpenAL调用还是在我的WAVE文件中。
下面是我在OpenAL中定义Source和Buffer对象并加载WAVE文件的两个相当大的代码块。
//------------------------------------------------------//
//---------------------SOURCE SETUP---------------------//
//------------------------------------------------------//
//Generate one (1) source for this id
alGenSources((ALuint)1, &this->source_id_);
//Set position
alSource3f(this->source_id_, AL_POSITION, this->pos_.x, this->pos_.y, this->pos_.z);
CheckForALError();
//SetVelocity
alSource3f(this->source_id_, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
CheckForALError();
//Set pitch
alSourcef(this->source_id_, AL_PITCH, 1);
CheckForALError();
//Set gain
alSourcef(this->source_id_, AL_GAIN, 1);
CheckForALError();
//Set if this audio source should loop
alSourcei(this->source_id_, AL_LOOPING, this->loop_);
CheckForALError();
//------------------------------------------------------//
//---------------------BUFFER SETUP---------------------//
//------------------------------------------------------//
//Generate one (1) buffer
alGenBuffers((ALuint)1, &this->buffer_id_);
CheckForALError();
//Load the WAVE file
this->LoadWAVEFile(wav_path);
//Link source and buffer
alSourcei(this->source_id_, AL_BUFFER, this->buffer_id_);
CheckForALError();
在这里加载WAVE文件(LoadWAVEFile()
函数的内容):
FILE* file_ptr = nullptr;
errno_t error;
error = fopen_s(&file_ptr, wav_path, "rb");
//Variables for storing file information
char file_type[4];
DWORD file_size;
DWORD chunk_size;
short format_type;
short channels;
DWORD sample_rate;
DWORD avg_bytes_per_sec;
short bytes_per_sample;
short bits_per_sample;
DWORD data_size;
//Confirm that the file is of the right format
//RIFF : 12 Bytes
//fmt : 24 Bytes
//data : 8 + <data size>
//------------------------------------------------------//
//----------------------'RIFF' PART---------------------//
//------------------------------------------------------//
//Check for RIFF
fread(file_type, sizeof(char), 4, file_ptr); //sizeof(char)*4 = 4 bytes
//Check for WAV
fread(&file_size, sizeof(DWORD), 1, file_ptr); //sizeof(DWORD)*1 = 4 bytes
fread(file_type, sizeof(char), 4, file_ptr); //sizeof(char) * 4 = 4 bytes
//Total: 12 bytes
//------------------------------------------------------//
//---------------------'fmt ' PART----------------------//
//------------------------------------------------------//
//Check for fmt
fread(file_type, sizeof(char), 4, file_ptr); //sizeof(char)*4 = 4 bytes
//Fill some of the file variables
fread(&chunk_size, sizeof(DWORD), 1, file_ptr); //sizeof(DWORD)*1 = 4 bytes
fread(&format_type, sizeof(short), 1, file_ptr); //sizeof(short)*1 = 2 bytes
fread(&channels, sizeof(short), 1, file_ptr); //sizeof(short)*1 = 2 bytes
fread(&sample_rate, sizeof(DWORD), 1, file_ptr); //sizeof(DWORD)*1 = 4 bytes
fread(&avg_bytes_per_sec, sizeof(DWORD), 1, file_ptr); //sizeof(DWORD)*1 = 4 bytes
fread(&bytes_per_sample, sizeof(short), 1, file_ptr); //sizeof(short)*1 = 2 bytes
fread(&bits_per_sample, sizeof(short), 1, file_ptr); //sizeof(short)*1 = 2 bytes
//Total: 24 bytes
//------------------------------------------------------//
//---------------------'data' PART----------------------//
//------------------------------------------------------//
//Check that we have reached the sound data
fread(file_type, sizeof(char), 4, file_ptr); //sizeof(char)*4 = 4 bytes
//Check data size
fread(&data_size, sizeof(DWORD), 1, file_ptr); //sizeof(DWORD)*1 = 4 bytes
//Allocate memory and read sound data
this->data_buffer_ptr_ = new unsigned char[data_size];
size_t read_values = fread(this->data_buffer_ptr_, sizeof(BYTE), data_size, file_ptr);
//sizeof(BYTE)*data_size = <data size>
//Total: 8 + <data size>
//------------------------------------------------------//
//-------------------OTHER STUFF PART-------------------//
//------------------------------------------------------//
//Set frequency and data buffer size
this->frequency_ = sample_rate;
this->data_buffer_size_ = data_size;
//Identify format (NTS: The if-cases are sub-optimal, yes, but they give me better oversight)
if (bits_per_sample == 8 && channels == 1) { this->format_ = AL_FORMAT_MONO8; }
else if (bits_per_sample == 8 && channels == 2) { this->format_ = AL_FORMAT_STEREO8; }
else if (bits_per_sample == 16 && channels == 1) { this->format_ = AL_FORMAT_MONO16; }
else if (bits_per_sample == 16 && channels == 2) { this->format_ = AL_FORMAT_STEREO16; }
//Close file stream
fclose(file_ptr);