21

The IBM PC and early successors came with an internal speaker that could play simple sounds. According to https://en.wikipedia.org/wiki/PC_speaker

However, because the method used to reproduce PCM was very sensitive to timing issues, these effects either caused noticeable sluggishness on slower PCs, or sometimes failed completely on faster PCs (that is, significantly faster than the program was originally developed for). Also, it was difficult for programs to do much else, even update the display, during the playing of such sounds.

That is the state of affairs I would expect if, like some early 8-bit computers, there was no sound chip and you had to bit-bang the speaker with the CPU at whatever frequency of sound you wanted.

However, according to https://en.wikipedia.org/wiki/Intel_8253 the 8253 timer chip was connected to the speaker and capable of generating a square wave, which suggests the CPU should only need to intervene when the volume or pitch needs to be changed, which would normally happen no more than a few times per second.

What am I missing?

3
  • 3
    On "when the volume needs to be changed" you might find this interesting since it's far from obvious on "1 bit sound": Was it possible programmatically to manipulate the volume as well as the pitch on computers with no sound chip? Jun 26, 2020 at 14:57
  • 1
    (not enough reputation to comment) There was a demo program written in BASIC that started with many K bytes of DATA statements. Then the execution code just pumped those bytes out of a CPU port (I forget what was on the other end of the port.) The sound output was what you would expect from a two-inch speaker responding to PCM data: bad, but amazing for its time and simplicity.
    – Bilbo
    Jun 28, 2020 at 21:48
  • 2
    Your quote references playing PCM sounds, then talk about the ability of the hardware to generate a square wave. Playing PCM requires playing a non-square waveform (eg using pulse width modulation, where the width of the "high" part of the wave varies in relation to the "low" part). These are different things.
    – occipita
    Jun 30, 2020 at 8:48

2 Answers 2

23

Short answer: If you are content with beeps, no. If you want arbitrary sound, yes.

There's three ways to get sound out of the PC Speaker:

  1. Put the timer chip into square wave mode and send frequencies (actually countdowns) to the timer chip. This is cheap and what's used for most PC Speaker sound effects in games. 140Hz is a popular rate to do it at.

  2. Set up the timer chip to output a constant voltage, then turn the speaker-enable gate on and off very quickly. Sounds terrible and uses a heap of CPU, but can play back arbitrary sounds. If you've encountered a game with extremely noisy speech in a couple of places with little player input, it was probably using this method.

  3. Put the timer chip into single-shot mode and send it countdowns very quickly. This implements a form of pulse-width modulation, which also allows arbitrary sound playback and can sound pretty good, but uses heaps of CPU. Not very popular among games, but pretty common in programs originating in the Demoscene, which at least have the excuse that you're not doing much interaction during playback.

Methods 2 and 3 require very precise timing of your playback routines. If you're playing something back at 16kHz, then that's how frequently your program needs to write to the timer chip or speaker-enable gate. You don't get DMA for this like you do with a sound card and pulling it off tends to be pretty CPU-intensive (this applies to LPT DACs like the Covox Speech Thing, too).

In the case of the famous 8088MPH demo, the closing credits is text-only because between mixing the MOD file for the music and the PWM PC Speaker playback, writing a few text-mode characters is all they could do with what CPU was left. If you've got good hearing, you can hear the 16kHz carrier tone during the sequence. The rest of the demo's music is a pretty good demonstration of what you can do with method 1 at 60Hz.

If you want to hear the difference between methods 2 and 3, Fast Tracker II supports both, along with LPT DACs and a couple of sound cards.

2
  • 1
    My two-voice or even three-voice music using on-off speaker bashing sounded decent on a stock 4.77MHz 8088 PC with a real speaker, though an AT&T clone with a piezo thingie they sounded pretty terrible. My four-voice music with speaker bashing sounded crummy on a 4.77MHz XT, but decent on a 6MHz AT.
    – supercat
    Jun 28, 2020 at 18:30
  • 1
    There was an old 90's DOS Golf game that used a method like this to output digitised speech. i think it used to disable the DOS interrupts for that extra bit of CPU, because after playing for a while your clock would be skewed afterwards. (DOS kept the clock up-to-date with a software interrupt).
    – Kingsley
    Jun 28, 2020 at 23:47
20

That paragraph should be understood in the context of the preceding paragraph:

The PC speaker was often used in very innovative ways to create the impression of polyphonic music or sound effects within computer games of its era

Effects such as those used in Pinball Fantasies in particular involve very rapid changes to the sound output by the PC speaker, far more than “a few times per second”, and that ends up being CPU-intensive. My jaw dropped the first time I saw Pinball Fantasies on PC; it really was impressive at the time, compare to typical bips and bleeps one would normally expect from the PC speaker, even in games.

See also Sound driver for DOS or Windows 3.x that used the PC speaker? which mentions issues caused by the need to disable interrupts (to control timing precisely).

Jim Leonard’s latest Oldskool PC video addresses the PC’s polyphonic capabilities (and other platforms’), and some of the trade-offs between using the square wave generator and controlling the speaker more finely.

1
  • Roughly what % of CPU budget was being used for sound? and what % for compute? Essentially the overall answer is "It depends what % of CPU you want to spend on sound, rather than compute"
    – smci
    Jun 27, 2020 at 20:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .