Game Development Reference
In-Depth Information
{
extra->m_LengthMilli = ( handle->Size() * 1000 ) /
extra->GetFormat()->nAvgBytesPerSec;
return true;
}
// Increment the pointer past the block we just read,
// and make sure the pointer is word aligned.
if (length & 1)
{
++pos;
++file;
}
}
// If we get to here, the .wav file didn
'
t contain all the right pieces.
return false;
}
The
ParseWave()
method has two parts. The first part initializes local and output
variables and makes sure the WAV file has the right beginning tag,
RIFF
, signifying
that the file is the IFF type, and the identifier immediately following is
WAVE
. If either
of these two checks fails, the method returns
false
.
Thecodeflowsintoa
while
loop that is looking for two blocks:
fmt
and
data
.They
can arrive in any order, and there may be other chunks interspersed. That
'
s fine, because
we
ll just ignore them and continue looking for the two we care about. Once they are
found, we return with success. If for some reason we get to the end of the file and we
didn
'
'
tfindthetwochunkswewerelookingfor,wereturn
false
, indicating a failure.
Loading the OGG Format
The
ParseOgg()
method decompresses an OGG stream already in memory. The
OggVorbis_File
object can load from a normal file or a memory buffer. Loading
from a memory buffer is a little trickier since you have to
“
fake
”
the operations of an
ANSI
FILE *
object with your own code.
This first task is to create a structure that will keep track of the memory buffer, the
size of this buffer, and where the
“
read
”
position is:
struct OggMemoryFile
{
unsigned char* dataPtr; // Pointer to the data in memory
size_t
dataSize;
// Size of the data
size_t
dataRead;
// Bytes read so far