wiwis.dev

The sound of a two-stroke engine, computed in real time

· Superkart

Dylan_PvP sitting in his cyan Superkart kart, hands on the square steering wheel, in front of the grandstand

The only sound in Superkart is the engines. No music, no crash sounds, no menu clicks: everything rests on that buzz that rises and falls with your speed. And that buzz isn't a recording being sped up or slowed down. It's computed in real time, as if a small single-cylinder engine were actually running, one firing after another.

Why not just record a real kart

The classic approach is to record an engine at several RPMs, then crossfade between the recordings while adjusting their playback speed. The flaw is well known: speeding up a recording raises all of its frequencies at once, including the resonance of the exhaust pipe, which in real life doesn't change. The engine ends up sounding like a tape being fast-forwarded.

Superkart tackles the problem from the other end: it simulates the cause of the sound rather than the result. A single-cylinder two-stroke engine fires once per turn of the crankshaft. Those combustion pulses travel through an exhaust pipe that resonates. The game generates the pulses, then sends them through a simulated pipe. The pitch of the engine comes from the rhythm of the firings; its tone comes from the pipe, and that tone stays the same at any RPM, just like on a real engine.

One firing, in a few milliseconds

Each firing is a tiny, very short pulse. It starts with a sharp crack, the moment the exhaust port opens: it rises in a tenth of a millisecond and falls back in six tenths. A brief 640 hertz ring follows, then, at around three and a half milliseconds, the signal dips below zero, the vacuum that follows the gases rushing out. A faint combustion noise is layered on top and dies away in just over a millisecond.

The shape of the pulse never changes. Only the rhythm of the pulses does. At 2,300 RPM, idle, the engine fires about 38 times per second: you can almost hear each one, that characteristic "pop-pop-pop." At 13,200 RPM, at full speed, it fires 220 times per second, and the pulses blend into a continuous note.

An engine that's never perfectly steady

A real engine isn't a clock. In Superkart, each cycle is shifted at random by a little over one percent, and the strength of each firing varies by about ten percent. Strength also depends on load: with the throttle floored, the firings are much stronger than at half throttle.

The liveliest detail shows up when you lift off the throttle at high RPM. The mixture becomes irregular: about one cycle in six misfires. Most of these misfires are weak, almost silent pops. The others are the opposite: backfires, nearly twice as loud as normal. That's what gives the karts their crackle when you lift off, just before a corner.

The simulated exhaust pipe

The exhaust pipe is imitated with a delay line: the sound goes into a buffer and comes back out 3.6 milliseconds later. Part of the signal is fed back to the input, inverted and attenuated, exactly like a pressure wave reflecting off the open end of a pipe. These round trips reinforce certain frequencies, starting at around 140 hertz: that's the pipe's resonance, and it gives the engine its tone. A second, shorter line imitates the muffler chamber.

The signal then goes through light saturation, stronger when the engine is under load, which makes it rougher with the throttle floored. Everything below 110 hertz is cut: a small kart engine has no deep bass.

RPM: a clutch, no gears

A kart has no gearbox: it has a centrifugal clutch, which only passes the engine's power to the wheels above a certain RPM. The game imitates that behavior. With the throttle floored, the RPM never drops below 4,300, then it follows your speed up to 13,200 at full speed. Off the throttle, it drops back down to idle, at 2,300.

Several situations make it change:

Several karts, several voices

Eight karts on the track would mean eight engines to compute nonstop. The game computes four: yours, plus three others, reassigned every frame to the three karts closest to the camera. Each of these voices has a pipe and a chamber of slightly different lengths, so no two karts ever sound quite the same.

The computation runs in a special browser audio module that works separately from the rest of the game and produces the sound sample by sample, without being slowed down by rendering. The sound starts when you click the button to play, because browsers don't let a page make sound before the user has interacted with it.

Distance and the Doppler effect

For the other karts, three settings depend on distance:

Finally, the game applies the Doppler effect: when a kart approaches, its pitch rises; when it moves away, the pitch drops. It's the "neeeeowww" of a passing car. The calculation uses the speed of sound, 343 meters per second, along with the speed of the kart and that of the camera. The resulting shift changes the rhythm of the firings but not the pipe's resonance, so the engine keeps its tone even as it passes you.

A sound that tells you things

All this work isn't just for show. The engine's RPM tells you whether you're really accelerating, whether you're spinning your wheels in the grass, or whether you've got a boost going. The position and volume of the other engines tell you where the karts coming up behind you are, without taking your eyes off the track. Warfront's gunshots rely on the same synthesis principle: see how Warfront builds its gunshots.

Play SuperkartGame page

Read next