DirectSound:
Stream From a File
By: Jack Hoxley
Written: June 2000
Download:
Ds_StreamFromFile.zip (16kb)
This is probably the hardest
way of using sounds in DirectX. It requires you to know how a wave files data
is stored; then reading it into a blank buffer. It is quite easy once you have
the code, as it's self adapting.
Streaming from a file basically
means that it won't store the entire file in memory, which is what happens when
you use CreateBufferFromFile. Streaming basically creates a half-a-second buffer
with only that much data in it, as the sound is played it updates this buffer
by adding new information to it. Because of this you only need a buffer the
size of a 500ms sound file - tiny. This becomes extremely useful when you want
to play a large file, for example, a speech. If you used the easy (and traditional)
method you would need 10-20Mb of space to hold that sound in memory, which will
easily bring anything but the fastest PC's to their knees. Not a good idea when
performance is critical. If, however, you used a streaming method you could
have hundreds of large files taking up less memory than 2 small sounds - as
each streaming buffer only needs 500ms (1/2 a second) of memory.
DirectSound has no native, and
easy, method of streaming sound into it's buffers; you have to write the code
for yourself. This flowchart explains (simply) how it works:
Open up the Wave
file
|
Look through the header and gather information on the file
|
Create a blank buffer with the settings stored in the header
|
The blank buffer is created to be 500ms Long
|
We then use arrays to load the wave data straight from the file
|
We then copy this data straight to the blank buffer
|
The Sound starts playing
|
We use DirectXEvents to keep track of where it is *
|
As it gets to the end we load some more data into the buffer *
|
This small loop(*) keeps going until the sound has finished.
You can download
a copy of this program from the top of the page, or from the downloads
page
|