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.
- Starting an effect only tells the sequencer which list to play; the first tone sounds at the next tick, 0 to 55 ms later.
- A new effect replaces the playing one, at once, without any priority (so of several effects started in one frame, the last one is heard). Starting the same effect again restarts it from the beginning.
- No sound while dead: requests are ignored when your shields are 0. Every hit plays its sound before the shield is taken, so the fatal hit is still heard; a sound already playing finishes. The rule also silences the menus after you leave a game while dead (Esc on the death screen): their Enter sounds return only when the next game restores the shields.
- Sound menu: "None" ignores all requests and stops the sequencer; "PC Speaker" (the default) turns it back on, and the rest of an interrupted effect then plays D.
The six effects
| Effect | Length | Tones (Hz, one per 55 ms tick) | Sounds like |
|---|---|---|---|
| 0 explosion | 16 ticks, 0.88 s | 146, 291, 146, then 117, 97, 83, 73 … down to 34 | a blip-blip, then a long falling growl |
| 1 you are hit | 8 ticks, 0.44 s | 1165, 583, 291, 146, 73, 36, 32, 29 | a fast falling sweep, an octave per tick at first |
| 2 pick-up | 4 ticks, 0.22 s | 717, 717, 1036, 1036 | a rising "ding-ding" |
| 3 warning | 8 ticks, 0.44 s | 666, 666, 518, 518, 666, 666, 518, 518 | a high-low siren, twice |
| 4 Enter accepted | 6 ticks, 0.33 s | 146, 291, 141, 274, 137, 259 | a low-high warble |
| 5 your laser | 10 ticks, 0.55 s | 233, 777, 194, 117, 83, 65 … down to 34 | a "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
| Effect | Single player | Multiplayer |
|---|---|---|
| 0 explosion | your 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 pod | another player is killed; you shoot a satellite |
| 1 you are hit | an alien laser, an enemy missile, or ramming an alien | another player's hit report arrives |
| 2 pick-up | any satellite collected | |
| 3 warning | "WARNING: MISSILE LAUNCH DETECTED", "WARNING: POD IN DANGER" | — |
| 4 Enter | accepting the start level, the level introduction, "Level completed", the instructions | taking a slot, starting to fly after the name, spectating; both modes: restarting after death |
| 5 laser | each 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.
- Held fire warbles or buzzes. Holding the trigger fires every third frame, and each shot restarts the laser effect. In single player (18.2 frames per second) that leaves room for three tones, so you hear 233, 777, 194 Hz over and over instead of whole "pews" (the demo's hold-fire box) D. In multiplayer, when three frames fit into one tick, only the first tone ever sounds: a steady 233 Hz buzz O.
- In multiplayer, frames can be shorter than a tick, so an explosion followed at once by a shot may never be heard O (once).
- Choosing "None" while a tone sounds leaves the speaker stuck on that tone, even after quitting D.
- The repeated-hit bug of chapter 8 also replays the hit sound every frame until your shields are gone; then, by the "no sound while dead" rule, the game goes silent although the bug goes on D.
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.