Osegi
Gaming·2 min read

FactoTune: an MP3 player in Factorio built from 2,000 speakers

A Factorio player wants real songs, not MIDI beeps. FactoTune mixes 360 speaker sounds with linear algebra to approximate any audio file.

A sprawling Factorio factory floor covered in rows of programmable speakers wired together with logic circuit cables.
Image: Хабр

Tired of alt-tabbing between Spotify and Factorio, only to return to a base overrun by biters? The creator of FactoTune decided the factory should play the music itself, driven by logic circuits.

Music has been played in Factorio before, but only as MIDI: notes on a fixed set of instruments from the programmable speaker. MIDI stores which key is pressed, when and for how long. It has no voice and no mixing. FactoTune aims for the actual recording, so you can load any audio file and have the game play it.

How does FactoTune work?

The programmable speaker offers eleven instruments and 360 sounds in total, each playable at a volume from 0 to 100. The idea is to play many of these sounds at once, each at a carefully chosen volume, so that together they add up to the original track. Finding those volumes is a linear algebra problem: turn every sound into a list of numbers, stack them into a big matrix and solve for the best fit.

The author's first attempt, nicknamed Smoothie, solved the whole song in one go. It failed, because almost every speaker sound lasts under a second while a song runs for minutes. The fix was to slice the song into tiny pieces and solve each one separately.

Why two audio blocks?

The game runs at 60 ticks per second, but audio is mixed on a separate thread in blocks of 512 samples, about 86 times a second. That thread only checks for new messages at the start of a block, so a sound may play for one block or two, and the game does not say which. So each slice is two audio blocks long, and the author had to guess how the threads line up.

Plain least squares produced negative volumes, which do not exist in the game. Simply setting them to zero (the "Eraser") did not help. The "Picky Eater" approach did: the wiki says only 50 sounds can play at once, so for each slice it picks the sound that fills the biggest gap, refits all the volumes and drops any that would go negative, until 50 remain. For the first time the error fell and the song became recognisable, though noisy.

Volumes must also be whole numbers, and the speaker plays one step louder than the value set, so one is added before the value enters the game. Most chosen volumes turned out to be five or lower.

Apps worth a look

SUMO: the number puzzle

Tap the right tiles, add them up, hit the target. Easy to learn, hard to master. Free, for all ages.

Tags:factoriofactotunemp3 playerprogrammable speakeraudiolinear algebra

More from this day

See all →

More in: Gaming

See all →