Game Development Reference
In-Depth Information
Loading the WAV Format with WaveResourceLoader
WAV files are what old-school game developers call a chunky file structure. Each
chunk is preceded by a unique identifier, which you
ll use to parse the data in
each chunk. The chunks can also be hierarchical; that is, a chunk can exist within
another chunk. Take a quick look at the code below, and you
'
'
'
m talking
about. The first identifier, RIFF , is a clue that the file has an IFF, or Indexed File
Format, basically the same thing as saying a chunky format. If the next identifier in
the file is WAVE , you can be sure the file is a WAV audio file.
You
ll see what I
ll notice the identifier is always four bytes and is immediately followed by a 4-
byte integer that stores the length of the chunk. Chunky file formats allow parsing
code to ignore chunks they don
'
'
t understand, which is a great way to create extensi-
ble file formats. As you
'
ll see next, we
'
re only looking for two chunks from our WAV
file, but that doesn
'
t mean that other chunks aren
'
t there:
bool WaveResourceLoader::ParseWave(char *wavStream, size_t bufferLength,
shared_ptr<ResHandle> handle)
{
shared_ptr<SoundResourceExtraData> extra =
static_pointer_cast<SoundResourceExtraData>(handle->GetExtra());
DWORD
file = 0;
DWORD
fileEnd = 0;
DWORD
length = 0;
DWORD
type = 0;
DWORD
pos = 0;
// mmioFOURCC
converts four chars into a 4 byte integer code.
// The first 4 bytes of a valid .wav file is
'
R
'
,
'
I
'
,
'
F
'
,
'
F
'
type = *((DWORD *)(wavStream+pos));
pos+=sizeof(DWORD);
if(type != mmioFOURCC(
'
R
'
,
'
I
'
,
'
F
'
,
'
F
'
))
return false;
length = *((DWORD *)(wavStream+pos));
pos+=sizeof(DWORD);
type = *((DWORD *)(wavStream+pos));
pos+=sizeof(DWORD);
//
for a legal .wav file
if(type != mmioFOURCC(
'
W
'
,
'
A
'
,
'
V
'
,
'
E
'
'
W
'
,
'
A
'
,
'
V
'
,
'
E
'
))
return false;
//not a WAV
// Find the end of the file
fileEnd = length - 4;
memset(&extra->m_WavFormatEx, 0, sizeof(WAVEFORMATEX));
 
Search WWH ::




Custom Search