'What's wrong with this? I tried to load a .wav file into XAudio2 and I'm getting errors, but it displays the bytes

I'm very new to this stuff. I've been trying to get a .wav file to play, specifically PCM S16LE. I probably did something terribly wrong.

fopen_s(&file, filename, "r");

// riff chunk
fread_s(&buffer, 4, riff_chunk.ChunkId, 4, file);
fread_s(&buffer, 4, riff_chunk.ChunkSize, 4, file);
fread_s(&buffer, 4, riff_chunk.Format, 4, file);
if (riff_chunk.ChunkId != 0x46464952)
{
    cout << "success: " << riff_chunk.ChunkId << endl;
}

//fmt chunk
fread_s(&buffer, 4, fmt_chunk.SubChunk1Id, 4, file);
fread_s(&buffer, 4, fmt_chunk.SubChunk1Size, 4, file);
fread_s(&buffer, 2, fmt_chunk.Audio_Format, 2, file);
fread_s(&buffer, 2, fmt_chunk.Channels, 2, file);
fread_s(&buffer, 4, fmt_chunk.SampleRate, 4, file);
fread_s(&buffer, 4, fmt_chunk.ByteRate, 4, file);
fread_s(&buffer, 2, fmt_chunk.BlockAlign, 2, file);
fread_s(&buffer, 2, fmt_chunk.BitsPerSample, 2, file);
if (fmt_chunk.SubChunk1Id != 0x20746D66)
{
    cout << "success: " << fmt_chunk.SubChunk1Id << endl;
}

// data chunk
fread_s(&buffer, 4, data_chunk.SubChunk2Id, 4, file);
fread_s(&buffer, 4, data_chunk.SubChunk2Size, 4, file);
fread_s(&buffer, 4, data_chunk.data, 4, file);
if (data_chunk.SubChunk2Id = 0x61746164)
{
    cout << "success: " << data_chunk.SubChunk2Id << endl;
}

buffer.pAudioData = reinterpret_cast<BYTE*>(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, data_chunk.data));
if (buffer.pAudioData == nullptr)
{
    cout << "failed: " << buffer.pAudioData << endl;
}

buffer.AudioBytes = data_chunk.data;
buffer.Flags = XAUDIO2_END_OF_STREAM;

return hr;

I tried loading a .wav file but with no luck.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source