Synthesizing Chiptune Music Programatically

There’s still another way to do it: via code. Particularly suitable for
videogame music in particular, as you can have fine-grained control
over the music that you autogenerate, giving it the ability to be
dynamic.

Every programming language worth their name will have a basic sound
synthesizing library, which will be more than what to need to
make chiptune sounds.

The basics are these: you’ll need something called an oscillator,
which is nothing more than a device (in our case it will be a software
device, but a device nonetheless) capable of generating basic sound
waves:
sine, square, triangle and sawtooth.

Then’ you’ll combine several sound waves together to form your sound.
This technique is known as additive synthesis, and it’s illustrated
just below.

Once you have a timbre that you like, the synthesizing library should
have functions to alter its frequency, which will result in
different tones being produced (this is called FM, or frequency modulation,
synthesis), and then you have all you need to make any melody.

There’s something that you have to take into account, and is that you
should limit yourself a bit with the kind and number of waves that
you’re using, to try to replicate the limitations of the hardware of
old, and thus achieve convincing chiptune sounds.
In this forum post
the author recommends using nothing more than square, triangle and
sawtooth waves (avoiding sine), and limiting yourself to four
simultaneous instruments (as old sound cards didn’t come with many
audio channels). Have in mind though that these are just guidelines,
in practice you should experiment with everything and pick whatever
you think sounds good and convincingly chiptuneish.

Synthesizing your
own game music programatically has several important advantages:
you wont waste any space storing your music assets (when your game is
going on mobile phones this is an important aspect to consider!), and
it allows for unlimited flexibility if your music is going to be
dynamic. So we personally are keen in exploring this approach in a
future game.