Download the WaveStream Sample application and class (47kb)
Before you Begin
This project requires the SSubTmr.DLL component. Make sure you have loaded and registered this before trying the project.
Playing digital audio can be achieved at a number of different levels under windows. At
the simplest level, there is the Multimedia Control Interface (MCI)
control provided with VB. This is turn is a very thin wrapper around the MCI API provided
with Win32, which offers about the same level of control for playing waves as the
APIPlaySound and sndPlaySound functions.
At a deeper level, your application can take charge of reading chunks of audio from disk
and streaming it directly to the sound card. The advantage of these methods is you
get more control over how the audio is played. With the MCI and PlaySound methods, the
system has to allocate memory for the entire sound before it starts. With a streaming
approach, you can read the sound in in small chunks (say 0.1s of audio) allowing you to
play audio samples of any length (and remember that a CD quality stereo WAV file
occupies around 10Mb/minute!). Another advantage of using the multimedia methods is
you can affect the sound before you play it. You could filter it by averaging the bytes
before putting them into the sound card's buffer. Or you could create an echo effect by
preparing an additional audio buffer which you save attenuated samples of the main sound
Using the multimedia API at this level gives you more control, but the downside is
additional complexity. This sample demonstrates how you can play back standard .WAV files,
however, if you need to be able to play back sounds which have are not directly compatible
with your soundcard (for example, .WAV files with ADPCM compression or .MP3 files) then
this sample alone will not work because it would need to invoke the Audio
Compression Manager (ACM) functions to do that (which is the subject of a
future article, if I can figure it out!).
The cWavPlayer class
The cWavPlayer class supplied with the download wraps up access to stream digital audio
to the sound card. Fundamentally, it works like this:
To use the class is straightforward. Add the class to your project, and ensure the
project references SSubTmr.DLL. Then
open a .WAV file with the OpenFile method. At this stage you can read
various information about the wave file, such as the sampling frequency (SamplesPerSecond),
the bit depth (BitsPerSample), whether the file is stereo or mono (Channels).
Opens a Multimedia IO device handle, reads the header of the WAV file
to check the format of the WAV file and then creates a number of buffers to hold
0.1s each of the wave data read in from the file.
Creates a WaveOut handle for passing data to the soundcard from the WAV file and
specifies that when data is needed by the soundcard it should post a message to
the window. It then prepares each of the buffers to send data to the wave out
handle and initiates the play callback loop by posting the message to the window.
- Play callback loop
Reads data from the file into the wave buffer pointed to by the wave header,
and then writes it out to the sound card's driver. When the data is exhausted,
the buffer is unprepared and finally once all the buffers have been unprepared
the WaveOut handle is closed.
To play the file, use the Play method. You can pause it at any time by choosing the Stop
method, and change the position in the file using the FileSeek method.
You can get the position of the playback at any time using the Position method.
Remember that the audio is buffered into 0.1s chunks for playback, so that is the minimum
resolution. To get a better resolution, you can reduce the buffer time in the cWavPlay
class by changing the BUFFER_SECONDS constant value, but this
can lead to problems on slower machines.
Back to top
Back to vbMedia
Back to Source Code Overview