PlayWave.exe
************

Brief description: This simple program shows how to output a sine wave through the soundcard output device using low-level Windows Multimedia API functions.

*************************************************************

An introduction to digital sound
**************************************

We all know that sound is basically a vibration. But how is sound represented and reproduced in a computer? Digitally speaking, the soundcard converts the amplitudes of a sound wave into a stream of digital values. This is done by 'sampling' the sound at regular intervals of time. So, when we say we're recording CD quality music, it means that the sounds you hear are being captured at a fixed rate of 44.1 kHz or 44100 Hz (1 Hz = 1 cycle per second). This is called the 'sampling rate' (the easiest way to understand this is to imagine a sound wave being sliced 44100 times in one second). The highest frequency you can hear (without distortion) is a little less than half of the sampling rate (this means that at a sampling rate of 44.1 Khz, the highest tone you will hear amidst all the music is 22.05 Khz). So, the higher the sampling rate, the higher the quality of sound.

The values that represent these sounds are called 'samples'. In the digital world, these samples consist of bits. The most common resolutions are 8 bits and 16 bits. 

In this program, we are going to output a sine wave of frequency 500 Hz at a sampling rate of 44.1 Khz with a resolution of 16 bits. Now, in one second, this sine wave will have completed 500 cycles. We are going to reconstruct it digitally by streaming out 44100 slices of the same wave in one second. In Windows, there are two ways of doing this using the soundcard: the Windows Multimedia API (found in Winmm.dll) or DirectSound (part of DirectX). We will be focussing on the multimedia API in this example. 

Both the methods mentioned above have one thing in common: they both use arrays of memory that store the required digital sound samples. These arrays are called 'buffers'. When using the multimedia API, we shall go according to the following steps:

1) Determine the format in which you plan to reproduce the sound. This means you have to decide on various factors like the sampling rate (quality), the number of channels (stereo or mono), resolution (8 bit or 16 bit) etc...

2) Once you determine the format, find an appropriate output device on your soundcard that supports the format you've chosen. Open that device and keep its 'handle' for further use with the other API functions that you will employ later.

3) Then, begin to set aside memory for your sound buffers. The size of each buffer depends on how much sound you want to save AND on the sampling rate AND on the number of channels AND on the bit resolution.

4) Now, in Windows, before you can send a buffer to the soundcard, you have to initialize something known as a Wave Header (WaveHdr). This is basically a structure that contains info about your buffer. It is good to create a separate WaveHdr structure for every buffer that you've set aside. Then, you can go ahead and save the pointers to each buffer separately into its corresponding WaveHdr.

5) When you're ready to begin playing the sine wave, you must first calculate the value of each sample and save it into the currently available buffer. After each buffer gets filled, its corresponding WaveHdr is 'prepared' 

6) The buffer is then sent to the soundcard to be outputted (or written...you take your pick from these choice of words). A short piece of sound will be heard from the soundcard's speakers.

7) When a buffer is returned from the soundcard after it has been written, it is necessary to Unprepare its corresponding WaveHdr.

8) Steps 5, 6 & 7 are repeated for the next buffer. This cycle goes on until you choose to stop it or until the buffers are all used up.

9) Reset the output device once you're finished, release the buffers and then, close the device.


That's it. Simple in all its complexity, ain't it?  ;))
If you have any doubts or clarifications to be made, please don't hesitate to contact me via email. I'm just an amateur SpAsm user who happens to know a little about Audio, but i'll try my best to answer your queries.


Yours truly,
Anand Alexander John

Email addresses: 
thesower@sify.com    OR    thesower@indiatimes.com

