DirectSound:
Stream To a File
By: Jack Hoxley
Written: June 2000
Download:
DS_StreamToFile.zip
(14kb)
Streaming to files isn't a very
commonly used feature; but it can be very useful when you need it. What if you
are intending to record a 30 minute speech through your computer? There is no
way that your sound card will have enough memory to hold that much data; system
memory will be filled up as well - this only leaves the hard drive. Normally
when you record or play large sound files that don't fit into memory DirectX
and Windows will sort out what to do with it; this isn't necessarily a good
thing, whilst it is busy sorting things out for you it is slowing your program
down and leaving you with very little control as to what is happening.
If you write code that will stream
the data to the hard drive you can control exactly what happens; and you can
play with your own optimisations to make it work faster and you can make it
work better with your program - by not slowing it down so much. This is a great
ability to use, but as with all things good with DirectX there is a hidden side
- you have to control everything; what goes wrong is your problem. If, however,
you decided to let windows sort it out for you you could turn a blind eye on
proceedings and 99% of the time expect the final outcome to be what you want.
Streaming to a file in this example
requires that the user has free hard drive space; all it does is create a temporary
file of data, then when the recording is finished it formats it as a wave file
and saves it. You must keep an eye on how much hard drive space there is. It
would he advisable to stop recording when 45-49% of the free space has been
used up. Why this small? At the point of conversion there may well be two copies
of the data (the temporary file). For example, we have 100mb free space; we
record 48mb of sound (52Mb left), we then copy this data to a more permanent
file (4Mb left), then we delete the temporary file (52Mb left). But, if we didn't
stop at 48 and carried on to 76mb of sound data, when it comes to converting
the data we'll get an error part of the way through. understand?
On last thing to note - it would
defeat the point of this exercise if we then tried to create a normal buffer
to play it back; so it is advisable to use a Stream-From-File method to play
your newly recorded sound.
You can get a copy of the example
project from the top of this page, or from the downloads
page.
|