BLASTER.TXT

                   PLAYING DIGITIZED SOUNDS ON THE PC

All I wanted was to add digitized sounds to my Sokoban game. The game
runs under DOS.

About 10 years ago I wrote TREK-N, which played digitized sounds thru the
PC's 1-bit beeper speaker, but now I wanted something a little better.
Virtually every computer nowadays has a Sound Blaster (SB) compatible
sound system, so that was the obvious choice.

The SB has several capabilities besides playing digitized sounds (such as
an FM synthesizer and playing MIDI music), but here we're only concerned
about digitized sound.

Since Sokoban is not a Windows application, it requires some effort to
get the Sound Blaster working.


CT-VOICE IN "21 DAYS"

The standard, approved method for running the SB is to use the
CT-VOICE.DVR from Creative Labs, the folks who designed it.

That's the way the game programs in the book "Teach Yourself Game
Programming in 21 Days" does it. Unfortunately, it didn't work at all!
None of the sounds in any of the games on any computers I tried, with and
without Windows, worked. I wasn't the first to discover this. There were
many complaints about "21 Days" on Usenet. The best suggestion was to get
updated drivers.

Now it seems to me that it's the hardware that is supposed to be upward
compatible, and it shouldn't be necessary to get updated drivers. I tried
it anyway, and of course even that didn't work.

Another annoying problem with the drivers is that they are proprietary.
You don't get the source code, and you don't get to see how they work (or
don't).


SFXMIX IN "MORE TRICKS"

A much better book is "More Tricks of the Game Programming Gurus",
copyright 1995 by Sams. Chapter 11 presents a program called SFXMIX.C
that actually works. It worked on two computers running Windows 98 and on
one of them in pure DOS mode. It also provided all the source code.

So I translated it to XPL. The translation was fairly loose; I did not
use an interrupt handler, necessary for playing sound samples longer than
64K (and possibly 16K).


HOW IT WORKS

Playing digitized sound requires a digital-to-analog converter (D/A), an
amplifier, and a speaker.

The SB has what is called a Digital Sound Processor (DSP -- not to be
confused with Digital Signal Processor). It provides the interface
between the PC and the sound hardware. The DSP also provides an interface
to the PC's Direct Memory Access (DMA).

DMA is important for playing sounds since it allows the PC's CPU to
continue running while sounds are being played. (An interrupt handler
could do the same thing but DMA is more efficient--especially for the
early, slow PCs--and it has evolved as the standard method.)


RESULTS

At first my program didn't work on one computer (the laptop) when running
Windows. Some experimenting showed a delay was necessary (see the code).
Also it was determined that, contrary to documentation, a ready signal
was not always sent back from the Sound Blaster's DSP to the PC.

No sound program ever ran on my Packard Bell Pentium in pure DOS mode.
Windows had to be running. Also I never got any sounds (except the
boot-up beep) to work on my Duron/Biostar. Apparently manufacturers skirt
patent rights, and perhaps save a buck, by using alternative hardware
that requires a driver to configure it to look like a genuine Sound
Blaster.

The sound code almost doubles the size of the Sokoban game.


OTHER USEFUL SOURCES OF INFO

"The Developer Kit for Sound Blaster Series", 1991, Creative Labs. This
is the official information. (Newer documentation is available, but I
don't have it.) Most of the book is devoted to explaining how to call
their driver code, but the appendices in the back explain briefly how to
program the registers directly. It's gospel, but not very well explained.
(It's printed in Singapore.)

"The Undocumented PC" by Frank van Gilluwe, 1997 Addison Wesley. This
book is essential if you want to know how the PC really works. It gives a
good (but not great) explanation of how the DMA registers work. (Creative
Labs might leave you with the impression that the DMA controller is part
of their product, but it's part of every PC.)

"Advanced MS-DOS Programming" Second Edition, 1988 Ray Duncan, Microsoft
Press. Chapter 12 discusses how to access the "environment block", a
collection of ASCIIZ strings that describe among other things the
configuration of the Sound Blaster. For instance, in your AUTOEXEC.BAT
file you might have line like this:

  SET BLASTER=A220 I5 D1

Where:
  A = Base Address of I/O ports (almost always set to 220 hex)
  I = Interrupt request (IRQ) line (usually set to IRQ 7 or IRQ 5)
  D = Direct Memory Access (DMA) channel (almost always set to 1.)


"WAVPLAY.PAS", on the Dutch Programmer's Webpage, provided another
(overly simplified) example of playing digitized sounds on the Blaster.
Since Pascal is similar to XPL, it confirmed my shaky understanding of
the C program, SFXMIX.
