.wav files in Pascal

Is it possible to use .wav files in Pascal programming?  If so, how do you access them?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Yes, it is possible ...
You load them in like any other file and use a procedure to
play the sound thourg your soundcard ...
So you have to know how to program your soundcard and how the .wav files are structured . AS you might understand it is to much to explain here so I will send you an example file later this day.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Krueger092397Author Commented:
Okay, thanks a lot.  If you can send me some kind of model, that would also be a good thing.  
First of all sorry for the long delay...
Beneath I've included the format of .wav files for stereo/mono 8-bits samples.
I didn't have the format definitions for 16-bit waves but I guess it should
be quite the same as the format below ...
After this definition some words on how to program the stuff
The rData chunk in a WAV file is split up into several further chunks:

 ³ rData                                                                   ³
 ³ Byte              Length                                                ³
 ³ Offset   Name   (in bytes)    Description                               ³
 ³ 00h      wID        4h        Contains the characters "WAVE"            ³
 ³ 04h      Format    14h        Contains data which specifies the format  ³
 ³          Chunk                  of the Data in the Data Chunk           ³
 ³ 18h      WAVE Data  ?         Contains the WAV audio data               ³
 ³          Chunk                                                          ³

³ The Format Chunk ³

The Format Chunk is split up into these fields:

³ Format                                                                  ³
³ Chunk                  Length                                           ³
³ Offset  Name         (in bytes)   Description                           ³
³ 00h     fId               4       Contains the characters "fmt"         ³
³ 04h     fLen              4       Length of data in the format chunk    ³
³ 08h     wFormatTag        2       *                                     ³
³ 0Ah     nChannels         2       Number of channels, 1=mono, 2=stereo  ³
³ 0Ch     nSamplesPerSec    2       Playback frequency                    ³
³ 0Eh     nAvgBytesPerSec   2       **                                    ³
³ 10h     nBlockAlign       2       ***                                   ³
³ 12h     FormatSpecific    2       Format specific data area             ³

* The wFormatTag specifies the wave format, eg 1 = Pulse Code Modulation
  (or in plain english, regular 8 bit sampled uncompressed sound)

** Indicates the average number of bytes a second the data should be
   transferred at = nChannels * nSamplesPerSec * (nBitsPerSample / 8)

*** Indicates the block alignment of the data in the data chunk. Software
    needs to process a multiplt of nBlockAlign at a time.
    nBlockAlign = nChannels * (nBitsPerSample / 8)

³ The Data Chunk ³

The Data Chunk is split up into these fields:

³  Data                                                                   ³
³ Chunk                  Length                                           ³
³ Offset  Name         (in bytes)   Description                           ³
³ 00h     dId              4        Contains the characters "data"        ³
³ 02h     dLen             4        Length of data in the dData field     ³
³ 00h     dData            dLen     The actual waveform data              ³

In mono 8-bit files each byte represents one sample. In stereo 8-bit files
two bytes are stored for each sample, the first byte is the left channel
value, the next is the right channel value.

So far the description, it is quite self explanary.
The way you should program depends on your own wishes, you have the possibility
of polling and DMA-playback.
If you use the polling method you should set up the timer-rate equal to the
sample frequency and hook an routine on the interrupt that reads out the buffer
sends the read data to your soundcard and after that increases your buffer offset.
So the routine should look like ...

   procedure Hooktimer;interrupt;
      Read (BufferOffset) {1 byte for mono / 2 bytes for stereo}

      Write (2SoundCard)

      BufferOffset + 1    {or 2 for stereo}


A fairly simple method, but it costs a lot of CPU overhead. I prefer the DMA method
which is set up as a double-buffer (if your samples >64K)
This method works as follow :

   1. Setup a DMA buffer of max 64 K (x Kb)
   2. Place DMA in Auto initialise mode
   3. Fill the total buffer with the first y Kb of the sample
   4. Tell the soundcard to play through DMA where the total size of
      the sample data to be played will be a half of the allocated size of the DMA buffer
   5. The soundcard will generate an irq when it has played the first half of the buffer
   6. A procedure should manage this irq by doing the followin:
         1. Tell the soundcard to play the second half through DMA
         2. Fill the first half of the buffer with new sample data that comes
            after the first y Kb you have placed in the beginning
   7. At the end of the 2nd half of the buffer the soundcard will again generat
      an irq. This one can be handled the same way as mentioned in 6. except that
      you now tell the soundcard to start at the beginning of the buffer and that
      you fill the 2nd half of the buffer.

If you use the above alghorithm you will be able to play samples of infinite
length which only uses 2 interrupts and a fillmem per 64Kb of sample data. As
you can see this is a quite an improvement compared to the polling method.

The only question for you still maybe how to program the soundcard you possess.
If you just search with a engine you can find some description on how to program
your soundcard.

I hope this basic alghorithms + the .wav format are enough to play your own wav
files in Pascal.

There is only one but... Those routines are primarly ment to use in a DOS
environment, if your using pascal under windows you shouldn't even try it
this way, your computer will surely hang. I am not a windows programmer,
but as far I know you can use the standard multi-media procedures provided by
pascal for windows. I guess that you can set up a buffer where you put your
data and tells the mm-function at which playback rate it should go through
that buffer and what the handle is of the mm-device. something like that ...
I should have made it in such way ! But who am I ... I am not Borland or

Hmm, it is not a very nice lay-out ... if you want it more clearer you could always leave your email here
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.