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).

  • Norman, that trick didn't work with the IBM PC, it was too well shielded.

    The TRS-80, on the other hand.....

    I had a friend in college who had a TRS-80 and he showed how he could make music on his stereo with it.
  • On my first day of work at NCR (1980), I walked up to the console of the mainframe and, since all the terminals in college made a beep when you pressed control-g, I pressed control-g.

    Unfortunately, on this particular NCR mainframe, it invoked the debugger and stopped the machine.

    Which several people were actually USING at the time.
  • Like a lot of other features in the IBM PC, the speaker requirement probably was to be better then the Apple ][.

    On the Apple ][ there was no timer chip, just a bit that the software could toggle. And I remember a game called Karateca, playing music while horizontally scrolling a "high res" background...

    Daniel
  • Thank you for "net stop beep"!
    I didn't find this _anywhere_ else.
    The beep from an MS-DOS app (e.g. running choice.com and pressing invalid key) was REALLY LOUD, even when the Windows volume control (in systray) was way low on my laptop. A Windows "ding" at the proper Windows volume would be better, but this is a big improvement. (I'm running Windows 2000.)

    FYI, if beeps bother you, I found another page

    <http://www.annoyances.org/exec/forum/winxp/1054090503>

    (works in Win 2000 also) that fixed a problem where the volume control slider in the system tray gave a BEEP at full volume whenever I adjusted it. Now it makes a nice "ding" at the selected volume.
  • I came across the speaker trick in a DDJ article 11 years ago. However the absense of Internet in India at that time meant that I could not get my hands on the sample code. I searched around and the closest I got to was a program called "Scream Tracker" that played mod files on the PC Speaker. Spending some time reverse engineering ST, and I got a good example for the DDJ article.
    I ended up writing a wav file player for DOS on the PC Speaker that would playback Stereo/Mono,16 bit or 8 bit .WAV files. The resolution was around 6 bits and I had a table which could downconvert 8 bits to 6 bits of audio. Unfortunately I put too many anti-debugging checks in there (stack tricks, pre-fetch queue and all) so it will no longer work. I remember showing it off to a non-techie friend at that time. He wasn't impressed. "My Radio has more clarity than that", was his response :-).
    I remember that I optimized the code sufficiently to be able to even get it to work on a PCT XT @10 Mhz. The biggest pain I had wa s the IDE interrupting the timer when reading the wave file. The audio suffered even though I was double buffering. I finally solved the problem by using XMS api's to cache the entire wave file in memory before playback.
    The old program is still available here http://www.xtendtech.com/rpm i.e. If you still have a PC XT@ 10 Mhz or higher running DOS :-)
  • Quote: "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"

    Sean, Access also made other games, most notably "Mean Streets" that was one of the first VGA games IIRC, and it too used RealSound. It had decent graphics even for those of us stuck with EGA, and the sound was indeed playable! :) I think they did a remake of that game, but I never got around checking it out. "Mean Streets" was an adventure game with a good exciting story. Can't get games of that caliber today! :/
  • Larry,


    I am not a programmer and know just enough hardware to be dangerous. I have a dos application that runs ok in 98 dos window but does not generate the beep supervisory tones while in the dos window or booted in dos mode. How do I get the program to connect to the windows audio card ( soft solution). I have been making a 10 to 1 dc isolated divider at the speaker and connecting to the aux input or the line in jack to make the trip. The later computer mother boards no longer use a speaker creating a problem for me. Can you guys help??

    Mrfx2001@comcast.net
  • Thanks for the 'net stop beep' command! I was searching for exactly that. One day while plugging away merrily in mysql command-line with my headphones on, I nearly vomited after entering invalid sql. The beep was so loud that even after I removed my headphones it played through my small earbuds louder than my pc speakers are usually set.

    Another nifty side effect I noticed was that encountering the same beep without my headphones plugged in caused the beep sound to overload my laptop's speakers and actually shutdown my machine (2005 Dell Inspiron). How's THAT for a bad sql error!

    For you ms batch masters out there, is there a way to ensure that 'net stop beep' gets run each time I open up a dos window in XP?
  • cool! now if only it would act as a real "Sounds and Audio Devices"!

    I have a c****** laptop running windows 98, and the sound card doesn't work ("Device not present or drivers not loaded" error?)

    So i went around looking for some sort of driver to use the PC Speaker (worked...)
  • Was mein Vista momentan an Ton zu wenig hat, das hat mein Dienstnotebook zu viel: Ton. Ständig piept einen der interne PC-Lautsprecher (oder seine heutige Entsprechung) an, wenn man sich vertippt, verklickt, vertut... Und das Ganze in einer Lautstärk

  • PingBack from http://outdoorceilingfansite.info/story.php?id=4754

  • PingBack from http://outdoorceilingfansite.info/story.php?id=22390

  • PingBack from http://quickdietsite.info/story.php?id=1126

  • PingBack from http://topalternativedating.info/story.php?id=3058

Page 3 of 3 (44 items) 123