How NetWars Works Chapter 10 of 13, plus appendices

10. Sound

NetWars has six sound effects and no music. All of them come from the PC's tiny built-in speaker, which can play one square-wave tone at a time. This chapter explains the little sequencer behind them, lists the six effects, says when each plays, and lets you hear them.

One beeper, one tone at a time

The PC speaker is driven by a timer chip that produces a square wave: the speaker cone is pushed fully out and pulled fully in, many times per second. The pitch is set by a divisor: the frequency is 1,193,182 ÷ divisor hertz, so a bigger divisor is a lower note. There is no volume and no way to play two notes at once.

A sound effect is a short list of divisors. A tiny sequencer runs on the PC's timer tick (18.2 times a second, every 55 ms, the same tick as in chapter 2): at each tick it takes the next divisor from the list and sets the speaker to it; when the list is used up, it switches the speaker off. So an effect of n entries lasts n ticks, and its pitch changes in steps of 55 ms.

The six effects

EffectLengthTones (Hz, one per 55 ms tick)Sounds like
0 explosion16 ticks, 0.88 s146, 291, 146, then 117, 97, 83, 73 … down to 34a blip-blip, then a long falling growl
1 you are hit8 ticks, 0.44 s1165, 583, 291, 146, 73, 36, 32, 29a fast falling sweep, an octave per tick at first
2 pick-up4 ticks, 0.22 s717, 717, 1036, 1036a rising "ding-ding"
3 warning8 ticks, 0.44 s666, 666, 518, 518, 666, 666, 518, 518a high-low siren, twice
4 Enter accepted6 ticks, 0.33 s146, 291, 141, 274, 137, 259a low-high warble
5 your laser10 ticks, 0.55 s233, 777, 194, 117, 83, 65 … down to 34a "pew": chirp up, then falling

The exact divisors are in src/sound.asm (for example the explosion is 8192, 4096, 8192, then 10240 rising in steps of 2048 to 34816). In recordings of the original every tone was within 1.5 % and every step exactly one tick (runtime notes, "Sound").

Play the effects as the original plays them (square wave, one tone per 55 ms tick, a new effect cuts the running one). Keep this tab in front: browsers slow the timers of background tabs.

When each effect plays

EffectSingle playerMultiplayer
0 explosionyour shot destroys an alien (not a mere hit), an enemy missile, a pod or your satellite; your missile reaches its target; a Commander destroys a podanother player is killed; you shoot a satellite
1 you are hitan alien laser, an enemy missile, or ramming an alienanother player's hit report arrives
2 pick-upany satellite collected
3 warning"WARNING: MISSILE LAUNCH DETECTED", "WARNING: POD IN DANGER"—
4 Enteraccepting the start level, the level introduction, "Level completed", the instructionstaking a slot, starting to fly after the name, spectating; both modes: restarting after death
5 lasereach of your shots

Silent: launching your own missile, the cloak, non-lethal hits on aliens, your shot hitting another player (only the victim hears it), collisions between players, the level bonus, the menus, Enter on the title screen, in the initials entry or on an occupied lobby slot, and closing the help page. There is no engine sound.

Quirks
For a rebuild

With Web Audio: one square-wave OscillatorNode into a GainNode, monophonic. Clock the sequencer at 18.2 ticks per second, independent of the picture; at each tick set the frequency with setValueAtTime (hard steps, no glides) and the gain on or off. Keep the cut-and-restart rule and the "no sound while dead" rule. A raw square wave is harsh: a gain around 0.1 to 0.2 and a volume control are reasonable. Browsers only allow audio after a user action, so create the audio context on the first key press. Schedule the tone changes on the audio clock (ctx.currentTime), not with setInterval, which drifts and is slowed in background tabs.