Larry Osterman's WebLog

Confessions of an Old Fogey
Blog - Title

Beep Beep

Beep Beep

Rate This
  • Comments 44
What's the deal with the Beep() API anyway?

It's one of the oldest Windows API, dating back to Windows 1.0.  It's also one of the few audio APIs that my team doesn't own.  The Beep API actually has its own dedicated driver (beep.sys).  The reason for this is that the Beep() API works totally differently from any other audio API in the system.

Back when IBM built the first IBM PCs, they realized that they needed to have the ability to do SOME level of audio, even if it wasn't particularly high quality.  So they built a speaker into the original PC hardware.

But how do you drive the speaker?  It turns out that the original PC hardware used an 8253 programmable interval timer to control the system hardware timer.  The 8253 was a pretty cool little chip - it would operate in 5 different modes - one shot timer, interrupt on terminal count, rate generator, square wave generator, software strobe or hardware strobe.  It also contained three independent counters - counter 0 was used by the operating system, counter 1 was reserved for the hardware.  The third counter, counter 2 was special.  The IBM hardware engineers tied the OUT2 line from the 8253 to the speaker line, and they programmed the timer to operate in square wave generation mode.

What that means is that whenever the 2nd counter of the 8253 counted to 0, it would toggle the output of the OUT2 line from the 8253.  This gave the PC a primitive way of generating very simple tones.

The original Windows Beep() API simply fiddled the controls on the 8253 to cause it to generate a square wave with the appropriate frequency, and that's what Beep.sys continues to do.  Legacy APIs can be hard to remove sometimes :)

Nowadays, the internal PC speaker is often also connected to the PCs audio solution, that allows the PC to have sound even when there are no external speakers connected to the machine.

In addition to the simple beep, some very clever people figured out how they could use the 8253 to generate honest to goodness audio, I'm not sure how they succeeded in doing it but I remember someone had a PC speaker based sound driver for DOS available at one point - it totally killed your PCs performance but it DID play something better than BEEEEEEP.

Edit: s/interrupt conroller/interval timer/

Edit2: fixed description of channel 1 (in case someone comes along later and decides to depend on my error).

  • Believe it or not, Microsoft actually released a Windows 3.1 PC speaker audio driver. I know because at the time I was too poor to afford a sound blaster for my 386-33, but I downloaded that driver from Microsoft (via a long-distance modem call to Redmond, no less) and could play actual low-quality audio through the PC speaker, both wav and MIDI.
  • At least a few motherboard manufacturers have taken advantage of the PC Speaker in similar ways; I have a Soyo board that, if it experiences a problem during POST (i.e. doesn't find a keyboard or video card/monitor) will actually tell you so, in very robotic but understandable English (or Chinese, if you flip a jumper on the board).

    It spooked my wife out the first time I turned it on with no video card in the box. ;)
  • You mean Windows 98? See http://support.microsoft.com/kb/q138857/

    A little scratchy, but works well on low-end machines - I loved that Windows 3.1 on my 16 MHz 286 could play the Windows '95 startup sound!

    (I've since put a network and sound card in and it runs RDP over a wireless bridge to XP, but that's another story...)
  • I remember using the PC Speaker driver for Win 3.1. You can still get it for Win95/98/ME:

    How to Install and Use the PC Speaker Driver with Windows
    http://support.microsoft.com/kb/q138857/

  • I actually wrote a program that could play music in seven parts using just the PC speaker. Fifteen years ago; time flies! I made it play a movement of a Bach concerto, with a duration of ten minutes. It was a pure DOS program, however, and I had to disable all interrupts when it played, since I used the stack pointer in the code...
  • Hah, this brings back some good memories. Some co workers late one evening deep in the vault of the server room. One programmer (me) upstairs know about this and knows how to make it run different tones different lengths. Imagine their puzzlement as a server starts beeping, like an alarm, then as they are looking to see which one is an alarm (I am watching them log into the terminals from terminal services) then as they get close to the server beeping I start playing Yankee Doodle Dandy.
  • I remember back in the day a few games and a actually capitalized on the sound driver feature.. From what I remember there was a game called World Class Leaderboard Golf made by Access Software (which was later acquired by Microsoft for the Links series) had something they licensed or developed called "RealSound" which had pretty decent audio (for the time) though the PC speaker

    Used to get a thrill on that old 8086 playing Leaderboard Golf (WCLB.exe) and acually hearing a metalliac "I think he hit a tree, Jim" from the PC speaker.

    Most of the audio at that time was the bleeps and bloops of monotone sound -- and if you were lucky enough to have an AdLib card, some rudimentary midi audio...

    Also recall that there was also a freeware sound driver for Windows 3.0 that someone hacked together which would allow to use the PC speaker as a wave audio device..

    Thanks for the trip down memory lane ;)
  • I remember that program, too. IIRC, it wasn't a driver, but an actual program that would let you load sound files (its own custom format, I think) and play them through the PC speaker. I wish I could remember it's name.

    I was in college at the time, and after 10 years of hearing PC be-boops I was blown away when I first heard it. But as my EE friend explained to me, why shouldn't it be hard to do? It's a speaker!

    'Course, anyone worth their salt back then had a Sound Blaster anyway. ;-)
  • That's all very interesting. I'm sitting next to a girl using the MySQL command line interface, which happens to call this API every time she mistypes a query.

    So my question is, does anyone know a good way to destroy beep.sys and/or physically disable the PC speaker?
  • http://support.microsoft.com/kb/q138857/

    Windows 95, 98, and ME support!
  • I use a program right now that uses the Beep() function. It takes several remote desktop windows and hides them away, and then exposes them in response to certain key sequences. This is intended to emulate a KVM switch in software.

    To switch to a different desktop requires you to hit a trigger key twice and then hit a number from the numeric row on the keyboard. When you hit the trigger key it plays a low-pitch acknowledgement, and then in response to a recognised command it plays a high-pitch acknlowledgement. It sounds a bit like "bloop blooop, bleeeep!", and uses the Beep() API. It maybe old, but it's handy!

    Beep(500, 50);
    Beep(500, 50);
    Beep(2000, 50);

    I remember a sound driver for Win9x which exposed the internal speaker as a wave device. That was a neat hack. I don't know of any similar neat hacks on NT, though.
  • Incidentally I wrote one of them (actually mine was a DOS program to listen wav files).
    The way it worked was actually simple (well depends on your concept of simple).

    If I remember well it hooked the IRQ 0 (the timer) which was pumped to very high frequency (the trick was to daisy chain to the old handler at about its expected frequency). That handler simply downsampled the wav to 1 bit (I used an algorithm I found which was used to downsample bitmaps). Then it drove the speaker as if it were a switch driving it to its two extremes (it was simply an out on some arcane I/O port...). The sound was not that great, but the inertia of the speaker's membrane was helping to reduce the artifacts of downsampling anyway.

    Incidentally I used Summer of 69 of Bryan Adams to test because it was the only wave I had at the time (a friend ripped it for me, I had not a CD drive at the time). Now I've written audio code many times and I always used that song for testing :)
  • I have some classic Infosys games that actually played music over the PC speaker...

    StickyKeys' notification sound comes over the PC speaker on my machine. Does it use Beep? It's particularly annoying since I work in a cubicle environment and most sound from my computer goes through my headphones.
  • Just a minor correction, I think you meant to say "Programmable Interval Timer", not "Programmable Interrupt Controller"
  • Maurits,
    Funny you should mention that :)....

    StickyKeys is Monday's post :)
Page 1 of 3 (44 items) 123